-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy2600.py
481 lines (434 loc) · 13.3 KB
/
py2600.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import sys
import time
import pygame
import pygame.locals
pygame.init()
# Organisation
MEM_SIZE = 8192
STACK_SIZE = 256
# ISA
NOP = 0x00
LOAD = 0x01
STORE = 0x02
INC = 0x03
INCM = 0x15
INCMIP = 0x17
DEC = 0x04
ADD = 0x05
SUB = 0x06
MUL = 0X07
DIV = 0X08
PUSH = 0x09
IFCMPEQ = 0x0A
IFCMPLT = 0x14
JMP = 0x16
CP = 0x0B
CPIP = 0x0C
CPINC = 0x0D
INT = 0x0E
PUSHA = 0x0F
BRK = 0x10
CPV = 0x11 # Video Buffer ops
CPIPV = 0x12 # Video Buffer ops
CPINCV = 0x13 # Video Buffer ops
END = 0xFF
memory = bytearray(MEM_SIZE)
stack = bytearray()
# def tty_print():
# txt = map(lambda x: chr(x), memory[3840:])
# print ''.join(txt)
it = []
# it.append((0x80, tty_print))
# ///////////////////////////// Graphics Display Config /////////////////////////
WIDTH = 800
HEIGHT = 800
TIA_WIDTH = 160
TIA_HEIGHT = 192
GFX_ADDR = 0x1DFF
pal = []
pal.append(0x000000)
pal.append(0x404040)
pal.append(0x6c6c6c)
pal.append(0x909090)
pal.append(0xb0b0b0)
pal.append(0xc8c8c8)
pal.append(0xdcdcdc)
pal.append(0xececec)
pal.append(0x444400)
pal.append(0x646410)
pal.append(0x848424)
pal.append(0xa0a034)
pal.append(0xb8b840)
pal.append(0xd0d050)
pal.append(0xe8e85c)
pal.append(0xfcfc68)
pal.append(0x702800)
pal.append(0x844414)
pal.append(0x985c28)
pal.append(0xac783c)
pal.append(0xbc8c4c)
pal.append(0xcca05c)
pal.append(0xdcb468)
pal.append(0xecc878)
pal.append(0x841800)
pal.append(0x983418)
pal.append(0xac5030)
pal.append(0xc06848)
pal.append(0xd0805c)
pal.append(0xe09470)
pal.append(0xeca880)
pal.append(0xfcbc94)
pal.append(0x880000)
pal.append(0x9c2020)
pal.append(0xb03c3c)
pal.append(0xc05858)
pal.append(0xd07070)
pal.append(0xe08888)
pal.append(0xeca0a0)
pal.append(0xfcb4b4)
pal.append(0x78005c)
pal.append(0x8c2074)
pal.append(0xa03c88)
pal.append(0xb0589c)
pal.append(0xc070b0)
pal.append(0xd084c0)
pal.append(0xdc9cd0)
pal.append(0xecb0e0)
pal.append(0x480078)
pal.append(0x602090)
pal.append(0x783ca4)
pal.append(0x8c58b8)
pal.append(0xa070cc)
pal.append(0xb484dc)
pal.append(0xc49cec)
pal.append(0xd4b0fc)
pal.append(0x140084)
pal.append(0x302098)
pal.append(0x4c3cac)
pal.append(0x6858c0)
pal.append(0x7c70d0)
pal.append(0x9488e0)
pal.append(0xa8a0ec)
pal.append(0xbcb4fc)
pal.append(0x000088)
pal.append(0x1c209c)
pal.append(0x3840b0)
pal.append(0x505cc0)
pal.append(0x6874d0)
pal.append(0x7c8ce0)
pal.append(0x90a4ec)
pal.append(0xa4b8fc)
pal.append(0x00187c)
pal.append(0x1c3890)
pal.append(0x3854a8)
pal.append(0x5070bc)
pal.append(0x6888cc)
pal.append(0x7c9cdc)
pal.append(0x90b4ec)
pal.append(0xa4c8fc)
pal.append(0x002c5c)
pal.append(0x1c4c78)
pal.append(0x386890)
pal.append(0x5084ac)
pal.append(0x689cc0)
pal.append(0x7cb4d4)
pal.append(0x90cce8)
pal.append(0xa4e0fc)
pal.append(0x003c2c)
pal.append(0x1c5c48)
pal.append(0x387c64)
pal.append(0x509c80)
pal.append(0x68b494)
pal.append(0x7cd0ac)
pal.append(0x90e4c0)
pal.append(0xa4fcd4)
pal.append(0x003c00)
pal.append(0x205c20)
pal.append(0x407c40)
pal.append(0x5c9c5c)
pal.append(0x74b474)
pal.append(0x8cd08c)
pal.append(0xa4e4a4)
pal.append(0xb8fcb8)
pal.append(0x143800)
pal.append(0x345c1c)
pal.append(0x507c38)
pal.append(0x6c9850)
pal.append(0x84b468)
pal.append(0x9ccc7c)
pal.append(0xb4e490)
pal.append(0xc8fca4)
pal.append(0x2c3000)
pal.append(0x4c501c)
pal.append(0x687034)
pal.append(0x848c4c)
pal.append(0x9ca864)
pal.append(0xb4c078)
pal.append(0xccd488)
pal.append(0xe0ec9c)
pal.append(0x442800)
pal.append(0x644818)
pal.append(0x846830)
pal.append(0xa08444)
pal.append(0xb89c58)
pal.append(0xd0b46c)
pal.append(0xe8cc7c)
pal.append(0xfce08c)
# pygame.init()
screen = None
tia = None
tia_buffer = [[0x00 for _ in range(0, TIA_HEIGHT)] for _ in range(0, TIA_WIDTH)]
def configure_gfx():
global screen, tia
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Py2600')
tia = pygame.Surface((TIA_WIDTH, TIA_HEIGHT)).convert()
print(tia.get_locked())
def blit_array():
_buffer = pygame.surfarray.array2d(tia)
_buffer[:] = tia_buffer[:]
pygame.surfarray.blit_array(tia, _buffer)
def gfx_blit():
global tia
blit_array()
# pygame.surfarray.blit_array(tia, tia_buffer)
# blit_array()
screen.blit(pygame.transform.scale(tia, (WIDTH, HEIGHT)), (0,0))
pygame.display.update()
# for i in range(TIA_HEIGHT):
# tia_buffer[i][:] = map(lambda x: pal[x], memory[GFX_ADDR:])
it.append((0x42, gfx_blit))
# ///////////////////////////// End of Graphics Display Config /////////////////////////
# //////////////////////////////////// Input Config ///////////////////////////////////
INPUT_ADDR = 0x1FFF
UP = pygame.locals.K_UP
DOWN = pygame.locals.K_DOWN
LEFT = pygame.locals.K_LEFT
RIGHT = pygame.locals.K_RIGHT
A = pygame.locals.K_z
B = pygame.locals.K_x
# //////////////////////////////////// End of Input Config ///////////////////////////
debug_msg = False
def debug_cmd():
cmd = input('dbg:> ')
if cmd == 'n' or cmd == '':
return True
if cmd == 'setm':
addr = int(raw_input('addr:> '), 16)
value = int(raw_input('value:> '), 16)
memory[addr] = value
return True
print('quitting.. Press any key to continue...')
return False
def print_stack():
print('->'.join(stack))
print('')
def run():
ip = 0
print("running")
while memory[ip] != END:
# print ip
op = memory[ip]
if op == PUSH:
if debug_msg:
print('push: ' + hex(memory[ip + 1]) + ' (' + str(memory[ip + 1]) + ')')
stack.append(memory[ip + 1])
ip += 1
elif op == PUSHA:
if debug_msg:
print('push_addr: ' + hex((memory[ip + 2] << 8) | memory[ip + 1]) + ' (' + str((memory[ip + 2] << 8) | memory[ip + 1]) + ')')
stack.append(memory[ip + 1])
stack.append(memory[ip + 2])
ip += 2
elif op == LOAD:
addr = (stack.pop() << 8) | stack.pop()
if debug_msg:
print('load ' + str(memory[addr]) + ' from ' + hex(addr) + '(' + str(addr) + ')')
stack.append(memory[addr])
elif op == STORE:
addr = (stack.pop() << 8) | stack.pop()
value = stack.pop()
memory[addr] = value
if debug_msg:
print('store ' + hex(value) + ' (' + str(value) + ') -> ' + hex(addr) + '(' + str(addr) + ')')
elif op == INC:
stack[-1] = min(0xFF, stack[-1] + 1)
elif op == INCMIP:
addr = (stack[-1] << 8) | stack[-2]
memory[addr] = min(0xFF, memory[addr] + 1)
if debug_msg:
print('incm at ' + hex(addr) + ' to ' + str(memory[addr]))
elif op == INCM:
addr = (stack.pop() << 8) | stack.pop()
memory[addr] = min(0xFF, memory[addr] + 1)
if debug_msg:
print('incm at ' + hex(addr) + ' to ' + str(memory[addr]))
elif op == DEC:
opr = stack.pop()
opr -= 1
opr = max(0, opr)
stack.append(opr)
elif op == ADD:
s1 = stack.pop()
s2 = stack.pop()
stack.append(min(255, s1 + s2))
elif op == SUB:
s1 = stack.pop()
s2 = stack.pop()
stack.append(max(0, s1 - s2))
elif op == MUL:
m1 = stack.pop()
m2 = stack.pop()
stack.append(min(255, m1 * m2))
elif op == DIV:
d1 = stack.pop()
d2 = stack.pop()
stack.append(m1 / m2)
elif op == IFCMPEQ:
if stack.pop() == stack.pop():
ip = max((memory[ip + 2] << 8) | memory[ip + 1], 0)
ip -= 1
if debug_msg:
print('ifcmpeq: true, jmp to ' + hex(ip) + '(' + str(ip) + ')')
else:
ip += 2
elif op == IFCMPLT:
v1 = stack.pop()
v2 = stack.pop()
if v2 < v1:
if debug_msg:
print('ifcmplt ' + str(v2) + ' < ' + str(v1))
ip = max((memory[ip + 2] << 8) | memory[ip + 1], 0)
ip -= 1
if debug_msg:
print('\tjump to ' + hex(ip) + ' (' + str(ip) + ')')
else:
if debug_msg:
print('ifcmplt ' + str(v2) + ' >= ' + str(v1))
ip += 2
elif op == JMP:
addr = (memory[ip + 2] << 8) | memory[ip + 1]
ip = max(addr, 0)
ip -= 1
if debug_msg:
print('jump to ' + hex(ip) + ' (' + str(ip) + ')')
elif op == CP:
src = (stack.pop() << 8) | stack.pop()
dest = (stack.pop() << 8) | stack.pop()
if debug_msg:
print('copy: ' + str(src) + ' -> ' + str(dest))
print('copy: ' + str(memory[src]) + ' -> ' + str(memory[dest]))
print(memory[src - 1])
memory[dest] = memory[src]
elif op == CPIP:
src = (stack[-1] << 8) | stack[-2]
dest = (stack[-3] << 8) | stack[-4]
memory[dest] = memory[src]
elif op == CPINC:
src = (stack.pop() << 8) | stack.pop()
dest = (stack.pop() << 8) | stack.pop()
if debug_msg:
print('copy_inc: ' + str(src) + ' -> ' + str(dest))
print('copy_inc: ' + str(memory[src]) + ' -> ' + str(memory[dest]))
memory[dest] = memory[src]
src += 1
dest += 1
stack.append(dest & 0x00FF)
stack.append((dest & 0xFF00) >> 8)
stack.append(src & 0x00FF)
stack.append(src & 0xFF00)
elif op == CPV:
src = (stack.pop() << 8) | stack.pop()
destx = stack.pop()
desty = stack.pop()
if debug_msg:
print('copyv: ' + str(src) + ' -> ' + str((destx, desty)))
# print 'copyv: ' + str(memory[src]) + ' -> ' + str(memory[dest])
print(memory[src - 1])
tia_buffer[destx][desty] = pal[memory[src]]
elif op == CPIPV:
src = (stack[-1] << 8) | stack[-2]
destx = stack[-3]
desty = stack[-4]
tia_buffer[destx][desty] = pal[memory[src]]
elif op == CPINCV:
src = (stack.pop() << 8) | stack.pop()
destx = stack.pop()
desty = stack.pop()
if debug_msg:
print('copy_incv: ' + hex(src) + '(' + hex(pal[memory[src]]) + ')' + ' -> ' + str((destx, desty)))
# print 'copy_inc: ' + str(memory[src]) + ' -> ' + str(memory[dest])
tia_buffer[destx][desty] = pal[memory[src]]
src += 1
destx += 1
if destx >= 160:
destx = 0
desty += 1
stack.append(desty)
stack.append(destx)
stack.append(src & 0x00FF)
stack.append(src & 0xFF00)
elif op == INT:
ic = memory[ip + 1]
if debug_msg:
print('interrupt: ' + str(ic))
cb = None
for ih in it:
if ih[0] == ic:
cb = ih[1]
if cb:
cb()
ip += 1
elif op == NOP:
if debug_msg:
print('nop')
ip += 1
for event in pygame.event.get():
if event.type == pygame.locals.QUIT:
return
if event.type == pygame.locals.KEYDOWN:
if event.key == UP:
memory[INPUT_ADDR] = memory[INPUT_ADDR] | 0x01
elif event.key == DOWN:
memory[INPUT_ADDR] = memory[INPUT_ADDR] | 0x02
elif event.key == LEFT:
memory[INPUT_ADDR] = memory[INPUT_ADDR] | 0x04
elif event.key == RIGHT:
memory[INPUT_ADDR] = memory[INPUT_ADDR] | 0x08
elif event.key == A:
memory[INPUT_ADDR] = memory[INPUT_ADDR] | 0x10
elif event.key == B:
memory[INPUT_ADDR] = memory[INPUT_ADDR] | 0x20
elif event.type == pygame.locals.KEYUP:
if event.key == UP:
memory[INPUT_ADDR] = memory[INPUT_ADDR] & 0xfe
elif event.key == DOWN:
memory[INPUT_ADDR] = memory[INPUT_ADDR] & 0xfd
elif event.key == LEFT:
memory[INPUT_ADDR] = memory[INPUT_ADDR] & 0xfb
elif event.key == RIGHT:
memory[INPUT_ADDR] = memory[INPUT_ADDR] & 0xf7
elif event.key == A:
memory[INPUT_ADDR] = memory[INPUT_ADDR] & 0xef
elif event.key == B:
memory[INPUT_ADDR] = memory[INPUT_ADDR] & 0xdf
if debug_msg:
print('\nStack:')
for e in reversed(stack):
print(hex(e) + ' == ' + str(e))
print('---------------')
if not debug_cmd():
return
print("done")
# if debug_msg:
# print_stack()
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: py2600 rom_file")
sys.exit()
with open(sys.argv[1], 'rb') as rom_file:
program = bytearray(rom_file.read(MEM_SIZE))
memory[0:len(program)] = program
configure_gfx()
run()
input()