add redirection of __Mux__ to allow overrides for more advanced behaviour
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 27 Sep 2021 17:33:44 +0000 (18:33 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 2 Oct 2021 14:58:16 +0000 (15:58 +0100)
without changing fundamental language characteristics or semantics in nmigen
https://bugs.libre-soc.org/show_bug.cgi?id=458

nmigen/hdl/ast.py

index 5ed3a77dd3cdbb9934dfb57054908c2b11b6a8a0..5970d91b3fdc97123e2e0156d1476a09bb5a8e1c 100644 (file)
@@ -150,6 +150,9 @@ class Value(metaclass=ABCMeta):
         super().__init__()
         self.src_loc = tracer.get_src_loc(1 + src_loc_at)
 
+    def __Mux__(self, val1, val0):
+        return _InternalMux(self, val1, val0)
+
     def __bool__(self):
         raise TypeError("Attempted to convert nMigen value to Python boolean")
 
@@ -720,6 +723,13 @@ class Operator(Value):
 
 
 def Mux(sel, val1, val0):
+    sel = Value.cast(sel)
+    if len(sel) != 1:
+        sel = sel.bool()
+    return sel.__Mux__(val1, val0)
+
+
+def _InternalMux(sel, val1, val0):
     """Choose between two values.
 
     Parameters