1 """ Unit tests for Buffered and Unbuffered pipelines
3 contains useful worked examples of how to use the Pipeline API,
6 * Combinatorial Stage "Chaining"
7 * class-based data stages
8 * nmigen module-based data stages
9 * special nmigen module-based data stage, where the stage *is* the module
10 * Record-based data stages
11 * static-class data stages
12 * multi-stage pipelines (and how to connect them)
13 * how to *use* the pipelines (see Test5) - how to get data in and out
17 from nmigen
import Module
, Signal
, Mux
18 from nmigen
.hdl
.rec
import Record
19 from nmigen
.compat
.sim
import run_simulation
20 from nmigen
.cli
import verilog
, rtlil
22 from example_buf_pipe
import ExampleBufPipe
, ExampleBufPipeAdd
23 from example_buf_pipe
import ExamplePipeline
, UnbufferedPipeline
24 from example_buf_pipe
import ExampleStageCls
25 from example_buf_pipe
import PrevControl
, NextControl
, BufferedPipeline
26 from example_buf_pipe
import StageChain
, ControlBase
, StageCls
28 from random
import randint
31 def check_o_n_valid(dut
, val
):
32 o_n_valid
= yield dut
.n
.o_valid
33 assert o_n_valid
== val
35 def check_o_n_valid2(dut
, val
):
36 o_n_valid
= yield dut
.n
.o_valid
37 assert o_n_valid
== val
41 #yield dut.i_p_rst.eq(1)
42 yield dut
.n
.i_ready
.eq(0)
43 yield dut
.p
.o_ready
.eq(0)
46 #yield dut.i_p_rst.eq(0)
47 yield dut
.n
.i_ready
.eq(1)
48 yield dut
.p
.i_data
.eq(5)
49 yield dut
.p
.i_valid
.eq(1)
52 yield dut
.p
.i_data
.eq(7)
53 yield from check_o_n_valid(dut
, 0) # effects of i_p_valid delayed
55 yield from check_o_n_valid(dut
, 1) # ok *now* i_p_valid effect is felt
57 yield dut
.p
.i_data
.eq(2)
59 yield dut
.n
.i_ready
.eq(0) # begin going into "stall" (next stage says ready)
60 yield dut
.p
.i_data
.eq(9)
62 yield dut
.p
.i_valid
.eq(0)
63 yield dut
.p
.i_data
.eq(12)
65 yield dut
.p
.i_data
.eq(32)
66 yield dut
.n
.i_ready
.eq(1)
68 yield from check_o_n_valid(dut
, 1) # buffer still needs to output
70 yield from check_o_n_valid(dut
, 1) # buffer still needs to output
72 yield from check_o_n_valid(dut
, 0) # buffer outputted, *now* we're done.
77 #yield dut.p.i_rst.eq(1)
78 yield dut
.n
.i_ready
.eq(0)
79 #yield dut.p.o_ready.eq(0)
82 #yield dut.p.i_rst.eq(0)
83 yield dut
.n
.i_ready
.eq(1)
84 yield dut
.p
.i_data
.eq(5)
85 yield dut
.p
.i_valid
.eq(1)
88 yield dut
.p
.i_data
.eq(7)
89 yield from check_o_n_valid2(dut
, 0) # effects of i_p_valid delayed 2 clocks
91 yield from check_o_n_valid2(dut
, 0) # effects of i_p_valid delayed 2 clocks
93 yield dut
.p
.i_data
.eq(2)
95 yield from check_o_n_valid2(dut
, 1) # ok *now* i_p_valid effect is felt
96 yield dut
.n
.i_ready
.eq(0) # begin going into "stall" (next stage says ready)
97 yield dut
.p
.i_data
.eq(9)
99 yield dut
.p
.i_valid
.eq(0)
100 yield dut
.p
.i_data
.eq(12)
102 yield dut
.p
.i_data
.eq(32)
103 yield dut
.n
.i_ready
.eq(1)
105 yield from check_o_n_valid2(dut
, 1) # buffer still needs to output
107 yield from check_o_n_valid2(dut
, 1) # buffer still needs to output
109 yield from check_o_n_valid2(dut
, 1) # buffer still needs to output
111 yield from check_o_n_valid2(dut
, 0) # buffer outputted, *now* we're done.
118 def __init__(self
, dut
, resultfn
):
120 self
.resultfn
= resultfn
122 for i
in range(num_tests
):
123 #data.append(randint(0, 1<<16-1))
124 self
.data
.append(i
+1)
129 while self
.o
!= len(self
.data
):
130 send_range
= randint(0, 3)
131 for j
in range(randint(1,10)):
135 send
= randint(0, send_range
) != 0
136 o_p_ready
= yield self
.dut
.p
.o_ready
140 if send
and self
.i
!= len(self
.data
):
141 yield self
.dut
.p
.i_valid
.eq(1)
142 yield self
.dut
.p
.i_data
.eq(self
.data
[self
.i
])
145 yield self
.dut
.p
.i_valid
.eq(0)
149 while self
.o
!= len(self
.data
):
150 stall_range
= randint(0, 3)
151 for j
in range(randint(1,10)):
152 stall
= randint(0, stall_range
) != 0
153 yield self
.dut
.n
.i_ready
.eq(stall
)
155 o_n_valid
= yield self
.dut
.n
.o_valid
156 i_n_ready
= yield self
.dut
.n
.i_ready
157 if not o_n_valid
or not i_n_ready
:
159 o_data
= yield self
.dut
.n
.o_data
160 self
.resultfn(o_data
, self
.data
[self
.o
], self
.i
, self
.o
)
162 if self
.o
== len(self
.data
):
165 def test3_resultfn(o_data
, expected
, i
, o
):
166 assert o_data
== expected
+ 1, \
167 "%d-%d data %x not match %x\n" \
168 % (i
, o
, o_data
, expected
)
170 def data_placeholder():
172 for i
in range(num_tests
):
174 d
.src1
= randint(0, 1<<16-1)
175 d
.src2
= randint(0, 1<<16-1)
181 for i
in range(num_tests
):
182 data
.append({'src1': randint(0, 1<<16-1),
183 'src2': randint(0, 1<<16-1)})
188 def __init__(self
, dut
, resultfn
, data
=None):
190 self
.resultfn
= resultfn
195 for i
in range(num_tests
):
196 self
.data
.append((randint(0, 1<<16-1), randint(0, 1<<16-1)))
201 while self
.o
!= len(self
.data
):
202 send_range
= randint(0, 3)
203 for j
in range(randint(1,10)):
207 send
= randint(0, send_range
) != 0
208 o_p_ready
= yield self
.dut
.p
.o_ready
212 if send
and self
.i
!= len(self
.data
):
213 yield self
.dut
.p
.i_valid
.eq(1)
214 for v
in self
.dut
.set_input(self
.data
[self
.i
]):
218 yield self
.dut
.p
.i_valid
.eq(0)
222 while self
.o
!= len(self
.data
):
223 stall_range
= randint(0, 3)
224 for j
in range(randint(1,10)):
225 stall
= randint(0, stall_range
) != 0
226 yield self
.dut
.n
.i_ready
.eq(stall
)
228 o_n_valid
= yield self
.dut
.n
.o_valid
229 i_n_ready
= yield self
.dut
.n
.i_ready
230 if not o_n_valid
or not i_n_ready
:
232 if isinstance(self
.dut
.n
.o_data
, Record
):
234 dod
= self
.dut
.n
.o_data
235 for k
, v
in dod
.fields
.items():
238 o_data
= yield self
.dut
.n
.o_data
239 self
.resultfn(o_data
, self
.data
[self
.o
], self
.i
, self
.o
)
241 if self
.o
== len(self
.data
):
244 def test5_resultfn(o_data
, expected
, i
, o
):
245 res
= expected
[0] + expected
[1]
246 assert o_data
== res
, \
247 "%d-%d data %x not match %s\n" \
248 % (i
, o
, o_data
, repr(expected
))
252 for i
in range(num_tests
):
253 #data.append(randint(0, 1<<16-1))
258 stall
= randint(0, 3) != 0
259 send
= randint(0, 5) != 0
260 yield dut
.n
.i_ready
.eq(stall
)
261 o_p_ready
= yield dut
.p
.o_ready
263 if send
and i
!= len(data
):
264 yield dut
.p
.i_valid
.eq(1)
265 yield dut
.p
.i_data
.eq(data
[i
])
268 yield dut
.p
.i_valid
.eq(0)
270 o_n_valid
= yield dut
.n
.o_valid
271 i_n_ready
= yield dut
.n
.i_ready
272 if o_n_valid
and i_n_ready
:
273 o_data
= yield dut
.n
.o_data
274 assert o_data
== data
[o
] + 2, "%d-%d data %x not match %x\n" \
275 % (i
, o
, o_data
, data
[o
])
280 ######################################################################
282 ######################################################################
284 class ExampleBufPipe2(ControlBase
):
286 connect these: ------|---------------|
288 i_p_valid >>in pipe1 o_n_valid out>> i_p_valid >>in pipe2
289 o_p_ready <<out pipe1 i_n_ready <<in o_p_ready <<out pipe2
290 p_i_data >>in pipe1 p_i_data out>> n_o_data >>in pipe2
293 ControlBase
.__init
__(self
)
296 self
.p
.i_data
= Signal(32) # >>in - comes in from the PREVIOUS stage
297 self
.n
.o_data
= Signal(32) # out>> - goes out to the NEXT stage
299 self
.pipe1
= ExampleBufPipe()
300 self
.pipe2
= ExampleBufPipe()
302 def elaborate(self
, platform
):
304 m
.submodules
.pipe1
= self
.pipe1
305 m
.submodules
.pipe2
= self
.pipe2
307 # connect inter-pipe input/output valid/ready/data
308 m
.d
.comb
+= self
.pipe1
.connect_to_next(self
.pipe2
)
310 # inputs/outputs to the module: pipe1 connections here (LHS)
311 m
.d
.comb
+= self
.pipe1
.connect_in(self
)
313 # now pipe2 connections (RHS)
314 m
.d
.comb
+= self
.pipe2
.connect_out(self
)
319 class ExampleBufPipeChain2(BufferedPipeline
):
320 """ connects two stages together as a *single* combinatorial stage.
323 stage1
= ExampleStageCls()
324 stage2
= ExampleStageCls()
325 combined
= StageChain([stage1
, stage2
])
326 BufferedPipeline
.__init
__(self
, combined
)
331 for i
in range(num_tests
):
332 data
.append(randint(0, 1<<16-2))
336 def test9_resultfn(o_data
, expected
, i
, o
):
338 assert o_data
== res
, \
339 "%d-%d data %x not match %s\n" \
340 % (i
, o
, o_data
, repr(expected
))
343 ######################################################################
345 ######################################################################
348 def __init__(self
, width
, signed
):
349 self
.src1
= Signal((width
, signed
))
350 self
.src2
= Signal((width
, signed
))
351 self
.output
= Signal(width
)
353 def elaborate(self
, platform
):
355 m
.d
.comb
+= self
.output
.eq(Mux(self
.src1
< self
.src2
, 1, 0))
359 class LTStage(StageCls
):
360 """ module-based stage example
363 self
.slt
= SetLessThan(16, True)
366 return (Signal(16), Signal(16))
371 def setup(self
, m
, i
):
373 m
.submodules
.slt
= self
.slt
374 m
.d
.comb
+= self
.slt
.src1
.eq(i
[0])
375 m
.d
.comb
+= self
.slt
.src2
.eq(i
[1])
376 m
.d
.comb
+= self
.o
.eq(self
.slt
.output
)
378 def process(self
, i
):
382 class LTStageDerived(SetLessThan
, StageCls
):
383 """ special version of a nmigen module where the module is also a stage
385 shows that you don't actually need to combinatorially connect
386 to the outputs, or add the module as a submodule: just return
387 the module output parameter(s) from the Stage.process() function
391 SetLessThan
.__init
__(self
, 16, True)
394 return (Signal(16), Signal(16))
399 def setup(self
, m
, i
):
400 m
.submodules
.slt
= self
401 m
.d
.comb
+= self
.src1
.eq(i
[0])
402 m
.d
.comb
+= self
.src2
.eq(i
[1])
404 def process(self
, i
):
408 class ExampleLTPipeline(UnbufferedPipeline
):
409 """ an example of how to use the unbuffered pipeline.
414 UnbufferedPipeline
.__init
__(self
, stage
)
417 class ExampleLTBufferedPipeDerived(BufferedPipeline
):
418 """ an example of how to use the buffered pipeline.
422 stage
= LTStageDerived()
423 BufferedPipeline
.__init
__(self
, stage
)
426 def test6_resultfn(o_data
, expected
, i
, o
):
427 res
= 1 if expected
[0] < expected
[1] else 0
428 assert o_data
== res
, \
429 "%d-%d data %x not match %s\n" \
430 % (i
, o
, o_data
, repr(expected
))
433 ######################################################################
435 ######################################################################
437 class ExampleAddRecordStage(StageCls
):
438 """ example use of a Record
441 record_spec
= [('src1', 16), ('src2', 16)]
443 """ returns a Record using the specification
445 return Record(self
.record_spec
)
448 return Record(self
.record_spec
)
450 def process(self
, i
):
451 """ process the input data, returning a dictionary with key names
452 that exactly match the Record's attributes.
454 return {'src1': i
.src1
+ 1,
457 ######################################################################
459 ######################################################################
461 class ExampleAddRecordPlaceHolderStage(StageCls
):
462 """ example use of a Record, with a placeholder as the processing result
465 record_spec
= [('src1', 16), ('src2', 16)]
467 """ returns a Record using the specification
469 return Record(self
.record_spec
)
472 return Record(self
.record_spec
)
474 def process(self
, i
):
475 """ process the input data, returning a PlaceHolder class instance
476 with attributes that exactly match those of the Record.
484 class PlaceHolder
: pass
487 class ExampleAddRecordPipe(UnbufferedPipeline
):
488 """ an example of how to use the combinatorial pipeline.
492 stage
= ExampleAddRecordStage()
493 UnbufferedPipeline
.__init
__(self
, stage
)
496 def test7_resultfn(o_data
, expected
, i
, o
):
497 res
= (expected
['src1'] + 1, expected
['src2'] + 1)
498 assert o_data
['src1'] == res
[0] and o_data
['src2'] == res
[1], \
499 "%d-%d data %s not match %s\n" \
500 % (i
, o
, repr(o_data
), repr(expected
))
503 class ExampleAddRecordPlaceHolderPipe(UnbufferedPipeline
):
504 """ an example of how to use the combinatorial pipeline.
508 stage
= ExampleAddRecordPlaceHolderStage()
509 UnbufferedPipeline
.__init
__(self
, stage
)
512 def test11_resultfn(o_data
, expected
, i
, o
):
513 res1
= expected
.src1
+ 1
514 res2
= expected
.src2
+ 1
515 assert o_data
['src1'] == res1
and o_data
['src2'] == res2
, \
516 "%d-%d data %s not match %s\n" \
517 % (i
, o
, repr(o_data
), repr(expected
))
520 ######################################################################
522 ######################################################################
525 class Example2OpClass
:
526 """ an example of a class used to store 2 operands.
527 requires an eq function, to conform with the pipeline stage API
531 self
.op1
= Signal(16)
532 self
.op2
= Signal(16)
535 return [self
.op1
.eq(i
.op1
), self
.op2
.eq(i
.op2
)]
538 class ExampleAddClassStage(StageCls
):
539 """ an example of how to use the buffered pipeline, as a class instance
543 """ returns an instance of an Example2OpClass.
545 return Example2OpClass()
548 """ returns an output signal which will happen to contain the sum
553 def process(self
, i
):
554 """ process the input data (sums the values in the tuple) and returns it
559 class ExampleBufPipeAddClass(BufferedPipeline
):
560 """ an example of how to use the buffered pipeline, using a class instance
564 addstage
= ExampleAddClassStage()
565 BufferedPipeline
.__init
__(self
, addstage
)
569 """ the eq function, called by set_input, needs an incoming object
570 that conforms to the Example2OpClass.eq function requirements
571 easiest way to do that is to create a class that has the exact
572 same member layout (self.op1, self.op2) as Example2OpClass
574 def __init__(self
, op1
, op2
):
579 def test8_resultfn(o_data
, expected
, i
, o
):
580 res
= expected
.op1
+ expected
.op2
# these are a TestInputAdd instance
581 assert o_data
== res
, \
582 "%d-%d data %x not match %s\n" \
583 % (i
, o
, o_data
, repr(expected
))
587 for i
in range(num_tests
):
588 data
.append(TestInputAdd(randint(0, 1<<16-1), randint(0, 1<<16-1)))
594 if __name__
== '__main__':
596 dut
= ExampleBufPipe()
597 run_simulation(dut
, testbench(dut
), vcd_name
="test_bufpipe.vcd")
600 dut
= ExampleBufPipe2()
601 run_simulation(dut
, testbench2(dut
), vcd_name
="test_bufpipe2.vcd")
604 dut
= ExampleBufPipe()
605 test
= Test3(dut
, test3_resultfn
)
606 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_bufpipe3.vcd")
609 dut
= ExamplePipeline()
610 test
= Test3(dut
, test3_resultfn
)
611 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_combpipe3.vcd")
614 dut
= ExampleBufPipe2()
615 run_simulation(dut
, testbench4(dut
), vcd_name
="test_bufpipe4.vcd")
618 dut
= ExampleBufPipeAdd()
619 test
= Test5(dut
, test5_resultfn
)
620 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_bufpipe5.vcd")
623 dut
= ExampleLTPipeline()
624 test
= Test5(dut
, test6_resultfn
)
625 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_ltcomb6.vcd")
627 ports
= [dut
.p
.i_valid
, dut
.n
.i_ready
,
628 dut
.n
.o_valid
, dut
.p
.o_ready
] + \
629 list(dut
.p
.i_data
) + [dut
.n
.o_data
]
630 vl
= rtlil
.convert(dut
, ports
=ports
)
631 with
open("test_ltcomb_pipe.il", "w") as f
:
635 dut
= ExampleAddRecordPipe()
637 test
= Test5(dut
, test7_resultfn
, data
=data
)
638 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_addrecord.vcd")
640 ports
= [dut
.p
.i_valid
, dut
.n
.i_ready
,
641 dut
.n
.o_valid
, dut
.p
.o_ready
,
642 dut
.p
.i_data
.src1
, dut
.p
.i_data
.src2
,
643 dut
.n
.o_data
.src1
, dut
.n
.o_data
.src2
]
644 vl
= rtlil
.convert(dut
, ports
=ports
)
645 with
open("test_recordcomb_pipe.il", "w") as f
:
649 dut
= ExampleBufPipeAddClass()
651 test
= Test5(dut
, test8_resultfn
, data
=data
)
652 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_bufpipe8.vcd")
655 dut
= ExampleBufPipeChain2()
656 ports
= [dut
.p
.i_valid
, dut
.n
.i_ready
,
657 dut
.n
.o_valid
, dut
.p
.o_ready
] + \
658 [dut
.p
.i_data
] + [dut
.n
.o_data
]
659 vl
= rtlil
.convert(dut
, ports
=ports
)
660 with
open("test_bufpipechain2.il", "w") as f
:
664 test
= Test5(dut
, test9_resultfn
, data
=data
)
665 run_simulation(dut
, [test
.send
, test
.rcv
],
666 vcd_name
="test_bufpipechain2.vcd")
669 dut
= ExampleLTBufferedPipeDerived()
670 test
= Test5(dut
, test6_resultfn
)
671 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_ltbufpipe10.vcd")
672 vl
= rtlil
.convert(dut
, ports
=ports
)
673 with
open("test_ltbufpipe10.il", "w") as f
:
677 dut
= ExampleAddRecordPlaceHolderPipe()
678 data
=data_placeholder()
679 test
= Test5(dut
, test11_resultfn
, data
=data
)
680 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_addrecord.vcd")