Skip to content

Commit efbb8a0

Browse files
Works on the assembler/disassembler
1 parent f49ed8e commit efbb8a0

6 files changed

+346
-197
lines changed

assemble_rom.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
5+
from tools import assembler
6+
7+
8+
def assemble(rompath, outpath):
9+
assembler.assemble_file(rompath, outpath)
10+
11+
12+
if __name__ == "__main__":
13+
if len(sys.argv) < 3:
14+
print("Usage: python disassemble_rom.py rompath output_path")
15+
exit(0)
16+
assemble(sys.argv[1], sys.argv[2])

disassemble_rom.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
5+
from tools import disassembler
6+
7+
8+
def disassemble(rompath, outpath):
9+
disassembler.disassemble_rom(rompath, outpath)
10+
11+
12+
if __name__ == "__main__":
13+
if len(sys.argv) < 3:
14+
print("Usage: python disassemble_rom.py rompath output_path")
15+
exit(0)
16+
disassemble(sys.argv[1], sys.argv[2])

scripts/disassemble_rom.py

-15
This file was deleted.

tools/Disassembler.py

-182
This file was deleted.

tools/assembler.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
def process_line(line) -> int:
2+
tokens = line.split(";")[0].strip(",").strip("\n").split(" ")
3+
4+
if tokens[0] == "CLS":
5+
return 0x00E0
6+
7+
if tokens[0] == "RET":
8+
return 0x00EE
9+
10+
if tokens[0] == "JP":
11+
if tokens[1] == "V0":
12+
return 0xB000 + int(tokens[1], base=16)
13+
else:
14+
return 0x1000 + int(tokens[1], base=16)
15+
16+
if tokens[0] == "CALL":
17+
return 0x2000 + int(tokens[1], base=16)
18+
19+
if tokens[0] == "SE":
20+
return 0x5000 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16) << 4
21+
22+
if tokens[0] == "SEB":
23+
return 0x3000 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16)
24+
25+
26+
27+
if tokens[0] == "SNE":
28+
if len(tokens[2]) == 4: # SNE Vx kk
29+
return 0x4000 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16)
30+
31+
if len(tokens[2]) == 3: # SNE Vx Vy
32+
return 0x9000 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16) << 4
33+
34+
if tokens[0] == "LD":
35+
if tokens[1] == "I":
36+
return 0xA000 + int(tokens[2], base=16)
37+
if tokens[2] == "DT":
38+
return 0xF007 + int(tokens[1], base=16) << 8
39+
if tokens[2] == "K":
40+
return 0xF00A + int(tokens[1], base=16) << 8
41+
if tokens[1] == "DT":
42+
return 0xF015 + int(tokens[2], base=16) << 8
43+
if tokens[1] == "ST":
44+
return 0xF018 + int(tokens[2], base=16) << 8
45+
if tokens[1] == "F":
46+
return 0xF029 + int(tokens[2], base=16) << 8
47+
if tokens[1] == "[I]":
48+
return 0xF055 + int(tokens[2], base=16) << 8
49+
if tokens[2] == "[I]":
50+
return 0xF065 + int(tokens[1], base=16) << 8
51+
52+
if tokens[0] == "ADD":
53+
if tokens[1] == "I":
54+
return 0xF01E + int(tokens[2], base=16) << 8
55+
56+
if len(tokens[2]) == 3: # ADD Vx Vy
57+
return 0x8004 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16) << 4
58+
59+
if tokens[0] == "ADDB":
60+
return 0x7000 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16)
61+
62+
if tokens[0] == "OR":
63+
return 0x8001 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16) << 4
64+
65+
if tokens[0] == "AND":
66+
return 0x8002 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16) << 4
67+
68+
if tokens[0] == "XOR":
69+
return 0x8003 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16) << 4
70+
71+
if tokens[0] == "SUB":
72+
return 0x8005 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16) << 4
73+
74+
if tokens[0] == "SHR":
75+
return 0x8006 + int(tokens[1], base=16) << 8
76+
77+
if tokens[0] == "SHL":
78+
return 0x800E + int(tokens[1], base=16) << 8
79+
80+
if tokens[0] == "SUBN":
81+
return 0x8007 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16) << 4
82+
83+
if tokens[0] == "RND":
84+
return 0xC000 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16)
85+
86+
if tokens[0] == "DRW":
87+
return 0xD000 + int(tokens[1], base=16) << 8 + int(tokens[2], base=16) << 4 + int(tokens[3], base=16)
88+
89+
if tokens[0] == "SKP":
90+
return 0xE092 + int(tokens[1], base=16) << 8
91+
92+
if tokens[0] == "SKNP":
93+
return 0xE0A1 + int(tokens[1], base=16) << 8
94+
95+
96+
def assemble_program(assembly) -> list:
97+
opcodes = []
98+
for line in assembly:
99+
opcodes.append(process_line(line))
100+
#opcodes = list(filter(lambda x: x is not None, opcodes))
101+
102+
bytearr = []
103+
for i in range(0, len(opcodes), 2):
104+
bytearr.append((opcodes[i] & 0xFF00) >> 8)
105+
bytearr.append(opcodes[i] & 0x00FF)
106+
107+
return bytearr
108+
109+
110+
def assemble_file(code_path, output_file):
111+
with open(code_path, "r") as f:
112+
assembly = f.readlines()
113+
114+
with open(output_file, "wb") as f:
115+
f.write(bytearray(assemble_program(assembly)))

0 commit comments

Comments
 (0)