-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenVM.py
96 lines (84 loc) · 2.43 KB
/
GenVM.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
from Common import getEncodings
import re
codeRegex = re.compile("(\w+)\s+\{\{(.*?)\}\}", re.DOTALL)
vmtab = open("vmtab.txt","rt").read()
codeDict = {}
for match in codeRegex.finditer(vmtab):
#print match.groups()
opname = match.group(1).upper()
code = match.group(2).strip()
codeDict[opname] = code
macros = {}
macros['setnz'] = 'N = (M > 127); Z = (M != 0);';
modeHelpers = {'imm':'IMM',
'zpage':'ZP',
'zpagex':'ZPX',
'zpagey':'ZPY',
'accum':'A',
'abs':'ABS',
'absx':'ABSX',
'absy':'ABSY',
'ind':'IND',
'indx':'INDX',
'indy':'INDY',
'imp':'IMPL',
'rel':'REL'}
modeSizes = {'imm':2,
'zpage':2,
'zpagex':2,
'zpagey':2,
'accum':1,
'abs':3,
'absx':3,
'absy':3,
'ind':3,
'indx':2,
'indy':2,
'imp':1,
'rel':2}
cycles = [
7,6,2,8,3,3,5,5,3,2,2,2,4,4,6,6,
2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,
6,6,2,8,3,3,5,5,4,2,2,2,4,4,6,6,
2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,
6,6,2,8,3,3,5,5,3,2,2,2,3,4,6,6,
2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,
6,6,2,8,3,3,5,5,4,2,2,2,5,4,6,6,
2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,
2,6,2,6,3,3,3,3,2,2,2,2,4,4,4,4,
2,6,2,6,4,4,4,4,2,5,2,5,5,5,5,5,
2,6,2,6,3,3,3,3,2,2,2,2,4,4,4,4,
2,5,2,5,4,4,4,4,2,4,2,4,4,4,4,4,
2,6,2,8,3,3,5,5,2,2,2,2,4,4,6,6,
2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,
2,6,3,8,3,3,5,5,2,2,2,2,4,4,6,6,
2,5,2,8,4,4,6,6,2,4,2,7,4,4,7,7,
]
encodings = getEncodings()
s = ''
for i in encodings:
if i is None:
continue
if not (i.name in codeDict):
continue
code = codeDict[i.name]
# TODO: The rest
s += '#define AGEN AGEN_%s\n' % modeHelpers[i.modename]
s += '#define READ READ_%s\n' % modeHelpers[i.modename]
s += '#define WRITE WRITE_%s\n' % modeHelpers[i.modename]
s += '#define CYCLES %d\n' % cycles[i.opcode]
s += '// %s (%s)\n' % (i.name, i.modename)
s += 'case 0x%02X:\n' % i.opcode
s += '{\n'
#s += 'BeginTiming();\n'
#s += 'Encountered("%s %s");\n' % (i.name, i.modename)
#s += 'Console.WriteLine("%s %s");\n' % (i.name, i.modename)
s += 'NPC = (ushort)(PC+%d);\n' % modeSizes[i.modename]
s += 'AGEN\n'
s += code+'\n'
#s += 'EndTiming("%s %s");\n'%(i.name, i.modename)
s += 'WaitCycles += CYCLES * 3;\n'
s += '}\n'
s += 'break;\n\n'
print s
open("vm_out.c","wt").write(s)