add 2 stage buffered pipeline unit test, reduce to 16-bit to make vcd clearer
[ieee754fpu.git] / src / add / example_buf_pipe.py
1 """ nmigen implementation of buffered pipeline stage, based on zipcpu:
2 https://zipcpu.com/blog/2017/08/14/strategies-for-pipelining.html
3
4 this module requires quite a bit of thought to understand how it works
5 (and why it is needed in the first place). reading the above is
6 *strongly* recommended.
7
8 unlike john dawson's IEEE754 FPU STB/ACK signalling, which requires
9 the STB / ACK signals to raise and lower (on separate clocks) before
10 data may proceeed (thus only allowing one piece of data to proceed
11 on *ALTERNATE* cycles), the signalling here is a true pipeline
12 where data will flow on *every* clock when the conditions are right.
13
14 input acceptance conditions are when:
15 * incoming previous-stage strobe (i_p_stb) is HIGH
16 * outgoing previous-stage busy (o_p_busy) is LOW
17
18 output transmission conditions are when:
19 * outgoing next-stage strobe (o_n_stb) is HIGH
20 * outgoing next-stage busy (i_n_busy) is LOW
21
22 the tricky bit is when the input has valid data and the output is not
23 ready to accept it. if it wasn't for the clock synchronisation, it
24 would be possible to tell the input "hey don't send that data, we're
25 not ready". unfortunately, it's not possible to "change the past":
26 the previous stage *has no choice* but to pass on its data.
27
28 therefore, the incoming data *must* be accepted - and stored.
29 on the same clock, it's possible to tell the input that it must
30 not send any more data. this is the "stall" condition.
31
32 we now effectively have *two* possible pieces of data to "choose" from:
33 the buffered data, and the incoming data. the decision as to which
34 to process and output is based on whether we are in "stall" or not.
35 i.e. when the next stage is no longer busy, the output comes from
36 the buffer if a stall had previously occurred, otherwise it comes
37 direct from processing the input.
38
39 it's quite a complex state machine!
40 """
41
42 from nmigen import Signal, Cat, Const, Mux, Module
43 from nmigen.cli import verilog, rtlil
44
45 class BufPipe:
46 """ buffered pipeline stage
47
48 stage-1 i_p_stb >>in stage o_n_stb out>> stage+1
49 stage-1 o_p_busy <<out stage i_n_busy <<in stage+1
50 stage-1 i_data >>in stage o_data out>> stage+1
51 | |
52 +-------> process
53 | |
54 +-- r_data ---+
55 """
56 def __init__(self):
57 # input
58 #self.i_p_rst = Signal() # >>in - comes in from PREVIOUS stage
59 self.i_p_stb = Signal() # >>in - comes in from PREVIOUS stage
60 self.i_n_busy = Signal() # in<< - comes in from the NEXT stage
61 self.i_data = Signal(16) # >>in - comes in from the PREVIOUS stage
62 #self.i_rst = Signal()
63
64 # buffered
65 self.r_data = Signal(16)
66
67 # output
68 self.o_n_stb = Signal() # out>> - goes out to the NEXT stage
69 self.o_p_busy = Signal() # <<out - goes out to the PREVIOUS stage
70 self.o_data = Signal(16) # out>> - goes out to the NEXT stage
71
72 def pre_process(self, d_in):
73 return d_in | 0xf0000
74
75 def process(self, d_in):
76 return d_in + 1
77
78 def elaborate(self, platform):
79 m = Module()
80
81 # establish some combinatorial temporaries
82 o_p_busyn = Signal(reset_less=True)
83 o_n_stbn = Signal(reset_less=True)
84 i_n_busyn = Signal(reset_less=True)
85 i_p_stb_o_p_busyn = Signal(reset_less=True)
86 m.d.comb += [i_n_busyn.eq(~self.i_n_busy),
87 o_n_stbn.eq(~self.o_n_stb),
88 o_p_busyn.eq(~self.o_p_busy),
89 i_p_stb_o_p_busyn.eq(self.i_p_stb & o_p_busyn),
90 ]
91
92 # store result of processing in combinatorial temporary
93 result = Signal(16)
94 with m.If(self.i_p_stb): # input is valid: process it
95 m.d.comb += result.eq(self.process(self.i_data))
96 with m.If(o_p_busyn): # not stalled
97 m.d.sync += self.r_data.eq(result)
98
99 #with m.If(self.i_p_rst): # reset
100 # m.d.sync += self.o_n_stb.eq(0)
101 # m.d.sync += self.o_p_busy.eq(0)
102 with m.If(i_n_busyn): # next stage is not busy
103 with m.If(o_p_busyn): # not stalled
104 # nothing in buffer: send input direct to output
105 m.d.sync += [self.o_n_stb.eq(self.i_p_stb),
106 self.o_data.eq(result),
107 ]
108 with m.Else(): # o_p_busy is true, and something is in our buffer.
109 # Flush the [already processed] buffer to the output port.
110 m.d.sync += [self.o_n_stb.eq(1),
111 self.o_data.eq(self.r_data),
112 # clear stall condition, declare register empty.
113 self.o_p_busy.eq(0),
114 ]
115 # ignore input, since o_p_busy is also true.
116
117 # (i_n_busy) is true here: next stage is busy
118 with m.Elif(o_n_stbn): # next stage being told "not busy"
119 m.d.sync += [self.o_n_stb.eq(self.i_p_stb),
120 self.o_p_busy.eq(0), # Keep the buffer empty
121 # set the output data (from comb result)
122 self.o_data.eq(result),
123 ]
124 # (i_n_busy) and (o_n_stb) both true:
125 with m.Elif(i_p_stb_o_p_busyn):
126 # If next stage *is* busy, and not stalled yet, accept input
127 m.d.sync += self.o_p_busy.eq(self.i_p_stb & self.o_n_stb)
128
129 with m.If(o_p_busyn): # not stalled
130 # turns out that from all of the above conditions, just
131 # always put result into buffer if not busy
132 m.d.sync += self.r_data.eq(result)
133
134 return m
135
136 def ports(self):
137 return [self.i_p_stb, self.i_n_busy, self.i_data,
138 self.r_data,
139 self.o_n_stb, self.o_p_busy, self.o_data
140 ]
141
142
143 if __name__ == '__main__':
144 dut = BufPipe()
145 vl = rtlil.convert(dut, ports=dut.ports())
146 with open("test_bufpipe.il", "w") as f:
147 f.write(vl)
148