-
Notifications
You must be signed in to change notification settings - Fork 1
/
chip8_disassemble.py
56 lines (45 loc) · 1.35 KB
/
chip8_disassemble.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
import sys
import binascii
from opcode_table import opcodeTable
from opcode_args_table import opcodeArgsTable
def main() :
if len(sys.argv) < 2:
print "Incorrect arguements."
exit()
filePath = sys.argv[1]
assemblyInstructions = generateAssembly(filePath)
for instruction in assemblyInstructions:
print instruction
def generateAssembly(filePath):
assemblyInstructions = []
with open(filePath, "rb") as f:
programCounter = 0x200
opcode = f.read(2)
while opcode:
hexOpcode = binascii.hexlify(opcode)
opcodeInstruction = lookupOpcode(int(hexOpcode, 16))
opcodeArgsList = findOpcodeArgs(int(hexOpcode, 16))
opcodeArgs = ""
for arg in opcodeArgsList:
opcodeArgs += arg + ", "
opcodeArgs = opcodeArgs[ : -2]
assemblyInstructions.append(hex(programCounter) + ":\t" + hexOpcode + "\t\t" + opcodeInstruction + "\t" + opcodeArgs)
programCounter += 2;
opcode = f.read(2)
return assemblyInstructions
def maskOpcode(opcode):
if (opcode & 0xF000) in [0x0000, 0xE000, 0xF000]:
opcode &= 0xF0FF
elif (opcode & 0xF000) in [0x8000]:
opcode &= 0xF00F
else:
opcode &= 0xF000
return opcode
def lookupOpcode(opcode):
opcode = maskOpcode(opcode)
return opcodeTable.get(opcode, "???");
def findOpcodeArgs(opcode):
maskedOpcode = maskOpcode(opcode)
return opcodeArgsTable.get(maskedOpcode)(opcode)
if __name__ == '__main__':
main()