Here is the python code of my initial CHIP-8 disassembler. I believe most things are there, but you never know! Here is a small script that I used to test it using the “Breakout.ch8” binary :
# Rob Russell
1
2
3
4
5
6
7
8
9
10
11
12
13
14 # Disassembler main loop
#
from disassembler import DisassembleChip8Op
fil=open("Breakout.ch8",'rb')
dat = fil.read()
sz = len(dat)
fil.close()
pc = 0
while(pc < sz):
DisassembleChip8Op(dat,pc)
pc += 2
print ("Finished...")
Here is the actual disassembler code:
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 # Rob Russell
#
def oc0x0(val,pc):
# either E0 or EE op codes
h = val[pc]
l = val[pc+1]
if h==0 and l != 0:
if (l == 224 ): # 00E0 - CLS
print("%-10s" % ("CLS "))
elif (l == 238 ): # 00EE - RTS Return from subroutine
print("%-10s" % ("RTS "))
else:
print("UNKNOWN 0")
return -1
else:
print("UNKNOWN 0")
return -1
return 0
def oc0x1(val,pc):
print("%-10s $%01x%02x" % ("JUMP", val[pc]&0xf,val[pc+1]), end=" ")
print(" Jump to $%01x%02x" % (val[pc]&0xf,val[pc+1]) )
return 0
def oc0x2(val,pc):
print("%-10s $%01x%02x" % ("CALL", val[pc]&0xf, val[pc+1]))
return 0
def oc0x3(val,pc):
print("%-10s V%01X,#$%02x" % ("SKIP.EQ", val[pc]&0xf, val[pc+1]), end = " ")
print(" Skip next op if V%01X equals #$%02x (%d)" % (val[pc]&0xf,val[pc+1], val[pc+1]))
return 0
def oc0x4(val,pc):
print("%-10s V%01X,#$%02x" % ("SKIP.NE", val[pc]&0xf, val[pc+1]), end=" ")
print(" Skip next op if V%01X doesn't equal #$%02x (%d)" % (val[pc]&0xf,val[pc+1], val[pc+1]))
return 0
def oc0x5(val,pc):
print("%-10s V%01X,V%01X" % ("SKIP.EQ", val[pc]&0xf, val[pc+1]>>4), end=" ")
print(" Skip next op if V%01X equals V%01X" & (val[pc]&0xf, val[pc+1]>>4))
return 0
def oc0x6(val,pc):
reg = val[pc] & 0x0f
print("%-10s V%01X,#$%02x" % ("MVI", reg, val[pc+1]) + " Move " + str(val[pc+1]) + " to V" + str(reg))
return 0
def oc0x7(val,pc):
print("%-10s V%01X,#$%02x"% ("ADI", val[pc]&0xf, val[pc+1]), end=" ")
print(" Add " + str(val[pc+1]) + " to V" + str(val[pc]&0xf))
return 0
def oc0x8(val,pc):
lastnib = val[pc+1]>>4
val1=val[pc]&0xf
val2=val[pc+1]>>4
if lastnib==0:
print("%-10s V%01X,V%01X" % ("MOV.", val1, val2), end = " ")
print(" Set V%01X = V%01X" % (val1, val2))
elif lastnib == 1:
print("%-10s V%01X,V%01X" % ("OR.", val1, val2), end = " ")
print(" Set V%01X = V%01X OR V%01X " % (val1,val1,val2))
elif lastnib == 2:
print("%-10s V%01X,V%01X" % ("AND.", val1, val2), end = " ")
print(" Set V%01X = V%01X AND V%01X" % (val1,val1,val2))
elif lastnib == 3:
print("%-10s V%01X,V%01X" % ("XOR.", val1, val2), end = " ")
print(" Set V%01X = V%01X XOR V%01X " % (val1,val1,val2))
elif lastnib == 4:
print("%-10s V%01X,V%01X" % ("ADD.", val1, val2), end = " ")
print(" V%01X = V%01X + V%01X, VF carry set to 1 or 0" % (val1,val1,val2))
elif lastnib == 5:
print("%-10s V%01X,V%01X,V%01X" % ("SUB.", val1, val1, val2), end = " ")
print(" V%01X = V%01X - V%01X, VF=1 for borrow" % (val1,val1,val2))
elif lastnib == 6:
print("%-10s V%01X,V%01X" % ("SHR.", val1, val2), end = " ")
print(" LSB of V%01X in VF, then V%01X >> 1" % (val1,val1))
elif lastnib == 7:
print("%-10s V%01X,V%01X,V%01X" % ("SUB.", val1, val2, val2), end = " ")
print(" V%01X = V%01X - V%01X, VF=1 for borrow" % (val1,val2,val1))
elif lastnib == 0xe : # double check this!
print("%-10s V%01X,V%01X" % ("SHL.", val1, val2), end = " ")
print(" MSB of V%01X in VF, then V%01X << 1" % (val1,val1))
else:
print("UNKNOWN 8")
return -1
return 0
def oc0x9(val,pc):
print("%-10s V%01X,V%01X" % ("SKIP.NE", val[pc]&0xf, val[pc+1]>>4), end=" ")
print(" Skip next op if V%01X doesn't equal V%01X" % (val[pc]&0xf, val[pc+1]>>4))
return 0
def oc0xa(val,pc):
addresshi = val[pc] & 0x0f
print("%-10s I,#$%01x%02x" % ("MVI", addresshi, val[pc+1]) + " Move " + str(val[pc+1]) + " to Index Register")
return 0
def oc0xb(val,pc):
print("%-10s $%01x%02x(V0)" % ("JUMP", val[pc]&0xf, val[pc+1]), end=" ")
print(" Jump to $%01x%02x plus V0" % (val[pc]&0xf, val[pc+1]))
return 0
def oc0xc(val,pc):
print("%-10s V%01X,#$%02X" % ("RNDMSK", val[pc]&0xf, val[pc+1]), end= " ")
print(" Set V%01X to a random number AND'd with #$%02X (%d)" % (val[pc]&0xf,val[pc+1],val[pc+1]) )
return 0
def oc0xd(val,pc):
print("%-10s V%01X,V%01X,#$%01x" % ("SPRITE", val[pc]&0xf, val[pc+1]>>4, val[pc+1]&0xf), end=" ")
print(" Draw sprite @ V%01X,V%01X #$%01x pixels high (%d)" % (val[pc]&0xf, val[pc+1]>>4, val[pc+1]&0xf,val[pc+1]&0xf) )
return 0
def oc0xe(val,pc):
if val[pc+1]==158: # 9E for SKIPKEY.Y
print("%-10s V%01X" % ("SKIPKEY.Y", val[pc]&0xf), end=" ")
print(" Skip next OP if V%01X is pressed" % (val[pc]&0xf) )
elif val[pc+1]==161: # A1 for SKIPKEY.N
print("%-10s V%01X" % ("SKIPKEY.N", val[pc]&0xf), end=" ")
print(" Skip next OP if V%01X ISN'T pressed" % (val[pc]&0xf) )
else:
print("UNKNOWN E")
return -1
return 0
def oc0xf(val,pc):
val1=val[pc]&0xf
val2 = val[pc+1]
if val2 == 0x07:
print("%-10s V%01X,DELAY" % ("MOV", val1), end=" ")
print(" Sets V%01X to value of delay timer" % (val1))
elif val2==0x0a:
print("%-10s V%01X" % ("KEY", val1), end=" ")
print(" Block for keypress stored into V%01X" % (val1))
elif val2 == 0x15:
print("%-10s DELAY,V%01X" % ( "MOV", val1), end=" ")
print(" Sets delay timer to V%01X" % (val1))
elif val2 == 0x18:
print("%-10s SOUND,V%01X" % ("MOV", val1), end=" ")
print(" Sets sound timer to V%01X" % (val1))
elif val2 == 0x1e:
print("%-10s I,V%01X" % ("ADI", val1), end=" ")
print(" I = I + V%01X" % (val1))
elif val2 == 0x29:
print("%-10s I,V%01X" % ("SPRITECHAR", val1), end=" ")
print(" Sets I to loc of sprite for character in V%01X" % (val1))
elif val2 == 0x33:
print("%-10s (I),V%01X" % ("MOVBCD", val1), end=" ")
print(" BCD rep of V%01X" % (val1))
elif val2 == 0x55:
print("%-10s (I),V0-V%01X" ("MOVM", val1), end=" ")
print(" Store V0-V%01X in mem starting at I" % (val1))
elif val2 == 0x65:
print("%-10s V0-V%01X,(I)" % ( "MOVM", val1), end=" ")
print(" Fill V0-V%01X in mem starting at I" % (val1))
else:
print("UNKNOWN F")
return -1
return 0
def DisassembleChip8Op(codebuffer, pc):
firstnib = hex(codebuffer[pc] >> 4)
case='oc'+firstnib + "(val,pc)" # oc prefix means 'opcode', passing 'val' and 'pc' to fcn
print("%04x %02x %02x " % (pc+0x200, codebuffer[pc], codebuffer[pc + 1] ), end=" ")
val=codebuffer
result=eval(case) # run switch statement replacement