1 from nmigen
import Module
, Signal
, Mux
2 from nmigen
.hdl
.rec
import Record
3 from nmigen
.compat
.sim
import run_simulation
4 from nmigen
.cli
import verilog
, rtlil
6 from example_buf_pipe
import ExampleBufPipe
, ExampleBufPipeAdd
7 from example_buf_pipe
import ExamplePipeline
, UnbufferedPipeline
8 from example_buf_pipe
import ExampleStageCls
9 from example_buf_pipe
import PrevControl
, NextControl
, BufferedPipeline
10 from example_buf_pipe
import StageChain
12 from random
import randint
15 def check_o_n_valid(dut
, val
):
16 o_n_valid
= yield dut
.n
[0].o_valid
17 assert o_n_valid
== val
19 def check_o_n_valid2(dut
, val
):
20 o_n_valid
= yield dut
.n
.o_valid
21 assert o_n_valid
== val
25 #yield dut.i_p_rst.eq(1)
26 yield dut
.n
[0].i_ready
.eq(0)
27 yield dut
.p
[0].o_ready
.eq(0)
30 #yield dut.i_p_rst.eq(0)
31 yield dut
.n
[0].i_ready
.eq(1)
32 yield dut
.p
[0].i_data
.eq(5)
33 yield dut
.p
[0].i_valid
.eq(1)
36 yield dut
.p
[0].i_data
.eq(7)
37 yield from check_o_n_valid(dut
, 0) # effects of i_p_valid delayed
39 yield from check_o_n_valid(dut
, 1) # ok *now* i_p_valid effect is felt
41 yield dut
.p
[0].i_data
.eq(2)
43 yield dut
.n
[0].i_ready
.eq(0) # begin going into "stall" (next stage says ready)
44 yield dut
.p
[0].i_data
.eq(9)
46 yield dut
.p
[0].i_valid
.eq(0)
47 yield dut
.p
[0].i_data
.eq(12)
49 yield dut
.p
[0].i_data
.eq(32)
50 yield dut
.n
[0].i_ready
.eq(1)
52 yield from check_o_n_valid(dut
, 1) # buffer still needs to output
54 yield from check_o_n_valid(dut
, 1) # buffer still needs to output
56 yield from check_o_n_valid(dut
, 0) # buffer outputted, *now* we're done.
61 #yield dut.p.i_rst.eq(1)
62 yield dut
.n
.i_ready
.eq(0)
63 #yield dut.p.o_ready.eq(0)
66 #yield dut.p.i_rst.eq(0)
67 yield dut
.n
.i_ready
.eq(1)
68 yield dut
.p
.i_data
.eq(5)
69 yield dut
.p
.i_valid
.eq(1)
72 yield dut
.p
.i_data
.eq(7)
73 yield from check_o_n_valid2(dut
, 0) # effects of i_p_valid delayed 2 clocks
75 yield from check_o_n_valid2(dut
, 0) # effects of i_p_valid delayed 2 clocks
77 yield dut
.p
.i_data
.eq(2)
79 yield from check_o_n_valid2(dut
, 1) # ok *now* i_p_valid effect is felt
80 yield dut
.n
.i_ready
.eq(0) # begin going into "stall" (next stage says ready)
81 yield dut
.p
.i_data
.eq(9)
83 yield dut
.p
.i_valid
.eq(0)
84 yield dut
.p
.i_data
.eq(12)
86 yield dut
.p
.i_data
.eq(32)
87 yield dut
.n
.i_ready
.eq(1)
89 yield from check_o_n_valid2(dut
, 1) # buffer still needs to output
91 yield from check_o_n_valid2(dut
, 1) # buffer still needs to output
93 yield from check_o_n_valid2(dut
, 1) # buffer still needs to output
95 yield from check_o_n_valid2(dut
, 0) # buffer outputted, *now* we're done.
102 def __init__(self
, dut
, resultfn
):
104 self
.resultfn
= resultfn
106 for i
in range(num_tests
):
107 #data.append(randint(0, 1<<16-1))
108 self
.data
.append(i
+1)
113 while self
.o
!= len(self
.data
):
114 send_range
= randint(0, 3)
115 for j
in range(randint(1,10)):
119 send
= randint(0, send_range
) != 0
120 o_p_ready
= yield self
.dut
.p
[0].o_ready
124 if send
and self
.i
!= len(self
.data
):
125 yield self
.dut
.p
[0].i_valid
.eq(1)
126 yield self
.dut
.p
[0].i_data
.eq(self
.data
[self
.i
])
129 yield self
.dut
.p
[0].i_valid
.eq(0)
133 while self
.o
!= len(self
.data
):
134 stall_range
= randint(0, 3)
135 for j
in range(randint(1,10)):
136 stall
= randint(0, stall_range
) != 0
137 yield self
.dut
.n
[0].i_ready
.eq(stall
)
139 o_n_valid
= yield self
.dut
.n
[0].o_valid
140 i_n_ready
= yield self
.dut
.n
[0].i_ready
141 if not o_n_valid
or not i_n_ready
:
143 o_data
= yield self
.dut
.n
[0].o_data
144 self
.resultfn(o_data
, self
.data
[self
.o
], self
.i
, self
.o
)
146 if self
.o
== len(self
.data
):
149 def test3_resultfn(o_data
, expected
, i
, o
):
150 assert o_data
== expected
+ 1, \
151 "%d-%d data %x not match %x\n" \
152 % (i
, o
, o_data
, expected
)
154 def data_placeholder():
156 for i
in range(num_tests
):
158 d
.src1
= randint(0, 1<<16-1)
159 d
.src2
= randint(0, 1<<16-1)
165 for i
in range(num_tests
):
166 data
.append({'src1': randint(0, 1<<16-1),
167 'src2': randint(0, 1<<16-1)})
172 def __init__(self
, dut
, resultfn
, data
=None):
174 self
.resultfn
= resultfn
179 for i
in range(num_tests
):
180 self
.data
.append((randint(0, 1<<16-1), randint(0, 1<<16-1)))
185 while self
.o
!= len(self
.data
):
186 send_range
= randint(0, 3)
187 for j
in range(randint(1,10)):
191 send
= randint(0, send_range
) != 0
192 o_p_ready
= yield self
.dut
.p
.o_ready
196 if send
and self
.i
!= len(self
.data
):
197 yield self
.dut
.p
.i_valid
.eq(1)
198 for v
in self
.dut
.set_input(self
.data
[self
.i
]):
202 yield self
.dut
.p
.i_valid
.eq(0)
206 while self
.o
!= len(self
.data
):
207 stall_range
= randint(0, 3)
208 for j
in range(randint(1,10)):
209 stall
= randint(0, stall_range
) != 0
210 yield self
.dut
.n
.i_ready
.eq(stall
)
212 o_n_valid
= yield self
.dut
.n
.o_valid
213 i_n_ready
= yield self
.dut
.n
.i_ready
214 if not o_n_valid
or not i_n_ready
:
216 if isinstance(self
.dut
.n
.o_data
, Record
):
218 dod
= self
.dut
.n
.o_data
219 for k
, v
in dod
.fields
.items():
222 o_data
= yield self
.dut
.n
.o_data
223 self
.resultfn(o_data
, self
.data
[self
.o
], self
.i
, self
.o
)
225 if self
.o
== len(self
.data
):
228 def test5_resultfn(o_data
, expected
, i
, o
):
229 res
= expected
[0] + expected
[1]
230 assert o_data
== res
, \
231 "%d-%d data %x not match %s\n" \
232 % (i
, o
, o_data
, repr(expected
))
236 for i
in range(num_tests
):
237 #data.append(randint(0, 1<<16-1))
242 stall
= randint(0, 3) != 0
243 send
= randint(0, 5) != 0
244 yield dut
.n
.i_ready
.eq(stall
)
245 o_p_ready
= yield dut
.p
.o_ready
247 if send
and i
!= len(data
):
248 yield dut
.p
.i_valid
.eq(1)
249 yield dut
.p
.i_data
.eq(data
[i
])
252 yield dut
.p
.i_valid
.eq(0)
254 o_n_valid
= yield dut
.n
.o_valid
255 i_n_ready
= yield dut
.n
.i_ready
256 if o_n_valid
and i_n_ready
:
257 o_data
= yield dut
.n
.o_data
258 assert o_data
== data
[o
] + 2, "%d-%d data %x not match %x\n" \
259 % (i
, o
, o_data
, data
[o
])
265 class ExampleBufPipe2
:
267 connect these: ------|---------------|
269 i_p_valid >>in pipe1 o_n_valid out>> i_p_valid >>in pipe2
270 o_p_ready <<out pipe1 i_n_ready <<in o_p_ready <<out pipe2
271 p_i_data >>in pipe1 p_i_data out>> n_o_data >>in pipe2
274 self
.pipe1
= ExampleBufPipe()
275 self
.pipe2
= ExampleBufPipe()
278 self
.p
= PrevControl()
279 self
.p
.i_data
= Signal(32) # >>in - comes in from the PREVIOUS stage
282 self
.n
= NextControl()
283 self
.n
.o_data
= Signal(32) # out>> - goes out to the NEXT stage
285 def elaborate(self
, platform
):
287 m
.submodules
.pipe1
= self
.pipe1
288 m
.submodules
.pipe2
= self
.pipe2
290 # connect inter-pipe input/output valid/ready/data
291 m
.d
.comb
+= self
.pipe1
.connect_to_next(self
.pipe2
)
293 # inputs/outputs to the module: pipe1 connections here (LHS)
294 m
.d
.comb
+= self
.pipe1
.connect_in(self
)
296 # now pipe2 connections (RHS)
297 m
.d
.comb
+= self
.pipe2
.connect_out(self
)
302 class ExampleBufPipeChain2(BufferedPipeline
):
303 """ connects two stages together as a *single* combinatorial stage.
306 stage1
= ExampleStageCls()
307 stage2
= ExampleStageCls()
308 combined
= StageChain([stage1
, stage2
])
309 BufferedPipeline
.__init
__(self
, combined
)
314 for i
in range(num_tests
):
315 data
.append(randint(0, 1<<16-2))
319 def test9_resultfn(o_data
, expected
, i
, o
):
321 assert o_data
== res
, \
322 "%d-%d data %x not match %s\n" \
323 % (i
, o
, o_data
, repr(expected
))
327 def __init__(self
, width
, signed
):
328 self
.src1
= Signal((width
, signed
))
329 self
.src2
= Signal((width
, signed
))
330 self
.output
= Signal(width
)
332 def elaborate(self
, platform
):
334 m
.d
.comb
+= self
.output
.eq(Mux(self
.src1
< self
.src2
, 1, 0))
340 self
.slt
= SetLessThan(16, True)
343 return (Signal(16), Signal(16))
348 def setup(self
, m
, i
):
350 m
.submodules
.slt
= self
.slt
351 m
.d
.comb
+= self
.slt
.src1
.eq(i
[0])
352 m
.d
.comb
+= self
.slt
.src2
.eq(i
[1])
353 m
.d
.comb
+= self
.o
.eq(self
.slt
.output
)
355 def process(self
, i
):
359 class LTStageDerived(SetLessThan
):
362 SetLessThan
.__init
__(self
, 16, True)
365 return (Signal(16), Signal(16))
370 def setup(self
, m
, i
):
371 m
.submodules
.slt
= self
372 m
.d
.comb
+= self
.src1
.eq(i
[0])
373 m
.d
.comb
+= self
.src2
.eq(i
[1])
375 def process(self
, i
):
379 class ExampleLTPipeline(UnbufferedPipeline
):
380 """ an example of how to use the combinatorial pipeline.
385 UnbufferedPipeline
.__init
__(self
, stage
)
388 class ExampleLTBufferedPipeDerived(BufferedPipeline
):
389 """ an example of how to use the combinatorial pipeline.
393 stage
= LTStageDerived()
394 BufferedPipeline
.__init
__(self
, stage
)
397 def test6_resultfn(o_data
, expected
, i
, o
):
398 res
= 1 if expected
[0] < expected
[1] else 0
399 assert o_data
== res
, \
400 "%d-%d data %x not match %s\n" \
401 % (i
, o
, o_data
, repr(expected
))
404 class ExampleAddRecordStage
:
405 """ example use of a Record
408 record_spec
= [('src1', 16), ('src2', 16)]
410 """ returns a Record using the specification
412 return Record(self
.record_spec
)
415 return Record(self
.record_spec
)
417 def process(self
, i
):
418 """ process the input data, returning a dictionary with key names
419 that exactly match the Record's attributes.
421 return {'src1': i
.src1
+ 1,
425 class ExampleAddRecordPlaceHolderStage
:
426 """ example use of a Record, with a placeholder as the processing result
429 record_spec
= [('src1', 16), ('src2', 16)]
431 """ returns a Record using the specification
433 return Record(self
.record_spec
)
436 return Record(self
.record_spec
)
438 def process(self
, i
):
439 """ process the input data, returning a PlaceHolder class instance
440 with attributes that exactly match those of the Record.
447 class PlaceHolder
: pass
450 class ExampleAddRecordPipe(UnbufferedPipeline
):
451 """ an example of how to use the combinatorial pipeline.
455 stage
= ExampleAddRecordStage()
456 UnbufferedPipeline
.__init
__(self
, stage
)
459 def test7_resultfn(o_data
, expected
, i
, o
):
460 res
= (expected
['src1'] + 1, expected
['src2'] + 1)
461 assert o_data
['src1'] == res
[0] and o_data
['src2'] == res
[1], \
462 "%d-%d data %s not match %s\n" \
463 % (i
, o
, repr(o_data
), repr(expected
))
466 class ExampleAddRecordPlaceHolderPipe(UnbufferedPipeline
):
467 """ an example of how to use the combinatorial pipeline.
471 stage
= ExampleAddRecordPlaceHolderStage()
472 UnbufferedPipeline
.__init
__(self
, stage
)
475 def test11_resultfn(o_data
, expected
, i
, o
):
476 res1
= expected
.src1
+ 1
477 res2
= expected
.src2
+ 1
478 assert o_data
['src1'] == res1
and o_data
['src2'] == res2
, \
479 "%d-%d data %s not match %s\n" \
480 % (i
, o
, repr(o_data
), repr(expected
))
483 class Example2OpClass
:
484 """ an example of a class used to store 2 operands.
485 requires an eq function, to conform with the pipeline stage API
489 self
.op1
= Signal(16)
490 self
.op2
= Signal(16)
493 return [self
.op1
.eq(i
.op1
), self
.op2
.eq(i
.op2
)]
496 class ExampleAddClassStage
:
497 """ an example of how to use the buffered pipeline, as a class instance
501 """ returns an instance of an Example2OpClass.
503 return Example2OpClass()
506 """ returns an output signal which will happen to contain the sum
511 def process(self
, i
):
512 """ process the input data (sums the values in the tuple) and returns it
517 class ExampleBufPipeAddClass(BufferedPipeline
):
518 """ an example of how to use the buffered pipeline, using a class instance
522 addstage
= ExampleAddClassStage()
523 BufferedPipeline
.__init
__(self
, addstage
)
527 """ the eq function, called by set_input, needs an incoming object
528 that conforms to the Example2OpClass.eq function requirements
529 easiest way to do that is to create a class that has the exact
530 same member layout (self.op1, self.op2) as Example2OpClass
532 def __init__(self
, op1
, op2
):
537 def test8_resultfn(o_data
, expected
, i
, o
):
538 res
= expected
.op1
+ expected
.op2
# these are a TestInputAdd instance
539 assert o_data
== res
, \
540 "%d-%d data %x not match %s\n" \
541 % (i
, o
, o_data
, repr(expected
))
545 for i
in range(num_tests
):
546 data
.append(TestInputAdd(randint(0, 1<<16-1), randint(0, 1<<16-1)))
552 if __name__
== '__main__':
554 dut
= ExampleBufPipe()
555 run_simulation(dut
, testbench(dut
), vcd_name
="test_bufpipe.vcd")
558 dut
= ExampleBufPipe2()
559 run_simulation(dut
, testbench2(dut
), vcd_name
="test_bufpipe2.vcd")
562 dut
= ExampleBufPipe()
563 test
= Test3(dut
, test3_resultfn
)
564 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_bufpipe3.vcd")
567 dut
= ExamplePipeline()
568 test
= Test3(dut
, test3_resultfn
)
569 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_combpipe3.vcd")
572 dut
= ExampleBufPipe2()
573 run_simulation(dut
, testbench4(dut
), vcd_name
="test_bufpipe4.vcd")
576 dut
= ExampleBufPipeAdd()
577 test
= Test5(dut
, test5_resultfn
)
578 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_bufpipe5.vcd")
581 dut
= ExampleLTPipeline()
582 test
= Test5(dut
, test6_resultfn
)
583 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_ltcomb6.vcd")
585 ports
= [dut
.p
.i_valid
, dut
.n
.i_ready
,
586 dut
.n
.o_valid
, dut
.p
.o_ready
] + \
587 list(dut
.p
.i_data
) + [dut
.n
.o_data
]
588 vl
= rtlil
.convert(dut
, ports
=ports
)
589 with
open("test_ltcomb_pipe.il", "w") as f
:
593 dut
= ExampleAddRecordPipe()
595 test
= Test5(dut
, test7_resultfn
, data
=data
)
596 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_addrecord.vcd")
598 ports
= [dut
.p
.i_valid
, dut
.n
.i_ready
,
599 dut
.n
.o_valid
, dut
.p
.o_ready
,
600 dut
.p
.i_data
.src1
, dut
.p
.i_data
.src2
,
601 dut
.n
.o_data
.src1
, dut
.n
.o_data
.src2
]
602 vl
= rtlil
.convert(dut
, ports
=ports
)
603 with
open("test_recordcomb_pipe.il", "w") as f
:
607 dut
= ExampleBufPipeAddClass()
609 test
= Test5(dut
, test8_resultfn
, data
=data
)
610 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_bufpipe8.vcd")
613 dut
= ExampleBufPipeChain2()
614 ports
= [dut
.p
.i_valid
, dut
.n
.i_ready
,
615 dut
.n
.o_valid
, dut
.p
.o_ready
] + \
616 [dut
.p
.i_data
] + [dut
.n
.o_data
]
617 vl
= rtlil
.convert(dut
, ports
=ports
)
618 with
open("test_bufpipechain2.il", "w") as f
:
622 test
= Test5(dut
, test9_resultfn
, data
=data
)
623 run_simulation(dut
, [test
.send
, test
.rcv
],
624 vcd_name
="test_bufpipechain2.vcd")
627 dut
= ExampleLTBufferedPipeDerived()
628 test
= Test5(dut
, test6_resultfn
)
629 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_ltbufpipe10.vcd")
630 vl
= rtlil
.convert(dut
, ports
=ports
)
631 with
open("test_ltbufpipe10.il", "w") as f
:
635 dut
= ExampleAddRecordPlaceHolderPipe()
636 data
=data_placeholder()
637 test
= Test5(dut
, test11_resultfn
, data
=data
)
638 run_simulation(dut
, [test
.send
, test
.rcv
], vcd_name
="test_addrecord.vcd")