-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjump-table.py
executable file
·57 lines (51 loc) · 1.9 KB
/
jump-table.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
#!/usr/bin/env python3
from sys import exit
TAB=" "*4
#Read in instruction macro steps from csv
OP_INFO={}
with open("op-codes.csv") as f:
#Assume first line is headers
line=f.readline()
line=f.readline()
while(line):
#Strip off \n at end
line_symbols=line[:-1].split(",")
if line_symbols[1]!="":
OP_INFO[int(line_symbols[0],16)]=tuple(line_symbols[1:])
line=f.readline()
OP_FULL_NAMES={}
with open("instructions.s","wt") as f:
f.write(TAB+"#Instruction routines\n")
f.write(TAB+"instructions_begin:\n\n")
for i in range(256):
if i in OP_INFO:
op_name=OP_INFO[i][0]
op_mode=OP_INFO[i][1]
op_full=op_name+"_"+op_mode
f.write(TAB+f"#0x{hex(i)[2:].upper()} - {op_name} {op_mode}\n")
f.write(TAB+f"{op_full}:\n")
f.write(TAB*2+f'OP_MACRO {i}, {op_name}, {op_mode}, {op_full}, ')
for j in range(2,6):
f.write(str(OP_INFO[i][j])+", ")
f.write("\n")
f.write(TAB*2+f"NEXT_MACRO\n")
f.write("\n")
else:
op_full="NOP_0x"+hex(i)[2:].upper()
f.write(TAB+f"#0x{hex(i)[2:].upper()} - NOP\n")
f.write(TAB+f"{op_full}:\n")
f.write(TAB*2+f'OP_MACRO {i}, NOP, IMP, {op_full}, , , , , \n')
f.write(TAB*2+f"NEXT_MACRO\n")
f.write("\n")
OP_FULL_NAMES[i]=op_full
f.write(TAB+"instructions_end:\n")
f.write(TAB+".set instructions_size, instructions_end-instructions_begin\n")
with open("jump-table.s","wt") as f:
f.write(TAB+"#Jump table\n")
f.write(TAB+"jump_table_begin:\n\n")
f.write(TAB+"jump_table_raw:\n")
for i in range(256):
f.write(TAB+f".word {OP_FULL_NAMES[i]}\n")
f.write("\n");
f.write(TAB+"jump_table_end:\n")
f.write(TAB+".set jump_table_size, jump_table_end-jump_table_begin\n")