Merge branch 'fix-tests'
[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 tst_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 = tst_op(Funct3.DIV, 0x00000000, 0x00000000, result=0xffffffff)
40 test_div_1 = tst_op(Funct3.DIV, 0x00000000, 0x00000001, result=0x00000000)
41 test_div_2 = tst_op(Funct3.DIV, 0x00000000, 0xffffffff, result=0x00000000)
42 test_div_3 = tst_op(Funct3.DIV, 0x00000000, 0x7fffffff, result=0x00000000)
43 test_div_4 = tst_op(Funct3.DIV, 0x00000000, 0x80000000, result=0x00000000)
44
45 test_div_5 = tst_op(Funct3.DIV, 0x00000001, 0x00000000, result=0xffffffff)
46 test_div_6 = tst_op(Funct3.DIV, 0x00000001, 0x00000001, result=0x00000001)
47 test_div_7 = tst_op(Funct3.DIV, 0x00000001, 0xffffffff, result=0xffffffff)
48 test_div_8 = tst_op(Funct3.DIV, 0x00000001, 0x7fffffff, result=0x00000000)
49 test_div_9 = tst_op(Funct3.DIV, 0x00000001, 0x80000000, result=0x00000000)
50
51 test_div_10 = tst_op(Funct3.DIV, 0xffffffff,
52 0x00000000, result=0xffffffff)
53 test_div_11 = tst_op(Funct3.DIV, 0xffffffff,
54 0x00000001, result=0xffffffff)
55 test_div_12 = tst_op(Funct3.DIV, 0xffffffff,
56 0xffffffff, result=0x00000001)
57 test_div_13 = tst_op(Funct3.DIV, 0xffffffff,
58 0x7fffffff, result=0x00000000)
59 test_div_14 = tst_op(Funct3.DIV, 0xffffffff,
60 0x80000000, result=0x00000000)
61
62 test_div_15 = tst_op(Funct3.DIV, 0x7fffffff,
63 0x00000000, result=0xffffffff)
64 test_div_16 = tst_op(Funct3.DIV, 0x7fffffff,
65 0x00000001, result=0x7fffffff)
66 test_div_17 = tst_op(Funct3.DIV, 0x7fffffff,
67 0xffffffff, result=0x80000001)
68 test_div_18 = tst_op(Funct3.DIV, 0x7fffffff,
69 0x7fffffff, result=0x00000001)
70 test_div_19 = tst_op(Funct3.DIV, 0x7fffffff,
71 0x80000000, result=0x00000000)
72
73 test_div_20 = tst_op(Funct3.DIV, 0x80000000,
74 0x00000000, result=0xffffffff)
75 test_div_21 = tst_op(Funct3.DIV, 0x80000000,
76 0x00000001, result=0x80000000)
77 test_div_22 = tst_op(Funct3.DIV, 0x80000000,
78 0xffffffff, result=0x80000000)
79 test_div_23 = tst_op(Funct3.DIV, 0x80000000,
80 0x7fffffff, result=0xffffffff)
81 test_div_24 = tst_op(Funct3.DIV, 0x80000000,
82 0x80000000, result=0x00000001)
83
84 # DIVU -----------------------------------------------------------------------
85
86 test_divu_0 = tst_op(Funct3.DIVU, 0x00000000,
87 0x00000000, result=0xffffffff)
88 test_divu_1 = tst_op(Funct3.DIVU, 0x00000000,
89 0x00000001, result=0x00000000)
90 test_divu_2 = tst_op(Funct3.DIVU, 0x00000000,
91 0xffffffff, result=0x00000000)
92 test_divu_3 = tst_op(Funct3.DIVU, 0x00000000,
93 0x7fffffff, result=0x00000000)
94 test_divu_4 = tst_op(Funct3.DIVU, 0x00000000,
95 0x80000000, result=0x00000000)
96
97 test_divu_5 = tst_op(Funct3.DIVU, 0x00000001,
98 0x00000000, result=0xffffffff)
99 test_divu_6 = tst_op(Funct3.DIVU, 0x00000001,
100 0x00000001, result=0x00000001)
101 test_divu_7 = tst_op(Funct3.DIVU, 0x00000001,
102 0xffffffff, result=0x00000000)
103 test_divu_8 = tst_op(Funct3.DIVU, 0x00000001,
104 0x7fffffff, result=0x00000000)
105 test_divu_9 = tst_op(Funct3.DIVU, 0x00000001,
106 0x80000000, result=0x00000000)
107
108 test_divu_10 = tst_op(Funct3.DIVU, 0xffffffff,
109 0x00000000, result=0xffffffff)
110 test_divu_11 = tst_op(Funct3.DIVU, 0xffffffff,
111 0x00000001, result=0xffffffff)
112 test_divu_12 = tst_op(Funct3.DIVU, 0xffffffff,
113 0xffffffff, result=0x00000001)
114 test_divu_13 = tst_op(Funct3.DIVU, 0xffffffff,
115 0x7fffffff, result=0x00000002)
116 test_divu_14 = tst_op(Funct3.DIVU, 0xffffffff,
117 0x80000000, result=0x00000001)
118
119 test_divu_15 = tst_op(Funct3.DIVU, 0x7fffffff,
120 0x00000000, result=0xffffffff)
121 test_divu_16 = tst_op(Funct3.DIVU, 0x7fffffff,
122 0x00000001, result=0x7fffffff)
123 test_divu_17 = tst_op(Funct3.DIVU, 0x7fffffff,
124 0xffffffff, result=0x00000000)
125 test_divu_18 = tst_op(Funct3.DIVU, 0x7fffffff,
126 0x7fffffff, result=0x00000001)
127 test_divu_19 = tst_op(Funct3.DIVU, 0x7fffffff,
128 0x80000000, result=0x00000000)
129
130 test_divu_20 = tst_op(Funct3.DIVU, 0x80000000,
131 0x00000000, result=0xffffffff)
132 test_divu_21 = tst_op(Funct3.DIVU, 0x80000000,
133 0x00000001, result=0x80000000)
134 test_divu_22 = tst_op(Funct3.DIVU, 0x80000000,
135 0xffffffff, result=0x00000000)
136 test_divu_23 = tst_op(Funct3.DIVU, 0x80000000,
137 0x7fffffff, result=0x00000001)
138 test_divu_24 = tst_op(Funct3.DIVU, 0x80000000,
139 0x80000000, result=0x00000001)
140
141 # REM ------------------------------------------------------------------------
142
143 test_rem_0 = tst_op(Funct3.REM, 0x00000000, 0x00000000, result=0x00000000)
144 test_rem_1 = tst_op(Funct3.REM, 0x00000000, 0x00000001, result=0x00000000)
145 test_rem_2 = tst_op(Funct3.REM, 0x00000000, 0xffffffff, result=0x00000000)
146 test_rem_3 = tst_op(Funct3.REM, 0x00000000, 0x7fffffff, result=0x00000000)
147 test_rem_4 = tst_op(Funct3.REM, 0x00000000, 0x80000000, result=0x00000000)
148
149 test_rem_5 = tst_op(Funct3.REM, 0x00000001, 0x00000000, result=0x00000001)
150 test_rem_6 = tst_op(Funct3.REM, 0x00000001, 0x00000001, result=0x00000000)
151 test_rem_7 = tst_op(Funct3.REM, 0x00000001, 0xffffffff, result=0x00000000)
152 test_rem_8 = tst_op(Funct3.REM, 0x00000001, 0x7fffffff, result=0x00000001)
153 test_rem_9 = tst_op(Funct3.REM, 0x00000001, 0x80000000, result=0x00000001)
154
155 test_rem_10 = tst_op(Funct3.REM, 0xffffffff,
156 0x00000000, result=0xffffffff)
157 test_rem_11 = tst_op(Funct3.REM, 0xffffffff,
158 0x00000001, result=0x00000000)
159 test_rem_12 = tst_op(Funct3.REM, 0xffffffff,
160 0xffffffff, result=0x00000000)
161 test_rem_13 = tst_op(Funct3.REM, 0xffffffff,
162 0x7fffffff, result=0xffffffff)
163 test_rem_14 = tst_op(Funct3.REM, 0xffffffff,
164 0x80000000, result=0xffffffff)
165
166 test_rem_15 = tst_op(Funct3.REM, 0x7fffffff,
167 0x00000000, result=0x7fffffff)
168 test_rem_16 = tst_op(Funct3.REM, 0x7fffffff,
169 0x00000001, result=0x00000000)
170 test_rem_17 = tst_op(Funct3.REM, 0x7fffffff,
171 0xffffffff, result=0x00000000)
172 test_rem_18 = tst_op(Funct3.REM, 0x7fffffff,
173 0x7fffffff, result=0x00000000)
174 test_rem_19 = tst_op(Funct3.REM, 0x7fffffff,
175 0x80000000, result=0x7fffffff)
176
177 test_rem_20 = tst_op(Funct3.REM, 0x80000000,
178 0x00000000, result=0x80000000)
179 test_rem_21 = tst_op(Funct3.REM, 0x80000000,
180 0x00000001, result=0x00000000)
181 test_rem_22 = tst_op(Funct3.REM, 0x80000000,
182 0xffffffff, result=0x00000000)
183 test_rem_23 = tst_op(Funct3.REM, 0x80000000,
184 0x7fffffff, result=0xffffffff)
185 test_rem_24 = tst_op(Funct3.REM, 0x80000000,
186 0x80000000, result=0x00000000)
187
188 # REMU -----------------------------------------------------------------------
189
190 test_remu_0 = tst_op(Funct3.REMU, 0x00000000,
191 0x00000000, result=0x00000000)
192 test_remu_1 = tst_op(Funct3.REMU, 0x00000000,
193 0x00000001, result=0x00000000)
194 test_remu_2 = tst_op(Funct3.REMU, 0x00000000,
195 0xffffffff, result=0x00000000)
196 test_remu_3 = tst_op(Funct3.REMU, 0x00000000,
197 0x7fffffff, result=0x00000000)
198 test_remu_4 = tst_op(Funct3.REMU, 0x00000000,
199 0x80000000, result=0x00000000)
200
201 test_remu_5 = tst_op(Funct3.REMU, 0x00000001,
202 0x00000000, result=0x00000001)
203 test_remu_6 = tst_op(Funct3.REMU, 0x00000001,
204 0x00000001, result=0x00000000)
205 test_remu_7 = tst_op(Funct3.REMU, 0x00000001,
206 0xffffffff, result=0x00000001)
207 test_remu_8 = tst_op(Funct3.REMU, 0x00000001,
208 0x7fffffff, result=0x00000001)
209 test_remu_9 = tst_op(Funct3.REMU, 0x00000001,
210 0x80000000, result=0x00000001)
211
212 test_remu_10 = tst_op(Funct3.REMU, 0xffffffff,
213 0x00000000, result=0xffffffff)
214 test_remu_11 = tst_op(Funct3.REMU, 0xffffffff,
215 0x00000001, result=0x00000000)
216 test_remu_12 = tst_op(Funct3.REMU, 0xffffffff,
217 0xffffffff, result=0x00000000)
218 test_remu_13 = tst_op(Funct3.REMU, 0xffffffff,
219 0x7fffffff, result=0x00000001)
220 test_remu_14 = tst_op(Funct3.REMU, 0xffffffff,
221 0x80000000, result=0x7fffffff)
222
223 test_remu_15 = tst_op(Funct3.REMU, 0x7fffffff,
224 0x00000000, result=0x7fffffff)
225 test_remu_16 = tst_op(Funct3.REMU, 0x7fffffff,
226 0x00000001, result=0x00000000)
227 test_remu_17 = tst_op(Funct3.REMU, 0x7fffffff,
228 0xffffffff, result=0x7fffffff)
229 test_remu_18 = tst_op(Funct3.REMU, 0x7fffffff,
230 0x7fffffff, result=0x00000000)
231 test_remu_19 = tst_op(Funct3.REMU, 0x7fffffff,
232 0x80000000, result=0x7fffffff)
233
234 test_remu_20 = tst_op(Funct3.REMU, 0x80000000,
235 0x00000000, result=0x80000000)
236 test_remu_21 = tst_op(Funct3.REMU, 0x80000000,
237 0x00000001, result=0x00000000)
238 test_remu_22 = tst_op(Funct3.REMU, 0x80000000,
239 0xffffffff, result=0x80000000)
240 test_remu_23 = tst_op(Funct3.REMU, 0x80000000,
241 0x7fffffff, result=0x00000001)
242 test_remu_24 = tst_op(Funct3.REMU, 0x80000000,
243 0x80000000, result=0x00000000)