def render(instr): if 'op' in instr: return "{} {}".format(instr['mnemonic'], instr['op']) else: return instr['mnemonic'] def disassemble(blob, offset): instr = {} instr['length'] = 1 instrbytes = blob[offset:offset + 2] # instrbytes.reverse() print("Decoding {}{}...".format(hex(instrbytes[0])[2:], hex(instrbytes[1])[2:])) if instrbytes[0] == 0x00: print(hex(instrbytes[1])) # there's a few instructions here... if instrbytes[1] == 0xff: instr['mnemonic'] = 'reset' else: instr['mnemonic'] = [ 'nop', 'BAD', 'BAD', 'sleep', 'clrwdt', 'push', 'pop', 'daw', 'tblrd*', 'tblrd*+', 'tblrd*-', 'tblrd+*', 'tblwr*', 'tblwr*+', 'tblwr*-', 'tblwr+*', 'retfie', 'retfie fast', 'return', 'return fast', 'callw*'][instrbytes[1]] if instrbytes[0] == 0x01: if instrbytes[1] > 0xf: instr['mnemonic'] = 'BAD' else: instr['mnemonic'] = 'movlb' instr['op'] = "#" + str(instrbytes[1]) elif instrbytes[0] == 0x02 or instrbytes[0] == 0x03: instr['mnemonic'] = 'mulwf' instr['op'] = 'TODO' elif instrbytes[0] == 0x04 or instrbytes[0] == 0x05 or instrbytes[0] == 0x06 or instrbytes[0] == 0x07: instr['mnemonic'] = 'decf' instr['op'] = 'TODO' elif instrbytes[0] == 0x08: instr['mnemonic'] = 'sublw' instr['op'] = '#' + hex(instrbytes[1]) elif instrbytes[0] == 0x09: instr['mnemonic'] = 'iorlw' instr['op'] = '#' + hex(instrbytes[1]) elif instrbytes[0] == 0x0a: instr['mnemonic'] = 'xorlw' instr['op'] = '#' + hex(instrbytes[1]) elif instrbytes[0] == 0x0b: instr['mnemonic'] = 'andlw' instr['op'] = '#' + hex(instrbytes[1]) elif instrbytes[0] == 0x0c: instr['mnemonic'] = 'retlw' instr['op'] = '#' + hex(instrbytes[1]) elif instrbytes[0] == 0x0d: instr['mnemonic'] = 'mullw' instr['op'] = '#' + hex(instrbytes[1]) elif instrbytes[0] == 0x0e: instr['mnemonic'] = 'movlw' instr['op'] = '#' + hex(instrbytes[1]) elif instrbytes[0] == 0x0f: instr['mnemonic'] = 'addlw' instr['op'] = '#' + hex(instrbytes[1]) elif instrbytes[0] >= 0x20 and instrbytes[0] <= 0x5f: mnemonicSel = instrbytes[0] >> 2 instr['mnemonic'] = [ 'iorwf', 'andwf', 'xorwf', 'comf', 'addwfc', 'addwf', 'incf', 'decfsz', 'rrcf', 'rlcf', 'swapf', 'incfsz', 'rrncf', 'rlncf', 'infsnz', 'dcfsnz', 'movf', 'subwfb', 'subwfb', 'subwf' ][mnemonicSel] instr['op'] = 'TODO' elif instrbytes[0] >= 0x60 and instrbytes[0] < 0x70: mnemonicSel = instrbytes[0] >> 1 instr['mnemonic'] = [ 'cpfslt', 'cpfseq', 'cpfsgt', 'tstfsz', 'setf', 'clrf', 'negf', 'movwf' ][mnemonicSel] instr['op'] = 'TODO' elif instrbytes[0] >= 0x70 and instrbytes[0] < 0xc0: mnemonicSel = instrbytes[0] >> 4 instr['mnemonic'] = [ 'BAD', 'BAD', 'BAD', 'BAD', 'BAD', 'BAD', 'BAD', 'BTG', 'BSF', 'BCF', 'BTFSS', 'BTFSC' ][mnemonicSel] instr['op'] = 'TODO' elif instrbytes[0] >= 0xc0 and instrbytes[0] < 0xd0: instr['mnemonic'] = 'MOVFF' instr['op'] = 'TODO' elif instrbytes[0] >= 0xd0 and instrbytes[0] < 0xd8: instr['mnemonic'] = 'BRA' instr['op'] = 'TODO' elif instrbytes[0] >= 0xd8 and instrbytes[0] < 0xe0: instr['mnemonic'] = 'RCALL' instr['op'] = 'TODO' elif instrbytes[0] >= 0xf0 and instrbytes[0] <= 0xff: instr['mnemonic'] = 'MOVFF' instr['op'] = 'TODO' elif instrbytes[0] == 0xec or instrbytes[0] == 0xed: instr['mnemonic'] = 'CALL' instr['op'] = 'TODO' instr['length'] = 2 elif instrbytes[0] == 0xee: instr['mnemonic'] = 'LFSR' instr['op'] = 'TODO' instr['length'] = 2 elif instrbytes[0] == 0xef: instr['mnemonic'] = 'GOTO' instr['op'] = 'TODO' instr['length'] = 2 else: instr['mnemonic'] = 'TODO' return (offset + instr['length'] * 2, instr)