Add raw binary to disassemly

This commit is contained in:
Juhani Krekelä 2022-07-23 15:51:11 +03:00
parent 099b09eee3
commit 29cb28fa49
1 changed files with 9 additions and 6 deletions

View File

@ -29,7 +29,7 @@ opcodes = [
Instruction = namedtuple('Instruction', ['opcode', 'rx', 'ry', 'addr']) Instruction = namedtuple('Instruction', ['opcode', 'rx', 'ry', 'addr'])
Data = namedtuple('Data', ['byte']) Data = namedtuple('Data', ['byte'])
Statement = namedtuple('Statement', ['addr', 'contents']) Statement = namedtuple('Statement', ['addr', 'raw', 'contents'])
def segment(binary, origin): def segment(binary, origin):
statements = [] statements = []
@ -47,22 +47,25 @@ def segment(binary, origin):
if not opcodes[opcode].ry and ry != 0: valid = False if not opcodes[opcode].ry and ry != 0: valid = False
if not valid: if not valid:
statements.append(Statement(ip, Data(byte))) raw = binary[ip:ip + 1]
statements.append(Statement(ip, raw, Data(byte)))
ip += 1 ip += 1
elif opcodes[opcode].addr: elif opcodes[opcode].addr:
raw = binary[ip:ip + 3]
addr = (binary[ip + 1] << 8) + binary[ip + 2] addr = (binary[ip + 1] << 8) + binary[ip + 2]
instruction = Instruction(opcode, rx, ry, addr) instruction = Instruction(opcode, rx, ry, addr)
statements.append(Statement(ip, instruction)) statements.append(Statement(ip, raw, instruction))
ip += 3 ip += 3
else: else:
raw = binary[ip:ip + 1]
instruction = Instruction(opcode, rx, ry, None) instruction = Instruction(opcode, rx, ry, None)
statements.append(Statement(ip, instruction)) statements.append(Statement(ip, raw, instruction))
ip += 1 ip += 1
return statements return statements
def disasm(binary, origin = 0): def disasm(binary, origin = 0):
for addr, contents in segment(binary, origin): for addr, raw, contents in segment(binary, origin):
if type(contents) == Data: if type(contents) == Data:
statement = f'db 0x{contents.byte:02x}' statement = f'db 0x{contents.byte:02x}'
else: else:
@ -84,7 +87,7 @@ def disasm(binary, origin = 0):
statement = f'{mnemonic} {fields}' statement = f'{mnemonic} {fields}'
else: else:
statement = mnemonic statement = mnemonic
print(f'{addr:04x} {statement}') print(f'{addr:04x} {raw.hex():6} {statement}')
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys