-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSChip.py
287 lines (270 loc) · 12.1 KB
/
SChip.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
279
280
281
282
283
284
285
286
287
import random, re
#4x5, 8x10 hex fontset
from fontset import fontset
class SCHIPError(Exception):
pass
class SCHIP:
def __init__(self, cartdata=bytes()):
self.type = "SCHIP"
self.memory = bytearray(4096)
self.V = bytearray(16)
self.I = 0
self.pc = 512
self.gfx = bytearray(128*64)
self.delay_timer = 0
self.sound_timer = 0
self.keys = bytearray(16)
self.drawFlag = False
self.stack = []
self.keypress_tmp = set()
self.hires = False
self.flags = bytearray(8)
for i in range(len(fontset)):
self.memory[i + 80] = fontset[i]
for i in range(len(cartdata)):
self.memory[i + 512] = cartdata[i]
def cycle(self, delta, noexit=False):
# Get Opcode
opcode = self.memory[self.pc] << 8 | self.memory[self.pc + 1]
ophex = "{:0>4}".format(hex(opcode)[2:]).upper()
# Decode and Execute Opcode
if re.fullmatch("00C.", ophex):
# 00Cx: Scroll the display down x pixels
amount = int(ophex[3],16) * 128
self.gfx = self.gfx[:-amount].rjust(128*64, b"\x00")
elif ophex == "00E0":
# 00E0: Clear screen
self.gfx = bytearray(128*64)
self.drawFlag = True
elif ophex == "00EE":
# 00EE: Return from subroutine
if len(self.stack) == 0:
raise SCHIPError(f"Return at {hex(self.pc)} has nowhere to go")
self.pc = self.stack.pop() - 2
elif ophex == "00E0":
# 00FD: Exit
if not noexit:
self.pc -= 2
return "exit"
elif ophex == "00FE":
# 00FF: Set low resolution
self.hires = False
# Same as 00E0: Clear screen
self.gfx = bytearray(128*64)
self.drawFlag = True
elif ophex == "00FF":
# 00FF: Set high resolution
self.hires = True
# Same as 00E0: Clear screen
self.gfx = bytearray(128*64)
self.drawFlag = True
elif re.fullmatch("1...", ophex):
# 1xxx: Jump to [nnn]
self.pc = int(ophex[1:],16) - 2
elif re.fullmatch("2...", ophex):
# 2nnn: Call subroutine at [nnn]
if len(self.stack) == 16:
raise SCHIPError(f"Stack is full, cannot call subroutine")
self.stack.append(self.pc + 2)
self.pc = int(ophex[1:],16) - 2
elif re.fullmatch("3...", ophex):
# 3xnn: Skips next instruction if V[x] equals [nn]
if self.V[int(ophex[1],16)] == int(ophex[2:],16):
self.pc += 2
elif re.fullmatch("4...", ophex):
# 4xnn: Skips next instruction if V[x] doesn't equal [nn]
if self.V[int(ophex[1],16)] != int(ophex[2:],16):
self.pc += 2
elif re.fullmatch("5..0", ophex):
# 5xy0: Skips next instruction if V[x] equals V[y]
if self.V[int(ophex[1],16)] == self.V[int(ophex[2],16)]:
self.pc += 2
elif re.fullmatch("6...", ophex):
# 6xnn: Set V[x] to [nn]
self.V[int(ophex[1],16)] = int(ophex[2:],16)
elif re.fullmatch("7...", ophex):
# 7xnn: Add [nn] to V[x]
self.V[int(ophex[1],16)] = (
self.V[int(ophex[1],16)] + int(ophex[2:],16)
) % 256
elif re.fullmatch("8..0", ophex):
# 8xy0: Set V[x] to V[y]
self.V[int(ophex[1],16)] = self.V[int(ophex[2],16)]
elif re.fullmatch("8..1", ophex):
# 8xy1: Set V[x] to V[x] OR V[y]
self.V[int(ophex[1],16)] |= self.V[int(ophex[2],16)]
elif re.fullmatch("8..2", ophex):
# 8xy2: Set V[x] to V[x] AND V[y]
self.V[int(ophex[1],16)] &= self.V[int(ophex[2],16)]
elif re.fullmatch("8..3", ophex):
# 8xy3: Set V[x] to V[x] XOR V[y]
self.V[int(ophex[1],16)] ^= self.V[int(ophex[2],16)]
elif re.fullmatch("8..4", ophex):
# 8xy4: Add V[y] to V[x], and set Vf to whether there was an
# overflow or not
total = self.V[int(ophex[1],16)] + self.V[int(ophex[2],16)]
self.V[int(ophex[1],16)] = total % 256
self.V[15] = total > 256
elif re.fullmatch("8..5", ophex):
# 8xy7: Subtract V[y] from V[x], and set Vf to whether there was
# a borrow or not
total = self.V[int(ophex[1],16)] - self.V[int(ophex[2],16)]
self.V[int(ophex[1],16)] = total % 256
self.V[15] = total >= 0
elif re.fullmatch("8..6", ophex):
# 8xy6: Shifts V[y] to the right by 1, putting the underflowflow
# in Vf, and putting the result in V[x]
self.V[15] = self.V[int(ophex[2],16)] & 1
self.V[int(ophex[1],16)] = (self.V[int(ophex[2],16)]>>1)%256
elif re.fullmatch("8..7", ophex):
# 8xy7: Subtract V[y] from V[x], and set Vf to whether there was
# a borrow or not
total = self.V[int(ophex[2],16)] - self.V[int(ophex[1],16)]
self.V[int(ophex[1],16)] = total % 256
self.V[15] = total >= 0
elif re.fullmatch("8..E", ophex):
# 8xyE: Shifts V[y] to the left by 1, putting the overflow in Vf,
# and putting the result in V[x]
self.V[15] = self.V[int(ophex[2],16)]//128
self.V[int(ophex[1],16)] = (self.V[int(ophex[2],16)]<<1)%256
elif re.fullmatch("9..0", ophex):
# 9xy0: Skips next instruction if V[x] doesn't equal V[y]
if self.V[int(ophex[1],16)] != self.V[int(ophex[2],16)]:
self.pc += 2
elif re.fullmatch("A...", ophex):
# Annn: Set I to [nnn]
self.I = int(ophex[1:],16)
elif re.fullmatch("B...", ophex):
# Bnnn: Jump to [nnn] plus V0
self.pc = int(ophex[1:],16) + self.V[0] - 2
elif re.fullmatch("C...", ophex):
# Cxnn: Set V[x] to a random number, and bitwise-AND it with [nn]
self.V[int(ophex[1],16)] = random.randint(0, 255) & int(ophex[2:],16)
elif re.fullmatch("D...", ophex):
# Dxyn: XOR sprite stored at the I pointer with height [n] at [x], [y] onto display
flipped_on = False
x, y = self.V[int(ophex[1],16)], self.V[int(ophex[2],16)]
if int(ophex[3],16) > 0:
for sy in range(int(ophex[3],16)):
for sx in range(8):
sprite_index = self.I + sy
if not self.memory[sprite_index] & (128 >> sx):
continue
if self.hires:
gfx_index = ((x+sx)%128) + (((y+sy)%64)*128)
else:
gfx_index = ((x+sx)%64) + (((y+sy)%32)*64)
if self.gfx[gfx_index]:
flipped_on = True
self.gfx[gfx_index] ^= 1
else:
for sy in range(16):
for sx in range(16):
sprite_index = self.I + sy*2 + sx//8
if not self.memory[sprite_index] & (128 >> (sx%8)):
continue
if self.hires:
gfx_index = ((x+sx)%128) + (((y+sy)%64)*128)
else:
gfx_index = ((x+sx)%64) + (((y+sy)%32)*64)
if self.gfx[gfx_index]:
flipped_on = True
self.gfx[gfx_index] ^= 1
self.V[15] = int(flipped_on)
self.drawFlag = True
elif re.fullmatch("E.9E", ophex):
# ExA1: Skips next instruction if the key V[x] is pressed
if self.V[int(ophex[1],16)] < 16 and self.keys[self.V[int(ophex[1],16)]]:
self.pc += 2
elif re.fullmatch("E.A1", ophex):
# ExA1: Skips next instruction if the key V[x] is not pressed
if self.V[int(ophex[1],16)] > 15 or not self.keys[self.V[int(ophex[1],16)]]:
self.pc += 2
elif re.fullmatch("F.07", ophex):
# Fx07: Set V[x] to delay timer
self.V[int(ophex[1],16)] = int(self.delay_timer)
elif re.fullmatch("F.15", ophex):
# Fx15: Set delay timer to V[x]
self.delay_timer = self.V[int(ophex[1],16)]
elif re.fullmatch("F.18", ophex):
# Fx15: Set sound timer to V[x]
self.sound_timer = self.V[int(ophex[1],16)]
elif re.fullmatch("F.0A", ophex):
# Fx0A: Await key press and release and store in V[x]
success = False
for i in range(16):
if self.keys[i]:
self.keypress_tmp.add(i)
to_remove = []
for i in self.keypress_tmp:
if not self.keys[i]:
self.V[int(ophex[1],16)] = i
success = True
to_remove.append(i)
for i in to_remove:
self.keypress_tmp.remove(i)
if not success:
self.pc -= 2
elif re.fullmatch("F.1E", ophex):
# Fx1E: Add V[x] to I, and set Vf to whether there was an overflow or not
total = self.I + self.V[int(ophex[1],16)]
self.I = total % 0x1000
self.V[15] = total > 0xFFF
elif re.fullmatch("F.29", ophex):
# Fx29: Set I to the fontset index of the value of V[x]
self.I = 0x50 + (len(fontset) // 48) * (
self.V[int(ophex[1],16)]%16
)
elif re.fullmatch("F.30", ophex):
# Fx30: Set I to the large fontset index of the value of V[x]
self.I = 0x50 + (len(fontset) // 48) * (16 +
self.V[int(ophex[1],16)]%16 * 2
)
elif re.fullmatch("F.33", ophex):
# Fx33: Dump the 3-digit decimal representation of V[x] into
# memory, starting at I
dec = str(self.V[int(ophex[1],16)]).zfill(3)
for i in range(3):
self.memory[self.I + i] = int(dec[i])
elif re.fullmatch("F.55", ophex):
# Fx55: Dump V0..V[x] into memory, starting at I
for i in range(int(ophex[1],16)+1):
self.memory[self.I + i] = self.V[i]
self.I += int(ophex[1],16)+1
elif re.fullmatch("F.65", ophex):
# Fx65: Load memory into V0..V[x], starting at I
for i in range(int(ophex[1],16)+1):
self.V[i] = self.memory[self.I + i]
self.I += int(ophex[1],16)+1
elif re.fullmatch("F.75", ophex):
# Fx75: Dump V0..V[x] into flag memory, starting at I
if int(ophex[1],16) >= 8:
raise CHIP8Error(f"Flag index to high: {int(ophex[1],16)}")
for i in range(int(ophex[1],16)+1):
self.flags[i] = self.V[i]
self.I += int(ophex[1],16)+1
elif re.fullmatch("F.85", ophex):
# Fx85: Load flag memory into V0..V[x<8], starting at I
if int(ophex[1],16) >= 8:
raise CHIP8Error(f"Flag index to high: {int(ophex[1],16)}")
for i in range(int(ophex[1],16)+1):
self.V[i] = self.flags[i]
self.I += int(ophex[1],16)+1
elif ophex == "00FB":
# 00FB: Scroll the display right 4 pixels
for row in range(64):
ind = row*128
self.gfx[ind:ind+128] = self.gfx[ind:ind+124].rjust(128, b"\x00")
elif ophex == "00FC":
# 00FC: Scroll the display left 4 pixels
for row in range(64):
ind = row*128
self.gfx[ind:ind+128] = self.gfx[ind+4:ind+128].ljust(128, b"\x00")
else:
raise CHIP8Error(f"Unknown OpCode at {hex(self.pc)}: 0x{ophex}")
self.pc += 2
# Update timers
self.delay_timer = max(0, self.delay_timer - (delta * 60))
self.sound_timer = max(0, self.sound_timer - (delta * 60))
CHIP8 = SCHIP
CHIP8Error = SCHIPError