opcode and disassembler generatory (python)

This commit is contained in:
jborza 2019-04-14 15:07:28 +02:00
parent 6cd265c578
commit 0d63342137
3 changed files with 250 additions and 0 deletions

176
data/6502_ops.csv Normal file
View File

@ -0,0 +1,176 @@
opcode,mnemonic,addressing mode,bytes,cycles,flags
0x69,ADC,IMM,2,2,CZidbVN
0x65,ADC,ZP,2,3,CZidbVN
0x75,ADC,ZPX,2,4,CZidbVN
0x6d,ADC,ABS,3,4,CZidbVN
0x7d,ADC,ABSX,3,4,CZidbVN
0x79,ADC,ABSY,3,4,CZidbVN
0x61,ADC,INDX,2,6,CZidbVN
0x71,ADC,INDY,2,5,CZidbVN
0x29,AND,IMM,2,2,cZidbvN
0x25,AND,ZP,2,3,cZidbvN
0x35,AND,ZPX,2,4,cZidbvN
0x2d,AND,ABS,3,4,cZidbvN
0x3d,AND,ABSX,3,4,cZidbvN
0x39,AND,ABSY,3,4,cZidbvN
0x21,AND,INDX,2,6,cZidbvN
0x31,AND,INDY,2,5,cZidbvN
0x0a,ASL,ACC,1,2,CZidbvN
0x06,ASL,ZP,2,5,CZidbvN
0x16,ASL,ZPX,2,6,CZidbvN
0x0e,ASL,ABS,3,6,CZidbvN
0x1e,ASL,ABSX,3,7,CZidbvN
0x90,BCC,REL,2,2/3,czidbvn
0xB0,BCS,REL,2,2/3,czidbvn
0xF0,BEQ,REL,2,2/3,czidbvn
0x30,BMI,REL,2,2/3,czidbvn
0xD0,BNE,REL,2,2/3,czidbvn
0x10,BPL,REL,2,2/3,czidbvn
0x50,BVC,REL,2,2/3,czidbvn
0x70,BVS,REL,2,2/3,czidbvn
0x24,BIT,ZP,2,3,cZidbVN
0x2c,BIT,ABS,3,4,cZidbVN
0x00,BRK,IMP,1,7,czidbvn
0x18,CLC,IMP,1,2,Czidbvn
0xd8,CLD,IMP,1,2,cziDbvn
0x58,CLI,IMP,1,2,czIdbvn
0xb8,CLV,IMP,1,2,czidbVn
0xea,NOP,IMP,1,2,czidbvn
0x48,PHA,IMP,1,3,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,ROL,ABSX,3,7,CZidbvN
0x6a,ROR,ACC,1,2,CZidbvN
0x66,ROR,ZP,2,5,CZidbvN
0x76,ROR,ZPX,2,6,CZidbvN
0x7e,ROR,ABS,3,6,CZidbvN
0x6e,ROR,ABSX,3,7,CZidbvN
0xe9,SBC,IMM,2,2,CZidbVN
0xe5,SBC,ZP,2,3,CZidbVN
0xf5,SBC,ZPX,2,4,CZidbVN
0xed,SBC,ABS,3,4,CZidbVN
0xfd,SBC,ABSX,3,4,CZidbVN
0xf9,SBC,ABSY,3,4,CZidbVN
0xe1,SBC,INDX,2,6,CZidbVN
0xf1,SBC,INDY,2,5,CZidbVN
0x85,STA,ZP,2,3,czidbvn
0x95,STA,ZPX,2,4,czidbvn
0x8d,STA,ABS,3,4,czidbvn
0x9d,STA,ABSX,3,5,czidbvn
0x99,STA,ABSY,3,5,czidbvn
0x81,STA,INDX,2,6,czidbvn
0x91,STA,INDY,2,6,czidbvn
0x86,STX,ZP,2,3,czidbvn
0x96,STX,ZPY,2,4,czidbvn
0x8e,STX,ABS,3,4,czidbvn
0x84,STY,ZP,2,3,czidbvn
0x94,STY,ZPX,2,4,czidbvn
0x8c,STY,ABS,3,4,czidbvn
1 opcode mnemonic addressing mode bytes cycles flags
2 0x69 ADC IMM 2 2 CZidbVN
3 0x65 ADC ZP 2 3 CZidbVN
4 0x75 ADC ZPX 2 4 CZidbVN
5 0x6d ADC ABS 3 4 CZidbVN
6 0x7d ADC ABSX 3 4 CZidbVN
7 0x79 ADC ABSY 3 4 CZidbVN
8 0x61 ADC INDX 2 6 CZidbVN
9 0x71 ADC INDY 2 5 CZidbVN
10 0x29 AND IMM 2 2 cZidbvN
11 0x25 AND ZP 2 3 cZidbvN
12 0x35 AND ZPX 2 4 cZidbvN
13 0x2d AND ABS 3 4 cZidbvN
14 0x3d AND ABSX 3 4 cZidbvN
15 0x39 AND ABSY 3 4 cZidbvN
16 0x21 AND INDX 2 6 cZidbvN
17 0x31 AND INDY 2 5 cZidbvN
18 0x0a ASL ACC 1 2 CZidbvN
19 0x06 ASL ZP 2 5 CZidbvN
20 0x16 ASL ZPX 2 6 CZidbvN
21 0x0e ASL ABS 3 6 CZidbvN
22 0x1e ASL ABSX 3 7 CZidbvN
23 0x90 BCC REL 2 2/3 czidbvn
24 0xB0 BCS REL 2 2/3 czidbvn
25 0xF0 BEQ REL 2 2/3 czidbvn
26 0x30 BMI REL 2 2/3 czidbvn
27 0xD0 BNE REL 2 2/3 czidbvn
28 0x10 BPL REL 2 2/3 czidbvn
29 0x50 BVC REL 2 2/3 czidbvn
30 0x70 BVS REL 2 2/3 czidbvn
31 0x24 BIT ZP 2 3 cZidbVN
32 0x2c BIT ABS 3 4 cZidbVN
33 0x00 BRK IMP 1 7 czidbvn
34 0x18 CLC IMP 1 2 Czidbvn
35 0xd8 CLD IMP 1 2 cziDbvn
36 0x58 CLI IMP 1 2 czIdbvn
37 0xb8 CLV IMP 1 2 czidbVn
38 0xea NOP IMP 1 2 czidbvn
39 0x48 PHA IMP 1 3 czidbvn
40 0x68 PLA IMP 1 4 cZidbvN
41 0x08 PHP IMP 1 3 czidbvn
42 0x28 PLP IMP 1 4 CZIDBVN
43 0x40 RTI IMP 1 6 czidbvn
44 0x60 RTS IMP 1 6 czidbvn
45 0x38 SEC IMP 1 2 Czidbvn
46 0xf8 SED IMP 1 2 cziDbvn
47 0x78 SEI IMP 1 2 czIdbvn
48 0xaa TAX IMP 1 2 cZidbvN
49 0x8a TXA IMP 1 2 cZidbvN
50 0xa8 TAY IMP 1 2 cZidbvN
51 0x98 TYA IMP 1 2 cZidbvN
52 0xba TSX IMP 1 2 cZidbvN
53 0x9a TXS IMP 1 2 czidbvn
54 0xc9 CMP IMM 2 2 CZidbvN
55 0xc5 CMP ZP 2 3 CZidbvN
56 0xd5 CMP ZPX 2 4 CZidbvN
57 0xcd CMP ABS 3 4 CZidbvN
58 0xdd CMP ABSX 3 4 CZidbvN
59 0xd9 CMP ABSY 3 4 CZidbvN
60 0xc1 CMP INDX 2 6 CZidbvN
61 0xd1 CMP INDY 2 5 CZidbvN
62 0xe0 CPX IMM 2 2 CZidbvN
63 0xe4 CPX ZP 2 3 CZidbvN
64 0xec CPX ABS 3 4 CZidbvN
65 0xc0 CPY IMM 2 2 CZidbvN
66 0xc4 CPY ZP 2 3 CZidbvN
67 0xcc CPY ABS 3 4 CZidbvN
68 0xc6 DEC ZP 2 5 cZidbvN
69 0xd6 DEC ZPX 2 6 cZidbvN
70 0xce DEC ABS 3 6 cZidbvN
71 0xde DEC ABSX 3 7 cZidbvN
72 0xca DEX IMP 1 2 cZidbvN
73 0x88 DEY IMP 1 2 cZidbvN
74 0xe8 INX IMP 1 2 cZidbvN
75 0xc8 INY IMP 1 2 cZidbvN
76 0x49 EOR IMM 2 2 cZidbvN
77 0x45 EOR ZP 2 3 cZidbvN
78 0x55 EOR ZPX 2 4 cZidbvN
79 0x4d EOR ABS 3 4 cZidbvN
80 0x5d EOR ABSX 3 4 cZidbvN
81 0x59 EOR ABSY 3 4 cZidbvN
82 0x41 EOR INDX 2 6 cZidbvN
83 0x51 EOR INDY 2 5 cZidbvN
84 0xe6 INC ZP 2 5 cZidbvN
85 0xf6 INC ZPX 2 6 cZidbvN
86 0xee INC ABS 3 6 cZidbvN
87 0xfe INC ABSX 3 7 cZidbvN
88 0x4c JMP ABS 3 3 czidbvn
89 0x6c JMP IND 3 5 czidbvn
90 0x20 JSR ABS 3 6 czidbvn
91 0xa9 LDA IMM 2 2 cZidbvN
92 0xa5 LDA ZP 2 3 cZidbvN
93 0xb5 LDA ZPX 2 4 cZidbvN
94 0xad LDA ABS 3 4 cZidbvN
95 0xbd LDA ABSX 3 4 cZidbvN
96 0xb9 LDA ABSY 3 4 cZidbvN
97 0xa1 LDA INDX 2 6 cZidbvN
98 0xb1 LDA INDY 2 5 cZidbvN
99 0xa2 LDX IMM 2 2 cZidbvN
100 0xa6 LDX ZP 2 3 cZidbvN
101 0xb6 LDX ZPY 2 4 cZidbvN
102 0xae LDX ABS 3 4 cZidbvN
103 0xbe LDX ABSY 3 4 cZidbvN
104 0xa0 LDY IMM 2 2 cZidbvN
105 0xa4 LDY ZP 2 3 cZidbvN
106 0xb4 LDY ZPX 2 4 cZidbvN
107 0xac LDY ABS 3 4 cZidbvN
108 0xbc LDY ABSX 3 4 cZidbvN
109 0x4a LSR ACC 1 2 CZidbvN
110 0x46 LSR ZP 2 5 CZidbvN
111 0x56 LSR ZPX 2 6 CZidbvN
112 0x4e LSR ABS 3 6 CZidbvN
113 0x5e LSR ABSX 3 7 CZidbvN
114 0x09 ORA IMM 2 2 cZidbvN
115 0x05 ORA ZP 2 3 cZidbvN
116 0x15 ORA ZPX 2 4 cZidbvN
117 0x0d ORA ABS 3 4 cZidbvN
118 0x1d ORA ABSX 3 4 cZidbvN
119 0x19 ORA ABSY 3 4 cZidbvN
120 0x01 ORA INDX 2 6 cZidbvN
121 0x11 ORA INDY 2 5 cZidbvN
122 0x2a ROL ACC 1 2 CZidbvN
123 0x26 ROL ZP 2 5 CZidbvN
124 0x36 ROL ZPX 2 6 CZidbvN
125 0x2e ROL ABS 3 6 CZidbvN
126 0x3e ROL ABSX 3 7 CZidbvN
127 0x6a ROR ACC 1 2 CZidbvN
128 0x66 ROR ZP 2 5 CZidbvN
129 0x76 ROR ZPX 2 6 CZidbvN
130 0x7e ROR ABS 3 6 CZidbvN
131 0x6e ROR ABSX 3 7 CZidbvN
132 0xe9 SBC IMM 2 2 CZidbVN
133 0xe5 SBC ZP 2 3 CZidbVN
134 0xf5 SBC ZPX 2 4 CZidbVN
135 0xed SBC ABS 3 4 CZidbVN
136 0xfd SBC ABSX 3 4 CZidbVN
137 0xf9 SBC ABSY 3 4 CZidbVN
138 0xe1 SBC INDX 2 6 CZidbVN
139 0xf1 SBC INDY 2 5 CZidbVN
140 0x85 STA ZP 2 3 czidbvn
141 0x95 STA ZPX 2 4 czidbvn
142 0x8d STA ABS 3 4 czidbvn
143 0x9d STA ABSX 3 5 czidbvn
144 0x99 STA ABSY 3 5 czidbvn
145 0x81 STA INDX 2 6 czidbvn
146 0x91 STA INDY 2 6 czidbvn
147 0x86 STX ZP 2 3 czidbvn
148 0x96 STX ZPY 2 4 czidbvn
149 0x8e STX ABS 3 4 czidbvn
150 0x84 STY ZP 2 3 czidbvn
151 0x94 STY ZPX 2 4 czidbvn
152 0x8c STY ABS 3 4 czidbvn

View File

@ -0,0 +1,61 @@
import csv
with open('disassembler.c','w') as outfile:
with open('6502_ops.csv','r') as file:
reader = csv.DictReader(file)
for op in reader:
print(op)
args = int(op['bytes'])
addr_mode = op['addressing mode']
suffix = '_'+ addr_mode if addr_mode != 'IMP' else ''
mnemonic = op['mnemonic']
name = mnemonic + suffix
outfile.write('case %s: ' % name)
outfile.write('sprintf(op, ')
if(addr_mode == 'IMP'):
outfile.write('"%s"' % mnemonic)
else: #all other addressing modes use name + suffix
outfile.write('"%s ' % mnemonic)
if(addr_mode == 'IMM'):
outfile.write('#$%02X')
elif(addr_mode == 'ACC'):
outfile.write("A")
elif(addr_mode.startswith('ZP')):
outfile.write("$%02X")
if(addr_mode == 'ZPX'):
outfile.write(",X")
if(addr_mode == 'ZPY'):
outfile.write(",Y")
elif(addr_mode.startswith('ABS')):
outfile.write("$%02X%02X")
if(addr_mode == 'ABSX'):
outfile.write(",X")
if(addr_mode == 'ABSY'):
outfile.write(",Y")
elif(addr_mode == 'IND'):
outfile.write("($%02X%02X)")
elif(addr_mode == 'INDX'):
outfile.write("($%02X,X)")
elif(addr_mode == 'INDY'):
outfile.write("($%02X),Y")
elif(addr_mode == 'REL'):
outfile.write("$%02X")
#end format string
outfile.write("\"")
#append bytes, reversed as of little endian
if(args > 2):
outfile.write(", code[2]")
if(args > 1):
outfile.write(", code[1]")
outfile.write(');')
if(args > 1):
outfile.write("bytes = %s;" % args)
#sprintf(op, "%s"' % ())
#outfile.write('#define %s %s' % (name, op['opcode']))
outfile.write('break;')
outfile.write('\n')

13
data/generate_opcodes.py Normal file
View File

@ -0,0 +1,13 @@
import csv
with open('opcodes.h','w') as outfile:
with open('6502_ops.csv','r') as file:
reader = csv.DictReader(file)
for op in reader:
print(op)
addr_mode = op['addressing mode']
suffix = '_'+ addr_mode if addr_mode != 'IMP' else ''
name = op['mnemonic'] + suffix
outfile.write('#define %s %s' % (name, op['opcode']))
outfile.write('\n')