5f34beb5cfc93f5ef0ed3b92fabe4386f70a96c6
[soc.git] / src / soc / minerva / test / test_units_divider.py
1 from nmigen import *
2 from nmigen.back.pysim import *
3 from nmigen.test.utils import *
4
5 from ..units.divider import *
6 from ..isa import Funct3
7
8
9 def test_op(funct3, src1, src2, result):
10 def test(self):
11 with Simulator(self.dut) as sim:
12 def process():
13 yield self.dut.x_op.eq(funct3)
14 yield self.dut.x_src1.eq(src1)
15 yield self.dut.x_src2.eq(src2)
16 yield self.dut.x_valid.eq(1)
17 yield self.dut.x_stall.eq(0)
18 yield Tick()
19 yield self.dut.x_valid.eq(0)
20 yield Tick()
21 while (yield self.dut.m_busy):
22 yield Tick()
23 self.assertEqual((yield self.dut.m_result), result)
24 sim.add_clock(1e-6)
25 sim.add_sync_process(process)
26 sim.run()
27 return test
28
29
30 class DividerTestCase(FHDLTestCase):
31 def setUp(self):
32 self.dut = Divider()
33
34 # Test cases are taken from the riscv-compliance testbench:
35 # https://github.com/riscv/riscv-compliance/tree/master/riscv-test-suite/rv32im
36
37 # DIV ------------------------------------------------------------------------
38
39 test_div_0 = test_op(Funct3.DIV, 0x00000000, 0x00000000, result=0xffffffff)
40 test_div_1 = test_op(Funct3.DIV, 0x00000000, 0x00000001, result=0x00000000)
41 test_div_2 = test_op(Funct3.DIV, 0x00000000, 0xffffffff, result=0x00000000)
42 test_div_3 = test_op(Funct3.DIV, 0x00000000, 0x7fffffff, result=0x00000000)
43 test_div_4 = test_op(Funct3.DIV, 0x00000000, 0x80000000, result=0x00000000)
44
45 test_div_5 = test_op(Funct3.DIV, 0x00000001, 0x00000000, result=0xffffffff)
46 test_div_6 = test_op(Funct3.DIV, 0x00000001, 0x00000001, result=0x00000001)
47 test_div_7 = test_op(Funct3.DIV, 0x00000001, 0xffffffff, result=0xffffffff)
48 test_div_8 = test_op(Funct3.DIV, 0x00000001, 0x7fffffff, result=0x00000000)
49 test_div_9 = test_op(Funct3.DIV, 0x00000001, 0x80000000, result=0x00000000)
50
51 test_div_10 = test_op(Funct3.DIV, 0xffffffff, 0x00000000, result=0xffffffff)
52 test_div_11 = test_op(Funct3.DIV, 0xffffffff, 0x00000001, result=0xffffffff)
53 test_div_12 = test_op(Funct3.DIV, 0xffffffff, 0xffffffff, result=0x00000001)
54 test_div_13 = test_op(Funct3.DIV, 0xffffffff, 0x7fffffff, result=0x00000000)
55 test_div_14 = test_op(Funct3.DIV, 0xffffffff, 0x80000000, result=0x00000000)
56
57 test_div_15 = test_op(Funct3.DIV, 0x7fffffff, 0x00000000, result=0xffffffff)
58 test_div_16 = test_op(Funct3.DIV, 0x7fffffff, 0x00000001, result=0x7fffffff)
59 test_div_17 = test_op(Funct3.DIV, 0x7fffffff, 0xffffffff, result=0x80000001)
60 test_div_18 = test_op(Funct3.DIV, 0x7fffffff, 0x7fffffff, result=0x00000001)
61 test_div_19 = test_op(Funct3.DIV, 0x7fffffff, 0x80000000, result=0x00000000)
62
63 test_div_20 = test_op(Funct3.DIV, 0x80000000, 0x00000000, result=0xffffffff)
64 test_div_21 = test_op(Funct3.DIV, 0x80000000, 0x00000001, result=0x80000000)
65 test_div_22 = test_op(Funct3.DIV, 0x80000000, 0xffffffff, result=0x80000000)
66 test_div_23 = test_op(Funct3.DIV, 0x80000000, 0x7fffffff, result=0xffffffff)
67 test_div_24 = test_op(Funct3.DIV, 0x80000000, 0x80000000, result=0x00000001)
68
69 # DIVU -----------------------------------------------------------------------
70
71 test_divu_0 = test_op(Funct3.DIVU, 0x00000000, 0x00000000, result=0xffffffff)
72 test_divu_1 = test_op(Funct3.DIVU, 0x00000000, 0x00000001, result=0x00000000)
73 test_divu_2 = test_op(Funct3.DIVU, 0x00000000, 0xffffffff, result=0x00000000)
74 test_divu_3 = test_op(Funct3.DIVU, 0x00000000, 0x7fffffff, result=0x00000000)
75 test_divu_4 = test_op(Funct3.DIVU, 0x00000000, 0x80000000, result=0x00000000)
76
77 test_divu_5 = test_op(Funct3.DIVU, 0x00000001, 0x00000000, result=0xffffffff)
78 test_divu_6 = test_op(Funct3.DIVU, 0x00000001, 0x00000001, result=0x00000001)
79 test_divu_7 = test_op(Funct3.DIVU, 0x00000001, 0xffffffff, result=0x00000000)
80 test_divu_8 = test_op(Funct3.DIVU, 0x00000001, 0x7fffffff, result=0x00000000)
81 test_divu_9 = test_op(Funct3.DIVU, 0x00000001, 0x80000000, result=0x00000000)
82
83 test_divu_10 = test_op(Funct3.DIVU, 0xffffffff, 0x00000000, result=0xffffffff)
84 test_divu_11 = test_op(Funct3.DIVU, 0xffffffff, 0x00000001, result=0xffffffff)
85 test_divu_12 = test_op(Funct3.DIVU, 0xffffffff, 0xffffffff, result=0x00000001)
86 test_divu_13 = test_op(Funct3.DIVU, 0xffffffff, 0x7fffffff, result=0x00000002)
87 test_divu_14 = test_op(Funct3.DIVU, 0xffffffff, 0x80000000, result=0x00000001)
88
89 test_divu_15 = test_op(Funct3.DIVU, 0x7fffffff, 0x00000000, result=0xffffffff)
90 test_divu_16 = test_op(Funct3.DIVU, 0x7fffffff, 0x00000001, result=0x7fffffff)
91 test_divu_17 = test_op(Funct3.DIVU, 0x7fffffff, 0xffffffff, result=0x00000000)
92 test_divu_18 = test_op(Funct3.DIVU, 0x7fffffff, 0x7fffffff, result=0x00000001)
93 test_divu_19 = test_op(Funct3.DIVU, 0x7fffffff, 0x80000000, result=0x00000000)
94
95 test_divu_20 = test_op(Funct3.DIVU, 0x80000000, 0x00000000, result=0xffffffff)
96 test_divu_21 = test_op(Funct3.DIVU, 0x80000000, 0x00000001, result=0x80000000)
97 test_divu_22 = test_op(Funct3.DIVU, 0x80000000, 0xffffffff, result=0x00000000)
98 test_divu_23 = test_op(Funct3.DIVU, 0x80000000, 0x7fffffff, result=0x00000001)
99 test_divu_24 = test_op(Funct3.DIVU, 0x80000000, 0x80000000, result=0x00000001)
100
101 # REM ------------------------------------------------------------------------
102
103 test_rem_0 = test_op(Funct3.REM, 0x00000000, 0x00000000, result=0x00000000)
104 test_rem_1 = test_op(Funct3.REM, 0x00000000, 0x00000001, result=0x00000000)
105 test_rem_2 = test_op(Funct3.REM, 0x00000000, 0xffffffff, result=0x00000000)
106 test_rem_3 = test_op(Funct3.REM, 0x00000000, 0x7fffffff, result=0x00000000)
107 test_rem_4 = test_op(Funct3.REM, 0x00000000, 0x80000000, result=0x00000000)
108
109 test_rem_5 = test_op(Funct3.REM, 0x00000001, 0x00000000, result=0x00000001)
110 test_rem_6 = test_op(Funct3.REM, 0x00000001, 0x00000001, result=0x00000000)
111 test_rem_7 = test_op(Funct3.REM, 0x00000001, 0xffffffff, result=0x00000000)
112 test_rem_8 = test_op(Funct3.REM, 0x00000001, 0x7fffffff, result=0x00000001)
113 test_rem_9 = test_op(Funct3.REM, 0x00000001, 0x80000000, result=0x00000001)
114
115 test_rem_10 = test_op(Funct3.REM, 0xffffffff, 0x00000000, result=0xffffffff)
116 test_rem_11 = test_op(Funct3.REM, 0xffffffff, 0x00000001, result=0x00000000)
117 test_rem_12 = test_op(Funct3.REM, 0xffffffff, 0xffffffff, result=0x00000000)
118 test_rem_13 = test_op(Funct3.REM, 0xffffffff, 0x7fffffff, result=0xffffffff)
119 test_rem_14 = test_op(Funct3.REM, 0xffffffff, 0x80000000, result=0xffffffff)
120
121 test_rem_15 = test_op(Funct3.REM, 0x7fffffff, 0x00000000, result=0x7fffffff)
122 test_rem_16 = test_op(Funct3.REM, 0x7fffffff, 0x00000001, result=0x00000000)
123 test_rem_17 = test_op(Funct3.REM, 0x7fffffff, 0xffffffff, result=0x00000000)
124 test_rem_18 = test_op(Funct3.REM, 0x7fffffff, 0x7fffffff, result=0x00000000)
125 test_rem_19 = test_op(Funct3.REM, 0x7fffffff, 0x80000000, result=0x7fffffff)
126
127 test_rem_20 = test_op(Funct3.REM, 0x80000000, 0x00000000, result=0x80000000)
128 test_rem_21 = test_op(Funct3.REM, 0x80000000, 0x00000001, result=0x00000000)
129 test_rem_22 = test_op(Funct3.REM, 0x80000000, 0xffffffff, result=0x00000000)
130 test_rem_23 = test_op(Funct3.REM, 0x80000000, 0x7fffffff, result=0xffffffff)
131 test_rem_24 = test_op(Funct3.REM, 0x80000000, 0x80000000, result=0x00000000)
132
133 # REMU -----------------------------------------------------------------------
134
135 test_remu_0 = test_op(Funct3.REMU, 0x00000000, 0x00000000, result=0x00000000)
136 test_remu_1 = test_op(Funct3.REMU, 0x00000000, 0x00000001, result=0x00000000)
137 test_remu_2 = test_op(Funct3.REMU, 0x00000000, 0xffffffff, result=0x00000000)
138 test_remu_3 = test_op(Funct3.REMU, 0x00000000, 0x7fffffff, result=0x00000000)
139 test_remu_4 = test_op(Funct3.REMU, 0x00000000, 0x80000000, result=0x00000000)
140
141 test_remu_5 = test_op(Funct3.REMU, 0x00000001, 0x00000000, result=0x00000001)
142 test_remu_6 = test_op(Funct3.REMU, 0x00000001, 0x00000001, result=0x00000000)
143 test_remu_7 = test_op(Funct3.REMU, 0x00000001, 0xffffffff, result=0x00000001)
144 test_remu_8 = test_op(Funct3.REMU, 0x00000001, 0x7fffffff, result=0x00000001)
145 test_remu_9 = test_op(Funct3.REMU, 0x00000001, 0x80000000, result=0x00000001)
146
147 test_remu_10 = test_op(Funct3.REMU, 0xffffffff, 0x00000000, result=0xffffffff)
148 test_remu_11 = test_op(Funct3.REMU, 0xffffffff, 0x00000001, result=0x00000000)
149 test_remu_12 = test_op(Funct3.REMU, 0xffffffff, 0xffffffff, result=0x00000000)
150 test_remu_13 = test_op(Funct3.REMU, 0xffffffff, 0x7fffffff, result=0x00000001)
151 test_remu_14 = test_op(Funct3.REMU, 0xffffffff, 0x80000000, result=0x7fffffff)
152
153 test_remu_15 = test_op(Funct3.REMU, 0x7fffffff, 0x00000000, result=0x7fffffff)
154 test_remu_16 = test_op(Funct3.REMU, 0x7fffffff, 0x00000001, result=0x00000000)
155 test_remu_17 = test_op(Funct3.REMU, 0x7fffffff, 0xffffffff, result=0x7fffffff)
156 test_remu_18 = test_op(Funct3.REMU, 0x7fffffff, 0x7fffffff, result=0x00000000)
157 test_remu_19 = test_op(Funct3.REMU, 0x7fffffff, 0x80000000, result=0x7fffffff)
158
159 test_remu_20 = test_op(Funct3.REMU, 0x80000000, 0x00000000, result=0x80000000)
160 test_remu_21 = test_op(Funct3.REMU, 0x80000000, 0x00000001, result=0x00000000)
161 test_remu_22 = test_op(Funct3.REMU, 0x80000000, 0xffffffff, result=0x80000000)
162 test_remu_23 = test_op(Funct3.REMU, 0x80000000, 0x7fffffff, result=0x00000001)
163 test_remu_24 = test_op(Funct3.REMU, 0x80000000, 0x80000000, result=0x00000000)