mirror of
https://github.com/mnaberez/py65.git
synced 2025-01-19 23:31:03 +00:00
Added maxwidth to specify maximum address width.
This commit is contained in:
parent
614a255ee5
commit
4245b24a40
@ -3,6 +3,22 @@ import sys
|
|||||||
from py65.utils.addressing import AddressParser
|
from py65.utils.addressing import AddressParser
|
||||||
|
|
||||||
class AddressParserTests(unittest.TestCase):
|
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):
|
def test_number_hex_literal(self):
|
||||||
parser = AddressParser()
|
parser = AddressParser()
|
||||||
self.assertEqual(49152, parser.number('$c000'))
|
self.assertEqual(49152, parser.number('$c000'))
|
||||||
@ -81,6 +97,17 @@ class AddressParserTests(unittest.TestCase):
|
|||||||
except KeyError, why:
|
except KeyError, why:
|
||||||
self.assertEqual('Label not found: bad_label', why[0])
|
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):
|
def test_label_for_returns_label(self):
|
||||||
parser = AddressParser(labels={'chrout':0xFFD2})
|
parser = AddressParser(labels={'chrout':0xFFD2})
|
||||||
self.assertEqual('chrout', parser.label_for(0xFFD2))
|
self.assertEqual('chrout', parser.label_for(0xFFD2))
|
||||||
|
@ -1,17 +1,28 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
class AddressParser:
|
class AddressParser(object):
|
||||||
"""Parse user input into addresses or ranges of addresses.
|
"""Parse user input into addresses or ranges of addresses.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, radix=16, labels={}):
|
def __init__(self, maxwidth=16, radix=16, labels={}):
|
||||||
"""Radix is the default radix to use when one is not specified
|
"""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
|
as a prefix of any input. Labels are a dictionary of label
|
||||||
names that can be substituted for addresses.
|
names that can be substituted for addresses.
|
||||||
"""
|
"""
|
||||||
self.radix = radix
|
self.radix = radix
|
||||||
self.labels = labels
|
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):
|
def label_for(self, address, default=None):
|
||||||
"""Given an address, return the corresponding label or a default.
|
"""Given an address, return the corresponding label or a default.
|
||||||
"""
|
"""
|
||||||
@ -53,8 +64,8 @@ class AddressParser:
|
|||||||
|
|
||||||
if address < 0:
|
if address < 0:
|
||||||
address = 0
|
address = 0
|
||||||
if address > 0xFFFF:
|
if address > self._maxaddr:
|
||||||
address = 0xFFFF
|
address = self._maxaddr
|
||||||
return address
|
return address
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user