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