2 from nmigen
.back
.pysim
import *
3 from nmigen
.test
.utils
import *
5 from ..units
.divider
import *
6 from ..isa
import Funct3
9 def tst_op(funct3
, src1
, src2
, result
):
11 with
Simulator(self
.dut
) as sim
:
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)
19 yield self
.dut
.x_valid
.eq(0)
21 while (yield self
.dut
.m_busy
):
23 self
.assertEqual((yield self
.dut
.m_result
), result
)
25 sim
.add_sync_process(process
)
30 class DividerTestCase(FHDLTestCase
):
34 # Test cases are taken from the riscv-compliance testbench:
35 # https://github.com/riscv/riscv-compliance/tree/master/riscv-test-suite/rv32im
37 # DIV ------------------------------------------------------------------------
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)
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)
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)
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)
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)
84 # DIVU -----------------------------------------------------------------------
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)
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)
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)
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)
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)
141 # REM ------------------------------------------------------------------------
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)
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)
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)
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)
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)
188 # REMU -----------------------------------------------------------------------
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)
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)
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)
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)
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)