Skip to content

Instantly share code, notes, and snippets.

@jarobins
Created May 31, 2015 20:21
Show Gist options
  • Save jarobins/b1c7173c0025edf59dbb to your computer and use it in GitHub Desktop.
Save jarobins/b1c7173c0025edf59dbb to your computer and use it in GitHub Desktop.
# Chip 8 OpCode Reader
# Loads a chip 8 rom and reads the opcodes
# Displays a human readable discribtion of the opcode
rom = open("pong", "rb")
data = []
simple_codes = [1,2,3,4,5,6,7,9,10,11,12,13]
for num, line in enumerate(rom.read()):
if num % 2 == 0:
part = format(ord(line), '02x')
else:
data.append(part+format(ord(line), '02x'))
for num, opcode in enumerate(data):
print format(num+512, '04x'),
first_nib = int(opcode[0], 16)
if first_nib in simple_codes:
if first_nib == 1:
print opcode, "JUMP TO", opcode[1:]
elif first_nib == 2:
print opcode, "CALL SUB AT", opcode[1:]
elif first_nib == 3:
print opcode, "SKIP IF REG", opcode[1], "==", opcode[2:]
elif first_nib == 4:
print opcode, "SKIP IF REG", opcode[1], "!=", opcode[2:]
elif first_nib == 5:
print opcode, "SKIP IF REG", opcode[1], "== REG", opcode[2]
elif first_nib == 6:
print opcode, "SET REG", opcode[1], "==", opcode[2:]
elif first_nib == 7:
print opcode, "ADD", opcode[2:], "TO REG", opcode[1]
elif first_nib == 9:
print opcode, "SKIP IF !="
elif first_nib == 10:
print opcode, "SET INSTR TO ADDR", opcode[1:]
elif first_nib == 11:
print opcode, "JUMP PLUS"
elif first_nib == 12:
print opcode, "RNDOM"
elif first_nib == 13:
print opcode, "DRAW"
else:
opcode, "SIMPLE"
else:
first_nib = int(opcode[0], 16)
last_nib = int(opcode[3], 16)
if opcode == "00e0":
print opcode, "CLEAR"
elif opcode == "00ee":
print opcode, "RETURN SUB"
elif first_nib == 0:
print opcode, "CALL RCA 1802"
elif first_nib == 8 and last_nib == 0:
print opcode, "SET"
elif first_nib == 8 and last_nib == 1:
print opcode, "SET OR"
elif first_nib == 8 and last_nib == 2:
print opcode, "SET AND"
elif first_nib == 8 and last_nib == 3:
print opcode, "SET XOR"
elif first_nib == 8 and last_nib == 4:
print opcode, "ADD"
elif first_nib == 8 and last_nib == 5:
print opcode, "SUBTRACT"
elif first_nib == 8 and last_nib == 6:
print opcode, "SHIFT"
elif first_nib == 8 and last_nib == 7:
print opcode, "SUBTRACT"
elif first_nib == 8 and last_nib == 14:
print opcode, "SHIFT"
elif first_nib == 14 and last_nib == 14:
print opcode, "SKIP IF PRESS"
elif first_nib == 14 and last_nib == 1:
print opcode, "SKIP IF NOT PRESS"
elif first_nib == 15 and last_nib == 7:
print opcode, "SET TO DELAY"
elif first_nib == 15 and last_nib == 10:
print opcode, "WAIT FOR KEY"
elif first_nib == 15 and last_nib == 5:
print opcode, "SET DELAY"
elif first_nib == 15 and last_nib == 8:
print opcode, "SET SOUND"
elif first_nib == 15 and last_nib == 14:
print opcode, "ADD TO INSTR"
elif first_nib == 15 and last_nib == 9:
print opcode, "DRAW INSTR"
elif first_nib == 15 and last_nib == 3:
print opcode, "SET BIN CODE"
else:
print opcode, "COMPLEX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment