#!/usr/bin/env python3 from sys import argv base = 0x2000 file = argv[1] addr = int(argv[2], 16) - base instrs = { 0x00: ("brk", 1), 0x01: ("mul_r0", 3), 0x02: ("mul_r0_r1", 1), 0x03: ("div_r0", 3), 0x04: ("div_r0_r1", 1), 0x05: ("ret", 1), 0x06: ("copr", 2), 0x07: ("illegal_07", 1), 0x08: ("nop", 1), 0x09: ("ld_sp", 3), 0x0a: ("ld_r0_sp", 1), 0x0b: ("ld_sp_r0", 1), 0x0c: ("nop_0c", 1), 0x0d: ("illegal_0d", 1), 0x0e: ("shl_r0", 1), 0x0f: ("shr_r0", 1), 0x10: ("ld_r0", 3), 0x11: ("ld_r1", 3), 0x12: ("ld_r2", 3), 0x13: ("ld_r3", 3), 0x14: ("illegal_14", 1), 0x15: ("illegal_15", 1), 0x16: ("illegal_16", 1), 0x17: ("illegal_17", 1), 0x18: ("illegal_18", 1), 0x19: ("illegal_19", 1), 0x1a: ("illegal_1a", 1), 0x1b: ("illegal_1b", 1), 0x1c: ("illegal_1c", 1), 0x1d: ("illegal_1d", 1), 0x1e: ("illegal_1e", 1), 0x1f: ("illegal_1f", 1), 0x20: ("ld_r0_r0", 1), 0x21: ("ld_r0_r1", 1), 0x22: ("ld_r0_r2", 1), 0x23: ("ld_r0_r3", 1), 0x24: ("ld_r1_r0", 1), 0x25: ("ld_r1_r1", 1), 0x26: ("ld_r1_r2", 1), 0x27: ("ld_r1_r3", 1), 0x28: ("ld_r2_r0", 1), 0x29: ("ld_r2_r1", 1), 0x2a: ("ld_r2_r2", 1), 0x2b: ("ld_r2_r3", 1), 0x2c: ("ld_r3_r0", 1), 0x2d: ("ld_r3_r1", 1), 0x2e: ("ld_r3_r2", 1), 0x2f: ("ld_r3_r3", 1), 0x30: ("add_r0_r0", 1), 0x31: ("add_r0_r1", 1), 0x32: ("add_r0_r2", 1), 0x33: ("add_r0_r3", 1), 0x34: ("add_r1_r0", 1), 0x35: ("add_r1_r1", 1), 0x36: ("add_r1_r2", 1), 0x37: ("add_r1_r3", 1), 0x38: ("add_r2_r0", 1), 0x39: ("add_r2_r1", 1), 0x3a: ("add_r2_r2", 1), 0x3b: ("add_r2_r3", 1), 0x3c: ("add_r3_r0", 1), 0x3d: ("add_r3_r1", 1), 0x3e: ("add_r3_r2", 1), 0x3f: ("add_r3_r3", 1), 0x40: ("ldrb_r0_r0", 1), 0x41: ("ldrb_r0_r1", 1), 0x42: ("ldrb_r0_r2", 1), 0x43: ("ldrb_r0_r3", 1), 0x44: ("ldrb_r1_r0", 1), 0x45: ("ldrb_r1_r1", 1), 0x46: ("ldrb_r1_r2", 1), 0x47: ("ldrb_r1_r3", 1), 0x48: ("ldrb_r2_r0", 1), 0x49: ("ldrb_r2_r1", 1), 0x4a: ("ldrb_r2_r2", 1), 0x4b: ("ldrb_r2_r3", 1), 0x4c: ("ldrb_r3_r0", 1), 0x4d: ("ldrb_r3_r1", 1), 0x4e: ("ldrb_r3_r2", 1), 0x4f: ("ldrb_r3_r3", 1), 0x50: ("ldr_r0_r0", 1), 0x51: ("ldr_r0_r1", 1), 0x52: ("ldr_r0_r2", 1), 0x53: ("ldr_r0_r3", 1), 0x54: ("ldr_r1_r0", 1), 0x55: ("ldr_r1_r1", 1), 0x56: ("ldr_r1_r2", 1), 0x57: ("ldr_r1_r3", 1), 0x58: ("ldr_r2_r0", 1), 0x59: ("ldr_r2_r1", 1), 0x5a: ("ldr_r2_r2", 1), 0x5b: ("ldr_r2_r3", 1), 0x5c: ("ldr_r3_r0", 1), 0x5d: ("ldr_r3_r1", 1), 0x5e: ("ldr_r3_r2", 1), 0x5f: ("ldr_r3_r3", 1), 0x60: ("strb_r0_r0", 1), 0x61: ("strb_r1_r0", 1), 0x62: ("strb_r2_r0", 1), 0x63: ("strb_r3_r0", 1), 0x64: ("strb_r0_r1", 1), 0x65: ("strb_r1_r1", 1), 0x66: ("strb_r2_r1", 1), 0x67: ("strb_r3_r1", 1), 0x68: ("strb_r0_r2", 1), 0x69: ("strb_r1_r2", 1), 0x6a: ("strb_r2_r2", 1), 0x6b: ("strb_r3_r2", 1), 0x6c: ("strb_r0_r3", 1), 0x6d: ("strb_r1_r3", 1), 0x6e: ("strb_r2_r3", 1), 0x6f: ("strb_r3_r3", 1), 0x70: ("str_r0_r0", 1), 0x71: ("str_r1_r0", 1), 0x72: ("str_r2_r0", 1), 0x73: ("str_r3_r0", 1), 0x74: ("str_r0_r1", 1), 0x75: ("str_r1_r1", 1), 0x76: ("str_r2_r1", 1), 0x77: ("str_r3_r1", 1), 0x78: ("str_r0_r2", 1), 0x79: ("str_r1_r2", 1), 0x7a: ("str_r2_r2", 1), 0x7b: ("str_r3_r2", 1), 0x7c: ("str_r0_r3", 1), 0x7d: ("str_r1_r3", 1), 0x7e: ("str_r2_r3", 1), 0x7f: ("str_r3_r3", 1), 0x80: ("cp_r0_r0", 1), 0x81: ("cp_r0_r1", 1), 0x82: ("cp_r0_r2", 1), 0x83: ("cp_r0_r3", 1), 0x84: ("cp_r1_r0", 1), 0x85: ("cp_r1_r1", 1), 0x86: ("cp_r1_r2", 1), 0x87: ("cp_r1_r3", 1), 0x88: ("cp_r2_r0", 1), 0x89: ("cp_r2_r1", 1), 0x8a: ("cp_r2_r2", 1), 0x8b: ("cp_r2_r3", 1), 0x8c: ("cp_r3_r0", 1), 0x8d: ("cp_r3_r1", 1), 0x8e: ("cp_r3_r2", 1), 0x8f: ("cp_r3_r3", 1), 0x90: ("push_r0", 1), 0x91: ("push_r1", 1), 0x92: ("push_r2", 1), 0x93: ("push_r3", 1), 0x94: ("pop_r0", 1), 0x95: ("pop_r1", 1), 0x96: ("pop_r2", 1), 0x97: ("pop_r3", 1), 0x98: ("jp", 3), 0x99: ("call", 3), 0x9a: ("jp_hi", 3), 0x9b: ("jp_lo", 3), 0x9c: ("jp_eq", 3), 0x9d: ("jp_ne", 3), 0x9e: ("call_hi", 3), 0x9f: ("call_lo", 3), 0xa0: ("call_eq", 3), 0xa1: ("call_ne", 3), 0xa2: ("cp_r0", 3), 0xa3: ("cp_r1", 3), 0xa4: ("cp_r2", 3), 0xa5: ("cp_r3", 3), 0xa6: ("push_sp", 1), 0xa7: ("push_pc", 1), 0xa8: ("inc_r0", 1), 0xa9: ("inc_r1", 1), 0xaa: ("inc_r2", 1), 0xab: ("inc_r3", 1), 0xac: ("dec_r0", 1), 0xad: ("dec_r1", 1), 0xae: ("dec_r2", 1), 0xaf: ("dec_r3", 1), 0xb0: ("ldrb_r0", 3), 0xb1: ("ldrb_r1", 3), 0xb2: ("ldrb_r2", 3), 0xb3: ("ldrb_r3", 3), 0xb4: ("ldr_r0", 3), 0xb5: ("ldr_r1", 3), 0xb6: ("ldr_r2", 3), 0xb7: ("ldr_r3", 3), 0xb8: ("strb_r0", 3), 0xb9: ("strb_r1", 3), 0xba: ("strb_r2", 3), 0xbb: ("strb_r3", 3), 0xbc: ("str_r0", 3), 0xbd: ("str_r1", 3), 0xbe: ("str_r2", 3), 0xbf: ("str_r3", 3), 0xc0: ("illegal_c0", 1), 0xc1: ("illegal_c1", 1), 0xc2: ("illegal_c2", 1), 0xc3: ("illegal_c3", 1), 0xc4: ("illegal_c4", 1), 0xc5: ("jp_r0", 1), 0xc6: ("jp_r1", 1), 0xc7: ("jp_r2", 1), 0xc8: ("jp_r3", 1), 0xc9: ("call_r0", 1), 0xca: ("call_r1", 1), 0xcb: ("call_r2", 1), 0xcc: ("call_r3", 1), 0xcd: ("and_r0", 3), 0xce: ("or_r0", 3), 0xcf: ("xor_r0", 3), 0xd0: ("and_r0_r0", 1), 0xd1: ("and_r0_r1", 1), 0xd2: ("and_r0_r2", 1), 0xd3: ("and_r0_r3", 1), 0xd4: ("or_r0_r0", 1), 0xd5: ("or_r0_r1", 1), 0xd6: ("or_r0_r2", 1), 0xd7: ("or_r0_r3", 1), 0xd8: ("xor_r0_r0", 1), 0xd9: ("xor_r0_r1", 1), 0xda: ("xor_r0_r2", 1), 0xdb: ("xor_r0_r3", 1), 0xdc: ("swap_r0", 1), 0xdd: ("swap_r1", 1), 0xde: ("swap_r2", 1), 0xdf: ("swap_r3", 1), 0xe0: ("add_r0", 3), 0xe1: ("add_r1", 3), 0xe2: ("add_r2", 3), 0xe3: ("add_r3", 3), 0xe4: ("illegal_e4", 1), 0xe5: ("illegal_e5", 1), 0xe6: ("illegal_e6", 1), 0xe7: ("illegal_e7", 1), 0xe8: ("illegal_e8", 1), 0xe9: ("illegal_e9", 1), 0xea: ("illegal_ea", 1), 0xeb: ("illegal_eb", 1), 0xec: ("illegal_ec", 1), 0xed: ("illegal_ed", 1), 0xee: ("illegal_ee", 1), 0xef: ("illegal_ef", 1), 0xf0: ("illegal_f0", 1), 0xf1: ("illegal_f1", 1), 0xf2: ("illegal_f2", 1), 0xf3: ("illegal_f3", 1), 0xf4: ("illegal_f4", 1), 0xf5: ("illegal_f5", 1), 0xf6: ("illegal_f6", 1), 0xf7: ("illegal_f7", 1), 0xf8: ("illegal_f8", 1), 0xf9: ("illegal_f9", 1), 0xfa: ("illegal_fa", 1), 0xfb: ("illegal_fb", 1), 0xfc: ("illegal_fc", 1), 0xfd: ("illegal_fd", 1), 0xfe: ("illegal_fe", 1), 0xff: ("illegal_ff", 1), } stop_instr = [ "ret", "jp" ] f = open(file, "rb") f.seek(addr) while True: o = f.tell() + base b = f.read(1)[0] if b not in instrs: print("Unhandled: %02x" % b) break i = instrs[b] a = 0 for x in range(i[1]-1): a |= f.read(1)[0] << (8 * x) if i[1] != 1: print("%s 0x%04x # 0x%04x" % (i[0], a, o)) else: print("%s # 0x%04x" % (i[0], o)) if i[0] in stop_instr: break