1 """ Combinatorial Multi-input and Multi-output multiplexer blocks
2 conforming to Pipeline API
4 Multi-input is complex because if any one input is ready, the output
5 can be ready, and the decision comes from a separate module.
7 Multi-output is simple (pretty much identical to UnbufferedPipeline),
8 and the selection is just a mux. The only proviso (difference) being:
9 the outputs not being selected have to have their ready_o signals
14 from nmigen
import Signal
, Cat
, Const
, Mux
, Module
, Array
, Elaboratable
15 from nmigen
.cli
import verilog
, rtlil
16 from nmigen
.lib
.coding
import PriorityEncoder
17 from nmigen
.hdl
.rec
import Record
, Layout
18 from nmutil
.stageapi
import _spec
20 from collections
.abc
import Sequence
22 from .nmoperator
import eq
23 from .iocontrol
import NextControl
, PrevControl
26 class MultiInControlBase(Elaboratable
):
27 """ Common functions for Pipeline API
29 def __init__(self
, in_multi
=None, p_len
=1, maskwid
=0, routemask
=False):
30 """ Multi-input Control class. Conforms to same API as ControlBase...
31 mostly. has additional indices to the *multiple* input stages
33 * p: contains ready/valid to the previous stages PLURAL
34 * n: contains ready/valid to the next stage
37 * add data_i members to PrevControl and
38 * add data_o member to NextControl
40 self
.routemask
= routemask
41 # set up input and output IO ACK (prev/next ready/valid)
42 print ("multi_in", maskwid
, p_len
)
44 for i
in range(p_len
):
45 p
.append(PrevControl(in_multi
, maskwid
=maskwid
))
48 nmaskwid
= maskwid
# straight route mask mode
50 nmaskwid
= maskwid
* p_len
# fan-in mode
51 self
.n
= NextControl(maskwid
=maskwid
*p_len
) # masks fan in (Cat)
53 def connect_to_next(self
, nxt
, p_idx
=0):
54 """ helper function to connect to the next stage data/valid/ready.
56 return self
.n
.connect_to_next(nxt
.p
[p_idx
])
58 def _connect_in(self
, prev
, idx
=0, prev_idx
=None):
59 """ helper function to connect stage to an input source. do not
60 use to connect stage-to-stage!
63 return self
.p
[idx
]._connect
_in
(prev
.p
)
64 return self
.p
[idx
]._connect
_in
(prev
.p
[prev_idx
])
66 def _connect_out(self
, nxt
):
67 """ helper function to connect stage to an output source. do not
68 use to connect stage-to-stage!
71 return self
.n
._connect
_out
(nxt
.n
)
72 return self
.n
._connect
_out
(nxt
.n
)
74 def set_input(self
, i
, idx
=0):
75 """ helper function to set the input data
77 return eq(self
.p
[idx
].data_i
, i
)
79 def elaborate(self
, platform
):
81 for i
, p
in enumerate(self
.p
):
82 setattr(m
.submodules
, "p%d" % i
, p
)
83 m
.submodules
.n
= self
.n
95 class MultiOutControlBase(Elaboratable
):
96 """ Common functions for Pipeline API
98 def __init__(self
, n_len
=1, in_multi
=None, maskwid
=0, routemask
=False):
99 """ Multi-output Control class. Conforms to same API as ControlBase...
100 mostly. has additional indices to the multiple *output* stages
101 [MultiInControlBase has multiple *input* stages]
103 * p: contains ready/valid to the previou stage
104 * n: contains ready/valid to the next stages PLURAL
107 * add data_i member to PrevControl and
108 * add data_o members to NextControl
112 nmaskwid
= maskwid
# straight route mask mode
114 nmaskwid
= maskwid
* n_len
# fan-out mode
116 # set up input and output IO ACK (prev/next ready/valid)
117 self
.p
= PrevControl(in_multi
, maskwid
=nmaskwid
)
119 for i
in range(n_len
):
120 n
.append(NextControl(maskwid
=maskwid
))
123 def connect_to_next(self
, nxt
, n_idx
=0):
124 """ helper function to connect to the next stage data/valid/ready.
126 return self
.n
[n_idx
].connect_to_next(nxt
.p
)
128 def _connect_in(self
, prev
, idx
=0):
129 """ helper function to connect stage to an input source. do not
130 use to connect stage-to-stage!
132 return self
.n
[idx
]._connect
_in
(prev
.p
)
134 def _connect_out(self
, nxt
, idx
=0, nxt_idx
=None):
135 """ helper function to connect stage to an output source. do not
136 use to connect stage-to-stage!
139 return self
.n
[idx
]._connect
_out
(nxt
.n
)
140 return self
.n
[idx
]._connect
_out
(nxt
.n
[nxt_idx
])
142 def elaborate(self
, platform
):
144 m
.submodules
.p
= self
.p
145 for i
, n
in enumerate(self
.n
):
146 setattr(m
.submodules
, "n%d" % i
, n
)
149 def set_input(self
, i
):
150 """ helper function to set the input data
152 return eq(self
.p
.data_i
, i
)
163 class CombMultiOutPipeline(MultiOutControlBase
):
164 """ A multi-input Combinatorial block conforming to the Pipeline API
168 p.data_i : stage input data (non-array). shaped according to ispec
169 n.data_o : stage output data array. shaped according to ospec
172 def __init__(self
, stage
, n_len
, n_mux
, maskwid
=0, routemask
=False):
173 MultiOutControlBase
.__init
__(self
, n_len
=n_len
, maskwid
=maskwid
,
176 self
.maskwid
= maskwid
177 self
.routemask
= routemask
180 # set up the input and output data
181 self
.p
.data_i
= _spec(stage
.ispec
, 'data_i') # input type
182 for i
in range(n_len
):
183 name
= 'data_o_%d' % i
184 self
.n
[i
].data_o
= _spec(stage
.ospec
, name
) # output type
186 def process(self
, i
):
187 if hasattr(self
.stage
, "process"):
188 return self
.stage
.process(i
)
191 def elaborate(self
, platform
):
192 m
= MultiOutControlBase
.elaborate(self
, platform
)
194 if hasattr(self
.n_mux
, "elaborate"): # TODO: identify submodule?
195 m
.submodules
.n_mux
= self
.n_mux
197 # need buffer register conforming to *input* spec
198 r_data
= _spec(self
.stage
.ispec
, 'r_data') # input type
199 if hasattr(self
.stage
, "setup"):
200 self
.stage
.setup(m
, r_data
)
202 # multiplexer id taken from n_mux
203 muxid
= self
.n_mux
.m_id
204 print ("self.n_mux", self
.n_mux
)
205 print ("self.n_mux.m_id", self
.n_mux
.m_id
)
208 p_valid_i
= Signal(reset_less
=True)
209 pv
= Signal(reset_less
=True)
210 m
.d
.comb
+= p_valid_i
.eq(self
.p
.valid_i_test
)
211 m
.d
.comb
+= pv
.eq(self
.p
.valid_i
& self
.p
.ready_o
)
213 # all outputs to next stages first initialised to zero (invalid)
214 # the only output "active" is then selected by the muxid
215 for i
in range(len(self
.n
)):
216 m
.d
.comb
+= self
.n
[i
].valid_o
.eq(0)
217 data_valid
= self
.n
[muxid
].valid_o
218 m
.d
.comb
+= self
.p
.ready_o
.eq(~data_valid | self
.n
[muxid
].ready_i
)
219 m
.d
.comb
+= data_valid
.eq(p_valid_i | \
220 (~self
.n
[muxid
].ready_i
& data_valid
))
224 m
.d
.comb
+= eq(r_data
, self
.p
.data_i
)
225 m
.d
.comb
+= eq(self
.n
[muxid
].data_o
, self
.process(r_data
))
228 if self
.routemask
: # straight "routing" mode - treat like data
229 m
.d
.comb
+= self
.n
[muxid
].stop_o
.eq(self
.p
.stop_i
)
231 m
.d
.comb
+= self
.n
[muxid
].mask_o
.eq(self
.p
.mask_i
)
233 ml
= [] # accumulate output masks
234 ms
= [] # accumulate output stops
236 # conditionally fan-out mask bits, always fan-out stop bits
237 for i
in range(len(self
.n
)):
238 ml
.append(self
.n
[i
].mask_o
)
239 ms
.append(self
.n
[i
].stop_o
)
240 m
.d
.comb
+= Cat(*ms
).eq(self
.p
.stop_i
)
242 m
.d
.comb
+= Cat(*ml
).eq(self
.p
.mask_i
)
246 class CombMultiInPipeline(MultiInControlBase
):
247 """ A multi-input Combinatorial block conforming to the Pipeline API
251 p.data_i : StageInput, shaped according to ispec
253 p.data_o : StageOutput, shaped according to ospec
255 r_data : input_shape according to ispec
256 A temporary (buffered) copy of a prior (valid) input.
257 This is HELD if the output is not ready. It is updated
261 def __init__(self
, stage
, p_len
, p_mux
, maskwid
=0, routemask
=False):
262 MultiInControlBase
.__init
__(self
, p_len
=p_len
, maskwid
=maskwid
,
265 self
.maskwid
= maskwid
268 # set up the input and output data
269 for i
in range(p_len
):
270 name
= 'data_i_%d' % i
271 self
.p
[i
].data_i
= _spec(stage
.ispec
, name
) # input type
272 self
.n
.data_o
= _spec(stage
.ospec
, 'data_o')
274 def process(self
, i
):
275 if hasattr(self
.stage
, "process"):
276 return self
.stage
.process(i
)
279 def elaborate(self
, platform
):
280 m
= MultiInControlBase
.elaborate(self
, platform
)
282 m
.submodules
.p_mux
= self
.p_mux
284 # need an array of buffer registers conforming to *input* spec
290 for i
in range(p_len
):
292 r
= _spec(self
.stage
.ispec
, name
) # input type
294 data_valid
.append(Signal(name
="data_valid", reset_less
=True))
295 p_valid_i
.append(Signal(name
="p_valid_i", reset_less
=True))
296 n_ready_in
.append(Signal(name
="n_ready_in", reset_less
=True))
297 if hasattr(self
.stage
, "setup"):
298 self
.stage
.setup(m
, r
)
300 r_data
= Array(r_data
)
301 p_valid_i
= Array(p_valid_i
)
302 n_ready_in
= Array(n_ready_in
)
303 data_valid
= Array(data_valid
)
305 nirn
= Signal(reset_less
=True)
306 m
.d
.comb
+= nirn
.eq(~self
.n
.ready_i
)
307 mid
= self
.p_mux
.m_id
308 for i
in range(p_len
):
309 m
.d
.comb
+= data_valid
[i
].eq(0)
310 m
.d
.comb
+= n_ready_in
[i
].eq(1)
311 m
.d
.comb
+= p_valid_i
[i
].eq(0)
312 m
.d
.comb
+= self
.p
[i
].ready_o
.eq(0)
313 m
.d
.comb
+= p_valid_i
[mid
].eq(self
.p_mux
.active
)
314 m
.d
.comb
+= self
.p
[mid
].ready_o
.eq(~data_valid
[mid
] | self
.n
.ready_i
)
315 m
.d
.comb
+= n_ready_in
[mid
].eq(nirn
& data_valid
[mid
])
316 anyvalid
= Signal(i
, reset_less
=True)
318 for i
in range(p_len
):
319 av
.append(data_valid
[i
])
321 m
.d
.comb
+= self
.n
.valid_o
.eq(anyvalid
.bool())
322 m
.d
.comb
+= data_valid
[mid
].eq(p_valid_i
[mid
] | \
323 (n_ready_in
[mid
] & data_valid
[mid
]))
326 m
.d
.comb
+= eq(self
.n
.stop_o
, self
.p
[mid
].stop_i
)
327 for i
in range(p_len
):
328 m
.d
.comb
+= eq(self
.n
.stop_o
, self
.p
[i
].stop_i
)
329 vr
= Signal(reset_less
=True)
330 m
.d
.comb
+= vr
.eq(self
.p
[i
].valid_i
& self
.p
[i
].ready_o
)
332 m
.d
.comb
+= eq(self
.n
.mask_o
, self
.p
[mid
].mask_i
)
334 ml
= [] # accumulate output masks
335 ms
= [] # accumulate output stops
336 for i
in range(p_len
):
337 vr
= Signal(reset_less
=True)
338 m
.d
.comb
+= vr
.eq(self
.p
[i
].valid_i
& self
.p
[i
].ready_o
)
340 m
.d
.comb
+= eq(r_data
[i
], self
.p
[i
].data_i
)
342 mlen
= len(self
.p
[i
].mask_i
)
345 ml
.append(Mux(vr
, self
.p
[i
].mask_i
, Const(0, mlen
)))
346 ms
.append(self
.p
[i
].stop_i
)
348 m
.d
.comb
+= self
.n
.mask_o
.eq(Cat(*ml
))
349 m
.d
.comb
+= self
.n
.stop_o
.eq(Cat(*ms
))
351 m
.d
.comb
+= eq(self
.n
.data_o
, self
.process(r_data
[mid
]))
356 class CombMuxOutPipe(CombMultiOutPipeline
):
357 def __init__(self
, stage
, n_len
, maskwid
=0, muxidname
=None,
359 muxidname
= muxidname
or "muxid"
360 # HACK: stage is also the n-way multiplexer
361 CombMultiOutPipeline
.__init
__(self
, stage
, n_len
=n_len
,
362 n_mux
=stage
, maskwid
=maskwid
,
365 # HACK: n-mux is also the stage... so set the muxid equal to input muxid
366 muxid
= getattr(self
.p
.data_i
, muxidname
)
367 print ("combmuxout", muxidname
, muxid
)
372 class InputPriorityArbiter(Elaboratable
):
373 """ arbitration module for Input-Mux pipe, baed on PriorityEncoder
375 def __init__(self
, pipe
, num_rows
):
377 self
.num_rows
= num_rows
378 self
.mmax
= int(log(self
.num_rows
) / log(2))
379 self
.m_id
= Signal(self
.mmax
, reset_less
=True) # multiplex id
380 self
.active
= Signal(reset_less
=True)
382 def elaborate(self
, platform
):
385 assert len(self
.pipe
.p
) == self
.num_rows
, \
386 "must declare input to be same size"
387 pe
= PriorityEncoder(self
.num_rows
)
388 m
.submodules
.selector
= pe
390 # connect priority encoder
392 for i
in range(self
.num_rows
):
393 p_valid_i
= Signal(reset_less
=True)
394 m
.d
.comb
+= p_valid_i
.eq(self
.pipe
.p
[i
].valid_i_test
)
395 in_ready
.append(p_valid_i
)
396 m
.d
.comb
+= pe
.i
.eq(Cat(*in_ready
)) # array of input "valids"
397 m
.d
.comb
+= self
.active
.eq(~pe
.n
) # encoder active (one input valid)
398 m
.d
.comb
+= self
.m_id
.eq(pe
.o
) # output one active input
403 return [self
.m_id
, self
.active
]
407 class PriorityCombMuxInPipe(CombMultiInPipeline
):
408 """ an example of how to use the combinatorial pipeline.
411 def __init__(self
, stage
, p_len
=2, maskwid
=0, routemask
=False):
412 p_mux
= InputPriorityArbiter(self
, p_len
)
413 CombMultiInPipeline
.__init
__(self
, stage
, p_len
, p_mux
,
414 maskwid
=maskwid
, routemask
=routemask
)
417 if __name__
== '__main__':
419 from nmutil
.test
.example_buf_pipe
import ExampleStage
420 dut
= PriorityCombMuxInPipe(ExampleStage
)
421 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
422 with
open("test_combpipe.il", "w") as f
: