-
Notifications
You must be signed in to change notification settings - Fork 1
/
chip8.py
executable file
·333 lines (307 loc) · 5.73 KB
/
chip8.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
#!/usr/bin/python
from time import sleep
from os import urandom
from pygame import display, HWSURFACE, DOUBLEBUF, Color, draw, key
import pygame
import sys
scale_factor=10
display.init()
s = display.set_mode(((64*scale_factor),(32*scale_factor)), HWSURFACE | DOUBLEBUF, 8)
s.fill(Color(0,0,0,255))
display.flip()
v = [0 for i in xrange(16)]
stack=[]
pc=0x200
mem = [0 for i in xrange(4096)]
I=0
disp = [[0 for i in xrange(64)] for j in xrange(32)]
key_map = {
49:1,
50:2,
51:3,
52:4,
53:5,
54:6,
55:7,
56:8,
57:9,
58:0,
97:0xa,
98:0xb,
99:0xc,
100:0xd,
101:0xe,
102:0xf
}
DT=0
key_inv_map = {v: k for k, v in key_map.iteritems()}
class PCOutOfBoundsError(Exception):
pass
colormap = {
0: Color(0, 0, 0, 255),
1: Color(255, 255, 255, 255)
}
def update_display():
for i in xrange(32):
for j in xrange(64):
draw.rect(s,colormap[disp[i][j]],(j*scale_factor,i*scale_factor,scale_factor,scale_factor))
display.flip()
def load_rom(fname):
try:
rom = map(ord,open(fname,'rb').read())
if len(rom) > 4096-0x200:
raise Exception
except:
print "Error importing CHIP-8 rom \"{}\".".format(fname)
exit(2)
font = open("hexsprites",'rb').read()
for i,val in enumerate(font):
mem[i]=ord(val)
for i in xrange(len(rom)):
mem[i + 0x200] = rom[i]
def get_inst():
return (( (mem[pc] << 8) & 0xffff )+( mem[pc+1] & 0xff )) & 0xffff
def getx(inst):
return (inst & 0xf00) >> 8
def gety(inst):
return (inst & 0xf0) >> 4
def getnnn(inst):
return inst & 0xfff
def getbyte(inst):
return inst & 0xff
def getnib(inst):
return inst & 0xf
def exec_inst():
global pc
global I
global DT
inst = get_inst()
nnn = getnnn(inst)
x = getx(inst)
y = gety(inst)
kk = getbyte(inst)
nib = getnib(inst)
if inst == 0x00e0:
pc+=2
for i in xrange(32):
disp[i]=[0 for j in xrange(64)]
update_display()
return 0
if inst == 0x00ee:
pc=stack.pop()
return 0
if inst & 0xf000 == 0x0000:
pc+=2
stack.append(pc)
pc = nnn
return 0
if inst & 0xf000 == 0x1000:
pc = nnn
return 0
if inst & 0xf000 == 0x2000:
pc+=2
stack.append(pc)
pc = nnn
return 0
if inst & 0xf000 == 0x3000:
pc+=2
if v[x] == kk:
pc+=2
return 0
if inst & 0xf000 == 0x4000:
pc+=2
if v[x] != kk:
pc+=2
return 0
if inst & 0xf00f == 0x5000:
pc+=2
if v[x] == v[y]:
pc+=2
return 0
if inst & 0xf000 == 0x6000:
pc+=2
v[x] = kk & 0xff
return 0
if inst & 0xf000 == 0x7000:
pc+=2
v[x] += kk
v[x] = v[x] & 0xff
return 0
if inst & 0xf00f == 0x8000:
pc+=2
v[x] = v[y]
return 0
if inst & 0xf00f == 0x8001:
pc+=2
v[x] |= v[y]
return 0
if inst & 0xf00f == 0x8002:
pc+=2
v[x] &= v[y]
return 0
if inst & 0xf00f == 0x8003:
pc+=2
v[x] ^= v[y]
return 0
if inst & 0xf00f == 0x8004:
pc+=2
v[x] += v[y]
if v[x] > 0xff:
v[0xf] = 1
else:
v[0xf] = 0
v[x] &= 0xff
return 0
if inst & 0xf00f == 0x8005:
pc+=2
if v[x] > v[y]:
v[0xf] = 1
else:
v[0xf] = 0
v[x] -= v[y]
v[x] &= 0xff
return 0
if inst & 0xf00f == 0x8006:
pc+=2
v[0xf] = v[y] & 0x1
v[y] = v[x] = v[y] >> 1
return 0
if inst & 0xf00f == 0x8007:
pc+=2
if v[x] < v[y]:
v[0xf] = 1
else:
v[0xf] = 0
v[x] = v[y]-v[x]
v[x] &= 0xff
return 0
if inst & 0xf00f == 0x800e:
pc+=2
v[0xf] = v[y] & 0x1
v[y] = v[x] = (v[y] << 1) & 0xff
return 0
if inst & 0xf00f == 0x9000:
pc+=2
if v[x] != v[y]:
pc+=2
return 0
if inst & 0xf000 == 0xa000:
pc+=2
I = nnn
return 0
if inst & 0xf000 == 0xb000:
pc = v[0]+nnn
return 0
if inst & 0xf000 == 0xc000:
pc += 2
v[x] = (ord(urandom(1)) & kk) & 0xff
return 0
if inst & 0xf000 == 0xd000:
pc+=2
sprite = [map(int,bin(i)[2:].rjust(8,"0")) for i in mem[I:I+nib]]
v[0xf] = 0
for i in xrange(nib):
for j in xrange(8):
if disp[(v[y]+i) % 32][(v[x]+j) % 64] == sprite[i][j] == 1:
v[0xf]=1
disp[(v[y]+i) % 32][(v[x]+j) % 64] ^= sprite[i][j]
update_display()
return 0
if inst & 0xf0ff == 0xe09e:
pc+=2
pygame.event.get()
key_stat = key.get_pressed()
if key_stat[key_inv_map[v[x]]]==1:
pc+=2
return 0
if inst & 0xf0ff == 0xe0a1:
pc+=4
key_stat=[]
pygame.event.get()
key_stat = key.get_pressed()
if key_stat[key_inv_map[v[x]]]==1:
pc-=2
return 0
if inst & 0xf0ff == 0xf007:
pc+=2
v[x] = DT & 0xff
return 0
if inst & 0xf0ff == 0xf00a:
pc+=2
f=0
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
temp = key.get_pressed()
for i in key_inv_map:
if temp[key_inv_map[i]]:
v[x]=i
f=1
break
if f==1:
break
if f==1:
break
return 0
if inst & 0xf0ff == 0xf015:
pc+=2
DT=v[x]
return 0
if inst & 0xf0ff == 0xf018:
pc+=2
return 0
if inst & 0xf0ff == 0xf01e:
pc+=2
I+=v[x]
return 0
if inst & 0xf0ff == 0xf029:
pc+=2
I = v[x]*5
return 0
if inst & 0xf0ff == 0xf033:
pc+=2
mem[I] = v[x]/100
mem[I+1] = v[x]/10 % 10
mem[I+2] = v[x] % 10
return 0
if inst & 0xf0ff == 0xf055:
pc+=2
for i in xrange(x+1):
mem[I+i] = v[i]
return 0
if inst & 0xf0ff == 0xf065:
pc+=2
for i in xrange(x+1):
v[i] = mem[I+i] & 0xff
return 0
return 1
def main():
global DT
if len(sys.argv)==1:
print "usage: ./chip8.py <chip-8-rom>"
exit(1)
pygame.init()
load_rom(sys.argv[1])
last_ticks = pygame.time.get_ticks()
while True: # main loop
try:
for event in pygame.event.get():
if event.type == pygame.QUIT:
display.quit()
pygame.quit()
exit()
exit_flag = exec_inst()
now_ticks = pygame.time.get_ticks()
if now_ticks - last_ticks > 16:
diff = now_ticks - last_ticks
ticks_passed = diff / 16
DT = max(0,DT-ticks_passed)
last_ticks = now_ticks - diff % 16
except Exception as e:
print e
break
if exit_flag:
break
if __name__ == "__main__":
main()
exit(0)