-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8_interpreter.py
377 lines (360 loc) · 12.5 KB
/
chip8_interpreter.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import sys
import os
import re
from time import time, sleep
from random import randint
from copy import deepcopy
import keyboard
import logging
import winsound
import threading
from ast import literal_eval
from graphics import GraphWin, Rectangle, Point
from chip8_engine import Chip8
chip_8 = Chip8()
# original COSMAC VIP emulation
orig = True
# original COSMAC VIP incrementation of index variable
inc = False
# debug mode
debug = False
# line by line mode
lbl = False
# rom filename
file_name = sys.argv[1]
# fn extracts the specific filename
fn = deepcopy(file_name)
while fn.find("/") != -1:
fn = fn[fn.find("/") + 1:]
if fn.find(".") != -1:
fn = fn[:fn.find(".")]
# additional commands
if len(sys.argv) > 2:
for comm in sys.argv[2:]:
if comm == "-o":
orig = False
elif comm == "-i":
inc = True
elif comm == "-d":
debug = True
elif comm == "-l":
lbl = True
# change logging level if debugging
if debug:
loglevel = logging.INFO
else:
loglevel = logging.WARNING
logger = logging.getLogger()
logger.setLevel(loglevel)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(loglevel)
handler.terminator = ""
logger.addHandler(handler)
# load rom into memory
with open(file_name, 'rb') as f:
file_content = f.read()
for i in range(len(file_content)):
chip_8.memory[0x200 + i] = file_content[i]
# create canvas
win = GraphWin(title=fn, width=64*16, height=32*16, autoflush=False)
win.setCoords(0, 0, 64, 32)
# sound function
def beep(length):
winsound.Beep(750, int(length * 1000 / 60))
# interpreter loop
while True:
# cycle timing
cycle = 1/700
if lbl:
input()
# fetch instruction
instr = (chip_8.memory[chip_8.pc] << 8) | chip_8.memory[chip_8.pc + 1]
chip_8.pc += 2
# debugging print statements
logging.info(f"0x{chip_8.pc - 2:03X}: 0x{instr:04X}\t")
for i in range(len(chip_8.reg)):
logging.info(f"v{i:X}: 0x{chip_8.reg[i]:02X} ")
logging.info("\nstk: ")
for s in chip_8.stk:
logging.info(f"0x{s:03X}, ")
logging.info("\n")
# nibble information
op = (instr >> 0xC) & 0xF # first nibble
x = (instr >> 0x8) & 0xF # second nibble
y = (instr >> 0x4) & 0xF # third nibble
n = instr & 0xF # fourth nibble
nn = instr & 0xFF # second byte (8 bit number)
nnn = instr & 0xFFF # second nibble plus second byte (12 bit memory address)
# decode and execute instruction
tick = time()
if instr == 0x0000:
# end of program
logging.info("program is over\n")
break
elif instr == 0x00E0:
# clear screen
logging.info("clear the screen\n")
cycle = 0
chip_8.display = [0x0 for i in chip_8.display]
for i in win.items[:]:
i.undraw()
win.update()
elif instr == 0x00EE:
# pop stack
logging.info("pop stack\n")
cycle = 0
chip_8.pc = chip_8.stk.pop()
elif op == 0x1:
# jump to nnn
logging.info(f"jump to 0x{nnn:02X}\n")
cycle = 0
if chip_8.pc - 2 == nnn:
# loops on last instruction
logging.info("program complete\n")
break
chip_8.pc = nnn
elif op == 0x2:
# call nnn, push current pc to stack
logging.info(f"push 0x{chip_8.pc:03X} to stack and jump to 0x{nnn:03X}\n")
cycle = 0
chip_8.stk.append(chip_8.pc)
chip_8.pc = nnn
elif op == 0x3:
# this and next three are skips
logging.info(f"if v{x:X} == 0x{nn:02X}, then skip\n")
cycle = 0
if chip_8.reg[x] == nn:
chip_8.pc += 2
elif op == 0x4:
logging.info(f"if v{x:X} != 0x{nn:02X}, then skip\n")
cycle = 0
if chip_8.reg[x] != nn:
chip_8.pc += 2
elif op == 0x5 and n == 0x0:
logging.info(f"if v{x:X} == v{y:X}, then skip\n")
cycle = 0
if chip_8.reg[x] == chip_8.reg[y]:
chip_8.pc += 2
elif op == 0x9 and n == 0x0:
logging.info(f"if v{x:X} != v{y:X}, then skip\n")
cycle = 0
if chip_8.reg[x] != chip_8.reg[y]:
chip_8.pc += 2
elif op == 0x6:
# set
logging.info(f"set v{x:X} to 0x{nn:02X}\n")
cycle = 0
chip_8.reg[x] = nn
elif op == 0x7:
# add
logging.info(f"add 0x{nn:02X} to 0x{chip_8.reg[x]:02X}, ")
cycle = 0
chip_8.reg[x] = (chip_8.reg[x] + nn) % 0x100
logging.info(f"sum is now 0x{chip_8.reg[x]:02X}\n")
elif op == 0x8:
# logic
cycle = 0
if n == 0x0:
# set
logging.info(f"v{x:X} is now set to the value in v{y:X}\n")
chip_8.reg[x] = chip_8.reg[y]
elif n == 0x1:
# or
chip_8.reg[x] = chip_8.reg[x] | chip_8.reg[y]
elif n == 0x2:
# and
chip_8.reg[x] = chip_8.reg[x] & chip_8.reg[y]
elif n == 0x3:
# xor
chip_8.reg[x] = chip_8.reg[x] ^ chip_8.reg[y]
elif n == 0x4:
# add
chip_8.reg[x] += chip_8.reg[y]
if chip_8.reg[x] > 0xFF:
chip_8.reg[x] %= 0x100
chip_8.reg[0xF] = 1
else:
chip_8.reg[0xF] = 0
elif n == 0x5:
# subtract
chip_8.reg[x] -= chip_8.reg[y]
if chip_8.reg[x] < 0:
chip_8.reg[x] += 0x100
chip_8.reg[0xF] = 0
else:
chip_8.reg[0xF] = 1
elif n == 0x7:
# subtract other way
chip_8.reg[x] = chip_8.reg[y] - chip_8.reg[x]
if chip_8.reg[x] < 0:
chip_8.reg[x] += 0x100
chip_8.reg[0xF] = 0
else:
chip_8.reg[0xF] = 1
elif n == 0x6:
# shift right
if orig:
chip_8.reg[x] = chip_8.reg[y]
logging.info(f"shift v{x:X} = 0x{chip_8.reg[x]:02X} right\n")
chip_8.reg[0xF] = chip_8.reg[x] & 0x1
chip_8.reg[x] = chip_8.reg[x] >> 1
logging.info(f"v{x:X} is now 0x{chip_8.reg[x]:02X}\n")
elif n == 0xE:
# shift left
if orig:
chip_8.reg[x] = chip_8.reg[y]
logging.info(f"shift v{x:X} = 0x{chip_8.reg[x]:02X} left\n")
chip_8.reg[0xF] = chip_8.reg[x] >> 7
chip_8.reg[x] = chip_8.reg[x] << 1
chip_8.reg[x] = chip_8.reg[x] & 0xFF
logging.info(f"v{x:X} is now 0x{chip_8.reg[x]:02X}\n")
elif op == 0xA:
# set index
logging.info(f"set index to 0x{nnn:03X} which points to 0x{chip_8.memory[nnn]:02X}\n")
cycle = .000055
chip_8.index = nnn
elif op == 0xB:
# offset jump
logging.info(f"jump to {nnn:03X} + {chip_8.reg[0]:02X}\n")
cycle = .000105
if orig:
chip_8.pc = nnn + chip_8.reg[0]
else:
chip_8.pc = nnn + chip_8.reg[x]
elif op == 0xC:
# random
logging.info(f"make v{x:01X} a random number &ed with 0x{nn:02X}\n")
cycle = .000164
r = randint(0, nn)
chip_8.reg[x] = r & nn
elif op == 0xD:
# display
logging.info(f"display instruction at ({chip_8.reg[x]:02X},{chip_8.reg[y]:02X}) that is {n} rows tall\n")
cycle = 0
dx = chip_8.reg[x] % 0x40
dy = 0x1F - chip_8.reg[y] % 0x20
chip_8.reg[0xF] = 0
for i in range(n):
if dy - i < 0:
break
if dy - i > 0x20:
break
membyt = chip_8.memory[chip_8.index + i]
for j in range(0x8):
if dx + j > 0x3F:
break
dispbit = (chip_8.display[dy - i] >> (0x3F - dx - j)) & 0x1
membit = (membyt >> (0x7 - j)) & 0x1
if membit == 1:
chip_8.display[dy - i] = chip_8.display[dy - i] ^ (0x1 << (0x3F - dx - j))
if dispbit == 1:
for item in win.items[:]:
first_point, second_point = literal_eval(re.split(r"\(|\)", str(item))[2]), literal_eval(re.split(r"\(|\)", str(item))[4])
if int(first_point[0]) == dx + j and int(first_point[1]) == dy - i:
item.undraw()
break
chip_8.reg[0xF] = 1
else:
rect = Rectangle(Point(dx + j, dy - i), Point(dx + j + 1, dy - i + 1))
rect.setFill("black")
rect.draw(win)
win.update()
elif op == 0xE and nn == 0x9E:
# skip if key is pressed
cycle = .000073
ind = chip_8.corr.index(chip_8.reg[x])
logging.info(f"skip if \"{chip_8.keys[ind]}\" is pressed\n")
if keyboard.is_pressed(chip_8.keys[ind]):
logging.info(f"\"{chip_8.keys[ind]}\" is indeed pressed\n")
chip_8.pc += 2
elif op == 0xE and nn == 0xA1:
# skip if key is not pressed
cycle = .000073
ind = chip_8.corr.index(chip_8.reg[x])
logging.info(f"skip if \"{chip_8.keys[ind]}\" is not pressed\n")
if not keyboard.is_pressed(chip_8.keys[ind]):
logging.info(f"\"{chip_8.keys[ind]}\" is indeed not pressed\n")
chip_8.pc += 2
elif op == 0xF:
# various instructions
if nn == 0x07:
# this and two next are timers
logging.info(f"v{x:01X} is now 0x{chip_8.reg[x]:03X} from delay\n")
cycle = .000045
chip_8.reg[x] = int(chip_8.delay)
elif nn == 0x15:
logging.info(f"delay is now 0x{chip_8.reg[x]:03X} from v{x:01X}\n")
cycle = .000045
chip_8.delay = chip_8.reg[x]
elif nn == 0x18:
logging.info(f"sound is now 0x{chip_8.reg[x]:03X} from v{x:01X}\n")
cycle = .000045
chip_8.sound = chip_8.reg[x]
sound_thread = threading.Thread(target=beep, args=(chip_8.sound,))
sound_thread.start()
elif nn == 0x1E:
# add to index
logging.info(f"add v{x:01X} = {chip_8.reg[x]:03X} to index\n")
cycle = .000086
chip_8.index += chip_8.reg[x]
if chip_8.index > 0xFFF:
chip_8.index %= 0x1000
chip_8.reg[0xF] = 1
logging.info(f"index is now {chip_8.index:03X}\n")
elif nn == 0x0A:
cycle = 0
while True:
k = keyboard.read_key()
if k not in chip_8.keys:
continue
ind = chip_8.keys.index(k)
break
chip_8.reg[x] = chip_8.corr[ind]
logging.info(f"store 0x{chip_8.reg[x]:03X} in v{x:01X} from \"{chip_8.corr[ind]}\" being pressed\n")
elif nn == 0x29:
# font character
cycle = .000091
chip_8.index = 0x50 + chip_8.reg[x] * 0x5
logging.info(f"index is now at the font character \"{chip_8.reg[x]:01X}\"\n")
elif nn == 0x33:
# binary-coded decimal conversion
logging.info(f"put v{x:01X} = {chip_8.reg[x]:02X} into memory[{chip_8.index:03X}], in decimal\n")
cycle = .000927
b = chip_8.reg[x]
chip_8.memory[chip_8.index + 2] = b % 10
b = b // 10
chip_8.memory[chip_8.index + 1] = b % 10
b = b // 10
chip_8.memory[chip_8.index] = b % 10
elif nn == 0x55:
# store memory
logging.info(f"store registers into memory at memory[{chip_8.index:03X}] up to v{x:01X}\n")
cycle = .000605
if inc:
for i in range(x + 1):
chip_8.memory[chip_8.index] = chip_8.reg[i]
chip_8.index += 1
else:
for i in range(x + 1):
chip_8.memory[chip_8.index + i] = chip_8.reg[i]
elif nn == 0x65:
# load memory
logging.info(f"load registers from memory from memory[{chip_8.index:03X}] up to v{x:01X}\n")
cycle = .000605
if inc:
for i in range(x + 1):
chip_8.reg[i] = chip_8.memory[chip_8.index]
chip_8.index += 1
else:
for i in range(x + 1):
chip_8.reg[i] = chip_8.memory[chip_8.index + i]
else:
# unmapped instruction
break
cycle /= 60
if cycle > 0:
sleep(cycle)
chip_8.delay -= (time() - tick) * 60
if chip_8.delay < 0:
chip_8.delay = 0
win.getMouse()