Skip to content

Commit dff6abd

Browse files
committed
memory: use path= instead of name= in ResourceInfo().
See amaranth-lang/amaranth#909.
1 parent 2d3d176 commit dff6abd

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

amaranth_soc/memory.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ def items(self):
5050
class ResourceInfo:
5151
"""Resource metadata.
5252
53-
A wrapper class for resource objects, with their assigned name and address range.
53+
A wrapper class for resource objects, with their assigned path and address range.
5454
5555
Parameters
5656
----------
5757
resource : object
5858
Arbitrary object representing a resource. See :meth:`MemoryMap.add_resource` for details.
59-
name : iter(str)
60-
Name assigned to the resource. It is prefixed by the name of each window sitting between
59+
path : iter(str)
60+
Path of the resource. It is composed of the names of each window sitting between
6161
the resource and the memory map from which this :class:`ResourceInfo` was obtained.
6262
See :meth:`MemoryMap.add_window` for details.
6363
start : int
@@ -69,12 +69,10 @@ class ResourceInfo:
6969
memory map from which this :class:`ResourceInfo` was obtained, or less if the resource
7070
is located behind a window that uses sparse addressing.
7171
"""
72-
def __init__(self, resource, name, start, end, width):
73-
if isinstance(name, str):
74-
name = (name,)
75-
if not name or not all(isinstance(part, str) and part for part in name):
76-
raise TypeError("Name must be a non-empty sequence of non-empty strings, not {!r}"
77-
.format(name))
72+
def __init__(self, resource, path, start, end, width):
73+
if not path or not all(isinstance(part, str) and part for part in path):
74+
raise TypeError("Path must be a non-empty sequence of non-empty strings, not {!r}"
75+
.format(path))
7876
if not isinstance(start, int) or start < 0:
7977
raise TypeError("Start address must be a non-negative integer, not {!r}"
8078
.format(start))
@@ -86,7 +84,7 @@ def __init__(self, resource, name, start, end, width):
8684
.format(width))
8785

8886
self._resource = resource
89-
self._name = tuple(name)
87+
self._path = tuple(path)
9088
self._start = start
9189
self._end = end
9290
self._width = width
@@ -96,8 +94,8 @@ def resource(self):
9694
return self._resource
9795

9896
@property
99-
def name(self):
100-
return self._name
97+
def path(self):
98+
return self._path
10199

102100
@property
103101
def start(self):
@@ -497,11 +495,11 @@ def _translate(resource_info, window, window_range):
497495
# layouts that cannot be easily represented, so reject those.
498496
assert window_range.step == 1 or resource_info.width == window.data_width
499497

500-
name = resource_info.name if window.name is None else (window.name, *resource_info.name)
498+
path = resource_info.path if window.name is None else (window.name, *resource_info.path)
501499
size = (resource_info.end - resource_info.start) // window_range.step
502500
start = resource_info.start + window_range.start
503501
width = resource_info.width * window_range.step
504-
return ResourceInfo(resource_info.resource, name, start, start + size, width)
502+
return ResourceInfo(resource_info.resource, path, start, start + size, width)
505503

506504
def all_resources(self):
507505
"""Iterate all resources and their address ranges.
@@ -516,7 +514,8 @@ def all_resources(self):
516514
for addr_range, assignment in self._ranges.items():
517515
if id(assignment) in self._resources:
518516
_, resource_name, _ = self._resources[id(assignment)]
519-
yield ResourceInfo(assignment, resource_name, addr_range.start, addr_range.stop,
517+
resource_path = (resource_name,)
518+
yield ResourceInfo(assignment, resource_path, addr_range.start, addr_range.stop,
520519
self.data_width)
521520
elif id(assignment) in self._windows:
522521
for resource_info in assignment.all_resources():
@@ -545,7 +544,8 @@ def find_resource(self, resource):
545544
"""
546545
if id(resource) in self._resources:
547546
_, resource_name, resource_range = self._resources[id(resource)]
548-
return ResourceInfo(resource, resource_name, resource_range.start, resource_range.stop,
547+
resource_path = (resource_name,)
548+
return ResourceInfo(resource, resource_path, resource_range.start, resource_range.stop,
549549
self.data_width)
550550

551551
for window, window_range in self._windows.values():

tests/test_memory.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,50 +42,46 @@ def test_get(self):
4242

4343
class ResourceInfoTestCase(unittest.TestCase):
4444
def test_simple(self):
45-
info = ResourceInfo("a", name=("foo", "bar"), start=0, end=1, width=8)
46-
self.assertEqual(info.name, ("foo", "bar"))
45+
info = ResourceInfo("a", path=("foo", "bar"), start=0, end=1, width=8)
46+
self.assertEqual(info.path, ("foo", "bar"))
4747
self.assertEqual(info.start, 0)
4848
self.assertEqual(info.end, 1)
4949
self.assertEqual(info.width, 8)
5050

51-
def test_name_cast(self):
52-
info = ResourceInfo("a", name="foo", start=0, end=1, width=8)
53-
self.assertEqual(info.name, ("foo",))
54-
55-
def test_wrong_name(self):
51+
def test_wrong_path(self):
5652
with self.assertRaisesRegex(TypeError,
57-
r"Name must be a non-empty sequence of non-empty strings, not \(1,\)"):
58-
ResourceInfo("a", name=(1,), start=0, end=1, width=8)
53+
r"Path must be a non-empty sequence of non-empty strings, not \(1,\)"):
54+
ResourceInfo("a", path=(1,), start=0, end=1, width=8)
5955
with self.assertRaisesRegex(TypeError,
60-
r"Name must be a non-empty sequence of non-empty strings, not \(\)"):
61-
ResourceInfo("a", name=(), start=0, end=1, width=8)
56+
r"Path must be a non-empty sequence of non-empty strings, not \(\)"):
57+
ResourceInfo("a", path=(), start=0, end=1, width=8)
6258
with self.assertRaisesRegex(TypeError,
63-
r"Name must be a non-empty sequence of non-empty strings, not \('foo', ''\)"):
64-
ResourceInfo("a", name=("foo", ""), start=0, end=1, width=8)
59+
r"Path must be a non-empty sequence of non-empty strings, not \('foo', ''\)"):
60+
ResourceInfo("a", path=("foo", ""), start=0, end=1, width=8)
6561

6662
def test_wrong_start_addr(self):
6763
with self.assertRaisesRegex(TypeError,
6864
r"Start address must be a non-negative integer, not 'foo'"):
69-
ResourceInfo("a", name="b", start="foo", end=1, width=8)
65+
ResourceInfo("a", path=("b",), start="foo", end=1, width=8)
7066
with self.assertRaisesRegex(TypeError,
7167
r"Start address must be a non-negative integer, not -1"):
72-
ResourceInfo("a", name="b", start=-1, end=1, width=8)
68+
ResourceInfo("a", path=("b",), start=-1, end=1, width=8)
7369

7470
def test_wrong_end_addr(self):
7571
with self.assertRaisesRegex(TypeError,
7672
r"End address must be an integer greater than the start address, not 'foo'"):
77-
ResourceInfo("a", name="b", start=0, end="foo", width=8)
73+
ResourceInfo("a", path=("b",), start=0, end="foo", width=8)
7874
with self.assertRaisesRegex(TypeError,
7975
r"End address must be an integer greater than the start address, not 0"):
80-
ResourceInfo("a", name="b", start=0, end=0, width=8)
76+
ResourceInfo("a", path=("b",), start=0, end=0, width=8)
8177

8278
def test_wrong_width(self):
8379
with self.assertRaisesRegex(TypeError,
8480
r"Width must be a non-negative integer, not 'foo'"):
85-
ResourceInfo("a", name="b", start=0, end=1, width="foo")
81+
ResourceInfo("a", path=("b",), start=0, end=1, width="foo")
8682
with self.assertRaisesRegex(TypeError,
8783
r"Width must be a non-negative integer, not -1"):
88-
ResourceInfo("a", name="b", start=0, end=1, width=-1)
84+
ResourceInfo("a", path=("b",), start=0, end=1, width=-1)
8985

9086

9187
class MemoryMapTestCase(unittest.TestCase):
@@ -391,37 +387,37 @@ def test_iter_all_resources(self):
391387
res_info = list(self.root.all_resources())
392388

393389
self.assertIs(res_info[0].resource, self.res1)
394-
self.assertEqual(res_info[0].name, ("name1",))
390+
self.assertEqual(res_info[0].path, ("name1",))
395391
self.assertEqual(res_info[0].start, 0x00000000)
396392
self.assertEqual(res_info[0].end, 0x00000010)
397393
self.assertEqual(res_info[0].width, 32)
398394

399395
self.assertIs(res_info[1].resource, self.res2)
400-
self.assertEqual(res_info[1].name, ("name2",))
396+
self.assertEqual(res_info[1].path, ("name2",))
401397
self.assertEqual(res_info[1].start, 0x00010000)
402398
self.assertEqual(res_info[1].end, 0x00010020)
403399
self.assertEqual(res_info[1].width, 32)
404400

405401
self.assertIs(res_info[2].resource, self.res3)
406-
self.assertEqual(res_info[2].name, ("name3",))
402+
self.assertEqual(res_info[2].path, ("name3",))
407403
self.assertEqual(res_info[2].start, 0x00010020)
408404
self.assertEqual(res_info[2].end, 0x00010040)
409405
self.assertEqual(res_info[2].width, 32)
410406

411407
self.assertIs(res_info[3].resource, self.res4)
412-
self.assertEqual(res_info[3].name, ("name4",))
408+
self.assertEqual(res_info[3].path, ("name4",))
413409
self.assertEqual(res_info[3].start, 0x00020000)
414410
self.assertEqual(res_info[3].end, 0x00020001)
415411
self.assertEqual(res_info[3].width, 32)
416412

417413
self.assertIs(res_info[4].resource, self.res5)
418-
self.assertEqual(res_info[4].name, ("name5",))
414+
self.assertEqual(res_info[4].path, ("name5",))
419415
self.assertEqual(res_info[4].start, 0x00030000)
420416
self.assertEqual(res_info[4].end, 0x00030010)
421417
self.assertEqual(res_info[4].width, 8)
422418

423419
self.assertIs(res_info[5].resource, self.res6)
424-
self.assertEqual(res_info[5].name, ("win3", "name6"))
420+
self.assertEqual(res_info[5].path, ("win3", "name6"))
425421
self.assertEqual(res_info[5].start, 0x00040000)
426422
self.assertEqual(res_info[5].end, 0x00040004)
427423
self.assertEqual(res_info[5].width, 32)
@@ -430,7 +426,7 @@ def test_find_resource(self):
430426
for res_info in self.root.all_resources():
431427
other = self.root.find_resource(res_info.resource)
432428
self.assertIs(other.resource, res_info.resource)
433-
self.assertEqual(other.name, res_info.name)
429+
self.assertEqual(other.path, res_info.path)
434430
self.assertEqual(other.start, res_info.start)
435431
self.assertEqual(other.end, res_info.end)
436432
self.assertEqual(other.width, res_info.width)

0 commit comments

Comments
 (0)