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):
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 # set up input and output IO ACK (prev/next ready/valid)
42 for i
in range(p_len
):
43 p
.append(PrevControl(in_multi
))
45 self
.n
= NextControl()
47 def connect_to_next(self
, nxt
, p_idx
=0):
48 """ helper function to connect to the next stage data/valid/ready.
50 return self
.n
.connect_to_next(nxt
.p
[p_idx
])
52 def _connect_in(self
, prev
, idx
=0, prev_idx
=None):
53 """ helper function to connect stage to an input source. do not
54 use to connect stage-to-stage!
57 return self
.p
[idx
]._connect
_in
(prev
.p
)
58 return self
.p
[idx
]._connect
_in
(prev
.p
[prev_idx
])
60 def _connect_out(self
, nxt
):
61 """ helper function to connect stage to an output source. do not
62 use to connect stage-to-stage!
65 return self
.n
._connect
_out
(nxt
.n
)
66 return self
.n
._connect
_out
(nxt
.n
)
68 def set_input(self
, i
, idx
=0):
69 """ helper function to set the input data
71 return eq(self
.p
[idx
].data_i
, i
)
73 def elaborate(self
, platform
):
75 for i
, p
in enumerate(self
.p
):
76 setattr(m
.submodules
, "p%d" % i
, p
)
77 m
.submodules
.n
= self
.n
89 class MultiOutControlBase(Elaboratable
):
90 """ Common functions for Pipeline API
92 def __init__(self
, n_len
=1, in_multi
=None):
93 """ Multi-output Control class. Conforms to same API as ControlBase...
94 mostly. has additional indices to the multiple *output* stages
95 [MultiInControlBase has multiple *input* stages]
97 * p: contains ready/valid to the previou stage
98 * n: contains ready/valid to the next stages PLURAL
101 * add data_i member to PrevControl and
102 * add data_o members to NextControl
105 # set up input and output IO ACK (prev/next ready/valid)
106 self
.p
= PrevControl(in_multi
)
108 for i
in range(n_len
):
109 n
.append(NextControl())
112 def connect_to_next(self
, nxt
, n_idx
=0):
113 """ helper function to connect to the next stage data/valid/ready.
115 return self
.n
[n_idx
].connect_to_next(nxt
.p
)
117 def _connect_in(self
, prev
, idx
=0):
118 """ helper function to connect stage to an input source. do not
119 use to connect stage-to-stage!
121 return self
.n
[idx
]._connect
_in
(prev
.p
)
123 def _connect_out(self
, nxt
, idx
=0, nxt_idx
=None):
124 """ helper function to connect stage to an output source. do not
125 use to connect stage-to-stage!
128 return self
.n
[idx
]._connect
_out
(nxt
.n
)
129 return self
.n
[idx
]._connect
_out
(nxt
.n
[nxt_idx
])
131 def elaborate(self
, platform
):
133 m
.submodules
.p
= self
.p
134 for i
, n
in enumerate(self
.n
):
135 setattr(m
.submodules
, "n%d" % i
, n
)
138 def set_input(self
, i
):
139 """ helper function to set the input data
141 return eq(self
.p
.data_i
, i
)
152 class CombMultiOutPipeline(MultiOutControlBase
):
153 """ A multi-input Combinatorial block conforming to the Pipeline API
157 p.data_i : stage input data (non-array). shaped according to ispec
158 n.data_o : stage output data array. shaped according to ospec
161 def __init__(self
, stage
, n_len
, n_mux
):
162 MultiOutControlBase
.__init
__(self
, n_len
=n_len
)
166 # set up the input and output data
167 self
.p
.data_i
= _spec(stage
.ispec
, 'data_i') # input type
168 for i
in range(n_len
):
169 name
= 'data_o_%d' % i
170 self
.n
[i
].data_o
= _spec(stage
.ospec
, name
) # output type
172 def process(self
, i
):
173 if hasattr(self
.stage
, "process"):
174 return self
.stage
.process(i
)
177 def elaborate(self
, platform
):
178 m
= MultiOutControlBase
.elaborate(self
, platform
)
180 if hasattr(self
.n_mux
, "elaborate"): # TODO: identify submodule?
181 m
.submodules
+= self
.n_mux
183 # need buffer register conforming to *input* spec
184 r_data
= _spec(self
.stage
.ispec
, 'r_data') # input type
185 if hasattr(self
.stage
, "setup"):
186 self
.stage
.setup(m
, r_data
)
188 # multiplexer id taken from n_mux
189 muxid
= self
.n_mux
.m_id
190 print ("self.n_mux", self
.n_mux
)
191 print ("self.n_mux.m_id", self
.n_mux
.m_id
)
194 p_valid_i
= Signal(reset_less
=True)
195 pv
= Signal(reset_less
=True)
196 m
.d
.comb
+= p_valid_i
.eq(self
.p
.valid_i_test
)
197 m
.d
.comb
+= pv
.eq(self
.p
.valid_i
& self
.p
.ready_o
)
199 # all outputs to next stages first initialised to zero (invalid)
200 # the only output "active" is then selected by the muxid
201 for i
in range(len(self
.n
)):
202 m
.d
.comb
+= self
.n
[i
].valid_o
.eq(0)
203 data_valid
= self
.n
[muxid
].valid_o
204 m
.d
.comb
+= self
.p
.ready_o
.eq(~data_valid | self
.n
[muxid
].ready_i
)
205 m
.d
.comb
+= data_valid
.eq(p_valid_i | \
206 (~self
.n
[muxid
].ready_i
& data_valid
))
208 m
.d
.comb
+= eq(r_data
, self
.p
.data_i
)
209 m
.d
.comb
+= eq(self
.n
[muxid
].data_o
, self
.process(r_data
))
214 class CombMultiInPipeline(MultiInControlBase
):
215 """ A multi-input Combinatorial block conforming to the Pipeline API
219 p.data_i : StageInput, shaped according to ispec
221 p.data_o : StageOutput, shaped according to ospec
223 r_data : input_shape according to ispec
224 A temporary (buffered) copy of a prior (valid) input.
225 This is HELD if the output is not ready. It is updated
229 def __init__(self
, stage
, p_len
, p_mux
):
230 MultiInControlBase
.__init
__(self
, p_len
=p_len
)
234 # set up the input and output data
235 for i
in range(p_len
):
236 name
= 'data_i_%d' % i
237 self
.p
[i
].data_i
= _spec(stage
.ispec
, name
) # input type
238 self
.n
.data_o
= _spec(stage
.ospec
, 'data_o')
240 def process(self
, i
):
241 if hasattr(self
.stage
, "process"):
242 return self
.stage
.process(i
)
245 def elaborate(self
, platform
):
246 m
= MultiInControlBase
.elaborate(self
, platform
)
248 m
.submodules
+= self
.p_mux
250 # need an array of buffer registers conforming to *input* spec
256 for i
in range(p_len
):
258 r
= _spec(self
.stage
.ispec
, name
) # input type
260 data_valid
.append(Signal(name
="data_valid", reset_less
=True))
261 p_valid_i
.append(Signal(name
="p_valid_i", reset_less
=True))
262 n_ready_in
.append(Signal(name
="n_ready_in", reset_less
=True))
263 if hasattr(self
.stage
, "setup"):
264 self
.stage
.setup(m
, r
)
266 r_data
= Array(r_data
)
267 p_valid_i
= Array(p_valid_i
)
268 n_ready_in
= Array(n_ready_in
)
269 data_valid
= Array(data_valid
)
271 nirn
= Signal(reset_less
=True)
272 m
.d
.comb
+= nirn
.eq(~self
.n
.ready_i
)
273 mid
= self
.p_mux
.m_id
274 for i
in range(p_len
):
275 m
.d
.comb
+= data_valid
[i
].eq(0)
276 m
.d
.comb
+= n_ready_in
[i
].eq(1)
277 m
.d
.comb
+= p_valid_i
[i
].eq(0)
278 m
.d
.comb
+= self
.p
[i
].ready_o
.eq(0)
279 m
.d
.comb
+= p_valid_i
[mid
].eq(self
.p_mux
.active
)
280 m
.d
.comb
+= self
.p
[mid
].ready_o
.eq(~data_valid
[mid
] | self
.n
.ready_i
)
281 m
.d
.comb
+= n_ready_in
[mid
].eq(nirn
& data_valid
[mid
])
282 anyvalid
= Signal(i
, reset_less
=True)
284 for i
in range(p_len
):
285 av
.append(data_valid
[i
])
287 m
.d
.comb
+= self
.n
.valid_o
.eq(anyvalid
.bool())
288 m
.d
.comb
+= data_valid
[mid
].eq(p_valid_i
[mid
] | \
289 (n_ready_in
[mid
] & data_valid
[mid
]))
291 for i
in range(p_len
):
292 vr
= Signal(reset_less
=True)
293 m
.d
.comb
+= vr
.eq(self
.p
[i
].valid_i
& self
.p
[i
].ready_o
)
295 m
.d
.comb
+= eq(r_data
[i
], self
.p
[i
].data_i
)
297 m
.d
.comb
+= eq(self
.n
.data_o
, self
.process(r_data
[mid
]))
302 class CombMuxOutPipe(CombMultiOutPipeline
):
303 def __init__(self
, stage
, n_len
):
304 # HACK: stage is also the n-way multiplexer
305 CombMultiOutPipeline
.__init
__(self
, stage
, n_len
=n_len
, n_mux
=stage
)
307 # HACK: n-mux is also the stage... so set the muxid equal to input muxid
308 print ("combmuxout", self
.p
.data_i
.muxid
)
309 stage
.m_id
= self
.p
.data_i
.muxid
313 class InputPriorityArbiter(Elaboratable
):
314 """ arbitration module for Input-Mux pipe, baed on PriorityEncoder
316 def __init__(self
, pipe
, num_rows
):
318 self
.num_rows
= num_rows
319 self
.mmax
= int(log(self
.num_rows
) / log(2))
320 self
.m_id
= Signal(self
.mmax
, reset_less
=True) # multiplex id
321 self
.active
= Signal(reset_less
=True)
323 def elaborate(self
, platform
):
326 assert len(self
.pipe
.p
) == self
.num_rows
, \
327 "must declare input to be same size"
328 pe
= PriorityEncoder(self
.num_rows
)
329 m
.submodules
.selector
= pe
331 # connect priority encoder
333 for i
in range(self
.num_rows
):
334 p_valid_i
= Signal(reset_less
=True)
335 m
.d
.comb
+= p_valid_i
.eq(self
.pipe
.p
[i
].valid_i_test
)
336 in_ready
.append(p_valid_i
)
337 m
.d
.comb
+= pe
.i
.eq(Cat(*in_ready
)) # array of input "valids"
338 m
.d
.comb
+= self
.active
.eq(~pe
.n
) # encoder active (one input valid)
339 m
.d
.comb
+= self
.m_id
.eq(pe
.o
) # output one active input
344 return [self
.m_id
, self
.active
]
348 class PriorityCombMuxInPipe(CombMultiInPipeline
):
349 """ an example of how to use the combinatorial pipeline.
352 def __init__(self
, stage
, p_len
=2):
353 p_mux
= InputPriorityArbiter(self
, p_len
)
354 CombMultiInPipeline
.__init
__(self
, stage
, p_len
, p_mux
)
357 if __name__
== '__main__':
359 from nmutil
.test
.example_buf_pipe
import ExampleStage
360 dut
= PriorityCombMuxInPipe(ExampleStage
)
361 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
362 with
open("test_combpipe.il", "w") as f
: