From 0a2cf66672364ed555c85682e948cff0b54c8f35 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sat, 24 Feb 2024 19:04:00 +0200 Subject: Fix bugs, add basic debugger --- atk16_emu/emu.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'atk16_emu/emu.py') diff --git a/atk16_emu/emu.py b/atk16_emu/emu.py index 591eeb5..8eeb6b1 100644 --- a/atk16_emu/emu.py +++ b/atk16_emu/emu.py @@ -208,6 +208,8 @@ class Machine: instr = self.mem_read(pc_addr) instruction = self.decode(instr) + #print(f"Executing instruction 0b{instr:>016b} (0x{instr:>04x}) at address 0x{pc_addr:>04x}") + try: match instruction: case ALR(target, left, right, alu_code): @@ -254,6 +256,8 @@ class Machine: self.pc.value = addr case JPI(imm): + # convert imm from signed (twos complement) 9-bit to a python int + imm = (imm & (0b011111111)) - (imm & 0b100000000) self.pc.value = (self.pc.value + imm) & 0xFFFF case BRR(flag, addr_reg): @@ -262,6 +266,8 @@ class Machine: self.pc.value = addr case BRI(flag, addr_imm): + # convert imm from signed (twos complement) 9-bit to a python int + addr_imm = (addr_imm & (0b011111111)) - (addr_imm & 0b100000000) if self.check_nth_flag(flag): self.pc.value = (self.pc.value + addr_imm) & 0xFFFF @@ -288,7 +294,7 @@ class Machine: self.running = False except: - print(f"Error while executing instruction {instr:>016b} ({instr:>04x}) at address {pc_addr:>04x}", file=sys.stderr) + print(f"Error while executing instruction 0b{instr:>016b} (0x{instr:>04x}) at address 0x{pc_addr:>04x}", file=sys.stderr) raise def decode(self, instr: int): -- cgit v1.3