def render(instruction): return "{} {}".format(instruction['mnemonic'], instruction['op']) def Table_5_12(nibble): return "Table 5.12 TODO" def Table_5_11(nibble): return "Table 5.11 TODO" def Table_5_10(nibble): return "Table 5.10 TODO" def YDataPrefetchDest_5_9(nibble): return "Table 5.9 TODO" def YDataPrefetchOperation_5_8(nibble): return "Table 5.8 TODO" def XDataPrefetchDest_5_7(nibble): return "Table 5.7 TODO" def XDataPrefetchOperation_5_6(nibble): return "Table 5.6 TODO" def readWd_5_5(octet): return [ "Wd", "[Wd]", "[Wd--]", "[Wd++]", "[--Wd]", "[++Wd]", "[Wd + Wb]", "[Wd + Wb]" ][octet] def readWs_5_4(octet): return [ "Ws", "[Ws]", "[Ws--]", "[Ws++]", "[--Ws]", "[++Ws]", "[Ws + Wb]", "[Ws + Wb]" ][octet] def readWd_5_3(octet): return [ "Wd", "[Wd]", "[Wd--]", "[Wd++]", "[--Wd]", "[++Wd]", "ERR", "ERR" ][octet] def readWs_5_2(octet): return [ "Ws", "[Ws]", "[Ws--]", "[Ws++]", "[--Ws]", "[++Ws]", "ERR", "ERR" ][octet] def disassemble(blob, offset): # print ("Disassembling " + str(blob)) instrbytes = blob[offset:offset + 3] # instrbytes.reverse() print("Disassembling {} {} {}".format(hex(instrbytes[0]), hex(instrbytes[1]), hex(instrbytes[2]))) instr = {} instr['op'] = '' size = 1 if instrbytes[2] == 0x0: instr['mnemonic'] = 'nop' if instrbytes[2] == 0x1: instr['mnemonic'] = 'br*' if instrbytes[2] == 0x2: instr['mnemonic'] = 'call' if instrbytes[2] == 0x3: instr['mnemonic'] = '-' if instrbytes[2] == 0x4: instr['mnemonic'] = 'goto' size = 2 w2 = blob[offset + 3:offset + 6] # w2.reverse() n_hi = ((instrbytes[1] << 8) + instrbytes[0]) if w2[0] != 0 or w2[1] != 0: instr['mnemonic'] = 'goto [BAD]' n_lo = w2[0] n = n_hi + (n_lo << 16) instr["op"] = hex(n) if instrbytes[2] == 0x5: instr['mnemonic'] = 'retlw' if instrbytes[2] == 0x6: instr['mnemonic'] = 'retfie' if instrbytes[2] == 0x7: instr['mnemonic'] = 'rcall' if instrbytes[2] == 0x8: instr['mnemonic'] = 'do' if instrbytes[2] == 0x9: instr['mnemonic'] = 'repeat' if instrbytes[2] == 0xa: instr['mnemonic'] = '-' if instrbytes[2] == 0xb: instr['mnemonic'] = '-' if instrbytes[2] == 0xc: instr['mnemonic'] = 'bra(oa)' instr['mnemonic'] += " (dsPIC3XF only)" if instrbytes[2] == 0xd: instr['mnemonic'] = 'bra(ob)' instr['mnemonic'] += " (dsPIC3XF only)" if instrbytes[2] == 0xe: instr['mnemonic'] = 'bra(sa)' instr['mnemonic'] += " (dsPIC3XF only)" if instrbytes[2] == 0xf: instr['mnemonic'] = 'bra(sb)' instr['mnemonic'] += " (dsPIC3XF only)" if instrbytes[2] >= 0x10 and instrbytes[2] < 0x18: instr['mnemonic'] = 'subr' if instrbytes[2] >= 0x18 and instrbytes[2] < 0x20: instr['mnemonic'] = 'subbr' if instrbytes[2] >= 0x20 and instrbytes[2] < 0x30: instr['mnemonic'] = 'mov' word = instrbytes[0] | (instrbytes[1] << 8) | (instrbytes[2] << 16) imm = (word & 0x0ffff0) >> 4 reg = word & 0xf instr['op'] = "#" + hex(imm) + ", W" + str(reg) if instrbytes[2] >= 0x80 and instrbytes[2] < 0x90: instr['mnemonic'] = 'mov' word = instrbytes[0] | (instrbytes[1] << 8) | (instrbytes[2] << 16) f = (word & 0x07fff0) >> 4 reg = word & 0xf if instrbytes[2] < 0x88: instr['op'] = "[" + hex(f) + "], W" + str(reg) else: instr['op'] = "W" + str(reg) + ", [" + hex(f) + "]" if instrbytes[2] >= 0xc0 and instrbytes[2] < 0xd0: instr['mnemonic'] = "(dsPIC3XF only)" if instrbytes[2] == 0xB4 and (instrbytes[1] & 0x80) == 0x00: instr['mnemonic'] = "ADD" B = instrbytes[1] & 0x40 D = instrbytes[1] & 0x20 f = ((instrbytes[1] & 0x1f) << 8) + instrbytes[0] if B: instr['mnemonic'] += '.B' instr['op'] = "RAM:" + hex(f) if D: instr['op'] += ', W0' if instrbytes[2] == 0xaa: instr['mnemonic'] = "BTG" # TODO: not clear what is .B mode bit4 = ((instrbytes[1] >> 4) & 0x0e) + (instrbytes[0] & 0x01) f = ((instrbytes[1] & 0x1f) << 7) + (instrbytes[0] >> 1) instr['op'] = "{}, #{}".format(hex(f), hex(bit4)) if instrbytes[2] == 0xba: instr['mnemonic'] = "TBLRD" if (instrbytes[1] & 0x80) != 0: instr['mnemonic'] += 'H' else: instr['mnemonic'] += 'L' if (instrbytes[1] & 0x40) != 0: instr['mnemonic'] += '.B' s = (instrbytes[0] & 0x0f) p = (instrbytes[0] >> 4) & 0x07 d = ((instrbytes[0] >> 7) & 0x01) + ((instrbytes[1] << 1) & 0xf) q = (instrbytes[1] & 0x38) >> 3 srcOp = readWs_5_2(p).replace("Ws", "W" + str(s)) dstOp = readWd_5_3(q).replace("Wd", "W" + str(d)) instr['op'] = "{}, {}".format(srcOp, dstOp) if instrbytes[2] == 0xbb: instr['mnemonic'] = "TBLWT" if (instrbytes[1] & 0x80) != 0: instr['mnemonic'] += 'H' else: instr['mnemonic'] += 'L' if (instrbytes[1] & 0x40) != 0: instr['mnemonic'] += '.B' s = (instrbytes[0] & 0x0f) p = (instrbytes[0] >> 4) & 0x07 d = ((instrbytes[0] >> 7) & 0x01) + ((instrbytes[1] << 1) & 0xf) q = (instrbytes[1] & 0x38) >> 3 srcOp = readWs_5_2(p).replace("Ws", "W" + str(s)) dstOp = readWd_5_3(q).replace("Wd", "W" + str(d)) instr['op'] = "{}, {}".format(srcOp, dstOp) if 'mnemonic' not in instr: instr = instrbytes[0] | (instrbytes[1] << 8) | (instrbytes[2] << 16) return (offset + 3, "Unknown instruction: " + hex(instr)) return (offset + 3 * size, instr)