Repository: amaranth-lang/amaranth
Pull request #1138 (2024-02-13 23:33:20)

sim: fix using 0-width `Switch`. [0.4 backport]

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1138?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`a669aa3`)](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/a669aa38525d32e34344c7fa6f108a3767ae6f5c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) 83.85% compared to head [(`f75a863`)](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1138?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) 83.82%.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           v0.4.x    #1138      +/-   ##
==========================================
- Coverage   83.85%   83.82%   -0.03%     
==========================================
  Files          55       55              
  Lines        8106     8106              
  Branches     1979     1979              
==========================================
- Hits         6797     6795       -2     
- Misses       1095     1098       +3     
+ Partials      214      213       -1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1138?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).


Patch:
diff --git a/amaranth/sim/_pyrtl.py b/amaranth/sim/_pyrtl.py
index a740060..17c7919 100644
--- a/amaranth/sim/_pyrtl.py
+++ b/amaranth/sim/_pyrtl.py
@@ -383,7 +383,7 @@ class _StatementCompiler(StatementVisitor, _Compiler):
                         value = int("".join("0" if b == "-" else  b  for b in pattern), 2)
                         gen_checks.append(f"{value} == ({mask} & {gen_test})")
                     else:
-                        value = int(pattern, 2)
+                        value = int(pattern or "0", 2)
                         gen_checks.append(f"{value} == {gen_test}")
             if index == 0:
                 self.emitter.append(f"if {' or '.join(gen_checks)}:")


Test patch:
diff --git a/tests/test_sim.py b/tests/test_sim.py
index 75ab66c..56cd666 100644
--- a/tests/test_sim.py
+++ b/tests/test_sim.py
@@ -1014,6 +1014,19 @@ class SimulatorIntegrationTestCase(FHDLTestCase):
         m.d.comb += a.eq(op)
         Simulator(m)
 
+    def test_switch_zero(self):
+        m = Module()
+        a = Signal(0)
+        o = Signal()
+        with m.Switch(a):
+            with m.Case(""):
+                m.d.comb += o.eq(1)
+        with self.assertSimulation(m) as sim:
+            def process():
+                yield Settle()
+                self.assertEqual((yield o), 1)
+            sim.add_process(process)
+
 
 class SimulatorRegressionTestCase(FHDLTestCase):
     def test_bug_325(self):