-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmainSim.py
More file actions
executable file
·137 lines (115 loc) · 5.58 KB
/
mainSim.py
File metadata and controls
executable file
·137 lines (115 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
# Copyright (c) 2014 Greg James, Visual6502.org
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import time
import params
import imageOpenGL
import imagePIL
from sim2600Console import Sim2600Console
class MainSim:
def __init__(self):
self.imageGL = imageOpenGL.getInterface()
self.imagePIL = imagePIL.getInterface()
self.imageRaw = None
self.elapsedHalfClocks = 0
# The console simulator ties together a simulation
# of the 6502 chip, a simulation of the TIA chip,
# an emulation of the 6532 RIOT (RAM, I/O, Timer), and
# a cartridge ROM file holding the program instructions.
#
self.sim = Sim2600Console(params.romFile)
# For measuring how fast the simulation is running
self.lastUpdateTimeSec = None
if self.imageGL != None:
print('Entering glut render loop with simulation callback')
self.imageGL.enterRenderLoop(self.callback_updateSim)
print('Exited glut render loop')
elif self.imagePIL != None or self.imageRaw != None:
print('Entering simulation loop')
while True:
self.callback_updateSim()
print('Exited simulation loop')
def callback_updateSim(self):
tia = self.sim.simTIA
pixels = []
i = 0
while i < params.numTIAHalfClocksPerRender:
self.sim.advanceOneHalfClock()
# Get pixel color when TIA clock (~3mHz) is low
if tia.isLow(tia.padIndCLK0):
restartImage = False
if self.sim.simTIA.isHigh(self.sim.simTIA.vsync):
print('VSYNC high at TIA halfclock %d'%(tia.halfClkCount))
restartImage = True
# grayscale
#lum = self.simTIA.get3BitLuminance() << 5
#rgba = (lum << 24) | (lum << 16) | (lum << 8) | 0xFF
# color
rgba = self.sim.simTIA.getColorRGBA8()
if self.imageGL != None:
if restartImage == True:
self.imageGL.restartImage()
self.imageGL.setNextPixel(rgba)
if self.imagePIL != None:
if restartImage == True:
self.imagePIL.restartImage()
self.imagePIL.setNextPixel(rgba)
if self.sim.simTIA.isHigh(self.sim.simTIA.vblank):
print('VBLANK at TIA halfclock %d'%(tia.halfClkCount))
#cpuStr = self.sim6502.getStateStr1()
#tiaStr = self.simTIA.getTIAStateStr1()
#print(cpuStr + ' ' + tiaStr)
i += 1
if self.lastUpdateTimeSec != None:
elapsedSec = time.time() - self.lastUpdateTimeSec
secPerSimClock = 2.0 * elapsedSec / params.numTIAHalfClocksPerRender
totalWires = self.sim.sim6507.numWiresRecalculated + \
self.sim.simTIA.numWiresRecalculated
wiresPerClock = 2.0 * totalWires / params.numTIAHalfClocksPerRender
print(' ' +
'%d wires/clk, %g msec/clk'%
(wiresPerClock, secPerSimClock * 1000))
self.sim.sim6507.numWiresRecalculated = 0
self.sim.simTIA.numWiresRecalculated = 0
self.lastUpdateTimeSec = time.time()
def getStateStr(self):
cpu = self.sim.sim6507
tia = self.sim.simTIA
sstr = 'CLKS %d%d'%(cpu.isHighWN('CLK0'), tia.isHighWN('CLK0')) + ' '
sstr += 'RS,RDY %d%d'%(cpu.isHighWN('RES'), cpu.isHighWN('RDY')) + ' '
sstr += 'ADDR 0x%4.4X DB 0x%2.2X '% \
(self.sim.sim6507.getAddressBusValue(),
self.sim.sim6507.getDataBusValue())
sstr += self.simTIA.getTIAStateStr1()
return sstr
def printStartupMsg():
print('---------------------------------------------------------')
print('| Visual6502.org transistor-level simulation of an |')
print('| Atari 2600 game console. |')
print('| ** Be patient ** It can take up to 10,000 or 40,000 |')
print('| half clock cycles of the 6502 CPU before visible |')
print('| pixels appear. |')
print('| Hit ESC to exit the OpenGL rendering or CTRL-C |')
print('| See params.py for the ROM file location. |')
print('---------------------------------------------------------')
if __name__ == '__main__':
printStartupMsg()
MainSim()
print('Exiting mainSim.py')