1 """ key strategic example showing how to do multi-input fan-in into a
2 multi-stage pipeline, then multi-output fanout.
4 the multiplex ID from the fan-in is passed in to the pipeline, preserved,
5 and used as a routing ID on the fanout.
8 from random
import randint
10 from nmigen
import Module
, Signal
, Cat
, Value
11 from nmigen
.compat
.sim
import run_simulation
12 from nmigen
.cli
import verilog
, rtlil
14 from multipipe
import CombMultiOutPipeline
15 from multipipe
import PriorityCombMuxInPipe
16 from singlepipe
import UnbufferedPipeline
19 class MuxCombPipeline(CombMultiOutPipeline
):
20 def __init__(self
, stage
, n_len
):
21 # HACK: stage is also the n-way multiplexer
22 CombMultiOutPipeline
.__init
__(self
, stage
, n_len
=n_len
, n_mux
=stage
)
24 # HACK: n-mux is also the stage... so set the muxid equal to input mid
25 stage
.m_id
= self
.p
.i_data
.mid
28 return self
.p_mux
.ports()
31 class PassData
: # (Value):
33 self
.mid
= Signal(2, reset_less
=True)
34 self
.idx
= Signal(8, reset_less
=True)
35 self
.data
= Signal(16, reset_less
=True)
37 def _rhs_signals(self
):
42 for elem_bits
, elem_sign
in (elem
.shape() for elem
in self
.ports()):
43 bits
= max(bits
, elem_bits
+ elem_sign
)
44 sign
= max(sign
, elem_sign
)
48 return [self
.mid
.eq(i
.mid
), self
.idx
.eq(i
.idx
), self
.data
.eq(i
.data
)]
51 return [self
.mid
, self
.idx
, self
.data
]
54 class PassThroughStage
:
58 return self
.ispec() # same as ospec
61 return i
# pass-through
65 class PassThroughPipe(UnbufferedPipeline
):
67 UnbufferedPipeline
.__init
__(self
, PassThroughStage())
71 def __init__(self
, dut
):
76 for mid
in range(dut
.num_rows
):
79 for i
in range(self
.tlen
):
80 self
.di
[mid
][i
] = randint(0, 255) + (mid
<<8)
81 self
.do
[mid
][i
] = self
.di
[mid
][i
]
84 for i
in range(self
.tlen
):
87 yield rs
.i_valid
.eq(1)
88 yield rs
.i_data
.data
.eq(op2
)
89 yield rs
.i_data
.idx
.eq(i
)
90 yield rs
.i_data
.mid
.eq(mid
)
92 o_p_ready
= yield rs
.o_ready
95 o_p_ready
= yield rs
.o_ready
97 print ("send", mid
, i
, hex(op2
))
99 yield rs
.i_valid
.eq(0)
100 # wait random period of time before queueing another value
101 for i
in range(randint(0, 3)):
104 yield rs
.i_valid
.eq(0)
107 print ("send ended", mid
)
109 ## wait random period of time before queueing another value
110 #for i in range(randint(0, 3)):
113 #send_range = randint(0, 3)
117 # send = randint(0, send_range) != 0
121 #stall_range = randint(0, 3)
122 #for j in range(randint(1,10)):
123 # stall = randint(0, stall_range) != 0
124 # yield self.dut.n[0].i_ready.eq(stall)
127 yield n
.i_ready
.eq(1)
129 o_n_valid
= yield n
.o_valid
130 i_n_ready
= yield n
.i_ready
131 if not o_n_valid
or not i_n_ready
:
134 out_mid
= yield n
.o_data
.mid
135 out_i
= yield n
.o_data
.idx
136 out_v
= yield n
.o_data
.data
138 print ("recv", out_mid
, out_i
, hex(out_v
))
140 # see if this output has occurred already, delete it if it has
141 assert mid
== out_mid
, "out_mid %d not correct %d" % (out_mid
, mid
)
142 assert out_i
in self
.do
[mid
], "out_i %d not in array %s" % \
143 (out_i
, repr(self
.do
[mid
]))
144 assert self
.do
[mid
][out_i
] == out_v
# pass-through data
145 del self
.do
[mid
][out_i
]
147 # check if there's any more outputs
148 if len(self
.do
[mid
]) == 0:
150 print ("recv ended", mid
)
153 class TestPriorityMuxPipe(PriorityCombMuxInPipe
):
154 def __init__(self
, num_rows
):
155 self
.num_rows
= num_rows
156 stage
= PassThroughStage()
157 PriorityCombMuxInPipe
.__init
__(self
, stage
, p_len
=self
.num_rows
)
161 for i
in range(len(self
.p
)):
162 res
+= [self
.p
[i
].i_valid
, self
.p
[i
].o_ready
] + \
163 self
.p
[i
].i_data
.ports()
164 res
+= [self
.n
.i_ready
, self
.n
.o_valid
] + \
165 self
.n
.o_data
.ports()
171 def __init__(self
, dut
):
176 for i
in range(self
.tlen
* dut
.num_rows
):
180 mid
= randint(0, dut
.num_rows
-1)
181 data
= randint(0, 255) + (mid
<<8)
184 for i
in range(self
.tlen
* dut
.num_rows
):
188 yield rs
.i_valid
.eq(1)
189 yield rs
.i_data
.data
.eq(op2
)
190 yield rs
.i_data
.mid
.eq(mid
)
192 o_p_ready
= yield rs
.o_ready
195 o_p_ready
= yield rs
.o_ready
197 print ("send", mid
, i
, hex(op2
))
199 yield rs
.i_valid
.eq(0)
200 # wait random period of time before queueing another value
201 for i
in range(randint(0, 3)):
204 yield rs
.i_valid
.eq(0)
207 class TestMuxOutPipe(MuxCombPipeline
):
208 def __init__(self
, num_rows
):
209 self
.num_rows
= num_rows
210 stage
= PassThroughStage()
211 MuxCombPipeline
.__init
__(self
, stage
, n_len
=self
.num_rows
)
214 res
= [self
.p
.i_valid
, self
.p
.o_ready
] + \
215 self
.p
.i_data
.ports()
216 for i
in range(len(self
.n
)):
217 res
+= [self
.n
[i
].i_ready
, self
.n
[i
].o_valid
] + \
218 self
.n
[i
].o_data
.ports()
223 def __init__(self
, num_rows
=4):
224 self
.num_rows
= num_rows
225 self
.inpipe
= TestPriorityMuxPipe(num_rows
) # fan-in (combinatorial)
226 self
.pipe1
= PassThroughPipe() # stage 1 (clock-sync)
227 self
.pipe2
= PassThroughPipe() # stage 2 (clock-sync)
228 self
.outpipe
= TestMuxOutPipe(num_rows
) # fan-out (combinatorial)
230 self
.p
= self
.inpipe
.p
# kinda annoying,
231 self
.n
= self
.outpipe
.n
# use pipe in/out as this class in/out
232 self
._ports
= self
.inpipe
.ports() + self
.outpipe
.ports()
234 def elaborate(self
, platform
):
236 m
.submodules
.inpipe
= self
.inpipe
237 m
.submodules
.pipe1
= self
.pipe1
238 m
.submodules
.pipe2
= self
.pipe2
239 m
.submodules
.outpipe
= self
.outpipe
241 m
.d
.comb
+= self
.inpipe
.n
.connect_to_next(self
.pipe1
.p
)
242 m
.d
.comb
+= self
.pipe1
.connect_to_next(self
.pipe2
)
243 m
.d
.comb
+= self
.pipe2
.connect_to_next(self
.outpipe
)
251 if __name__
== '__main__':
252 dut
= TestInOutPipe()
253 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
254 with
open("test_inoutmux_pipe.il", "w") as f
:
256 #run_simulation(dut, testbench(dut), vcd_name="test_inputgroup.vcd")
258 test
= InputTest(dut
)
259 run_simulation(dut
, [test
.rcv(1), test
.rcv(0),
260 test
.rcv(3), test
.rcv(2),
261 test
.send(0), test
.send(1),
262 test
.send(3), test
.send(2),
264 vcd_name
="test_inoutmux_pipe.vcd")