-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbrick.py
194 lines (155 loc) · 5.67 KB
/
brick.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from PyQt6 import QtCore
from PyQt6.QtCore import QObject, pyqtSignal, pyqtSlot
import time
from cores import *
FPS = 60
EXAMINE_RATE = 30
DISPLAY_UPDTE_NS = 1000000000 / FPS
EXAMINE_UPDTE_NS = 1000000000 / EXAMINE_RATE
class Brick(QObject):
btnPressSignal = pyqtSignal(str, int, int)
btnReleaseSignal = pyqtSignal(str, int)
stepSignal = pyqtSignal()
pauseSignal = pyqtSignal()
runSignal = pyqtSignal()
stopSignal = pyqtSignal()
setSpeedSignal = pyqtSignal(float)
setBreakpointSignal = pyqtSignal(int, bool)
examineSignal = pyqtSignal(dict)
editStateSignal = pyqtSignal(dict)
uiDisplayUpdateSignal = pyqtSignal(tuple)
setConfigSignal = pyqtSignal(dict)
errorSignal = pyqtSignal(str)
def __init__(self, config):
super().__init__()
self._config = config
self.btnPressSignal.connect(self._btnPressed)
self.btnReleaseSignal.connect(self._btnReleased)
self.setBreakpointSignal.connect(self._setBreakpoint)
self.runSignal.connect(self._run)
self.pauseSignal.connect(self._pause)
self.stepSignal.connect(self._step)
self.setConfigSignal.connect(self._setConfig)
self.stopSignal.connect(self._stop)
self.setSpeedSignal.connect(self._setSpeed)
self.editStateSignal.connect(self._editState)
@pyqtSlot()
def run(self):
self._breakpoints = {}
self._debug = False
self._cycleTimeNs = self._getCicleTimeNs()
self._icounterOnStop = 0
try:
self._setConfig(self._config)
except Exception as e:
self.errorSignal.emit(str(e))
return
self._uiDisplayUpdate()
self._uiExamineUpdate()
self._clock()
@pyqtSlot(dict)
def _editState(self, state):
if ("BRKPT" in state):
self._setBreakpoint(state["BRKPT"][0], state["BRKPT"][1])
self._CPU.edit_state(state)
self._uiDisplayUpdate()
self._uiExamineUpdate()
def _clock(self):
thread = self.thread().currentThread()
lastTick = time.perf_counter_ns()
lastExamine = lastTick
lastDisplayUpdate = lastTick
while not(thread.isInterruptionRequested() or self._debug):
cycleTimeNs = self._cycleTimeNs
ns = time.perf_counter_ns()
while (ns > lastTick and ns < lastDisplayUpdate and ns < lastExamine and not self._debug):
lastTick += self._CPU.clock() * cycleTimeNs
if (self._breakpoints and self._CPU.pc() in self._breakpoints):
self.examineSignal.emit({"DEBUG": True})
self._pause()
ns = time.perf_counter_ns()
if (ns > lastDisplayUpdate):
lastDisplayUpdate += DISPLAY_UPDTE_NS
self._uiDisplayUpdate()
elif (ns > lastExamine):
lastExamine += EXAMINE_UPDTE_NS
self._uiExamineUpdate()
if (lastTick > ns):
time.sleep((lastTick - ns) / 1000000000)
else:
lastTick = ns
QtCore.QCoreApplication.processEvents()
@pyqtSlot()
def _run(self):
self._debug = False
self._clock()
@pyqtSlot()
def _pause(self):
self._debug = True
self._uiDisplayUpdate()
self._uiExamineUpdate()
self._icounterOnStop = self._CPU.istr_counter()
@pyqtSlot()
def _step(self):
self._CPU.clock()
self._uiDisplayUpdate()
self._uiExamineUpdate()
@pyqtSlot()
def _stop(self):
self._debug = True
self._CPU.reset()
self._uiDisplayUpdate()
self._uiExamineUpdate()
self._icounterOnStop = self._CPU.istr_counter()
@pyqtSlot(int, bool)
def _setBreakpoint(self, pc, add):
if (add):
self._breakpoints[pc] = True
else:
if (pc in self._breakpoints):
del self._breakpoints[pc]
@pyqtSlot(dict)
def _setConfig(self, config):
self._config = config
core = self._config["core"]
if (core in cores_map):
self._CPU = cores_map[core]["core"](config['mask_options'], config["clock"])
self.examineSignal.emit(cores_map[core]["dasm"]().disassemble(self._CPU.get_ROM()))
def _uiDisplayUpdate(self):
self.uiDisplayUpdateSignal.emit(self._CPU.get_VRAM())
def _uiExamineUpdate(self):
self.examineSignal.emit({
**self._CPU.examine(),
"ICTR": self._CPU.istr_counter() - self._icounterOnStop
})
@pyqtSlot(str, int, int)
def _btnPressed(self, port, pin, level):
self._CPU.pin_set(port, pin, level)
@pyqtSlot(str, int)
def _btnReleased(self, port, pin):
self._CPU.pin_release(port, pin)
@pyqtSlot(float)
def _setSpeed(self, speed):
self._cycleTimeNs = int(self._getCicleTimeNs() * speed)
def _getCicleTimeNs(self):
return 1000000000 / self._config["clock"]
def debugRun(self):
self.runSignal.emit()
def debugStep(self):
self.stepSignal.emit()
def debugPause(self):
self.pauseSignal.emit()
def debugStop(self):
self.stopSignal.emit()
def btnPressed(self, port, pin, level):
self.btnPressSignal.emit(port, pin, level)
def btnReleased(self, port, pinMask):
self.btnReleaseSignal.emit(port, pinMask)
def setConfig(self, config):
self.setConfigSignal.emit(config)
def setSpeed(self, speed):
self.setSpeedSignal.emit(speed)
def editState(self, state):
self.editStateSignal.emit(state)
def setBreakpoint(self, pc, add):
self.setBreakpointSignal.emit(pc, add)