-
Notifications
You must be signed in to change notification settings - Fork 60
/
gen6502Disassembler.py
executable file
·63 lines (55 loc) · 2.81 KB
/
gen6502Disassembler.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
#!/usr/bin/env python3
import os, sys, csv
#opcode,nmeonic,addressing mode,bytes,cycles
############################################################################
# handle command line arguments and variable setup
############################################################################
if len(sys.argv) != 2:
print ("Usage: %s ops.csv" % (sys.argv[0]))
print (" where ops.csv is a csv file with the format")
print (" opcode,nmeonic,addressing mode,bytes,cycles,flags")
sys.exit(1)
ops=csv.reader(open(sys.argv[1], 'r', ))
opdict = {}
for op in ops:
if len(op)>0 and op[0] == "opcode":
continue
#ignore lines of formats I don't recognize
# allows for blanks and comments
if len(op) == 6:
opcode=int(op[0],16)
opdict[opcode] = [op[1], op[2], op[3], op[4], op[5]]
print (opdict)
opnums = list(opdict.keys())
opnums.sort()
for op in opnums:
#disassemble depends on addressing mode
addressingmode = opdict[op][1]
if addressingmode == "IMP":
print ('case 0x%02x: sprintf(opstr, "%s"); break;' % (op, opdict[op][0]))
elif addressingmode == "ACC":
print ('case 0x%02x: sprintf(opstr, "%s A"); break;' % (op, opdict[op][0]))
elif addressingmode == "IMM":
print ('case 0x%02x: sprintf(opstr, "%s %s", opcodes[1]); count = 2; break;' % (op, opdict[op][0],"#$%02x"))
elif addressingmode == "ABS":
print ('case 0x%02x: sprintf(opstr, "%s %s", opcodes[2], opcodes[1]); count = 3; break;' % (op, opdict[op][0], "$%02x%02x"))
elif addressingmode == "IND":
print ('case 0x%02x: sprintf(opstr, "%s (%s)", opcodes[2], opcodes[1]); count = 3; break;' % (op, opdict[op][0], "$%02x%02x"))
elif addressingmode == "ABSX":
print ('case 0x%02x: sprintf(opstr, "%s %s,X", opcodes[2], opcodes[1]); count = 3; break;' % (op, opdict[op][0], "$%02x%02x"))
elif addressingmode == "ABSY":
print ('case 0x%02x: sprintf(opstr, "%s %s,Y", opcodes[2], opcodes[1]); count = 3; break;' % (op, opdict[op][0], "$%02x%02x"))
elif addressingmode == "INDX":
print ('case 0x%02x: sprintf(opstr, "%s (%s,X)", opcodes[1]); count = 2; break;' % (op, opdict[op][0], "$%02x"))
elif addressingmode == "INDY":
print ('case 0x%02x: sprintf(opstr, "%s (%s),Y", opcodes[1]); count = 2; break;' % (op, opdict[op][0], "$%02x"))
elif addressingmode == "ZP":
print ('case 0x%02x: sprintf(opstr, "%s %s", opcodes[1]); count = 2; break;' % (op, opdict[op][0], "$%02x"))
elif addressingmode == "ZPX":
print ('case 0x%02x: sprintf(opstr, "%s %s,X", opcodes[1]); count = 2; break;' % (op, opdict[op][0], "$%02x"))
elif addressingmode == "ZPY":
print ('case 0x%02x: sprintf(opstr, "%s %s,Y", opcodes[1]); count = 2; break;' % (op, opdict[op][0], "$%02x"))
elif addressingmode == "REL":
print ('case 0x%02x: sprintf(opstr, "%s %s", opcodes[1]); count = 2; break;' % (op, opdict[op][0], "$%02x"))
else:
print ('case 0x%02x: sprintf(opstr, "UNKNOWN"); break;' % (op))