-
Notifications
You must be signed in to change notification settings - Fork 2
/
awvm-asm.py
executable file
·421 lines (361 loc) · 12 KB
/
awvm-asm.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
#!/usr/bin/env python3
#
# (c) 2022 Felipe Correa da Silva Sanches <[email protected]>
# Licensed under GPL version 3 or later
symbols = {}
rom = False
address = 0
second_pass = False
VIDEO2=0 # shared polygon resource
CINEMATIC=1 # level-specific bank of polygonal data
def parse_value(v_str):
try:
if v_str.startswith("0x"):
return int(v_str.split("0x")[1], 16)
else:
return int(v_str)
except:
# print (f"not a number! ({v_str})")
return v_str
def parse_operand(operand_str):
operand_str = operand_str.strip()
key = None
if "=" in operand_str:
key, operand_str = operand_str.split("=")
if '[' in operand_str and ']' in operand_str:
value = operand_str.split('[')[1].strip()
value = value.split(']')[0].strip()
return {"type": "var",
"key": key,
"value": parse_value(value)}
else:
return {"type": "value",
"key": key,
"value": parse_value(operand_str)}
def parse_label(line_str):
return line_str.split(":")[0]
def parse_common(name, line):
retval = {"name": name}
line = line.split(name)[1].strip()
tokens = line.split(",")
retval["operands"] = [parse_operand(t) for t in tokens]
return retval
def byte(v):
global address
rom.seek(address)
address += 1
if isinstance(v, str):
if v in symbols:
v = symbols[v]
else:
v = 0
rom.write(bytes([v]))
def word(v, negative=False):
if v in symbols:
v = symbols[v]
# we need to do it here, right after
# resolving symbolic names above.
if negative:
v = 0x10000 - v
try:
byte(v >> 8)
byte(v & 0xFF)
except:
word(0x0000)
if second_pass:
print (f"Symbol '{v}' could not be found.")
def encode(instr):
if instr["name"] == "org":
pass # for now we ignore this, as it is always 0x0000 for AW VM
# (at least for the original game bytecode)
if instr["name"] == "db":
for data_byte in instr["operands"]:
byte(data_byte["value"])
if instr["name"] == "mov":
dest, data = instr["operands"]
if data["type"] == "var":
byte(0x01)
byte(dest["value"])
byte(data["value"])
else: #value
byte(0x00)
byte(dest["value"])
word(data["value"])
elif instr["name"] == "add":
dest, data = instr["operands"]
if data["type"] == "var":
byte(0x02)
byte(dest["value"])
byte(data["value"])
else: #value
byte(0x03)
byte(dest["value"])
word(data["value"])
elif instr["name"] == "sub":
dest, src = instr["operands"]
if src["type"] == "var":
byte(0x13)
byte(dest["value"])
byte(src["value"])
else:
# here we actually emit an add instruction with the
# second operand multiplied by -1 and represented in
# two's complement notation
byte(0x03)
byte(dest["value"])
word(src["value"], negative=True)
elif instr["name"] == "and":
dest, src = instr["operands"]
if src["type"] != "value":
print ("ERROR: 'AND' instruction second operand must be an immediate 16bit value.")
byte(0x14)
byte(dest["value"])
word(src["value"])
elif instr["name"] == "or":
dest, src = instr["operands"]
if src["type"] != "value":
print ("ERROR: 'OR' instruction second operand must be an immediate 16bit value.")
byte(0x15)
byte(dest["value"])
word(src["value"])
elif instr["name"] == "shl":
dest, src = instr["operands"]
if src["type"] != "value":
print ("ERROR: 'SHL' instruction second operand must be an immediate 16bit value.")
byte(0x16)
byte(dest["value"])
word(src["value"])
elif instr["name"] == "shr":
dest, src = instr["operands"]
if src["type"] != "value":
print ("ERROR: 'SHR' instruction second operand must be an immediate 16bit value.")
byte(0x17)
byte(dest["value"])
word(src["value"])
elif instr["name"] == "jmp":
addr = instr["operands"][0]
byte(0x07)
word(addr["value"])
elif instr["name"] == "call":
addr = instr["operands"][0]
byte(0x04)
word(addr["value"])
elif instr["name"] == "ret":
byte(0x05)
elif instr["name"] == "killChannel":
byte(0x11)
elif instr["name"] == "break":
byte(0x06)
elif instr["name"] == "text":
ops = keyword_operands(instr)
byte(0x12)
word(ops["id"]["value"])
byte(ops["x"]["value"])
byte(ops["y"]["value"])
byte(ops["color"]["value"])
elif instr["name"] == "play":
ops = keyword_operands(instr)
byte(0x18)
word(ops["id"]["value"])
byte(ops["freq"]["value"])
byte(ops["vol"]["value"])
byte(ops["channel"]["value"])
elif instr["name"] == "song":
ops = keyword_operands(instr)
byte(0x1A)
word(ops["id"]["value"])
word(ops["delay"]["value"])
byte(ops["pos"]["value"])
elif instr["name"] == "GameOver":
byte(0x1B)
elif instr["name"] == "freezeChannels":
first, last = instr["operands"]
byte(0x0C)
byte(first["value"])
byte(last["value"])
byte(0x00)
elif instr["name"] == "unfreezeChannels":
first, last = instr["operands"]
byte(0x0C)
byte(first["value"])
byte(last["value"])
byte(0x01)
elif instr["name"] == "deleteChannels":
first, last = instr["operands"]
byte(0x0C)
byte(first["value"])
byte(last["value"])
byte(0x02)
elif instr["name"] == "djnz":
var, address = instr["operands"]
byte(0x09)
byte(var["value"])
word(address["value"])
elif instr["name"] in ["je", "jne", "jg", "jge", "jl", "jle"]:
b, c, addr = instr["operands"]
subopcode = ["je", "jne", "jg", "jge", "jl", "jle"].index(instr["name"])
if c["type"] == "var":
subopcode |= 0x80
elif c["value"] > 0xFF:
subopcode |= 0x40
byte(0x0A)
byte(subopcode)
byte(b["value"])
if c["type"] == "value" and c["value"] > 0xFF:
word(c["value"])
else:
byte(c["value"])
word(addr["value"])
elif instr["name"] == "setPalette":
paletteIndex = instr["operands"][0]
byte(0x0B)
word(paletteIndex["value"] << 8 | 0xFF)
elif instr["name"] == "load":
ops = keyword_operands(instr)
byte(0x19)
word(ops["id"]["value"])
elif instr["name"] == "bankSwitch":
bankId = instr["operands"][0]
byte(0x19)
if bankId["value"] > 0xf:
print ("ERROR: 'bankSwitch' instruction has bankId grater than 15.")
word(0x3E80 | bankId["value"])
elif instr["name"] == "selectVideoPage":
pageId = instr["operands"][0]
byte(0x0D)
byte(pageId["value"])
elif instr["name"] == "copyVideoPage":
ops = keyword_operands(instr)
byte(0x0F)
byte(ops["src"]["value"])
byte(ops["dst"]["value"])
elif instr["name"] == "blitFramebuffer":
pageId = instr["operands"][0]
byte(0x10)
byte(pageId["value"])
elif instr["name"] == "fill":
ops = keyword_operands(instr)
byte(0x0E)
byte(ops["page"]["value"])
byte(ops["color"]["value"])
elif instr["name"] == "setup":
ops = keyword_operands(instr)
byte(0x08)
byte(ops["channel"]["value"])
word(ops["address"]["value"])
elif instr["name"] == "video":
ops = keyword_operands(instr)
offs = ops["offset"]["value"]
if offs in symbols:
offs = symbols[offs]
else:
offs = 0x0000
x = ops["x"]
y = ops["y"]
if "zoom" not in ops:
word(0x8000 | ((offs >> 1) & 0x7FFF))
byte(x["value"])
byte(y["value"])
# TODO: error if type is VIDEO2
else:
zoom = ops["zoom"]
opcode = 0x40
if ops["type"]["value"] == VIDEO2:
opcode |= 0x03
operand_bytes = [
offs >> 9,
(offs >> 1) & 0xFF
]
if x["type"] == "var":
operand_bytes.append(x["value"])
opcode |= 0x10
else:
if x["value"] <= 0x1FF:
opcode |= 0x20
operand_bytes.append(x["value"] & 0xFF)
if x["value"] > 0xFF:
opcode |= 0x10
else:
operand_bytes.append(x["value"] >> 8)
operand_bytes.append(x["value"] & 0xFF)
if y["type"] == "var":
operand_bytes.append(y["value"])
opcode |= 0x04
else:
if y["value"] <= 0xFF:
opcode |= 0x08
operand_bytes.append(y["value"])
else:
operand_bytes.append(y["value"] >> 8)
operand_bytes.append(y["value"] & 0xFF)
if zoom["type"] == "var":
operand_bytes.append(zoom["value"])
opcode |= 0x01
else:
if zoom["value"] != 0x40:
print ("ERROR! Zoom can't be a constant other than 0x40!")
byte(opcode)
for b in operand_bytes:
byte(b)
def keyword_operands(instr):
ops = {}
for op in instr["operands"]:
value = op["value"]
key = op["key"]
if key:
ops[key] = op
return ops
def assemble(input_filename):
print (f"\nAssembling '{input_filename}' ...")
global address, rom, symbols
instructions = []
label = None
output_filename = input_filename.split(".asm")[0] + ".bin"
rom = open(output_filename, "w+b")
lines = open(input_filename).readlines()
for line in lines:
line = line.split(";")[0].strip() #remove comments
if "EQU" in line:
tokens = line.split("EQU")
symbols[tokens[0].strip()] = parse_value(tokens[1].strip())
instr = None
if ":" in line.split(" ")[0]:
label = parse_label(line)
line = line.split(":")[1]
for instruction_name in ["org", "bankSwitch", "db", "mov", "add", "sub", "jmp",
"call", "ret", "break", "setPalette",
"selectVideoPage", "copyVideoPage",
"blitFramebuffer", "video", "fill",
"je", "jne", "jge", "jg", "jle", "jl",
"load", "setup", "djnz", "freezeChannels",
"unfreezeChannels", "deleteChannels",
"killChannel", "text", "sub", "and", "or",
"shl", "shr", "play", "song", "GameOver"]:
if line.strip().startswith(instruction_name):
instr = parse_common(instruction_name, line)
instructions.append((label, instr))
label = None
break
# print symbols
# print "\n".join([str(instr) for instr in instructions])
print ("First Pass.")
address = 0
# print "\n".join(map(str, instructions))
for label, instruction in instructions:
if label:
symbols[label] = address
encode(instruction)
# print symbols
print ("Second Pass.")
second_pass = True
address = 0
for label, instruction in instructions:
if label:
symbols[label] = address
encode(instruction)
# print symbols
import sys
if len(sys.argv) != 2:
print(f"usage: {sys.argv[0]} input.asm")
sys.exit(-1)
assemble(sys.argv[1])