diff --git a/src/py65/tests/utils/test_addressing.py b/src/py65/tests/utils/test_addressing.py index bc1d624..2741c23 100644 --- a/src/py65/tests/utils/test_addressing.py +++ b/src/py65/tests/utils/test_addressing.py @@ -3,6 +3,22 @@ import sys from py65.utils.addressing import AddressParser class AddressParserTests(unittest.TestCase): + def test_maxwidth_can_be_set_in_constructor(self): + parser = AddressParser(maxwidth=24) + self.assertEqual(24, parser.maxwidth) + self.assertEqual(0xFFFFFF, parser._maxaddr) + + def test_maxwidth_defaults_to_16_bits(self): + parser = AddressParser() + self.assertEqual(16, parser.maxwidth) + self.assertEqual(0xFFFF, parser._maxaddr) + + def test_maxwidth_setter(self): + parser = AddressParser() + parser.maxwidth = 24 + self.assertEqual(24, parser.maxwidth) + self.assertEqual(0xFFFFFF, parser._maxaddr) + def test_number_hex_literal(self): parser = AddressParser() self.assertEqual(49152, parser.number('$c000')) @@ -81,6 +97,17 @@ class AddressParserTests(unittest.TestCase): except KeyError, why: self.assertEqual('Label not found: bad_label', why[0]) + def test_number_truncates_address_at_maxwidth_16(self): + parser = AddressParser() + parser.labels = {'foo': 0xFFFF} + self.assertEqual(0xFFFF, parser.number('foo+5')) + + def test_number_truncates_address_at_maxwidth_24(self): + parser = AddressParser() + parser.maxwidth = 24 + parser.labels = {'foo': 0xFFFFFF} + self.assertEqual(0xFFFFFF, parser.number('foo+5')) + def test_label_for_returns_label(self): parser = AddressParser(labels={'chrout':0xFFD2}) self.assertEqual('chrout', parser.label_for(0xFFD2)) diff --git a/src/py65/utils/addressing.py b/src/py65/utils/addressing.py index d331797..bc577d7 100644 --- a/src/py65/utils/addressing.py +++ b/src/py65/utils/addressing.py @@ -1,17 +1,28 @@ import re -class AddressParser: +class AddressParser(object): """Parse user input into addresses or ranges of addresses. """ - def __init__(self, radix=16, labels={}): - """Radix is the default radix to use when one is not specified + def __init__(self, maxwidth=16, radix=16, labels={}): + """Maxwidth is the maximum width of an address in bits. + Radix is the default radix to use when one is not specified as a prefix of any input. Labels are a dictionary of label names that can be substituted for addresses. """ self.radix = radix self.labels = labels + self.maxwidth = maxwidth + + def _get_maxwidth(self): + return self._maxwidth + def _set_maxwidth(self, width): + self._maxwidth = width + self._maxaddr = pow(2, width) - 1 + + maxwidth = property(_get_maxwidth, _set_maxwidth) + def label_for(self, address, default=None): """Given an address, return the corresponding label or a default. """ @@ -53,8 +64,8 @@ class AddressParser: if address < 0: address = 0 - if address > 0xFFFF: - address = 0xFFFF + if address > self._maxaddr: + address = self._maxaddr return address else: