This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
166 lines (139 loc) · 4.18 KB
/
compile.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
from jinja2 import Template
from typing import Dict
import re
def hexa(i):
h = hex(i)
if len(h) == 3:
return "0" + h[2]
else:
return h[2:]
MNEMS = [
("00E0" , lambda : "Clear"),
("00EE" , lambda : "Return"),
("0(...)", lambda x: "Deprecated (Machine Exec)"),
("1(...)" , lambda x: f"Jump {x}"),
("2(...)" , lambda x: f"Exec {x}"),
("3(.)(..)" , lambda x, y: f"Skip v{x}=={y}"),
("4(.)(..)" , lambda x, y: f"Skip v{x}!={y}"),
("5(.)(.)0" , lambda x, y: f"Skip v{x}==v{y}"),
("6(.)(..)" , lambda x, y: f"v{x} = {y}"),
("7(.)(..)" , lambda x, y: f"v{x} += {y}"),
("8(.)(.)0" , lambda x, y: f"v{x} = v{y}"),
("8(.)(.)1" , lambda x, y : f"v{x} |= v{y}"),
("8(.)(.)2" , lambda x, y: f"v{x} &= v{y}"),
("8(.)(.)3" , lambda x, y: f"v{x} ^= v{y}"),
("8(.)(.)4" , lambda x, y: f"v{x} += v{y}"),
("8(.)(.)5" , lambda x, y: f"v{x} -= v{y}"),
("8(.)(.)6" , lambda x, y: f"v{x} =>> v{y}"),
("8(.)(.)7" , lambda x, y: f"v{x} = v{y} - vX"),
("8(.)(.)E" , lambda x, y: f"v{x} =<< v{y}"),
("9(.)(.)0" , lambda x, y: f"Skip{x}!={y}"),
("A(...)" , lambda x: f"i = {x}"),
("B(...)" , lambda x: f"Jump {x} + v0"),
("C(.)(..)" , lambda x, y: f"v{x} = Rand && {y}"),
("D(.)(.)(.)" , lambda x, y, z: f"Sprite {x} {y} h{z}"),
("E(.)9E" , lambda x: f"SkipKey v{x}"),
("E(.)A1" , lambda x: f"SkipNotKey v{x}"),
("F(.)07" , lambda x: f"DelayTo v{x}"),
("F(.)0A" , lambda x: f"Key v{x}"),
("F(.)15" , lambda x: f"v{x} ToDelay"),
("F(.)18" , lambda x: f"Sound v{x}"),
("F(.)1E" , lambda x: f"I+=v{x}"),
("F(.)29" , lambda x: f"i = Font v{x}"),
("F(.)33" , lambda x: f"StoreBinary v{x}"),
("F(.)55" , lambda x: f"StoreRegisters v{x}"),
("F(.)65" , lambda x: f"LoadRegisters v{x}"),
]
with open("code") as f:
lines = f.readlines()
out = []
counter = 512
labels : Dict[str, int] = {}
def op(s):
out.append(int(s, 16))
global counter
counter += 1
def fake(s):
out.append(s)
global counter
counter += 2
def location(s):
if s[0] == "@":
l = labels[s[1:]]
else:
l = int(s, 16)
h = hexa(l)
return int(h[::-1][2:4][::-1], 16), int(h[::-1][0:2][::-1], 16)
for line in lines:
l = line.split(" ")
l[-1] = l[-1].strip()
if l[0] == "set":
fake("A" + l[1])
elif l[0] == "jump":
fake("1"+ l[1])
elif l[0] == "exec":
fake("2"+ l[1])
elif l[0] == "label":
labels[l[1]] = counter
elif l[0][0] == "v":
op("6" + l[0][1])
op(l[1])
elif l[0] == "sprite":
op("D" + l[1][1])
op(l[2][1] + l[3])
elif l[0] == "inc":
op("F" + l[1][1]) # l1 = "vX"
op("18")
elif l[0] == "key":
v = l[1][1]
op("F" + v)
op("0A")
elif l[0] == "add":
op("7" + l[1][1])
op(l[2])
elif l[0] == "sub":
op("8" + l[1][1])
op(l[2][1] + "5")
elif l[0] == "return":
op("00")
op("EE")
elif l[0] == "skip" and l[1] == "if" and l[3] == "not":
op("4" + l[2][1])
op(l[4])
else:
op(l[0])
codes = []
def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
return ((num == 0) and numerals[0]) or (baseN(num // b, b, numerals).lstrip(numerals[0]) + numerals[num % b])
for c in out:
if type(c) == str:
l1, l2 = location(c[1:])
print(c[1:])
print(hexa(l1))
print(hexa(l2))
codes.append(int(c[0] + hex(l1)[2:], 16))
codes.append(l2)
else:
codes.append(c)
for label in labels:
print(f"{label}: {baseN(labels[label], 16)}")
def get_mem(code):
for mem in MNEMS:
m = re.match(mem[0], code)
if m:
return mem[1](*m.groups())
return "?"
for i in range(0, len(codes), 2):
if i < len(codes)-1:
h1, h2 = hexa(codes[i]).upper(), hexa(codes[i+1]).upper()
mem = get_mem(h1+h2)
print(f"{hexa(i+512)} {h1} {h2} {mem}")
else:
h1, h2 = hexa(codes[i]).upper(), "00"
mem = get_mem(h1+h2)
print(f"{hexa(i+512)} {h1} {h2} {mem}")
with open("template.html") as f:
template = Template(f.read())
rendered = template.render(ROM=",".join(str(op) for op in codes))
with open("out.html", mode="w") as f:
f.write(rendered)