455a2040fb869ff8fb5c97770da938d6566b9400
[nmutil.git] / src / nmutil / pipemodbase.py
1 """
2 This work is funded through NLnet under Grant 2019-02-012
3
4 License: LGPLv3+
5
6 Associated bugreports:
7 * https://bugs.libre-soc.org/show_bug.cgi?id=538
8
9 """
10
11 from nmigen import Elaboratable
12 from ieee754.pipeline import DynamicPipe
13 from nmutil.singlepipe import StageChain
14
15
16 class PipeModBase(Elaboratable):
17 """PipeModBase: common code between nearly every pipeline module
18 """
19
20 def __init__(self, pspec, modname):
21 self.modname = modname # use this to give a name to this module
22 self.pspec = pspec
23 self.i = self.ispec()
24 self.o = self.ospec()
25
26 def process(self, i):
27 return self.o
28
29 def setup(self, m, i):
30 """ links module to inputs and outputs
31 """
32 setattr(m.submodules, self.modname, self)
33 m.d.comb += self.i.eq(i)
34
35
36 class PipeModBaseChain(DynamicPipe):
37 """PipeModBaseChain: common code between stage-chained pipes
38
39 Links a set of combinatorial modules (get_chain) together
40 and uses pspec.pipekls to dynamically select the pipeline type
41 Also conforms to the Pipeline Stage API
42 """
43
44 def __init__(self, pspec):
45 self.pspec = pspec
46 self.chain = self.get_chain()
47 super().__init__(pspec)
48
49 def ispec(self):
50 """ returns the input spec of the first module in the chain
51 """
52 return self.chain[0].ispec()
53
54 def ospec(self):
55 """ returns the output spec of the last module in the chain
56 """
57 return self.chain[-1].ospec()
58
59 def process(self, i):
60 return self.o # ... returned here (see setup comment below)
61
62 def setup(self, m, i):
63 """ links module to inputs and outputs
64 """
65 StageChain(self.chain).setup(m, i) # input linked here, through chain
66 self.o = self.chain[-1].o # output is the last thing in the chain...