From fdf4145b5539ee83eb1f2bf057d4ffecbc5161db Mon Sep 17 00:00:00 2001 From: Mike Naberezny Date: Sun, 25 Nov 2012 15:11:45 -0800 Subject: [PATCH] Fix disassembly output when instruction wraps past the top of memory --- CHANGES.txt | 3 +++ py65/monitor.py | 19 ++++++++++++++----- py65/tests/test_disassembler.py | 13 +++++++++++++ py65/tests/test_monitor.py | 12 ++++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index a705124..df73610 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -18,6 +18,9 @@ - Assembling now shows an Overflow Error if the statement won't fit at the top of memory. + - Fixed a bug in the disassembler where instructions that wrap past + the top of memory would not be displayed properly. + 0.13 (2012-11-15) - Fixed a bug where negative numbers could be entered diff --git a/py65/monitor.py b/py65/monitor.py index 143b426..e3902d0 100644 --- a/py65/monitor.py +++ b/py65/monitor.py @@ -369,14 +369,23 @@ class Monitor(cmd.Cmd): self._output(self._format_disassembly(address, bytes, disasm)) address += bytes - def _format_disassembly(self, address, bytes, disasm): - mem = '' - for byte in self._mpu.memory[address:address + bytes]: - mem += self.byteFmt % byte + " " + def _format_disassembly(self, address, length, disasm): + cur_address = address + max_address = (2 ** self._mpu.ADDR_WIDTH) - 1 + + bytes_remaining = length + dump = '' + + while bytes_remaining: + if cur_address > max_address: + cur_address = 0 + dump += self.byteFmt % self._mpu.memory[cur_address] + " " + cur_address += 1 + bytes_remaining -= 1 fieldwidth = 1 + (1 + self.byteWidth / 4) * 3 fieldfmt = "%%-%ds" % fieldwidth - return "$" + self.addrFmt % address + " " + fieldfmt % mem + disasm + return "$" + self.addrFmt % address + " " + fieldfmt % dump + disasm def help_disassemble(self): self._output("disassemble ") diff --git a/py65/tests/test_disassembler.py b/py65/tests/test_disassembler.py index 1841592..648eff3 100644 --- a/py65/tests/test_disassembler.py +++ b/py65/tests/test_disassembler.py @@ -7,6 +7,19 @@ from py65.utils.addressing import AddressParser class DisassemblerTests(unittest.TestCase): + def test_disassemble_wraps_after_top_of_mem(self): + mpu = MPU() + mpu.memory[0xFFFF] = 0x20 # JSR + mpu.memory[0x0000] = 0xD2 # + mpu.memory[0x0001] = 0xFF # $FFD2 + + dis = Disassembler(mpu) +# length, disasm = dis.instruction_at(0xFFFF) + from pprint import pprint as pp +# pp(disasm) + +# return disasm.instruction_at(pc) + def test_disassembles_00(self): length, disasm = self.disassemble([0x00]) self.assertEqual(1, length) diff --git a/py65/tests/test_monitor.py b/py65/tests/test_monitor.py index 2fab92a..528c4f8 100644 --- a/py65/tests/test_monitor.py +++ b/py65/tests/test_monitor.py @@ -301,6 +301,18 @@ class MonitorTests(unittest.TestCase): disasm = "$c000 ea NOP\n$c001 ea NOP\n" self.assertEqual(out, disasm) + def test_disassemble_wraps_to_0_after_to_of_memory(self): + stdout = StringIO() + mon = Monitor(stdout=stdout) + mon._mpu.memory[0xffff] = 0x20 # => JSR + mon._mpu.memory[0x0000] = 0xD2 # + mon._mpu.memory[0x0001] = 0xFF # => $FFD2 + mon.do_disassemble("ffff") + + out = stdout.getvalue() + disasm = "$ffff 20 d2 ff JSR $ffd2\n" + self.assertEqual(out, disasm) + # fill def test_shortcut_f_for_fill(self):