-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instruction.py
278 lines (203 loc) · 6.46 KB
/
Instruction.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import functools
def register(*decorators):
def register_wrapper(func):
for deco in decorators[::-1]:
func=deco(func)
func._decorators=decorators
return func
return register_wrapper
class OP:
def __init__(self, code):
self.code = format(code, '02x').zfill(4).upper()
def match(self, pattern):
result = True
for chr in zip(list(self.code), list(str(pattern).upper())):
if not chr[0] == chr[1] and chr[1] != '.':
return False
else:
return True
def get(self, pattern):
zipped = list(zip(pattern, self.code))
n = x = y = None
if 'N' in pattern:
n = int(''.join(x[1] for x in zipped if x[0] == 'N'), 16)
if 'X' in pattern:
x = int(''.join(x[1] for x in zipped if x[0] == 'X'), 16)
if 'Y' in pattern:
y = int(''.join(x[1] for x in zipped if x[0] == 'Y'), 16)
return {'N': n, 'X': x, 'Y': y}
def __eq__(self, other):
if isinstance(other, str) or isinstance(other, self.__class__):
return self.match(other)
else:
return super.__eq__(self, other)
def __repr__(self):
return str(self.code)
def __str__(self):
return str(self.code)
def Code(opcode):
def CodeDec(f):
@functools.wraps(f)
def wrapper(*args, **kwds):
f(*args, **kwds)
wrapper._opcode = opcode
return wrapper
return CodeDec
class InstructionParser:
def __init__(self, service_manager):
self.op_functions = self.__class__._InstructionCommands()
self.memory = None
self.service_manager = service_manager
self.Do = self.service_manager.Do
def parse(self, opcode):
for code in self.op_functions:
if opcode == code._opcode:
code(self, opcode)
break
else:
# raise NotImplemented(f"Unknown opcode ({opcode}).")
print(f"Unknown opcode ({opcode}).")
"""
@Code("0...")
def Call(self):
raise NotImplemented("RCA 1802 call not implemented")
"""
@Code("00E0")
def DispClear(self, code):
self.Do("draw").DispClear()
@Code("00EE")
def Return(self, code):
self.Do("flow").Return()
@Code("1...")
def Goto(self, code):
v = code.get('.NNN')
self.Do("flow").Goto(v["N"])
@Code("2...")
def Subroutine(self, code): #2NNN
v = code.get('.NNN')
self.Do("flow").Subroutine(v["N"])
@Code("3...")
def SkipEqN(self, code): #3XNN
v = code.get('.XNN')
self.Do("flow").SkipEqN(v["X"], v["N"])
@Code("4...")
def SkipNEqN(self, code):
v = code.get('.XNN')
self.Do("flow").SkipNEqN(v["X"], v["N"])
@Code("5...")
def SkipEq(self, code):
v = code.get('.XY.')
self.Do("flow").SkipEq(v["X"], v["Y"])
@Code("6...")
def SetN(self, code):
v = code.get('.XNN')
self.Do("maths").SetN(v["X"], v["N"])
@Code("7...")
def AddN(self, code):
v = code.get('.XNN')
self.Do("maths").AddN(v["X"], v["N"])
@Code("8..0")
def Set(self, code):
v = code.get('.XY.')
self.Do("maths").Set(v["X"], v["Y"])
@Code("8..1")
def BitOpOr(self, code):
v = code.get('.XY.')
self.Do("bitop").BitOpOr(v["X"], v["Y"])
@Code("8..2")
def BitOpAnd(self, code):
v = code.get('.XY.')
self.Do("bitop").BitOpAnd(v["X"], v["Y"])
@Code("8..3")
def BitOpXor(self, code):
v = code.get('.XY.')
self.Do("bitop").BitOpXor(v["X"], v["Y"])
@Code("8..4")
def MathAdd(self, code):
v = code.get('.XY.')
self.Do("maths").MathAdd(v["X"], v["Y"])
@Code("8..5")
def MathSub(self, code):
v = code.get('.XY.')
self.Do("maths").MathSub(v["X"], v["Y"])
@Code("8..6")
def BitOpSHR(self, code):
v = code.get('.XY.')
self.Do("bitop").BitOpSHR(v["X"], v["Y"])
@Code("8..7")
def MathDiff(self, code):
v = code.get('.XY.')
self.Do("maths").MathDiff(v["X"], v["Y"])
@Code("8..E")
def BitOpSHL(self, code):
v = code.get('.XY.')
self.Do("bitop").BitOpSHL(v["X"], v["Y"])
@Code("9..0")
def SkipNEq(self, code):
v = code.get('.XY.')
self.Do("flow").SkipNEq(v["X"], v["Y"])
@Code("A...")
def MemSetI(self, code):
v = code.get('.NNN')
self.Do("maths").MemSetI(v["N"])
@Code("B...")
def JumpN(self, code):
v = code.get('.NNN')
self.Do("flow").JumpN(v["N"])
@Code("C...")
def Rand(self, code):
v = code.get('.XNN')
self.Do("maths").Rand(v["X"], v["N"])
@Code("D...")
def Draw(self, code):
v = code.get('.XYN')
self.Do("draw").Draw(v["X"], v["Y"], v["N"])
@Code("E.9E")
def KeyOpPressed(self, code):
v = code.get('.X..')
self.Do("flow").KeyOpPressed(v["X"])
@Code("E.A1")
def KepOpReleased(self, code):
v = code.get('.X..')
self.Do("flow").KeyOpReleased(v["X"])
@Code("F.0A")
def WaitForKey(self, code):
v = code.get('.X..')
while not self.Do("misc").WaitForKey(v["X"]):
pass
@Code("F.07")
def GetDelayTimer(self, code):
v = code.get('.X..')
self.Do("misc").GetDelayTimer(v["X"])
@Code("F.15")
def SetDelayTimer(self, code):
v = code.get('.X..')
self.Do("misc").SetDelayTimer(v["X"])
@Code("F.18")
def SetSoundTimer(self, code):
v = code.get('.X..')
self.Do("misc").SetSoundTimer(v["X"])
@Code("F.1E")
def MemAddI(self, code):
v = code.get('.X..')
self.Do("maths").MemAddI(v["X"])
@Code("F.29")
def MemSetISprite(self, code):
v = code.get('.X..')
self.Do("draw").MemSetISprite(v["X"])
@Code("F.33")
def BCD(self, code):
v = code.get('.X..')
self.Do("maths").BCD(v["X"])
@Code("F.55")
def RegDump(self, code):
v = code.get('.X..')
self.Do("misc").RegDump(v["X"])
@Code("F.65")
def RegLoad(self, code):
v = code.get('.X..')
self.Do("misc").RegLoad(v["X"])
@staticmethod
def _InstructionCommands():
all_methods = [getattr(InstructionParser, name) for name in dir(InstructionParser) if callable(getattr(InstructionParser, name))]
return list(filter(lambda k: hasattr(k, "_opcode"), all_methods))