back.rtlil: add decodings to cases when switching on a signal.
authorwhitequark <whitequark@whitequark.org>
Tue, 9 Jul 2019 19:45:15 +0000 (19:45 +0000)
committerwhitequark <whitequark@whitequark.org>
Tue, 9 Jul 2019 19:48:15 +0000 (19:48 +0000)
Fixes #134.

nmigen/back/rtlil.py

index 1a83675bb95a3f94321be8f52a56f7992f79ce63..4f6592ef5ff08eaa9196f3ad002ad3d4e935c094 100644 (file)
@@ -603,10 +603,10 @@ class _StatementCompiler(xfrm.StatementVisitor):
         self._has_rhs     = False
 
     @contextmanager
-    def case(self, switch, values, src=""):
+    def case(self, switch, values, attrs={}, src=""):
         try:
             old_case = self._case
-            with switch.case(*values, src=src) as self._case:
+            with switch.case(*values, attrs=attrs, src=src) as self._case:
                 yield
         finally:
             self._case = old_case
@@ -659,11 +659,18 @@ class _StatementCompiler(xfrm.StatementVisitor):
 
         with self._case.switch(test_sigspec, src=src(stmt.src_loc)) as switch:
             for values, stmts in stmt.cases.items():
+                case_attrs = {}
                 if values in stmt.case_src_locs:
-                    case_src = src(stmt.case_src_locs[values])
-                else:
-                    case_src = ""
-                with self.case(switch, values, src=case_src):
+                    case_attrs["src"] = src(stmt.case_src_locs[values])
+                if isinstance(stmt.test, ast.Signal) and stmt.test.decoder:
+                    decoded_values = []
+                    for value in values:
+                        if "-" in value:
+                            decoded_values.append("<multiple>")
+                        else:
+                            decoded_values.append(stmt.test.decoder(int(value, 2)))
+                    case_attrs["nmigen.decoding"] = "|".join(decoded_values)
+                with self.case(switch, values, attrs=case_attrs):
                     self.on_statements(stmts)
 
     def on_statement(self, stmt):