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