1
0
mirror of https://github.com/mnaberez/py65.git synced 2025-01-16 18:33:00 +00:00

Handle overflow when setting registers

This commit is contained in:
Mike Naberezny 2014-12-14 16:44:13 -08:00
parent 1b639ebfd0
commit a941071c92
2 changed files with 33 additions and 7 deletions

View File

@ -223,8 +223,7 @@ class Monitor(cmd.Cmd):
self._output("\n" + repr(self._mpu))
def _output(self, stuff):
if stuff is not None:
self.stdout.write(stuff + "\n")
self.stdout.write("%s\n" % stuff)
def _exit(self, exitcode=0):
sys.exit(exitcode)
@ -523,12 +522,23 @@ class Monitor(cmd.Cmd):
self._output("Invalid register: %s" % register)
else:
try:
intval = self._address_parser.number(value) & self.addrMask
if len(register) == 1:
intval &= self.byteMask
setattr(self._mpu, register, intval)
except KeyError as exc:
intval = self._address_parser.number(value)
except KeyError as exc: # label not found
self._output(exc.args[0])
continue
except OverflowError as exc: # wider than address space
overflow = True
msg = "Overflow: %r too wide for register %r"
self._output(msg % (value, register))
continue
if register != 'pc':
if intval != (intval & self.byteMask):
msg = "Overflow: %r too wide for register %r"
self._output(msg % (value, register))
continue
setattr(self._mpu, register, intval)
def help_cd(self):
self._output("cd <directory>")

View File

@ -854,6 +854,22 @@ class MonitorTests(unittest.TestCase):
self.assertEqual(0x46, mon._mpu.sp)
self.assertEqual(0x4600, mon._mpu.pc)
def test_registers_pc_overflow(self):
stdout = StringIO()
mon = Monitor(stdout=stdout)
mon.do_registers('pc=10000')
out = stdout.getvalue()
expected = "Overflow: '10000' too wide for register 'pc'"
self.assertTrue(out.startswith(expected))
def test_registers_a_overflow(self):
stdout = StringIO()
mon = Monitor(stdout=stdout)
mon.do_registers('a=100')
out = stdout.getvalue()
expected = "Overflow: '100' too wide for register 'a'"
self.assertTrue(out.startswith(expected))
def test_help_registers(self):
stdout = StringIO()
mon = Monitor(stdout=stdout)