diff --git a/CHANGES.txt b/CHANGES.txt index 64eda3a..a705124 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -15,6 +15,9 @@ - Added support for 65C02 opcode 0x7C: JMP (abs,x). + - Assembling now shows an Overflow Error if the statement won't + fit at the top of memory. + 0.13 (2012-11-15) - Fixed a bug where negative numbers could be entered diff --git a/py65/assembler.py b/py65/assembler.py index 58771c6..958c70d 100644 --- a/py65/assembler.py +++ b/py65/assembler.py @@ -130,6 +130,11 @@ class Assembler: operands = [int(hex, 16) for hex in operands] bytes.extend(operands) + + # raise if the assembled bytes would exceed top of memory + if (pc + len(bytes)) > (2 ** self._mpu.ADDR_WIDTH): + raise OverflowError + return bytes # assembly failed diff --git a/py65/tests/test_assembler.py b/py65/tests/test_assembler.py index a5bfa70..fa3a4ae 100644 --- a/py65/tests/test_assembler.py +++ b/py65/tests/test_assembler.py @@ -19,6 +19,17 @@ class AssemblerTests(unittest.TestCase): self.assertRaises(OverflowError, self.assemble, 'lda #$fff') + def test_assemble_1_byte_at_top_of_mem_should_not_overflow(self): + self.assemble('nop', pc=0xFFFF) # should not raise + + def test_assemble_3_bytes_at_top_of_mem_should_not_overflow(self): + self.assemble('jmp $1234', pc=0xFFFD) # should not raise + + def test_assemble_should_overflow_if_over_top_of_mem(self): + # jmp $1234 requires 3 bytes but there's only 2 at $FFFE-FFFF + self.assertRaises(OverflowError, + self.assemble, "jmp $1234", pc=0xFFFE) + def test_assembles_00(self): self.assertEqual([0x00], self.assemble('BRK'))