Skip to content

Commit

Permalink
hdl._dsl: fix using 0-width Switch with integer keys.
Browse files Browse the repository at this point in the history
Fixes #1133.
  • Loading branch information
wanda-phi authored and whitequark committed Feb 14, 2024
1 parent fd4ba36 commit 8450104
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions amaranth/hdl/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ def Case(self, *patterns):
"expression, not {!r}"
.format(pattern)) from e
pattern_len = bits_for(pattern.value)
if pattern.value == 0:
pattern_len = 0
if pattern_len > len(switch_data["test"]):
warnings.warn("Case pattern '{!r}' ({}'{:b}) is wider than switch value "
"(which has width {}); comparison will never be true"
Expand Down
15 changes: 15 additions & 0 deletions tests/test_hdl_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,21 @@ class Color(Enum):
)
""")

def test_Switch_zero_width(self):
m = Module()
s = Signal(0)
with m.Switch(s):
with m.Case(0):
m.d.comb += self.c1.eq(1)
m._flush()
self.assertRepr(m._statements, """
(
(switch (sig s)
(case (eq (sig c1) (const 1'd1)))
)
)
""")

def test_Case_bits_wrong(self):
m = Module()
with m.Switch(self.w1):
Expand Down

0 comments on commit 8450104

Please sign in to comment.