pass in flatten/processing function into _connect_in/out
[ieee754fpu.git] / src / add / example_buf_pipe.py
1 """ Pipeline and BufferedHandshake examples
2 """
3
4 from singlepipe import (PrevControl, NextControl, ControlBase,
5 StageCls, Stage, StageChain,
6 BufferedHandshake, UnbufferedPipeline, eq)
7
8 from nmigen import Signal, Module
9 from nmigen.cli import verilog, rtlil
10
11
12 class ExampleAddStage(StageCls):
13 """ an example of how to use the buffered pipeline, as a class instance
14 """
15
16 def ispec(self):
17 """ returns a tuple of input signals which will be the incoming data
18 """
19 return (Signal(16), Signal(16))
20
21 def ospec(self):
22 """ returns an output signal which will happen to contain the sum
23 of the two inputs
24 """
25 return Signal(16)
26
27 def process(self, i):
28 """ process the input data (sums the values in the tuple) and returns it
29 """
30 return i[0] + i[1]
31
32
33 class ExampleBufPipeAdd(BufferedHandshake):
34 """ an example of how to use the buffered pipeline, using a class instance
35 """
36
37 def __init__(self):
38 addstage = ExampleAddStage()
39 BufferedHandshake.__init__(self, addstage)
40
41
42 class ExampleStage(Stage):
43 """ an example of how to use the buffered pipeline, in a static class
44 fashion
45 """
46
47 def ispec():
48 return Signal(16, name="example_input_signal")
49
50 def ospec():
51 return Signal(16, name="example_output_signal")
52
53 def process(i):
54 """ process the input data and returns it (adds 1)
55 """
56 return i + 1
57
58
59 class ExampleStageCls(StageCls):
60 """ an example of how to use the buffered pipeline, in a static class
61 fashion
62 """
63
64 def ispec(self):
65 return Signal(16, name="example_input_signal")
66
67 def ospec(self):
68 return Signal(16, name="example_output_signal")
69
70 def process(self, i):
71 """ process the input data and returns it (adds 1)
72 """
73 return i + 1
74
75
76 class ExampleBufPipe(BufferedHandshake):
77 """ an example of how to use the buffered pipeline.
78 """
79
80 def __init__(self):
81 BufferedHandshake.__init__(self, ExampleStage)
82
83
84 class ExamplePipeline(UnbufferedPipeline):
85 """ an example of how to use the unbuffered pipeline.
86 """
87
88 def __init__(self):
89 UnbufferedPipeline.__init__(self, ExampleStage)
90
91
92 if __name__ == '__main__':
93 dut = ExampleBufPipe()
94 vl = rtlil.convert(dut, ports=dut.ports())
95 with open("test_bufpipe.il", "w") as f:
96 f.write(vl)
97
98 dut = ExamplePipeline()
99 vl = rtlil.convert(dut, ports=dut.ports())
100 with open("test_combpipe.il", "w") as f:
101 f.write(vl)