Skip to content

Commit b11abe2

Browse files
committed
wishbone: restrict addr_width to positive integers.
1 parent e6b4002 commit b11abe2

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

amaranth_soc/wishbone/bus.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def check_parameters(cls, *, addr_width, data_width, granularity, features):
155155
Raises
156156
------
157157
:exc:`TypeError`
158-
If ``addr_width`` is not an integer greater than or equal to 0.
158+
If ``addr_width`` is not an integer greater than to 0.
159159
:exc:`ValueError`
160160
If ``data_width`` is not 8, 16, 32 or 64.
161161
:exc:`ValueError`
@@ -165,8 +165,8 @@ def check_parameters(cls, *, addr_width, data_width, granularity, features):
165165
:exc:`ValueError`
166166
If ``features`` contains other values than :class:`Feature` members.
167167
"""
168-
if not isinstance(addr_width, int) or addr_width < 0:
169-
raise TypeError(f"Address width must be a non-negative integer, not {addr_width!r}")
168+
if not isinstance(addr_width, int) or addr_width <= 0:
169+
raise TypeError(f"Address width must be a positive integer, not {addr_width!r}")
170170
if data_width not in (8, 16, 32, 64):
171171
raise ValueError(f"Data width must be one of 8, 16, 32, 64, not {data_width!r}")
172172
if granularity not in (8, 16, 32, 64):

tests/test_wishbone_bus.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,45 +105,45 @@ def test_eq(self):
105105

106106
def test_wrong_addr_width(self):
107107
with self.assertRaisesRegex(TypeError,
108-
r"Address width must be a non-negative integer, not -1"):
109-
wishbone.Signature(addr_width=-1, data_width=8)
108+
r"Address width must be a positive integer, not 0"):
109+
wishbone.Signature(addr_width=0, data_width=8)
110110
with self.assertRaisesRegex(TypeError,
111-
r"Address width must be a non-negative integer, not -1"):
112-
wishbone.Signature.check_parameters(addr_width=-1, data_width=8, granularity=8,
111+
r"Address width must be a positive integer, not 0"):
112+
wishbone.Signature.check_parameters(addr_width=0, data_width=8, granularity=8,
113113
features=())
114114

115115
def test_wrong_data_width(self):
116116
with self.assertRaisesRegex(ValueError,
117117
r"Data width must be one of 8, 16, 32, 64, not 7"):
118-
wishbone.Signature(addr_width=0, data_width=7)
118+
wishbone.Signature(addr_width=1, data_width=7)
119119
with self.assertRaisesRegex(ValueError,
120120
r"Data width must be one of 8, 16, 32, 64, not 7"):
121-
wishbone.Signature.check_parameters(addr_width=0, data_width=7, granularity=7,
121+
wishbone.Signature.check_parameters(addr_width=1, data_width=7, granularity=7,
122122
features=())
123123

124124
def test_wrong_granularity(self):
125125
with self.assertRaisesRegex(ValueError,
126126
r"Granularity must be one of 8, 16, 32, 64, not 7"):
127-
wishbone.Signature(addr_width=0, data_width=32, granularity=7)
127+
wishbone.Signature(addr_width=1, data_width=32, granularity=7)
128128
with self.assertRaisesRegex(ValueError,
129129
r"Granularity must be one of 8, 16, 32, 64, not 7"):
130-
wishbone.Signature.check_parameters(addr_width=0, data_width=32, granularity=7,
130+
wishbone.Signature.check_parameters(addr_width=1, data_width=32, granularity=7,
131131
features=())
132132

133133
def test_wrong_granularity_wide(self):
134134
with self.assertRaisesRegex(ValueError,
135135
r"Granularity 32 may not be greater than data width 8"):
136-
wishbone.Signature(addr_width=0, data_width=8, granularity=32)
136+
wishbone.Signature(addr_width=1, data_width=8, granularity=32)
137137
with self.assertRaisesRegex(ValueError,
138138
r"Granularity 32 may not be greater than data width 8"):
139-
wishbone.Signature.check_parameters(addr_width=0, data_width=8, granularity=32,
139+
wishbone.Signature.check_parameters(addr_width=1, data_width=8, granularity=32,
140140
features=())
141141

142142
def test_wrong_features(self):
143143
with self.assertRaisesRegex(ValueError, r"'foo' is not a valid Feature"):
144-
wishbone.Signature(addr_width=0, data_width=8, features={"foo"})
144+
wishbone.Signature(addr_width=1, data_width=8, features={"foo"})
145145
with self.assertRaisesRegex(ValueError, r"'foo' is not a valid Feature"):
146-
wishbone.Signature.check_parameters(addr_width=0, data_width=8, granularity=8,
146+
wishbone.Signature.check_parameters(addr_width=1, data_width=8, granularity=8,
147147
features={"foo"})
148148

149149

@@ -158,7 +158,7 @@ def test_simple(self):
158158
self.assertEqual(iface.cyc.name, "foo__bar__cyc")
159159

160160
def test_get_map_wrong(self):
161-
iface = wishbone.Interface(addr_width=0, data_width=8, path=("iface",))
161+
iface = wishbone.Interface(addr_width=1, data_width=8, path=("iface",))
162162
with self.assertRaisesRegex(NotImplementedError,
163163
r"wishbone.Interface\(.*\) does not have a memory map"):
164164
iface.memory_map
@@ -171,7 +171,7 @@ def test_get_map_frozen(self):
171171
iface.memory_map.add_resource("foo", name="foo", size=1)
172172

173173
def test_set_map_wrong(self):
174-
iface = wishbone.Interface(addr_width=0, data_width=8, path=("iface",))
174+
iface = wishbone.Interface(addr_width=1, data_width=8, path=("iface",))
175175
with self.assertRaisesRegex(TypeError,
176176
r"Memory map must be an instance of MemoryMap, not 'foo'"):
177177
iface.memory_map = "foo"

0 commit comments

Comments
 (0)