2 from nmigen
.back
.pysim
import *
3 from nmigen
.test
.utils
import *
5 from ..units
.multiplier
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_stall
.eq(0)
18 yield self
.dut
.m_stall
.eq(0)
21 self
.assertEqual((yield self
.dut
.w_result
), result
)
23 sim
.add_sync_process(process
)
28 class MultiplierTestCase(FHDLTestCase
):
30 self
.dut
= Multiplier()
32 # Test cases are taken from the riscv-compliance testbench:
33 # https://github.com/riscv/riscv-compliance/tree/master/riscv-test-suite/rv32im
35 # MUL ----------------------------------------------------------------------------
37 test_mul_0
= tst_op(Funct3
.MUL
, 0x00000000,
38 0x00000000, result
=0x00000000)
39 test_mul_1
= tst_op(Funct3
.MUL
, 0x00000000,
40 0x00000001, result
=0x00000000)
41 test_mul_2
= tst_op(Funct3
.MUL
, 0x00000000,
42 0xffffffff, result
=0x00000000)
43 test_mul_3
= tst_op(Funct3
.MUL
, 0x00000000,
44 0x7fffffff, result
=0x00000000)
45 test_mul_4
= tst_op(Funct3
.MUL
, 0x00000000,
46 0x80000000, result
=0x00000000)
48 test_mul_5
= tst_op(Funct3
.MUL
, 0x00000001,
49 0x00000000, result
=0x00000000)
50 test_mul_6
= tst_op(Funct3
.MUL
, 0x00000001,
51 0x00000001, result
=0x00000001)
52 test_mul_7
= tst_op(Funct3
.MUL
, 0x00000001,
53 0xffffffff, result
=0xffffffff)
54 test_mul_8
= tst_op(Funct3
.MUL
, 0x00000001,
55 0x7fffffff, result
=0x7fffffff)
56 test_mul_9
= tst_op(Funct3
.MUL
, 0x00000001,
57 0x80000000, result
=0x80000000)
59 test_mul_10
= tst_op(Funct3
.MUL
, 0xffffffff,
60 0x00000000, result
=0x00000000)
61 test_mul_11
= tst_op(Funct3
.MUL
, 0xffffffff,
62 0x00000001, result
=0xffffffff)
63 test_mul_12
= tst_op(Funct3
.MUL
, 0xffffffff,
64 0xffffffff, result
=0x00000001)
65 test_mul_13
= tst_op(Funct3
.MUL
, 0xffffffff,
66 0x7fffffff, result
=0x80000001)
67 test_mul_14
= tst_op(Funct3
.MUL
, 0xffffffff,
68 0x80000000, result
=0x80000000)
70 test_mul_15
= tst_op(Funct3
.MUL
, 0x7fffffff,
71 0x00000000, result
=0x00000000)
72 test_mul_16
= tst_op(Funct3
.MUL
, 0x7fffffff,
73 0x00000001, result
=0x7fffffff)
74 test_mul_17
= tst_op(Funct3
.MUL
, 0x7fffffff,
75 0xffffffff, result
=0x80000001)
76 test_mul_18
= tst_op(Funct3
.MUL
, 0x7fffffff,
77 0x7fffffff, result
=0x00000001)
78 test_mul_19
= tst_op(Funct3
.MUL
, 0x7fffffff,
79 0x80000000, result
=0x80000000)
81 test_mul_20
= tst_op(Funct3
.MUL
, 0x80000000,
82 0x00000000, result
=0x00000000)
83 test_mul_21
= tst_op(Funct3
.MUL
, 0x80000000,
84 0x00000001, result
=0x80000000)
85 test_mul_22
= tst_op(Funct3
.MUL
, 0x80000000,
86 0xffffffff, result
=0x80000000)
87 test_mul_23
= tst_op(Funct3
.MUL
, 0x80000000,
88 0x7fffffff, result
=0x80000000)
89 test_mul_24
= tst_op(Funct3
.MUL
, 0x80000000,
90 0x80000000, result
=0x00000000)
92 # MULH ---------------------------------------------------------------------------
94 test_mulh_0
= tst_op(Funct3
.MULH
, 0x00000000,
95 0x00000000, result
=0x00000000)
96 test_mulh_1
= tst_op(Funct3
.MULH
, 0x00000000,
97 0x00000001, result
=0x00000000)
98 test_mulh_2
= tst_op(Funct3
.MULH
, 0x00000000,
99 0xffffffff, result
=0x00000000)
100 test_mulh_3
= tst_op(Funct3
.MULH
, 0x00000000,
101 0x7fffffff, result
=0x00000000)
102 test_mulh_4
= tst_op(Funct3
.MULH
, 0x00000000,
103 0x80000000, result
=0x00000000)
105 test_mulh_5
= tst_op(Funct3
.MULH
, 0x00000001,
106 0x00000000, result
=0x00000000)
107 test_mulh_6
= tst_op(Funct3
.MULH
, 0x00000001,
108 0x00000001, result
=0x00000000)
109 test_mulh_7
= tst_op(Funct3
.MULH
, 0x00000001,
110 0xffffffff, result
=0xffffffff)
111 test_mulh_8
= tst_op(Funct3
.MULH
, 0x00000001,
112 0x7fffffff, result
=0x00000000)
113 test_mulh_9
= tst_op(Funct3
.MULH
, 0x00000001,
114 0x80000000, result
=0xffffffff)
116 test_mulh_10
= tst_op(Funct3
.MULH
, 0xffffffff,
117 0x00000000, result
=0x00000000)
118 test_mulh_11
= tst_op(Funct3
.MULH
, 0xffffffff,
119 0x00000001, result
=0xffffffff)
120 test_mulh_12
= tst_op(Funct3
.MULH
, 0xffffffff,
121 0xffffffff, result
=0x00000000)
122 test_mulh_13
= tst_op(Funct3
.MULH
, 0xffffffff,
123 0x7fffffff, result
=0xffffffff)
124 test_mulh_14
= tst_op(Funct3
.MULH
, 0xffffffff,
125 0x80000000, result
=0x00000000)
127 test_mulh_15
= tst_op(Funct3
.MULH
, 0x7fffffff,
128 0x00000000, result
=0x00000000)
129 test_mulh_16
= tst_op(Funct3
.MULH
, 0x7fffffff,
130 0x00000001, result
=0x00000000)
131 test_mulh_17
= tst_op(Funct3
.MULH
, 0x7fffffff,
132 0xffffffff, result
=0xffffffff)
133 test_mulh_18
= tst_op(Funct3
.MULH
, 0x7fffffff,
134 0x7fffffff, result
=0x3fffffff)
135 test_mulh_19
= tst_op(Funct3
.MULH
, 0x7fffffff,
136 0x80000000, result
=0xc0000000)
138 test_mulh_20
= tst_op(Funct3
.MULH
, 0x80000000,
139 0x00000000, result
=0x00000000)
140 test_mulh_21
= tst_op(Funct3
.MULH
, 0x80000000,
141 0x00000001, result
=0xffffffff)
142 test_mulh_22
= tst_op(Funct3
.MULH
, 0x80000000,
143 0xffffffff, result
=0x00000000)
144 test_mulh_23
= tst_op(Funct3
.MULH
, 0x80000000,
145 0x7fffffff, result
=0xc0000000)
146 test_mulh_24
= tst_op(Funct3
.MULH
, 0x80000000,
147 0x80000000, result
=0x40000000)
149 # MULHSU -------------------------------------------------------------------------
151 test_mulhsu_0
= tst_op(Funct3
.MULHSU
, 0x00000000,
152 0x00000000, result
=0x00000000)
153 test_mulhsu_1
= tst_op(Funct3
.MULHSU
, 0x00000000,
154 0x00000001, result
=0x00000000)
155 test_mulhsu_2
= tst_op(Funct3
.MULHSU
, 0x00000000,
156 0xffffffff, result
=0x00000000)
157 test_mulhsu_3
= tst_op(Funct3
.MULHSU
, 0x00000000,
158 0x7fffffff, result
=0x00000000)
159 test_mulhsu_4
= tst_op(Funct3
.MULHSU
, 0x00000000,
160 0x80000000, result
=0x00000000)
162 test_mulhsu_5
= tst_op(Funct3
.MULHSU
, 0x00000001,
163 0x00000000, result
=0x00000000)
164 test_mulhsu_6
= tst_op(Funct3
.MULHSU
, 0x00000001,
165 0x00000001, result
=0x00000000)
166 test_mulhsu_7
= tst_op(Funct3
.MULHSU
, 0x00000001,
167 0xffffffff, result
=0x00000000)
168 test_mulhsu_8
= tst_op(Funct3
.MULHSU
, 0x00000001,
169 0x7fffffff, result
=0x00000000)
170 test_mulhsu_9
= tst_op(Funct3
.MULHSU
, 0x00000001,
171 0x80000000, result
=0x00000000)
173 test_mulhsu_10
= tst_op(Funct3
.MULHSU
, 0xffffffff,
174 0x00000000, result
=0x00000000)
175 test_mulhsu_11
= tst_op(Funct3
.MULHSU
, 0xffffffff,
176 0x00000001, result
=0xffffffff)
177 test_mulhsu_12
= tst_op(Funct3
.MULHSU
, 0xffffffff,
178 0xffffffff, result
=0xffffffff)
179 test_mulhsu_13
= tst_op(Funct3
.MULHSU
, 0xffffffff,
180 0x7fffffff, result
=0xffffffff)
181 test_mulhsu_14
= tst_op(Funct3
.MULHSU
, 0xffffffff,
182 0x80000000, result
=0xffffffff)
184 test_mulhsu_15
= tst_op(Funct3
.MULHSU
, 0x7fffffff,
185 0x00000000, result
=0x00000000)
186 test_mulhsu_16
= tst_op(Funct3
.MULHSU
, 0x7fffffff,
187 0x00000001, result
=0x00000000)
188 test_mulhsu_17
= tst_op(Funct3
.MULHSU
, 0x7fffffff,
189 0xffffffff, result
=0x7ffffffe)
190 test_mulhsu_18
= tst_op(Funct3
.MULHSU
, 0x7fffffff,
191 0x7fffffff, result
=0x3fffffff)
192 test_mulhsu_19
= tst_op(Funct3
.MULHSU
, 0x7fffffff,
193 0x80000000, result
=0x3fffffff)
195 test_mulhsu_20
= tst_op(Funct3
.MULHSU
, 0x80000000,
196 0x00000000, result
=0x00000000)
197 test_mulhsu_21
= tst_op(Funct3
.MULHSU
, 0x80000000,
198 0x00000001, result
=0xffffffff)
199 test_mulhsu_22
= tst_op(Funct3
.MULHSU
, 0x80000000,
200 0xffffffff, result
=0x80000000)
201 test_mulhsu_23
= tst_op(Funct3
.MULHSU
, 0x80000000,
202 0x7fffffff, result
=0xc0000000)
203 test_mulhsu_24
= tst_op(Funct3
.MULHSU
, 0x80000000,
204 0x80000000, result
=0xc0000000)
206 # MULHU --------------------------------------------------------------------------
208 test_mulhu_0
= tst_op(Funct3
.MULHU
, 0x00000000,
209 0x00000000, result
=0x00000000)
210 test_mulhu_1
= tst_op(Funct3
.MULHU
, 0x00000000,
211 0x00000001, result
=0x00000000)
212 test_mulhu_2
= tst_op(Funct3
.MULHU
, 0x00000000,
213 0xffffffff, result
=0x00000000)
214 test_mulhu_3
= tst_op(Funct3
.MULHU
, 0x00000000,
215 0x7fffffff, result
=0x00000000)
216 test_mulhu_4
= tst_op(Funct3
.MULHU
, 0x00000000,
217 0x80000000, result
=0x00000000)
219 test_mulhu_5
= tst_op(Funct3
.MULHU
, 0x00000001,
220 0x00000000, result
=0x00000000)
221 test_mulhu_6
= tst_op(Funct3
.MULHU
, 0x00000001,
222 0x00000001, result
=0x00000000)
223 test_mulhu_7
= tst_op(Funct3
.MULHU
, 0x00000001,
224 0xffffffff, result
=0x00000000)
225 test_mulhu_8
= tst_op(Funct3
.MULHU
, 0x00000001,
226 0x7fffffff, result
=0x00000000)
227 test_mulhu_9
= tst_op(Funct3
.MULHU
, 0x00000001,
228 0x80000000, result
=0x00000000)
230 test_mulhu_10
= tst_op(Funct3
.MULHU
, 0xffffffff,
231 0x00000000, result
=0x00000000)
232 test_mulhu_11
= tst_op(Funct3
.MULHU
, 0xffffffff,
233 0x00000001, result
=0x00000000)
234 test_mulhu_12
= tst_op(Funct3
.MULHU
, 0xffffffff,
235 0xffffffff, result
=0xfffffffe)
236 test_mulhu_13
= tst_op(Funct3
.MULHU
, 0xffffffff,
237 0x7fffffff, result
=0x7ffffffe)
238 test_mulhu_14
= tst_op(Funct3
.MULHU
, 0xffffffff,
239 0x80000000, result
=0x7fffffff)
241 test_mulhu_15
= tst_op(Funct3
.MULHU
, 0x7fffffff,
242 0x00000000, result
=0x00000000)
243 test_mulhu_16
= tst_op(Funct3
.MULHU
, 0x7fffffff,
244 0x00000001, result
=0x00000000)
245 test_mulhu_17
= tst_op(Funct3
.MULHU
, 0x7fffffff,
246 0xffffffff, result
=0x7ffffffe)
247 test_mulhu_18
= tst_op(Funct3
.MULHU
, 0x7fffffff,
248 0x7fffffff, result
=0x3fffffff)
249 test_mulhu_19
= tst_op(Funct3
.MULHU
, 0x7fffffff,
250 0x80000000, result
=0x3fffffff)
252 test_mulhu_20
= tst_op(Funct3
.MULHU
, 0x80000000,
253 0x00000000, result
=0x00000000)
254 test_mulhu_21
= tst_op(Funct3
.MULHU
, 0x80000000,
255 0x00000001, result
=0x00000000)
256 test_mulhu_22
= tst_op(Funct3
.MULHU
, 0x80000000,
257 0xffffffff, result
=0x7fffffff)
258 test_mulhu_23
= tst_op(Funct3
.MULHU
, 0x80000000,
259 0x7fffffff, result
=0x3fffffff)
260 test_mulhu_24
= tst_op(Funct3
.MULHU
, 0x80000000,
261 0x80000000, result
=0x40000000)