-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembler.py
56 lines (41 loc) · 1.35 KB
/
assembler.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
import sys
import py2600
code = ''
with open(sys.argv[1], 'r') as source_file:
code = source_file.read().strip()
bytecode = bytearray()
program = code.split('\n')
ip = 0
data_segs = False
while ip < len(program):
if not data_segs:
segs = program[ip].split(' ')
if segs[0] == 'PUSH':
bytecode.append(py2600.PUSH)
bytecode.append(int(segs[1], 16))
elif segs[0] == 'ADD':
bytecode.append(py2600.ADD)
elif segs[0] == 'INC':
bytecode.append(py2600.INC)
elif segs[0] == 'DEC':
bytecode.append(py2600.DEC)
elif segs[0] == 'CP':
bytecode.append(py2600.CP)
elif segs[0] == 'CPINC':
bytecode.append(py2600.CPINC)
elif segs[0] == 'INT':
bytecode.append(py2600.INT)
bytecode.append(int(segs[1], 16))
elif segs[0] == 'END':
bytecode.append(0xFF)
data_segs = True
print 'end of program'
elif segs[0] == 'label':
labels.append(segs[1])
else:
bytecode.append(int(program[ip], 16))
print int(program[ip], 16)
ip += 1
# bytecode.append(0xff)
with open(sys.argv[1].replace('.asm', '.rom'), 'wb') as rom_file:
rom_file.write(memoryview(bytecode).tobytes())