-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpu_gen_tables.py
executable file
·73 lines (56 loc) · 1.57 KB
/
cpu_gen_tables.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
"""
Some table generators for 8080 CPU emulator/MEGA65
(C)2017 LGB Gabor Lenart
Note: these in fact can be implemented in assembly
in M65 code at pre-init time still shorter than storing
these tables. But currently it's more flexible and bug-free
to use pre-generated tables!
"""
inc8_f_tab = [None] * 0x100
dec8_f_tab = [None] * 0x100
szp_f_tab = [None] * 0x100
and_f_tab = [None] * 0x100
S_FLAG = 128
Z_FLAG = 64
H_FLAG = 16
P_FLAG = 4
C_FLAG = 1
def get_parity(n):
result = 0
for a in range(8):
result += n & 1
n >>= 1
return 0 if result & 1 else P_FLAG
for i in range(0x100):
inc8_f_tab[i] = dec8_f_tab[i] = i & 128 # MSB of result is the sign flag directly
if i == 0:
inc8_f_tab[i] |= Z_FLAG
dec8_f_tab[i] |= Z_FLAG
if (i & 0xF) == 0:
inc8_f_tab[i] |= H_FLAG
if not ((i & 0xF) == 0xF):
dec8_f_tab[i] |= H_FLAG
inc8_f_tab[i] |= get_parity(i)
dec8_f_tab[i] |= get_parity(i)
# SZP table
szp_f_tab[i] = (i & 128) | get_parity(i) | (Z_FLAG if i == 0 else 0)
# AND table
and_f_tab[i] = szp_f_tab[i] | H_FLAG
def dump_table(tab, name, do_flags_filt):
s = name + ":\n"
for i in range(0x100):
if do_flags_filt:
c = tab[i] & 215 | 2
else:
c = tab[i]
if not (i & 0xF):
s += "\t.BYTE "
s += str(c) + ("," if (i & 0xF) != 0xF else "\n")
print(s)
#print([get_parity(n) for n in range(0x100)])
#dump_table([get_parity(n) for n in range(0x100)], "parity", False)
dump_table(inc8_f_tab, "inc8_f_tab", True)
dump_table(dec8_f_tab, "dec8_f_tab", True)
dump_table(szp_f_tab, "szp_f_tab", True)
dump_table(and_f_tab, "and_f_tab", True)