looking for replacements of the hard-coded control blocks
[ieee754fpu.git] / src / add / multipipe.py
index 337e2dca44b90879c76821d25f79cffbbabb9bff..35da5c2ec741aafc66e97804e842be0395b8a863 100644 (file)
@@ -1,4 +1,13 @@
-""" Combinatorial Multi-input multiplexer block conforming to Pipeline API
+""" Combinatorial Multi-input and Multi-output multiplexer blocks
+    conforming to Pipeline API
+
+    Multi-input is complex because if any one input is ready, the output
+    can be ready, and the decision comes from a separate module.
+
+    Multi-output is simple (pretty much identical to UnbufferedPipeline),
+    and the selection is just a mux.  The only proviso (difference) being:
+    the outputs not being selected have to have their o_ready signals
+    DEASSERTED.
 """
 
 from math import log
@@ -12,23 +21,20 @@ from collections.abc import Sequence
 from example_buf_pipe import eq, NextControl, PrevControl, ExampleStage
 
 
-class PipelineBase:
+class MultiInControlBase:
     """ Common functions for Pipeline API
     """
-    def __init__(self, stage, in_multi=None, p_len=1):
-        """ pass in a "stage" which may be either a static class or a class
-            instance, which has four functions (one optional):
-            * ispec: returns input signals according to the input specification
-            * ispec: returns output signals to the output specification
-            * process: takes an input instance and returns processed data
-            * setup: performs any module linkage if the stage uses one.
+    def __init__(self, in_multi=None, p_len=1):
+        """ Multi-input Control class.  Conforms to same API as ControlBase...
+            mostly.  has additional indices to the *multiple* input stages
+
+            * p: contains ready/valid to the previous stages PLURAL
+            * n: contains ready/valid to the next stage
 
             User must also:
-            * add i_data member to PrevControl and
-            * add o_data member to NextControl
+            * add i_data members to PrevControl and
+            * add o_data member  to NextControl
         """
-        self.stage = stage
-
         # set up input and output IO ACK (prev/next ready/valid)
         p = []
         for i in range(p_len):
@@ -41,21 +47,21 @@ class PipelineBase:
         """
         return self.n.connect_to_next(nxt.p[p_idx])
 
-    def connect_in(self, prev, idx=0, prev_idx=None):
+    def _connect_in(self, prev, idx=0, prev_idx=None):
         """ helper function to connect stage to an input source.  do not
             use to connect stage-to-stage!
         """
         if prev_idx is None:
-            return self.p[idx].connect_in(prev.p)
-        return self.p[idx].connect_in(prev.p[prev_idx])
+            return self.p[idx]._connect_in(prev.p)
+        return self.p[idx]._connect_in(prev.p[prev_idx])
 
-    def connect_out(self, nxt):
+    def _connect_out(self, nxt):
         """ helper function to connect stage to an output source.  do not
             use to connect stage-to-stage!
         """
         if nxt_idx is None:
-            return self.n.connect_out(nxt.n)
-        return self.n.connect_out(nxt.n)
+            return self.n._connect_out(nxt.n)
+        return self.n._connect_out(nxt.n)
 
     def set_input(self, i, idx=0):
         """ helper function to set the input data
@@ -65,15 +71,146 @@ class PipelineBase:
     def ports(self):
         res = []
         for i in range(len(self.p)):
-            res += [self.p[i].i_valid, self.p[i].o_ready,
-                    self.p[i].i_data]# XXX need flattening!]
-        res += [self.n.i_ready, self.n.o_valid,
-                self.n.o_data]   # XXX need flattening!]
+            p = self.p[i]
+            res += [p.i_valid, p.o_ready]
+            if hasattr(p.i_data, "ports"):
+                res += p.i_data.ports()
+            else:
+                rres = p.i_data
+                if not isinstance(rres, Sequence):
+                    rres = [rres]
+                res += rres
+        n = self.n
+        res += [n.i_ready, n.o_valid]
+        if hasattr(n.o_data, "ports"):
+            res += n.o_data.ports()
+        else:
+            rres = n.o_data
+            if not isinstance(rres, Sequence):
+                rres = [rres]
+            res += rres
         return res
 
 
+class MultiOutControlBase:
+    """ Common functions for Pipeline API
+    """
+    def __init__(self, n_len=1, in_multi=None):
+        """ Multi-output Control class.  Conforms to same API as ControlBase...
+            mostly.  has additional indices to the multiple *output* stages
+            [MultiInControlBase has multiple *input* stages]
+
+            * p: contains ready/valid to the previou stage
+            * n: contains ready/valid to the next stages PLURAL
+
+            User must also:
+            * add i_data member to PrevControl and
+            * add o_data members to NextControl
+        """
+
+        # set up input and output IO ACK (prev/next ready/valid)
+        self.p = PrevControl(in_multi)
+        n = []
+        for i in range(n_len):
+            n.append(NextControl())
+        self.n = Array(n)
 
-class CombMultiInPipeline(PipelineBase):
+    def connect_to_next(self, nxt, n_idx=0):
+        """ helper function to connect to the next stage data/valid/ready.
+        """
+        return self.n[n_idx].connect_to_next(nxt.p)
+
+    def _connect_in(self, prev, idx=0):
+        """ helper function to connect stage to an input source.  do not
+            use to connect stage-to-stage!
+        """
+        return self.n[idx]._connect_in(prev.p)
+
+    def _connect_out(self, nxt, idx=0, nxt_idx=None):
+        """ helper function to connect stage to an output source.  do not
+            use to connect stage-to-stage!
+        """
+        if nxt_idx is None:
+            return self.n[idx]._connect_out(nxt.n)
+        return self.n[idx]._connect_out(nxt.n[nxt_idx])
+
+    def set_input(self, i):
+        """ helper function to set the input data
+        """
+        return eq(self.p.i_data, i)
+
+    def ports(self):
+        res = [self.p.i_valid, self.p.o_ready]
+        if hasattr(self.p.i_data, "ports"):
+            res += self.p.i_data.ports()
+        else:
+            res += self.p.i_data
+
+        for i in range(len(self.n)):
+            n = self.n[i]
+            res += [n.i_ready, n.o_valid]
+            if hasattr(n.o_data, "ports"):
+                res += n.o_data.ports()
+            else:
+                res += n.o_data
+        return res
+
+
+class CombMultiOutPipeline(MultiOutControlBase):
+    """ A multi-input Combinatorial block conforming to the Pipeline API
+
+        Attributes:
+        -----------
+        p.i_data : stage input data (non-array).  shaped according to ispec
+        n.o_data : stage output data array.       shaped according to ospec
+    """
+
+    def __init__(self, stage, n_len, n_mux):
+        MultiOutControlBase.__init__(self, n_len=n_len)
+        self.stage = stage
+        self.n_mux = n_mux
+
+        # set up the input and output data
+        self.p.i_data = stage.ispec() # input type
+        for i in range(n_len):
+            self.n[i].o_data = stage.ospec() # output type
+
+    def elaborate(self, platform):
+        m = Module()
+
+        if hasattr(self.n_mux, "elaborate"): # TODO: identify submodule?
+            m.submodules += self.n_mux
+
+        # need buffer register conforming to *input* spec
+        r_data = self.stage.ispec() # input type
+        if hasattr(self.stage, "setup"):
+            self.stage.setup(m, r_data)
+
+        # multiplexer id taken from n_mux
+        mid = self.n_mux.m_id
+
+        # temporaries
+        p_i_valid = Signal(reset_less=True)
+        pv = Signal(reset_less=True)
+        m.d.comb += p_i_valid.eq(self.p.i_valid_test)
+        m.d.comb += pv.eq(self.p.i_valid & self.p.o_ready)
+
+        # all outputs to next stages first initialised to zero (invalid)
+        # the only output "active" is then selected by the muxid
+        for i in range(len(self.n)):
+            m.d.comb += self.n[i].o_valid.eq(0)
+        data_valid = self.n[mid].o_valid
+        m.d.comb += self.p.o_ready.eq(~data_valid | self.n[mid].i_ready)
+        m.d.comb += data_valid.eq(p_i_valid | \
+                                    (~self.n[mid].i_ready & data_valid))
+        with m.If(pv):
+            m.d.comb += eq(r_data, self.p.i_data)
+        m.d.comb += eq(self.n[mid].o_data, self.stage.process(r_data))
+
+        return m
+
+
+class CombMultiInPipeline(MultiInControlBase):
     """ A multi-input Combinatorial block conforming to the Pipeline API
 
         Attributes:
@@ -89,7 +226,8 @@ class CombMultiInPipeline(PipelineBase):
     """
 
     def __init__(self, stage, p_len, p_mux):
-        PipelineBase.__init__(self, stage, p_len=p_len)
+        MultiInControlBase.__init__(self, p_len=p_len)
+        self.stage = stage
         self.p_mux = p_mux
 
         # set up the input and output data
@@ -122,6 +260,8 @@ class CombMultiInPipeline(PipelineBase):
             n_i_readyn = Array(n_i_readyn)
             data_valid = Array(data_valid)
 
+        nirn = Signal(reset_less=True)
+        m.d.comb += nirn.eq(~self.n.i_ready)
         mid = self.p_mux.m_id
         for i in range(p_len):
             m.d.comb += data_valid[i].eq(0)
@@ -130,7 +270,7 @@ class CombMultiInPipeline(PipelineBase):
             m.d.comb += self.p[i].o_ready.eq(0)
         m.d.comb += p_i_valid[mid].eq(self.p_mux.active)
         m.d.comb += self.p[mid].o_ready.eq(~data_valid[mid] | self.n.i_ready)
-        m.d.comb += n_i_readyn[mid].eq(~self.n.i_ready & data_valid[mid])
+        m.d.comb += n_i_readyn[mid].eq(nirn & data_valid[mid])
         anyvalid = Signal(i, reset_less=True)
         av = []
         for i in range(p_len):
@@ -151,7 +291,19 @@ class CombMultiInPipeline(PipelineBase):
         return m
 
 
+class CombMuxOutPipe(CombMultiOutPipeline):
+    def __init__(self, stage, n_len):
+        # HACK: stage is also the n-way multiplexer
+        CombMultiOutPipeline.__init__(self, stage, n_len=n_len, n_mux=stage)
+
+        # HACK: n-mux is also the stage... so set the muxid equal to input mid
+        stage.m_id = self.p.i_data.mid
+
+
+
 class InputPriorityArbiter:
+    """ arbitration module for Input-Mux pipe, baed on PriorityEncoder
+    """
     def __init__(self, pipe, num_rows):
         self.pipe = pipe
         self.num_rows = num_rows
@@ -171,7 +323,7 @@ class InputPriorityArbiter:
         in_ready = []
         for i in range(self.num_rows):
             p_i_valid = Signal(reset_less=True)
-            m.d.comb += p_i_valid.eq(self.pipe.p[i].i_valid_logic())
+            m.d.comb += p_i_valid.eq(self.pipe.p[i].i_valid_test)
             in_ready.append(p_i_valid)
         m.d.comb += pe.i.eq(Cat(*in_ready)) # array of input "valids"
         m.d.comb += self.active.eq(~pe.n)   # encoder active (one input valid)
@@ -184,18 +336,18 @@ class InputPriorityArbiter:
 
 
 
-class ExamplePipeline(CombMultiInPipeline):
+class PriorityCombMuxInPipe(CombMultiInPipeline):
     """ an example of how to use the combinatorial pipeline.
     """
 
-    def __init__(self, p_len=2):
+    def __init__(self, stage, p_len=2):
         p_mux = InputPriorityArbiter(self, p_len)
-        CombMultiInPipeline.__init__(self, ExampleStage, p_len, p_mux)
+        CombMultiInPipeline.__init__(self, stage, p_len, p_mux)
 
 
 if __name__ == '__main__':
 
-    dut = ExamplePipeline()
+    dut = PriorityCombMuxInPipe(ExampleStage)
     vl = rtlil.convert(dut, ports=dut.ports())
     with open("test_combpipe.il", "w") as f:
         f.write(vl)