e4b2963fb75843a3a1162c0496042e88dddd842d
2 from nmigen
.back
.pysim
import *
3 from nmigen
.test
.utils
import *
5 from ..units
.multiplier
import *
6 from ..isa
import Funct3
9 def test_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
= test_op(Funct3
.MUL
, 0x00000000, 0x00000000, result
=0x00000000)
38 test_mul_1
= test_op(Funct3
.MUL
, 0x00000000, 0x00000001, result
=0x00000000)
39 test_mul_2
= test_op(Funct3
.MUL
, 0x00000000, 0xffffffff, result
=0x00000000)
40 test_mul_3
= test_op(Funct3
.MUL
, 0x00000000, 0x7fffffff, result
=0x00000000)
41 test_mul_4
= test_op(Funct3
.MUL
, 0x00000000, 0x80000000, result
=0x00000000)
43 test_mul_5
= test_op(Funct3
.MUL
, 0x00000001, 0x00000000, result
=0x00000000)
44 test_mul_6
= test_op(Funct3
.MUL
, 0x00000001, 0x00000001, result
=0x00000001)
45 test_mul_7
= test_op(Funct3
.MUL
, 0x00000001, 0xffffffff, result
=0xffffffff)
46 test_mul_8
= test_op(Funct3
.MUL
, 0x00000001, 0x7fffffff, result
=0x7fffffff)
47 test_mul_9
= test_op(Funct3
.MUL
, 0x00000001, 0x80000000, result
=0x80000000)
49 test_mul_10
= test_op(Funct3
.MUL
, 0xffffffff, 0x00000000, result
=0x00000000)
50 test_mul_11
= test_op(Funct3
.MUL
, 0xffffffff, 0x00000001, result
=0xffffffff)
51 test_mul_12
= test_op(Funct3
.MUL
, 0xffffffff, 0xffffffff, result
=0x00000001)
52 test_mul_13
= test_op(Funct3
.MUL
, 0xffffffff, 0x7fffffff, result
=0x80000001)
53 test_mul_14
= test_op(Funct3
.MUL
, 0xffffffff, 0x80000000, result
=0x80000000)
55 test_mul_15
= test_op(Funct3
.MUL
, 0x7fffffff, 0x00000000, result
=0x00000000)
56 test_mul_16
= test_op(Funct3
.MUL
, 0x7fffffff, 0x00000001, result
=0x7fffffff)
57 test_mul_17
= test_op(Funct3
.MUL
, 0x7fffffff, 0xffffffff, result
=0x80000001)
58 test_mul_18
= test_op(Funct3
.MUL
, 0x7fffffff, 0x7fffffff, result
=0x00000001)
59 test_mul_19
= test_op(Funct3
.MUL
, 0x7fffffff, 0x80000000, result
=0x80000000)
61 test_mul_20
= test_op(Funct3
.MUL
, 0x80000000, 0x00000000, result
=0x00000000)
62 test_mul_21
= test_op(Funct3
.MUL
, 0x80000000, 0x00000001, result
=0x80000000)
63 test_mul_22
= test_op(Funct3
.MUL
, 0x80000000, 0xffffffff, result
=0x80000000)
64 test_mul_23
= test_op(Funct3
.MUL
, 0x80000000, 0x7fffffff, result
=0x80000000)
65 test_mul_24
= test_op(Funct3
.MUL
, 0x80000000, 0x80000000, result
=0x00000000)
67 # MULH ---------------------------------------------------------------------------
69 test_mulh_0
= test_op(Funct3
.MULH
, 0x00000000, 0x00000000, result
=0x00000000)
70 test_mulh_1
= test_op(Funct3
.MULH
, 0x00000000, 0x00000001, result
=0x00000000)
71 test_mulh_2
= test_op(Funct3
.MULH
, 0x00000000, 0xffffffff, result
=0x00000000)
72 test_mulh_3
= test_op(Funct3
.MULH
, 0x00000000, 0x7fffffff, result
=0x00000000)
73 test_mulh_4
= test_op(Funct3
.MULH
, 0x00000000, 0x80000000, result
=0x00000000)
75 test_mulh_5
= test_op(Funct3
.MULH
, 0x00000001, 0x00000000, result
=0x00000000)
76 test_mulh_6
= test_op(Funct3
.MULH
, 0x00000001, 0x00000001, result
=0x00000000)
77 test_mulh_7
= test_op(Funct3
.MULH
, 0x00000001, 0xffffffff, result
=0xffffffff)
78 test_mulh_8
= test_op(Funct3
.MULH
, 0x00000001, 0x7fffffff, result
=0x00000000)
79 test_mulh_9
= test_op(Funct3
.MULH
, 0x00000001, 0x80000000, result
=0xffffffff)
81 test_mulh_10
= test_op(Funct3
.MULH
, 0xffffffff, 0x00000000, result
=0x00000000)
82 test_mulh_11
= test_op(Funct3
.MULH
, 0xffffffff, 0x00000001, result
=0xffffffff)
83 test_mulh_12
= test_op(Funct3
.MULH
, 0xffffffff, 0xffffffff, result
=0x00000000)
84 test_mulh_13
= test_op(Funct3
.MULH
, 0xffffffff, 0x7fffffff, result
=0xffffffff)
85 test_mulh_14
= test_op(Funct3
.MULH
, 0xffffffff, 0x80000000, result
=0x00000000)
87 test_mulh_15
= test_op(Funct3
.MULH
, 0x7fffffff, 0x00000000, result
=0x00000000)
88 test_mulh_16
= test_op(Funct3
.MULH
, 0x7fffffff, 0x00000001, result
=0x00000000)
89 test_mulh_17
= test_op(Funct3
.MULH
, 0x7fffffff, 0xffffffff, result
=0xffffffff)
90 test_mulh_18
= test_op(Funct3
.MULH
, 0x7fffffff, 0x7fffffff, result
=0x3fffffff)
91 test_mulh_19
= test_op(Funct3
.MULH
, 0x7fffffff, 0x80000000, result
=0xc0000000)
93 test_mulh_20
= test_op(Funct3
.MULH
, 0x80000000, 0x00000000, result
=0x00000000)
94 test_mulh_21
= test_op(Funct3
.MULH
, 0x80000000, 0x00000001, result
=0xffffffff)
95 test_mulh_22
= test_op(Funct3
.MULH
, 0x80000000, 0xffffffff, result
=0x00000000)
96 test_mulh_23
= test_op(Funct3
.MULH
, 0x80000000, 0x7fffffff, result
=0xc0000000)
97 test_mulh_24
= test_op(Funct3
.MULH
, 0x80000000, 0x80000000, result
=0x40000000)
99 # MULHSU -------------------------------------------------------------------------
101 test_mulhsu_0
= test_op(Funct3
.MULHSU
, 0x00000000, 0x00000000, result
=0x00000000)
102 test_mulhsu_1
= test_op(Funct3
.MULHSU
, 0x00000000, 0x00000001, result
=0x00000000)
103 test_mulhsu_2
= test_op(Funct3
.MULHSU
, 0x00000000, 0xffffffff, result
=0x00000000)
104 test_mulhsu_3
= test_op(Funct3
.MULHSU
, 0x00000000, 0x7fffffff, result
=0x00000000)
105 test_mulhsu_4
= test_op(Funct3
.MULHSU
, 0x00000000, 0x80000000, result
=0x00000000)
107 test_mulhsu_5
= test_op(Funct3
.MULHSU
, 0x00000001, 0x00000000, result
=0x00000000)
108 test_mulhsu_6
= test_op(Funct3
.MULHSU
, 0x00000001, 0x00000001, result
=0x00000000)
109 test_mulhsu_7
= test_op(Funct3
.MULHSU
, 0x00000001, 0xffffffff, result
=0x00000000)
110 test_mulhsu_8
= test_op(Funct3
.MULHSU
, 0x00000001, 0x7fffffff, result
=0x00000000)
111 test_mulhsu_9
= test_op(Funct3
.MULHSU
, 0x00000001, 0x80000000, result
=0x00000000)
113 test_mulhsu_10
= test_op(Funct3
.MULHSU
, 0xffffffff, 0x00000000, result
=0x00000000)
114 test_mulhsu_11
= test_op(Funct3
.MULHSU
, 0xffffffff, 0x00000001, result
=0xffffffff)
115 test_mulhsu_12
= test_op(Funct3
.MULHSU
, 0xffffffff, 0xffffffff, result
=0xffffffff)
116 test_mulhsu_13
= test_op(Funct3
.MULHSU
, 0xffffffff, 0x7fffffff, result
=0xffffffff)
117 test_mulhsu_14
= test_op(Funct3
.MULHSU
, 0xffffffff, 0x80000000, result
=0xffffffff)
119 test_mulhsu_15
= test_op(Funct3
.MULHSU
, 0x7fffffff, 0x00000000, result
=0x00000000)
120 test_mulhsu_16
= test_op(Funct3
.MULHSU
, 0x7fffffff, 0x00000001, result
=0x00000000)
121 test_mulhsu_17
= test_op(Funct3
.MULHSU
, 0x7fffffff, 0xffffffff, result
=0x7ffffffe)
122 test_mulhsu_18
= test_op(Funct3
.MULHSU
, 0x7fffffff, 0x7fffffff, result
=0x3fffffff)
123 test_mulhsu_19
= test_op(Funct3
.MULHSU
, 0x7fffffff, 0x80000000, result
=0x3fffffff)
125 test_mulhsu_20
= test_op(Funct3
.MULHSU
, 0x80000000, 0x00000000, result
=0x00000000)
126 test_mulhsu_21
= test_op(Funct3
.MULHSU
, 0x80000000, 0x00000001, result
=0xffffffff)
127 test_mulhsu_22
= test_op(Funct3
.MULHSU
, 0x80000000, 0xffffffff, result
=0x80000000)
128 test_mulhsu_23
= test_op(Funct3
.MULHSU
, 0x80000000, 0x7fffffff, result
=0xc0000000)
129 test_mulhsu_24
= test_op(Funct3
.MULHSU
, 0x80000000, 0x80000000, result
=0xc0000000)
131 # MULHU --------------------------------------------------------------------------
133 test_mulhu_0
= test_op(Funct3
.MULHU
, 0x00000000, 0x00000000, result
=0x00000000)
134 test_mulhu_1
= test_op(Funct3
.MULHU
, 0x00000000, 0x00000001, result
=0x00000000)
135 test_mulhu_2
= test_op(Funct3
.MULHU
, 0x00000000, 0xffffffff, result
=0x00000000)
136 test_mulhu_3
= test_op(Funct3
.MULHU
, 0x00000000, 0x7fffffff, result
=0x00000000)
137 test_mulhu_4
= test_op(Funct3
.MULHU
, 0x00000000, 0x80000000, result
=0x00000000)
139 test_mulhu_5
= test_op(Funct3
.MULHU
, 0x00000001, 0x00000000, result
=0x00000000)
140 test_mulhu_6
= test_op(Funct3
.MULHU
, 0x00000001, 0x00000001, result
=0x00000000)
141 test_mulhu_7
= test_op(Funct3
.MULHU
, 0x00000001, 0xffffffff, result
=0x00000000)
142 test_mulhu_8
= test_op(Funct3
.MULHU
, 0x00000001, 0x7fffffff, result
=0x00000000)
143 test_mulhu_9
= test_op(Funct3
.MULHU
, 0x00000001, 0x80000000, result
=0x00000000)
145 test_mulhu_10
= test_op(Funct3
.MULHU
, 0xffffffff, 0x00000000, result
=0x00000000)
146 test_mulhu_11
= test_op(Funct3
.MULHU
, 0xffffffff, 0x00000001, result
=0x00000000)
147 test_mulhu_12
= test_op(Funct3
.MULHU
, 0xffffffff, 0xffffffff, result
=0xfffffffe)
148 test_mulhu_13
= test_op(Funct3
.MULHU
, 0xffffffff, 0x7fffffff, result
=0x7ffffffe)
149 test_mulhu_14
= test_op(Funct3
.MULHU
, 0xffffffff, 0x80000000, result
=0x7fffffff)
151 test_mulhu_15
= test_op(Funct3
.MULHU
, 0x7fffffff, 0x00000000, result
=0x00000000)
152 test_mulhu_16
= test_op(Funct3
.MULHU
, 0x7fffffff, 0x00000001, result
=0x00000000)
153 test_mulhu_17
= test_op(Funct3
.MULHU
, 0x7fffffff, 0xffffffff, result
=0x7ffffffe)
154 test_mulhu_18
= test_op(Funct3
.MULHU
, 0x7fffffff, 0x7fffffff, result
=0x3fffffff)
155 test_mulhu_19
= test_op(Funct3
.MULHU
, 0x7fffffff, 0x80000000, result
=0x3fffffff)
157 test_mulhu_20
= test_op(Funct3
.MULHU
, 0x80000000, 0x00000000, result
=0x00000000)
158 test_mulhu_21
= test_op(Funct3
.MULHU
, 0x80000000, 0x00000001, result
=0x00000000)
159 test_mulhu_22
= test_op(Funct3
.MULHU
, 0x80000000, 0xffffffff, result
=0x7fffffff)
160 test_mulhu_23
= test_op(Funct3
.MULHU
, 0x80000000, 0x7fffffff, result
=0x3fffffff)
161 test_mulhu_24
= test_op(Funct3
.MULHU
, 0x80000000, 0x80000000, result
=0x40000000)