1 """ Pipeline and BufferedHandshake implementation, conforming to the same API.
2 For multi-input and multi-output variants, see multipipe.
4 Associated development bugs:
5 * http://bugs.libre-riscv.org/show_bug.cgi?id=64
6 * http://bugs.libre-riscv.org/show_bug.cgi?id=57
11 a strategically very important function that is identical in function
12 to nmigen's Signal.eq function, except it may take objects, or a list
13 of objects, or a tuple of objects, and where objects may also be
19 stage requires compliance with a strict API that may be
20 implemented in several means, including as a static class.
21 the methods of a stage instance must be as follows:
23 * ispec() - Input data format specification
24 returns an object or a list or tuple of objects, or
25 a Record, each object having an "eq" function which
26 takes responsibility for copying by assignment all
28 * ospec() - Output data format specification
29 requirements as for ospec
30 * process(m, i) - Processes an ispec-formatted object
31 returns a combinatorial block of a result that
32 may be assigned to the output, by way of the "eq"
34 * setup(m, i) - Optional function for setting up submodules
35 may be used for more complex stages, to link
36 the input (i) to submodules. must take responsibility
37 for adding those submodules to the module (m).
38 the submodules must be combinatorial blocks and
39 must have their inputs and output linked combinatorially.
41 Both StageCls (for use with non-static classes) and Stage (for use
42 by static classes) are abstract classes from which, for convenience
43 and as a courtesy to other developers, anything conforming to the
44 Stage API may *choose* to derive.
49 A useful combinatorial wrapper around stages that chains them together
50 and then presents a Stage-API-conformant interface. By presenting
51 the same API as the stages it wraps, it can clearly be used recursively.
56 A convenience class that takes an input shape, output shape, a
57 "processing" function and an optional "setup" function. Honestly
58 though, there's not much more effort to just... create a class
59 that returns a couple of Records (see ExampleAddRecordStage in
65 A convenience class that takes a single function as a parameter,
66 that is chain-called to create the exact same input and output spec.
67 It has a process() function that simply returns its input.
69 Instances of this class are completely redundant if handed to
70 StageChain, however when passed to UnbufferedPipeline they
71 can be used to introduce a single clock delay.
76 The base class for pipelines. Contains previous and next ready/valid/data.
77 Also has an extremely useful "connect" function that can be used to
78 connect a chain of pipelines and present the exact same prev/next
84 A simple stalling clock-synchronised pipeline that has no buffering
85 (unlike BufferedHandshake). Data flows on *every* clock cycle when
86 the conditions are right (this is nominally when the input is valid
87 and the output is ready).
89 A stall anywhere along the line will result in a stall back-propagating
90 down the entire chain. The BufferedHandshake by contrast will buffer
91 incoming data, allowing previous stages one clock cycle's grace before
94 An advantage of the UnbufferedPipeline over the Buffered one is
95 that the amount of logic needed (number of gates) is greatly
96 reduced (no second set of buffers basically)
98 The disadvantage of the UnbufferedPipeline is that the valid/ready
99 logic, if chained together, is *combinatorial*, resulting in
100 progressively larger gate delay.
102 PassThroughHandshake:
105 A Control class that introduces a single clock delay, passing its
106 data through unaltered. Unlike RegisterPipeline (which relies
107 on UnbufferedPipeline and PassThroughStage) it handles ready/valid
113 A convenience class that, because UnbufferedPipeline introduces a single
114 clock delay, when its stage is a PassThroughStage, it results in a Pipeline
115 stage that, duh, delays its (unmodified) input by one clock cycle.
120 nmigen implementation of buffered pipeline stage, based on zipcpu:
121 https://zipcpu.com/blog/2017/08/14/strategies-for-pipelining.html
123 this module requires quite a bit of thought to understand how it works
124 (and why it is needed in the first place). reading the above is
125 *strongly* recommended.
127 unlike john dawson's IEEE754 FPU STB/ACK signalling, which requires
128 the STB / ACK signals to raise and lower (on separate clocks) before
129 data may proceeed (thus only allowing one piece of data to proceed
130 on *ALTERNATE* cycles), the signalling here is a true pipeline
131 where data will flow on *every* clock when the conditions are right.
133 input acceptance conditions are when:
134 * incoming previous-stage strobe (p.i_valid) is HIGH
135 * outgoing previous-stage ready (p.o_ready) is LOW
137 output transmission conditions are when:
138 * outgoing next-stage strobe (n.o_valid) is HIGH
139 * outgoing next-stage ready (n.i_ready) is LOW
141 the tricky bit is when the input has valid data and the output is not
142 ready to accept it. if it wasn't for the clock synchronisation, it
143 would be possible to tell the input "hey don't send that data, we're
144 not ready". unfortunately, it's not possible to "change the past":
145 the previous stage *has no choice* but to pass on its data.
147 therefore, the incoming data *must* be accepted - and stored: that
148 is the responsibility / contract that this stage *must* accept.
149 on the same clock, it's possible to tell the input that it must
150 not send any more data. this is the "stall" condition.
152 we now effectively have *two* possible pieces of data to "choose" from:
153 the buffered data, and the incoming data. the decision as to which
154 to process and output is based on whether we are in "stall" or not.
155 i.e. when the next stage is no longer ready, the output comes from
156 the buffer if a stall had previously occurred, otherwise it comes
157 direct from processing the input.
159 this allows us to respect a synchronous "travelling STB" with what
160 dan calls a "buffered handshake".
162 it's quite a complex state machine!
167 Synchronised pipeline, Based on:
168 https://github.com/ZipCPU/dbgbus/blob/master/hexbus/rtl/hbdeword.v
171 from nmigen
import Signal
, Cat
, Const
, Mux
, Module
, Value
172 from nmigen
.cli
import verilog
, rtlil
173 from nmigen
.lib
.fifo
import SyncFIFO
, SyncFIFOBuffered
174 from nmigen
.hdl
.ast
import ArrayProxy
175 from nmigen
.hdl
.rec
import Record
, Layout
177 from abc
import ABCMeta
, abstractmethod
178 from collections
.abc
import Sequence
179 from queue
import Queue
182 class RecordObject(Record
):
183 def __init__(self
, layout
=None, name
=None):
184 Record
.__init
__(self
, layout
=layout
or [], name
=None)
186 def __setattr__(self
, k
, v
):
187 if k
in dir(Record
) or "fields" not in self
.__dict
__:
188 return object.__setattr
__(self
, k
, v
)
190 if isinstance(v
, Record
):
191 newlayout
= {k
: (k
, v
.layout
)}
193 newlayout
= {k
: (k
, v
.shape())}
194 self
.layout
.fields
.update(newlayout
)
197 for x
in self
.fields
.values():
202 """ contains signals that come *from* the previous stage (both in and out)
203 * i_valid: previous stage indicating all incoming data is valid.
204 may be a multi-bit signal, where all bits are required
205 to be asserted to indicate "valid".
206 * o_ready: output to next stage indicating readiness to accept data
207 * i_data : an input - added by the user of this class
210 def __init__(self
, i_width
=1, stage_ctl
=False):
211 self
.stage_ctl
= stage_ctl
212 self
.i_valid
= Signal(i_width
, name
="p_i_valid") # prev >>in self
213 self
._o
_ready
= Signal(name
="p_o_ready") # prev <<out self
214 self
.i_data
= None # XXX MUST BE ADDED BY USER
216 self
.s_o_ready
= Signal(name
="p_s_o_rdy") # prev <<out self
217 self
.trigger
= Signal(reset_less
=True)
221 """ public-facing API: indicates (externally) that stage is ready
224 return self
.s_o_ready
# set dynamically by stage
225 return self
._o
_ready
# return this when not under dynamic control
227 def _connect_in(self
, prev
, direct
=False, fn
=None):
228 """ internal helper function to connect stage to an input source.
229 do not use to connect stage-to-stage!
231 i_valid
= prev
.i_valid
if direct
else prev
.i_valid_test
232 i_data
= fn(prev
.i_data
) if fn
is not None else prev
.i_data
233 return [self
.i_valid
.eq(i_valid
),
234 prev
.o_ready
.eq(self
.o_ready
),
235 eq(self
.i_data
, i_data
),
239 def i_valid_test(self
):
240 vlen
= len(self
.i_valid
)
242 # multi-bit case: valid only when i_valid is all 1s
243 all1s
= Const(-1, (len(self
.i_valid
), False))
244 i_valid
= (self
.i_valid
== all1s
)
246 # single-bit i_valid case
247 i_valid
= self
.i_valid
249 # when stage indicates not ready, incoming data
250 # must "appear" to be not ready too
252 i_valid
= i_valid
& self
.s_o_ready
256 def elaborate(self
, platform
):
258 m
.d
.comb
+= self
.trigger
.eq(self
.i_valid_test
& self
.o_ready
)
262 return [self
.i_data
.eq(i
.i_data
),
263 self
.o_ready
.eq(i
.o_ready
),
264 self
.i_valid
.eq(i
.i_valid
)]
269 if hasattr(self
.i_data
, "ports"):
270 yield from self
.i_data
.ports()
271 elif isinstance(self
.i_data
, Sequence
):
272 yield from self
.i_data
281 """ contains the signals that go *to* the next stage (both in and out)
282 * o_valid: output indicating to next stage that data is valid
283 * i_ready: input from next stage indicating that it can accept data
284 * o_data : an output - added by the user of this class
286 def __init__(self
, stage_ctl
=False):
287 self
.stage_ctl
= stage_ctl
288 self
.o_valid
= Signal(name
="n_o_valid") # self out>> next
289 self
.i_ready
= Signal(name
="n_i_ready") # self <<in next
290 self
.o_data
= None # XXX MUST BE ADDED BY USER
292 self
.d_valid
= Signal(reset
=1) # INTERNAL (data valid)
293 self
.trigger
= Signal(reset_less
=True)
296 def i_ready_test(self
):
298 return self
.i_ready
& self
.d_valid
301 def connect_to_next(self
, nxt
):
302 """ helper function to connect to the next stage data/valid/ready.
303 data/valid is passed *TO* nxt, and ready comes *IN* from nxt.
304 use this when connecting stage-to-stage
306 return [nxt
.i_valid
.eq(self
.o_valid
),
307 self
.i_ready
.eq(nxt
.o_ready
),
308 eq(nxt
.i_data
, self
.o_data
),
311 def _connect_out(self
, nxt
, direct
=False, fn
=None):
312 """ internal helper function to connect stage to an output source.
313 do not use to connect stage-to-stage!
315 i_ready
= nxt
.i_ready
if direct
else nxt
.i_ready_test
316 o_data
= fn(nxt
.o_data
) if fn
is not None else nxt
.o_data
317 return [nxt
.o_valid
.eq(self
.o_valid
),
318 self
.i_ready
.eq(i_ready
),
319 eq(o_data
, self
.o_data
),
322 def elaborate(self
, platform
):
324 m
.d
.comb
+= self
.trigger
.eq(self
.i_ready_test
& self
.o_valid
)
330 if hasattr(self
.o_data
, "ports"):
331 yield from self
.o_data
.ports()
332 elif isinstance(self
.o_data
, Sequence
):
333 yield from self
.o_data
342 """ a helper class for iterating twin-argument compound data structures.
344 Record is a special (unusual, recursive) case, where the input may be
345 specified as a dictionary (which may contain further dictionaries,
346 recursively), where the field names of the dictionary must match
347 the Record's field spec. Alternatively, an object with the same
348 member names as the Record may be assigned: it does not have to
351 ArrayProxy is also special-cased, it's a bit messy: whilst ArrayProxy
352 has an eq function, the object being assigned to it (e.g. a python
353 object) might not. despite the *input* having an eq function,
354 that doesn't help us, because it's the *ArrayProxy* that's being
355 assigned to. so.... we cheat. use the ports() function of the
356 python object, enumerate them, find out the list of Signals that way,
359 def iterator2(self
, o
, i
):
360 if isinstance(o
, dict):
361 yield from self
.dict_iter2(o
, i
)
363 if not isinstance(o
, Sequence
):
365 for (ao
, ai
) in zip(o
, i
):
366 #print ("visit", fn, ao, ai)
367 if isinstance(ao
, Record
):
368 yield from self
.record_iter2(ao
, ai
)
369 elif isinstance(ao
, ArrayProxy
) and not isinstance(ai
, Value
):
370 yield from self
.arrayproxy_iter2(ao
, ai
)
374 def dict_iter2(self
, o
, i
):
375 for (k
, v
) in o
.items():
376 print ("d-iter", v
, i
[k
])
380 def _not_quite_working_with_all_unit_tests_record_iter2(self
, ao
, ai
):
381 print ("record_iter2", ao
, ai
, type(ao
), type(ai
))
382 if isinstance(ai
, Value
):
383 if isinstance(ao
, Sequence
):
385 for o
, i
in zip(ao
, ai
):
388 for idx
, (field_name
, field_shape
, _
) in enumerate(ao
.layout
):
389 if isinstance(field_shape
, Layout
):
393 if hasattr(val
, field_name
): # check for attribute
394 val
= getattr(val
, field_name
)
396 val
= val
[field_name
] # dictionary-style specification
397 yield from self
.iterator2(ao
.fields
[field_name
], val
)
399 def record_iter2(self
, ao
, ai
):
400 for idx
, (field_name
, field_shape
, _
) in enumerate(ao
.layout
):
401 if isinstance(field_shape
, Layout
):
405 if hasattr(val
, field_name
): # check for attribute
406 val
= getattr(val
, field_name
)
408 val
= val
[field_name
] # dictionary-style specification
409 yield from self
.iterator2(ao
.fields
[field_name
], val
)
411 def arrayproxy_iter2(self
, ao
, ai
):
413 op
= getattr(ao
, p
.name
)
414 print ("arrayproxy - p", p
, p
.name
)
415 yield from self
.iterator2(op
, p
)
419 """ a helper class for iterating single-argument compound data structures.
422 def iterate(self
, i
):
423 """ iterate a compound structure recursively using yield
425 if not isinstance(i
, Sequence
):
428 print ("iterate", ai
)
429 if isinstance(ai
, Record
):
430 print ("record", list(ai
.layout
))
431 yield from self
.record_iter(ai
)
432 elif isinstance(ai
, ArrayProxy
) and not isinstance(ai
, Value
):
433 yield from self
.array_iter(ai
)
437 def record_iter(self
, ai
):
438 for idx
, (field_name
, field_shape
, _
) in enumerate(ai
.layout
):
439 if isinstance(field_shape
, Layout
):
443 if hasattr(val
, field_name
): # check for attribute
444 val
= getattr(val
, field_name
)
446 val
= val
[field_name
] # dictionary-style specification
447 print ("recidx", idx
, field_name
, field_shape
, val
)
448 yield from self
.iterate(val
)
450 def array_iter(self
, ai
):
452 yield from self
.iterate(p
)
456 """ makes signals equal: a helper routine which identifies if it is being
457 passed a list (or tuple) of objects, or signals, or Records, and calls
458 the objects' eq function.
461 for (ao
, ai
) in Visitor2().iterator2(o
, i
):
463 if not isinstance(rres
, Sequence
):
473 print ("shape?", part
)
480 """ flattens a compound structure recursively using Cat
482 from nmigen
.tools
import flatten
483 #res = list(flatten(i)) # works (as of nmigen commit f22106e5) HOWEVER...
484 res
= list(Visitor().iterate(i
)) # needed because input may be a sequence
488 class StageCls(metaclass
=ABCMeta
):
489 """ Class-based "Stage" API. requires instantiation (after derivation)
491 see "Stage API" above.. Note: python does *not* require derivation
492 from this class. All that is required is that the pipelines *have*
493 the functions listed in this class. Derivation from this class
494 is therefore merely a "courtesy" to maintainers.
497 def ispec(self
): pass # REQUIRED
499 def ospec(self
): pass # REQUIRED
501 #def setup(self, m, i): pass # OPTIONAL
503 def process(self
, i
): pass # REQUIRED
506 class Stage(metaclass
=ABCMeta
):
507 """ Static "Stage" API. does not require instantiation (after derivation)
509 see "Stage API" above. Note: python does *not* require derivation
510 from this class. All that is required is that the pipelines *have*
511 the functions listed in this class. Derivation from this class
512 is therefore merely a "courtesy" to maintainers.
524 #def setup(m, i): pass
531 class RecordBasedStage(Stage
):
532 """ convenience class which provides a Records-based layout.
533 honestly it's a lot easier just to create a direct Records-based
534 class (see ExampleAddRecordStage)
536 def __init__(self
, in_shape
, out_shape
, processfn
, setupfn
=None):
537 self
.in_shape
= in_shape
538 self
.out_shape
= out_shape
539 self
.__process
= processfn
540 self
.__setup
= setupfn
541 def ispec(self
): return Record(self
.in_shape
)
542 def ospec(self
): return Record(self
.out_shape
)
543 def process(seif
, i
): return self
.__process
(i
)
544 def setup(seif
, m
, i
): return self
.__setup
(m
, i
)
547 class StageChain(StageCls
):
548 """ pass in a list of stages, and they will automatically be
549 chained together via their input and output specs into a
552 the end result basically conforms to the exact same Stage API.
554 * input to this class will be the input of the first stage
555 * output of first stage goes into input of second
556 * output of second goes into input into third (etc. etc.)
557 * the output of this class will be the output of the last stage
559 def __init__(self
, chain
, specallocate
=False):
561 self
.specallocate
= specallocate
564 return self
.chain
[0].ispec()
567 return self
.chain
[-1].ospec()
569 def _specallocate_setup(self
, m
, i
):
570 for (idx
, c
) in enumerate(self
.chain
):
571 if hasattr(c
, "setup"):
572 c
.setup(m
, i
) # stage may have some module stuff
573 o
= self
.chain
[idx
].ospec() # last assignment survives
574 m
.d
.comb
+= eq(o
, c
.process(i
)) # process input into "o"
575 if idx
== len(self
.chain
)-1:
577 i
= self
.chain
[idx
+1].ispec() # new input on next loop
578 m
.d
.comb
+= eq(i
, o
) # assign to next input
579 return o
# last loop is the output
581 def _noallocate_setup(self
, m
, i
):
582 for (idx
, c
) in enumerate(self
.chain
):
583 if hasattr(c
, "setup"):
584 c
.setup(m
, i
) # stage may have some module stuff
585 i
= o
= c
.process(i
) # store input into "o"
586 return o
# last loop is the output
588 def setup(self
, m
, i
):
589 if self
.specallocate
:
590 self
.o
= self
._specallocate
_setup
(m
, i
)
592 self
.o
= self
._noallocate
_setup
(m
, i
)
594 def process(self
, i
):
595 return self
.o
# conform to Stage API: return last-loop output
599 """ Common functions for Pipeline API
601 def __init__(self
, stage
=None, in_multi
=None, stage_ctl
=False):
602 """ Base class containing ready/valid/data to previous and next stages
604 * p: contains ready/valid to the previous stage
605 * n: contains ready/valid to the next stage
607 Except when calling Controlbase.connect(), user must also:
608 * add i_data member to PrevControl (p) and
609 * add o_data member to NextControl (n)
613 # set up input and output IO ACK (prev/next ready/valid)
614 self
.p
= PrevControl(in_multi
, stage_ctl
)
615 self
.n
= NextControl(stage_ctl
)
617 # set up the input and output data
618 if stage
is not None:
619 self
.p
.i_data
= stage
.ispec() # input type
620 self
.n
.o_data
= stage
.ospec()
622 def connect_to_next(self
, nxt
):
623 """ helper function to connect to the next stage data/valid/ready.
625 return self
.n
.connect_to_next(nxt
.p
)
627 def _connect_in(self
, prev
):
628 """ internal helper function to connect stage to an input source.
629 do not use to connect stage-to-stage!
631 return self
.p
._connect
_in
(prev
.p
)
633 def _connect_out(self
, nxt
):
634 """ internal helper function to connect stage to an output source.
635 do not use to connect stage-to-stage!
637 return self
.n
._connect
_out
(nxt
.n
)
639 def connect(self
, pipechain
):
640 """ connects a chain (list) of Pipeline instances together and
641 links them to this ControlBase instance:
643 in <----> self <---> out
646 [pipe1, pipe2, pipe3, pipe4]
649 out---in out--in out---in
651 Also takes care of allocating i_data/o_data, by looking up
652 the data spec for each end of the pipechain. i.e It is NOT
653 necessary to allocate self.p.i_data or self.n.o_data manually:
654 this is handled AUTOMATICALLY, here.
656 Basically this function is the direct equivalent of StageChain,
657 except that unlike StageChain, the Pipeline logic is followed.
659 Just as StageChain presents an object that conforms to the
660 Stage API from a list of objects that also conform to the
661 Stage API, an object that calls this Pipeline connect function
662 has the exact same pipeline API as the list of pipline objects
665 Thus it becomes possible to build up larger chains recursively.
666 More complex chains (multi-input, multi-output) will have to be
669 eqs
= [] # collated list of assignment statements
671 # connect inter-chain
672 for i
in range(len(pipechain
)-1):
674 pipe2
= pipechain
[i
+1]
675 eqs
+= pipe1
.connect_to_next(pipe2
)
677 # connect front of chain to ourselves
679 self
.p
.i_data
= front
.stage
.ispec()
680 eqs
+= front
._connect
_in
(self
)
682 # connect end of chain to ourselves
684 self
.n
.o_data
= end
.stage
.ospec()
685 eqs
+= end
._connect
_out
(self
)
689 def _postprocess(self
, i
): # XXX DISABLED
690 return i
# RETURNS INPUT
691 if hasattr(self
.stage
, "postprocess"):
692 return self
.stage
.postprocess(i
)
695 def set_input(self
, i
):
696 """ helper function to set the input data
698 return eq(self
.p
.i_data
, i
)
707 def _elaborate(self
, platform
):
708 """ handles case where stage has dynamic ready/valid functions
711 m
.submodules
.p
= self
.p
712 m
.submodules
.n
= self
.n
714 if self
.stage
is not None and hasattr(self
.stage
, "setup"):
715 self
.stage
.setup(m
, self
.p
.i_data
)
717 if not self
.p
.stage_ctl
:
720 # intercept the previous (outgoing) "ready", combine with stage ready
721 m
.d
.comb
+= self
.p
.s_o_ready
.eq(self
.p
._o
_ready
& self
.stage
.d_ready
)
723 # intercept the next (incoming) "ready" and combine it with data valid
724 sdv
= self
.stage
.d_valid(self
.n
.i_ready
)
725 m
.d
.comb
+= self
.n
.d_valid
.eq(self
.n
.i_ready
& sdv
)
730 class BufferedHandshake(ControlBase
):
731 """ buffered pipeline stage. data and strobe signals travel in sync.
732 if ever the input is ready and the output is not, processed data
733 is shunted in a temporary register.
735 Argument: stage. see Stage API above
737 stage-1 p.i_valid >>in stage n.o_valid out>> stage+1
738 stage-1 p.o_ready <<out stage n.i_ready <<in stage+1
739 stage-1 p.i_data >>in stage n.o_data out>> stage+1
745 input data p.i_data is read (only), is processed and goes into an
746 intermediate result store [process()]. this is updated combinatorially.
748 in a non-stall condition, the intermediate result will go into the
749 output (update_output). however if ever there is a stall, it goes
750 into r_data instead [update_buffer()].
752 when the non-stall condition is released, r_data is the first
753 to be transferred to the output [flush_buffer()], and the stall
756 on the next cycle (as long as stall is not raised again) the
757 input may begin to be processed and transferred directly to output.
760 def elaborate(self
, platform
):
761 self
.m
= ControlBase
._elaborate
(self
, platform
)
763 result
= self
.stage
.ospec()
764 r_data
= self
.stage
.ospec()
766 # establish some combinatorial temporaries
767 o_n_validn
= Signal(reset_less
=True)
768 n_i_ready
= Signal(reset_less
=True, name
="n_i_rdy_data")
769 nir_por
= Signal(reset_less
=True)
770 nir_por_n
= Signal(reset_less
=True)
771 p_i_valid
= Signal(reset_less
=True)
772 nir_novn
= Signal(reset_less
=True)
773 nirn_novn
= Signal(reset_less
=True)
774 por_pivn
= Signal(reset_less
=True)
775 npnn
= Signal(reset_less
=True)
776 self
.m
.d
.comb
+= [p_i_valid
.eq(self
.p
.i_valid_test
),
777 o_n_validn
.eq(~self
.n
.o_valid
),
778 n_i_ready
.eq(self
.n
.i_ready_test
),
779 nir_por
.eq(n_i_ready
& self
.p
._o
_ready
),
780 nir_por_n
.eq(n_i_ready
& ~self
.p
._o
_ready
),
781 nir_novn
.eq(n_i_ready | o_n_validn
),
782 nirn_novn
.eq(~n_i_ready
& o_n_validn
),
783 npnn
.eq(nir_por | nirn_novn
),
784 por_pivn
.eq(self
.p
._o
_ready
& ~p_i_valid
)
787 # store result of processing in combinatorial temporary
788 self
.m
.d
.comb
+= eq(result
, self
.stage
.process(self
.p
.i_data
))
790 # if not in stall condition, update the temporary register
791 with self
.m
.If(self
.p
.o_ready
): # not stalled
792 self
.m
.d
.sync
+= eq(r_data
, result
) # update buffer
794 # data pass-through conditions
795 with self
.m
.If(npnn
):
796 o_data
= self
._postprocess
(result
)
797 self
.m
.d
.sync
+= [self
.n
.o_valid
.eq(p_i_valid
), # valid if p_valid
798 eq(self
.n
.o_data
, o_data
), # update output
800 # buffer flush conditions (NOTE: can override data passthru conditions)
801 with self
.m
.If(nir_por_n
): # not stalled
802 # Flush the [already processed] buffer to the output port.
803 o_data
= self
._postprocess
(r_data
)
804 self
.m
.d
.sync
+= [self
.n
.o_valid
.eq(1), # reg empty
805 eq(self
.n
.o_data
, o_data
), # flush buffer
807 # output ready conditions
808 self
.m
.d
.sync
+= self
.p
._o
_ready
.eq(nir_novn | por_pivn
)
813 class SimpleHandshake(ControlBase
):
814 """ simple handshake control. data and strobe signals travel in sync.
815 implements the protocol used by Wishbone and AXI4.
817 Argument: stage. see Stage API above
819 stage-1 p.i_valid >>in stage n.o_valid out>> stage+1
820 stage-1 p.o_ready <<out stage n.i_ready <<in stage+1
821 stage-1 p.i_data >>in stage n.o_data out>> stage+1
826 Inputs Temporary Output Data
827 ------- ---------- ----- ----
828 P P N N PiV& ~NiR& N P
835 0 0 1 0 0 0 0 1 process(i_data)
836 0 0 1 1 0 0 0 1 process(i_data)
840 0 1 1 0 0 0 0 1 process(i_data)
841 0 1 1 1 0 0 0 1 process(i_data)
845 1 0 1 0 0 0 0 1 process(i_data)
846 1 0 1 1 0 0 0 1 process(i_data)
848 1 1 0 0 1 0 1 0 process(i_data)
849 1 1 0 1 1 1 1 0 process(i_data)
850 1 1 1 0 1 0 1 1 process(i_data)
851 1 1 1 1 1 0 1 1 process(i_data)
855 def elaborate(self
, platform
):
856 self
.m
= m
= ControlBase
._elaborate
(self
, platform
)
859 result
= self
.stage
.ospec()
861 # establish some combinatorial temporaries
862 n_i_ready
= Signal(reset_less
=True, name
="n_i_rdy_data")
863 p_i_valid_p_o_ready
= Signal(reset_less
=True)
864 p_i_valid
= Signal(reset_less
=True)
865 m
.d
.comb
+= [p_i_valid
.eq(self
.p
.i_valid_test
),
866 n_i_ready
.eq(self
.n
.i_ready_test
),
867 p_i_valid_p_o_ready
.eq(p_i_valid
& self
.p
.o_ready
),
870 # store result of processing in combinatorial temporary
871 m
.d
.comb
+= eq(result
, self
.stage
.process(self
.p
.i_data
))
873 # previous valid and ready
874 with m
.If(p_i_valid_p_o_ready
):
875 o_data
= self
._postprocess
(result
)
876 m
.d
.sync
+= [r_busy
.eq(1), # output valid
877 eq(self
.n
.o_data
, o_data
), # update output
879 # previous invalid or not ready, however next is accepting
880 with m
.Elif(n_i_ready
):
881 o_data
= self
._postprocess
(result
)
882 m
.d
.sync
+= [eq(self
.n
.o_data
, o_data
)]
883 # TODO: could still send data here (if there was any)
884 #m.d.sync += self.n.o_valid.eq(0) # ...so set output invalid
885 m
.d
.sync
+= r_busy
.eq(0) # ...so set output invalid
887 m
.d
.comb
+= self
.n
.o_valid
.eq(r_busy
)
888 # if next is ready, so is previous
889 m
.d
.comb
+= self
.p
._o
_ready
.eq(n_i_ready
)
894 class UnbufferedPipeline(ControlBase
):
895 """ A simple pipeline stage with single-clock synchronisation
896 and two-way valid/ready synchronised signalling.
898 Note that a stall in one stage will result in the entire pipeline
901 Also that unlike BufferedHandshake, the valid/ready signalling does NOT
902 travel synchronously with the data: the valid/ready signalling
903 combines in a *combinatorial* fashion. Therefore, a long pipeline
904 chain will lengthen propagation delays.
906 Argument: stage. see Stage API, above
908 stage-1 p.i_valid >>in stage n.o_valid out>> stage+1
909 stage-1 p.o_ready <<out stage n.i_ready <<in stage+1
910 stage-1 p.i_data >>in stage n.o_data out>> stage+1
918 p.i_data : StageInput, shaped according to ispec
920 p.o_data : StageOutput, shaped according to ospec
922 r_data : input_shape according to ispec
923 A temporary (buffered) copy of a prior (valid) input.
924 This is HELD if the output is not ready. It is updated
926 result: output_shape according to ospec
927 The output of the combinatorial logic. it is updated
928 COMBINATORIALLY (no clock dependence).
932 Inputs Temp Output Data
954 1 1 0 0 0 1 1 process(i_data)
955 1 1 0 1 1 1 0 process(i_data)
956 1 1 1 0 0 1 1 process(i_data)
957 1 1 1 1 0 1 1 process(i_data)
960 Note: PoR is *NOT* involved in the above decision-making.
963 def elaborate(self
, platform
):
964 self
.m
= m
= ControlBase
._elaborate
(self
, platform
)
966 data_valid
= Signal() # is data valid or not
967 r_data
= self
.stage
.ospec() # output type
970 p_i_valid
= Signal(reset_less
=True)
971 pv
= Signal(reset_less
=True)
972 buf_full
= Signal(reset_less
=True)
973 m
.d
.comb
+= p_i_valid
.eq(self
.p
.i_valid_test
)
974 m
.d
.comb
+= pv
.eq(self
.p
.i_valid
& self
.p
.o_ready
)
975 m
.d
.comb
+= buf_full
.eq(~self
.n
.i_ready_test
& data_valid
)
977 m
.d
.comb
+= self
.n
.o_valid
.eq(data_valid
)
978 m
.d
.comb
+= self
.p
._o
_ready
.eq(~data_valid | self
.n
.i_ready_test
)
979 m
.d
.sync
+= data_valid
.eq(p_i_valid | buf_full
)
982 m
.d
.sync
+= eq(r_data
, self
.stage
.process(self
.p
.i_data
))
983 o_data
= self
._postprocess
(r_data
)
984 m
.d
.comb
+= eq(self
.n
.o_data
, o_data
)
988 class UnbufferedPipeline2(ControlBase
):
989 """ A simple pipeline stage with single-clock synchronisation
990 and two-way valid/ready synchronised signalling.
992 Note that a stall in one stage will result in the entire pipeline
995 Also that unlike BufferedHandshake, the valid/ready signalling does NOT
996 travel synchronously with the data: the valid/ready signalling
997 combines in a *combinatorial* fashion. Therefore, a long pipeline
998 chain will lengthen propagation delays.
1000 Argument: stage. see Stage API, above
1002 stage-1 p.i_valid >>in stage n.o_valid out>> stage+1
1003 stage-1 p.o_ready <<out stage n.i_ready <<in stage+1
1004 stage-1 p.i_data >>in stage n.o_data out>> stage+1
1006 +- process-> buf <-+
1009 p.i_data : StageInput, shaped according to ispec
1011 p.o_data : StageOutput, shaped according to ospec
1013 buf : output_shape according to ospec
1014 A temporary (buffered) copy of a valid output
1015 This is HELD if the output is not ready. It is updated
1018 Inputs Temp Output Data
1020 P P N N ~NiR& N P (buf_full)
1025 0 0 0 0 0 0 1 process(i_data)
1026 0 0 0 1 1 1 0 reg (odata, unchanged)
1027 0 0 1 0 0 0 1 process(i_data)
1028 0 0 1 1 0 0 1 process(i_data)
1030 0 1 0 0 0 0 1 process(i_data)
1031 0 1 0 1 1 1 0 reg (odata, unchanged)
1032 0 1 1 0 0 0 1 process(i_data)
1033 0 1 1 1 0 0 1 process(i_data)
1035 1 0 0 0 0 1 1 process(i_data)
1036 1 0 0 1 1 1 0 reg (odata, unchanged)
1037 1 0 1 0 0 1 1 process(i_data)
1038 1 0 1 1 0 1 1 process(i_data)
1040 1 1 0 0 0 1 1 process(i_data)
1041 1 1 0 1 1 1 0 reg (odata, unchanged)
1042 1 1 1 0 0 1 1 process(i_data)
1043 1 1 1 1 0 1 1 process(i_data)
1046 Note: PoR is *NOT* involved in the above decision-making.
1049 def elaborate(self
, platform
):
1050 self
.m
= m
= ControlBase
._elaborate
(self
, platform
)
1052 buf_full
= Signal() # is data valid or not
1053 buf
= self
.stage
.ospec() # output type
1056 p_i_valid
= Signal(reset_less
=True)
1057 m
.d
.comb
+= p_i_valid
.eq(self
.p
.i_valid_test
)
1059 m
.d
.comb
+= self
.n
.o_valid
.eq(buf_full | p_i_valid
)
1060 m
.d
.comb
+= self
.p
._o
_ready
.eq(~buf_full
)
1061 m
.d
.sync
+= buf_full
.eq(~self
.n
.i_ready_test
& self
.n
.o_valid
)
1063 o_data
= Mux(buf_full
, buf
, self
.stage
.process(self
.p
.i_data
))
1064 o_data
= self
._postprocess
(o_data
)
1065 m
.d
.comb
+= eq(self
.n
.o_data
, o_data
)
1066 m
.d
.sync
+= eq(buf
, self
.n
.o_data
)
1071 class PassThroughStage(StageCls
):
1072 """ a pass-through stage which has its input data spec equal to its output,
1073 and "passes through" its data from input to output.
1075 def __init__(self
, iospecfn
):
1076 self
.iospecfn
= iospecfn
1077 def ispec(self
): return self
.iospecfn()
1078 def ospec(self
): return self
.iospecfn()
1079 def process(self
, i
): return i
1082 class PassThroughHandshake(ControlBase
):
1083 """ A control block that delays by one clock cycle.
1085 Inputs Temporary Output Data
1086 ------- ------------------ ----- ----
1087 P P N N PiV& PiV| NiR| pvr N P (pvr)
1088 i o i o PoR ~PoR ~NoV o o
1092 0 0 0 0 0 1 1 0 1 1 odata (unchanged)
1093 0 0 0 1 0 1 0 0 1 0 odata (unchanged)
1094 0 0 1 0 0 1 1 0 1 1 odata (unchanged)
1095 0 0 1 1 0 1 1 0 1 1 odata (unchanged)
1097 0 1 0 0 0 0 1 0 0 1 odata (unchanged)
1098 0 1 0 1 0 0 0 0 0 0 odata (unchanged)
1099 0 1 1 0 0 0 1 0 0 1 odata (unchanged)
1100 0 1 1 1 0 0 1 0 0 1 odata (unchanged)
1102 1 0 0 0 0 1 1 1 1 1 process(in)
1103 1 0 0 1 0 1 0 0 1 0 odata (unchanged)
1104 1 0 1 0 0 1 1 1 1 1 process(in)
1105 1 0 1 1 0 1 1 1 1 1 process(in)
1107 1 1 0 0 1 1 1 1 1 1 process(in)
1108 1 1 0 1 1 1 0 0 1 0 odata (unchanged)
1109 1 1 1 0 1 1 1 1 1 1 process(in)
1110 1 1 1 1 1 1 1 1 1 1 process(in)
1115 def elaborate(self
, platform
):
1116 self
.m
= m
= ControlBase
._elaborate
(self
, platform
)
1118 r_data
= self
.stage
.ospec() # output type
1121 p_i_valid
= Signal(reset_less
=True)
1122 pvr
= Signal(reset_less
=True)
1123 m
.d
.comb
+= p_i_valid
.eq(self
.p
.i_valid_test
)
1124 m
.d
.comb
+= pvr
.eq(p_i_valid
& self
.p
.o_ready
)
1126 m
.d
.comb
+= self
.p
.o_ready
.eq(~self
.n
.o_valid | self
.n
.i_ready_test
)
1127 m
.d
.sync
+= self
.n
.o_valid
.eq(p_i_valid | ~self
.p
.o_ready
)
1129 odata
= Mux(pvr
, self
.stage
.process(self
.p
.i_data
), r_data
)
1130 m
.d
.sync
+= eq(r_data
, odata
)
1131 r_data
= self
._postprocess
(r_data
)
1132 m
.d
.comb
+= eq(self
.n
.o_data
, r_data
)
1137 class RegisterPipeline(UnbufferedPipeline
):
1138 """ A pipeline stage that delays by one clock cycle, creating a
1139 sync'd latch out of o_data and o_valid as an indirect byproduct
1140 of using PassThroughStage
1142 def __init__(self
, iospecfn
):
1143 UnbufferedPipeline
.__init
__(self
, PassThroughStage(iospecfn
))
1146 class FIFOControl(ControlBase
):
1147 """ FIFO Control. Uses SyncFIFO to store data, coincidentally
1148 happens to have same valid/ready signalling as Stage API.
1150 i_data -> fifo.din -> FIFO -> fifo.dout -> o_data
1153 def __init__(self
, depth
, stage
, in_multi
=None, stage_ctl
=False,
1154 fwft
=True, buffered
=False, pipe
=False):
1157 * depth: number of entries in the FIFO
1158 * stage: data processing block
1159 * fwft : first word fall-thru mode (non-fwft introduces delay)
1160 * buffered: use buffered FIFO (introduces extra cycle delay)
1162 NOTE 1: FPGAs may have trouble with the defaults for SyncFIFO
1163 (fwft=True, buffered=False)
1165 NOTE 2: i_data *must* have a shape function. it can therefore
1166 be a Signal, or a Record, or a RecordObject.
1168 data is processed (and located) as follows:
1170 self.p self.stage temp fn temp fn temp fp self.n
1171 i_data->process()->result->cat->din.FIFO.dout->cat(o_data)
1173 yes, really: cat produces a Cat() which can be assigned to.
1174 this is how the FIFO gets de-catted without needing a de-cat
1178 assert not (fwft
and buffered
), "buffered cannot do fwft"
1182 self
.buffered
= buffered
1185 ControlBase
.__init
__(self
, stage
, in_multi
, stage_ctl
)
1187 def elaborate(self
, platform
):
1188 self
.m
= m
= ControlBase
._elaborate
(self
, platform
)
1190 # make a FIFO with a signal of equal width to the o_data.
1191 (fwidth
, _
) = shape(self
.n
.o_data
)
1193 fifo
= SyncFIFOBuffered(fwidth
, self
.fdepth
)
1195 fifo
= Queue(fwidth
, self
.fdepth
, fwft
=self
.fwft
, pipe
=self
.pipe
)
1196 m
.submodules
.fifo
= fifo
1198 # store result of processing in combinatorial temporary
1199 result
= self
.stage
.ospec()
1200 m
.d
.comb
+= eq(result
, self
.stage
.process(self
.p
.i_data
))
1202 # connect previous rdy/valid/data - do cat on i_data
1203 # NOTE: cannot do the PrevControl-looking trick because
1204 # of need to process the data. shaaaame....
1205 m
.d
.comb
+= [fifo
.we
.eq(self
.p
.i_valid_test
),
1206 self
.p
.o_ready
.eq(fifo
.writable
),
1207 eq(fifo
.din
, cat(result
)),
1210 # connect next rdy/valid/data - do cat on o_data
1211 connections
= [self
.n
.o_valid
.eq(fifo
.readable
),
1212 fifo
.re
.eq(self
.n
.i_ready_test
),
1214 if self
.fwft
or self
.buffered
:
1215 m
.d
.comb
+= connections
1217 m
.d
.sync
+= connections
# unbuffered fwft mode needs sync
1218 o_data
= cat(self
.n
.o_data
).eq(fifo
.dout
)
1219 o_data
= self
._postprocess
(o_data
)
1226 class UnbufferedPipeline(FIFOControl
):
1227 def __init__(self
, stage
, in_multi
=None, stage_ctl
=False):
1228 FIFOControl
.__init
__(self
, 1, stage
, in_multi
, stage_ctl
,
1229 fwft
=True, pipe
=False)
1231 # aka "BreakReadyStage" XXX had to set fwft=True to get it to work
1232 class PassThroughHandshake(FIFOControl
):
1233 def __init__(self
, stage
, in_multi
=None, stage_ctl
=False):
1234 FIFOControl
.__init
__(self
, 1, stage
, in_multi
, stage_ctl
,
1235 fwft
=True, pipe
=True)
1237 # this is *probably* BufferedHandshake, although test #997 now succeeds.
1238 class BufferedHandshake(FIFOControl
):
1239 def __init__(self
, stage
, in_multi
=None, stage_ctl
=False):
1240 FIFOControl
.__init
__(self
, 2, stage
, in_multi
, stage_ctl
,
1241 fwft
=True, pipe
=False)
1244 # this is *probably* SimpleHandshake (note: memory cell size=0)
1245 class SimpleHandshake(FIFOControl
):
1246 def __init__(self
, stage
, in_multi
=None, stage_ctl
=False):
1247 FIFOControl
.__init
__(self
, 0, stage
, in_multi
, stage_ctl
,
1248 fwft
=True, pipe
=False)