-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_opcodes.py
74 lines (57 loc) · 1.7 KB
/
generate_opcodes.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
import urllib2
from bs4 import BeautifulSoup as BS
import re
html = urllib2.urlopen("http://pastraiser.com/cpu/i8080/i8080_opcodes.html").read().replace('<br>', '\n')
soup = BS(html, 'lxml')
ops = []
wide_trans = {
'PSW': 'AF',
'B': 'BC',
'D': 'DE',
'H': 'HL',
}
table = soup.find('table')
for row in table.findAll('tr')[1:]:
for cell in row.findAll('td')[1:]:
is_wide = cell['bgcolor'] in ('#ccffcc', '#ffcccc')
op_data = cell.text.split('\n')[0]
op_data = op_data.lstrip('*')
op_data = map(str, re.split('[,\s]', op_data))
for i, w in enumerate(op_data):
if is_wide:
op_data[i] = wide_trans.get(w, op_data[i])
ops.append(op_data)
print len(ops)
func_defs = []
func_labels = []
lookup_table = []
def param_map(x):
if x in ('a16', 'd16', 'd8'):
return '_' + x
if x in '7531':
return 'num'
return x
for op in ops:
name = op[0]
data = map(param_map, op[1:])
trans = [['X', 'Y'], ['WX', 'WY']]
func_labels.append('''%s: %s(%s)''' % ('_'.join(op), op[0], ', '.join(op[1:])))
lookup_table.append('&&%s' % '_'.join(op))
func_defs.append('''#define %s(%s) \\
printf("%s unimplemented\\n"); \\
assert(false); // TODO \\
DONE
''' % (name, ', '.join(map(lambda (i, d): trans[len(d) == 2][i], enumerate(data))), name))
with open('opcode_defs.h', 'w') as o:
done = set()
for f in func_defs:
if f not in done:
done.add(f)
print>>o, f
with open('opcode_labels.h', 'w') as o:
for l in func_labels:
print>>o, l
with open('opcode_lookup.h', 'w') as o:
print>>o, '''static void *ops[] = {
%s
};''' % ', '.join(lookup_table)