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