interconnect/stream: add multiplexer and demultiplexer
authorSebastien Bourdeauducq <sb@m-labs.hk>
Wed, 30 Sep 2015 11:43:14 +0000 (19:43 +0800)
committerSebastien Bourdeauducq <sb@m-labs.hk>
Wed, 30 Sep 2015 11:43:51 +0000 (19:43 +0800)
misoc/interconnect/stream.py

index 3e6cf619edd993bbf6972cdf928502e2871feaa3..bdbe53a4c703b364707cba1a3f07fcbaf8ab6354 100644 (file)
@@ -113,3 +113,32 @@ class SyncFIFO(_FIFOWrapper):
 class AsyncFIFO(_FIFOWrapper):
     def __init__(self, layout, depth):
         _FIFOWrapper.__init__(self, fifo.AsyncFIFO, layout, depth)
+
+
+class Multiplexer(Module):
+    def __init__(self, layout, n):
+        self.source = Source(layout)
+        sinks = []
+        for i in range(n):
+            sink = Sink(layout)
+            setattr(self, "sink"+str(i), sink)
+            sinks.append(sink)
+        self.sel = Signal(max=n)
+
+        # # #
+
+        cases = {}
+        for i, sink in enumerate(sinks):
+            cases[i] = Record.connect(sink, self.source)
+        self.comb += Case(self.sel, cases)
+
+
+class Demultiplexer(Module):
+    def __init__(self, layout, n):
+        self.sink = Sink(layout)
+        sources = []
+        for i in range(n):
+            source = Source(layout)
+            setattr(self, "source"+str(i), source)
+            sources.append(source)
+        self.sel = Signal(max=n)