5fa649ef83f79dcbce7d3338da418d02dd9b9b2d
[ieee754fpu.git] / src / add / test_multishift.py
1 from random import randint
2 from nmigen import Module, Signal
3 from nmigen.compat.sim import run_simulation
4
5 from fpbase import MultiShift, MultiShiftR
6
7 class MultiShiftModL:
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)
13
14 def get_fragment(self, platform=None):
15
16 m = Module()
17 m.d.comb += self.x.eq(self.ms.lshift(self.a, self.b))
18
19 return m
20
21 class MultiShiftModR:
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)
27
28 def get_fragment(self, platform=None):
29
30 m = Module()
31 m.d.comb += self.x.eq(self.ms.rshift(self.a, self.b))
32
33 return m
34
35 class MultiShiftModRMod:
36 def __init__(self, width):
37 self.ms = MultiShiftR(width)
38 self.a = Signal(width)
39 self.b = Signal(self.ms.smax)
40 self.x = Signal(width)
41
42 def get_fragment(self, platform=None):
43
44 m = Module()
45 m.submodules += self.ms
46 m.d.comb += self.ms.i.eq(self.a)
47 m.d.comb += self.ms.s.eq(self.b)
48 m.d.comb += self.x.eq(self.ms.o)
49
50 return m
51
52 def check_case(dut, width, a, b):
53 yield dut.a.eq(a)
54 yield dut.b.eq(b)
55 yield
56
57 x = (a << b) & ((1<<width)-1)
58
59 out_x = yield dut.x
60 assert out_x == x, "Output x 0x%x not equal to expected 0x%x" % (out_x, x)
61
62 def check_caser(dut, width, a, b):
63 yield dut.a.eq(a)
64 yield dut.b.eq(b)
65 yield
66
67 x = (a >> b) & ((1<<width)-1)
68
69 out_x = yield dut.x
70 assert out_x == x, "Output x 0x%x not equal to expected 0x%x" % (out_x, x)
71
72 def testbench(dut):
73 for i in range(32):
74 for j in range(1000):
75 a = randint(0, (1<<32)-1)
76 yield from check_case(dut, 32, a, i)
77
78 def testbenchr(dut):
79 for i in range(32):
80 for j in range(1000):
81 a = randint(0, (1<<32)-1)
82 yield from check_caser(dut, 32, a, i)
83
84 if __name__ == '__main__':
85 dut = MultiShiftModRMod(width=32)
86 run_simulation(dut, testbenchr(dut), vcd_name="test_multishift.vcd")
87
88 dut = MultiShiftModR(width=32)
89 run_simulation(dut, testbenchr(dut), vcd_name="test_multishift.vcd")
90
91 dut = MultiShiftModL(width=32)
92 run_simulation(dut, testbench(dut), vcd_name="test_multishift.vcd")
93