1 from random
import randint
2 from nmigen
import Module
, Signal
3 from nmigen
.compat
.sim
import run_simulation
5 from fpbase
import MultiShift
8 def __init__(self
, width
):
9 self
.ms
= MultiShift(width
)
10 self
.a
= Signal(width
)
11 self
.b
= Signal(self
.ms
.smax
)
12 self
.x
= Signal(width
)
14 def get_fragment(self
, platform
=None):
17 m
.d
.comb
+= self
.x
.eq(self
.ms
.lshift(self
.a
, self
.b
))
22 def __init__(self
, width
):
23 self
.ms
= MultiShift(width
)
24 self
.a
= Signal(width
)
25 self
.b
= Signal(self
.ms
.smax
)
26 self
.x
= Signal(width
)
28 def get_fragment(self
, platform
=None):
31 m
.d
.comb
+= self
.x
.eq(self
.ms
.rshift(self
.a
, self
.b
))
35 def check_case(dut
, width
, a
, b
):
40 x
= (a
<< b
) & ((1<<width
)-1)
43 assert out_x
== x
, "Output x 0x%x not equal to expected 0x%x" % (out_x
, x
)
45 def check_caser(dut
, width
, a
, b
):
50 x
= (a
>> b
) & ((1<<width
)-1)
53 assert out_x
== x
, "Output x 0x%x not equal to expected 0x%x" % (out_x
, x
)
58 a
= randint(0, (1<<32)-1)
59 yield from check_case(dut
, 32, a
, i
)
64 a
= randint(0, (1<<32)-1)
65 yield from check_caser(dut
, 32, a
, i
)
67 if __name__
== '__main__':
68 dut
= MultiShiftModR(width
=32)
69 run_simulation(dut
, testbenchr(dut
), vcd_name
="test_multishift.vcd")
71 dut
= MultiShiftModL(width
=32)
72 run_simulation(dut
, testbench(dut
), vcd_name
="test_multishift.vcd")