-
Notifications
You must be signed in to change notification settings - Fork 5
/
tocfile.py
executable file
·71 lines (65 loc) · 1.58 KB
/
tocfile.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
#!/usr/bin/env python
from __future__ import print_function
import sys
import re
import struct
import os
import random
def parse_instructions():
ops = os.popen("./nanac.exe -X").read()
out = dict()
for line in ops.split("\n"):
line = line.strip()
if not line:
continue
line = line.split()
names = (line[2], line[3])
nums = (int(line[0],16), int(line[1],16))
out[nums] = names
return out
def main():
instructions = parse_instructions()
offset = 0
print("""
#include <stdio.h>
#include "nanac_builtins.c"
int native_nanac (struct nanac_s *cpu) {
int escape = 0;
while( escape >= 0 ) {
escape = 0;
#ifdef NANAC_TRACE
printf("@%d\\n", cpu->eip);
#endif
switch( cpu->eip ) {
""")
for filename in sys.argv[1:]:
with open(filename, "rb") as handle:
cases = []
print("/* " + filename + " */")
while True:
data = handle.read(4)
if not data:
break
mod, cmd, arga, argb = ord(data[0]), ord(data[1]), ord(data[2]), ord(data[3])
modstr, cmdstr = '?'+str(mod), '?'+str(cmd)
if (mod,cmd) in instructions:
modstr, cmdstr = instructions[(mod,cmd)]
asmstr = "%s %s %X %X" % (modstr, cmdstr, arga, argb)
cases.append("\n".join([
"\t\t\tcase %d: /* %02X%02X%02X%02X %s */" % (
offset, mod, cmd, arga, argb, asmstr),
"\t\t\t\tescape = %s_%s(cpu, %d, %d);" % (modstr, cmdstr, arga, argb),
"\t\t\t\tbreak;"
]))
offset += 1
random.shuffle(cases)
print("\n".join(cases))
print(""" default: escape = -666; break;
}
escape = nanac_step_epilogue(cpu, escape);
}
return escape;
}
""")
if __name__ == "__main__":
main()