3 from random
import randint
4 from random
import seed
5 from sfpy
import Float32
7 from nmigen
import Module
, Signal
8 from nmigen
.compat
.sim
import run_simulation
10 from nmigen_add_experiment
import FPADD
16 return ((x
& 0x7f800000) >> 23) - 127
19 return ((x
& 0x80000000) >> 31)
22 return get_exponent(x
) == 128 and get_mantissa(x
) != 0
25 return get_exponent(x
) == 128 and get_mantissa(x
) == 0
28 return is_inf(x
) and not get_sign(x
)
31 return is_inf(x
) and get_sign(x
)
35 (is_pos_inf(x
) and is_pos_inf(y
)) or
36 (is_neg_inf(x
) and is_neg_inf(y
)) or
37 (is_nan(x
) and is_nan(y
)) or
41 def get_case(dut
, a
, b
):
42 yield dut
.in_a
.v
.eq(a
)
43 yield dut
.in_a
.stb
.eq(1)
46 a_ack
= (yield dut
.in_a
.ack
)
48 yield dut
.in_b
.v
.eq(b
)
49 yield dut
.in_b
.stb
.eq(1)
50 b_ack
= (yield dut
.in_b
.ack
)
55 out_z_stb
= (yield dut
.out_z
.stb
)
58 yield dut
.in_a
.stb
.eq(0)
59 yield dut
.in_b
.stb
.eq(0)
60 yield dut
.out_z
.ack
.eq(1)
62 yield dut
.out_z
.ack
.eq(0)
67 out_z
= yield dut
.out_z
.v
70 def check_case(dut
, a
, b
, z
):
71 out_z
= yield from get_case(dut
, a
, b
)
72 assert out_z
== z
, "Output z 0x%x not equal to expected 0x%x" % (out_z
, z
)
75 def run_test(dut
, stimulus_a
, stimulus_b
):
77 expected_responses
= []
79 for a
, b
in zip(stimulus_a
, stimulus_b
):
80 af
= Float32
.from_bits(a
)
81 bf
= Float32
.from_bits(b
)
83 expected_responses
.append(z
.get_bits())
85 actual
= yield from get_case(dut
, a
, b
)
86 actual_responses
.append(actual
)
88 if len(actual_responses
) < len(expected_responses
):
89 print ("Fail ... not enough results")
92 for expected
, actual
, a
, b
in zip(expected_responses
, actual_responses
,
93 stimulus_a
, stimulus_b
):
94 passed
= match(expected
, actual
)
98 print ("Fail ... expected:", hex(expected
), "actual:", hex(actual
))
101 print ("a mantissa:", a
& 0x7fffff)
102 print ("a exponent:", ((a
& 0x7f800000) >> 23) - 127)
103 print ("a sign:", ((a
& 0x80000000) >> 31))
106 print ("b mantissa:", b
& 0x7fffff)
107 print ("b exponent:", ((b
& 0x7f800000) >> 23) - 127)
108 print ("b sign:", ((b
& 0x80000000) >> 31))
110 print (hex(expected
))
111 print ("expected mantissa:", expected
& 0x7fffff)
112 print ("expected exponent:", ((expected
& 0x7f800000) >> 23) - 127)
113 print ("expected sign:", ((expected
& 0x80000000) >> 31))
116 print ("actual mantissa:", actual
& 0x7fffff)
117 print ("actual exponent:", ((actual
& 0x7f800000) >> 23) - 127)
118 print ("actual sign:", ((actual
& 0x80000000) >> 31))
123 yield from check_case(dut
, 0xfe34f995, 0xff5d59ad, 0xff800000)
124 yield from check_case(dut
, 0x82471f51, 0x243985f, 0x801c3790)
125 yield from check_case(dut
, 0, 0, 0)
126 yield from check_case(dut
, 0x40000000, 0xc0000000, 0x00000000)
127 yield from check_case(dut
, 0x3F800000, 0x40000000, 0x40400000)
128 yield from check_case(dut
, 0x40000000, 0x3F800000, 0x40400000)
129 yield from check_case(dut
, 0x447A0000, 0x4488B000, 0x4502D800)
130 yield from check_case(dut
, 0x463B800A, 0x42BA8A3D, 0x463CF51E)
131 yield from check_case(dut
, 0x42BA8A3D, 0x463B800A, 0x463CF51E)
132 yield from check_case(dut
, 0x463B800A, 0xC2BA8A3D, 0x463A0AF6)
133 yield from check_case(dut
, 0xC2BA8A3D, 0x463B800A, 0x463A0AF6)
134 yield from check_case(dut
, 0xC63B800A, 0x42BA8A3D, 0xC63A0AF6)
135 yield from check_case(dut
, 0x42BA8A3D, 0xC63B800A, 0xC63A0AF6)
136 yield from check_case(dut
, 0xFFFFFFFF, 0xC63B800A, 0xFFC00000)
137 yield from check_case(dut
, 0x7F800000, 0x00000000, 0x7F800000)
138 yield from check_case(dut
, 0x00000000, 0x7F800000, 0x7F800000)
139 yield from check_case(dut
, 0xFF800000, 0x00000000, 0xFF800000)
140 yield from check_case(dut
, 0x00000000, 0xFF800000, 0xFF800000)
141 yield from check_case(dut
, 0x7F800000, 0x7F800000, 0x7F800000)
142 yield from check_case(dut
, 0xFF800000, 0xFF800000, 0xFF800000)
143 yield from check_case(dut
, 0x7F800000, 0xFF800000, 0xFFC00000)
144 yield from check_case(dut
, 0xFF800000, 0x7F800000, 0x7FC00000)
145 yield from check_case(dut
, 0x00018643, 0x00FA72A4, 0x00FBF8E7)
146 yield from check_case(dut
, 0x001A2239, 0x00FA72A4, 0x010A4A6E)
147 yield from check_case(dut
, 0x3F7FFFFE, 0x3F7FFFFE, 0x3FFFFFFE)
148 yield from check_case(dut
, 0x7EFFFFEE, 0x7EFFFFEE, 0x7F7FFFEE)
149 yield from check_case(dut
, 0x7F7FFFEE, 0xFEFFFFEE, 0x7EFFFFEE)
150 yield from check_case(dut
, 0x7F7FFFEE, 0x756CA884, 0x7F7FFFFD)
151 yield from check_case(dut
, 0x7F7FFFEE, 0x758A0CF8, 0x7F7FFFFF)
152 yield from check_case(dut
, 0x42500000, 0x51A7A358, 0x51A7A358)
153 yield from check_case(dut
, 0x51A7A358, 0x42500000, 0x51A7A358)
154 yield from check_case(dut
, 0x4E5693A4, 0x42500000, 0x4E5693A5)
155 yield from check_case(dut
, 0x42500000, 0x4E5693A4, 0x4E5693A5)
156 #yield from check_case(dut, 1, 0, 1)
157 #yield from check_case(dut, 1, 1, 1)
162 stimulus_a
= [0x22cb525a, 0x40000000, 0x83e73d5c, 0xbf9b1e94, 0x34082401, 0x5e8ef81, 0x5c75da81, 0x2b017]
163 stimulus_b
= [0xadd79efa, 0xC0000000, 0x1c800000, 0xc038ed3a, 0xb328cd45, 0x114f3db, 0x2f642a39, 0xff3807ab]
164 yield from run_test(dut
, stimulus_a
, stimulus_b
)
165 count
+= len(stimulus_a
)
166 print (count
, "vectors passed")
169 from itertools
import permutations
170 stimulus_a
= [i
[0] for i
in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)]
171 stimulus_b
= [i
[1] for i
in permutations([0x80000000, 0x00000000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000], 2)]
172 yield from run_test(dut
, stimulus_a
, stimulus_b
)
173 count
+= len(stimulus_a
)
174 print (count
, "vectors passed")
177 stimulus_a
= [0x80000000 for i
in range(1000)]
178 stimulus_b
= [randint(0, 1<<32) for i
in range(1000)]
179 yield from run_test(dut
, stimulus_a
, stimulus_b
)
180 count
+= len(stimulus_a
)
181 print (count
, "vectors passed")
183 stimulus_a
= [0x00000000 for i
in range(1000)]
184 stimulus_b
= [randint(0, 1<<32) for i
in range(1000)]
185 yield from run_test(dut
, stimulus_a
, stimulus_b
)
186 count
+= len(stimulus_a
)
187 print (count
, "vectors passed")
189 stimulus_b
= [0x80000000 for i
in range(1000)]
190 stimulus_a
= [randint(0, 1<<32) for i
in range(1000)]
191 yield from run_test(dut
, stimulus_a
, stimulus_b
)
192 count
+= len(stimulus_a
)
193 print (count
, "vectors passed")
195 stimulus_b
= [0x00000000 for i
in range(1000)]
196 stimulus_a
= [randint(0, 1<<32) for i
in range(1000)]
197 yield from run_test(dut
, stimulus_a
, stimulus_b
)
198 count
+= len(stimulus_a
)
199 print (count
, "vectors passed")
201 stimulus_a
= [0x7F800000 for i
in range(1000)]
202 stimulus_b
= [randint(0, 1<<32) for i
in range(1000)]
203 yield from run_test(dut
, stimulus_a
, stimulus_b
)
204 count
+= len(stimulus_a
)
205 print (count
, "vectors passed")
207 stimulus_a
= [0xFF800000 for i
in range(1000)]
208 stimulus_b
= [randint(0, 1<<32) for i
in range(1000)]
209 yield from run_test(dut
, stimulus_a
, stimulus_b
)
210 count
+= len(stimulus_a
)
211 print (count
, "vectors passed")
213 stimulus_b
= [0x7F800000 for i
in range(1000)]
214 stimulus_a
= [randint(0, 1<<32) for i
in range(1000)]
215 yield from run_test(dut
, stimulus_a
, stimulus_b
)
216 count
+= len(stimulus_a
)
217 print (count
, "vectors passed")
219 stimulus_b
= [0xFF800000 for i
in range(1000)]
220 stimulus_a
= [randint(0, 1<<32) for i
in range(1000)]
221 yield from run_test(dut
, stimulus_a
, stimulus_b
)
222 count
+= len(stimulus_a
)
223 print (count
, "vectors passed")
225 stimulus_a
= [0x7FC00000 for i
in range(1000)]
226 stimulus_b
= [randint(0, 1<<32) for i
in range(1000)]
227 yield from run_test(dut
, stimulus_a
, stimulus_b
)
228 count
+= len(stimulus_a
)
229 print (count
, "vectors passed")
231 stimulus_a
= [0xFFC00000 for i
in range(1000)]
232 stimulus_b
= [randint(0, 1<<32) for i
in range(1000)]
233 yield from run_test(dut
, stimulus_a
, stimulus_b
)
234 count
+= len(stimulus_a
)
235 print (count
, "vectors passed")
237 stimulus_b
= [0x7FC00000 for i
in range(1000)]
238 stimulus_a
= [randint(0, 1<<32) for i
in range(1000)]
239 yield from run_test(dut
, stimulus_a
, stimulus_b
)
240 count
+= len(stimulus_a
)
241 print (count
, "vectors passed")
243 stimulus_b
= [0xFFC00000 for i
in range(1000)]
244 stimulus_a
= [randint(0, 1<<32) for i
in range(1000)]
245 yield from run_test(dut
, stimulus_a
, stimulus_b
)
246 count
+= len(stimulus_a
)
247 print (count
, "vectors passed")
250 for i
in range(100000):
251 stimulus_a
= [randint(0, 1<<32) for i
in range(1000)]
252 stimulus_b
= [randint(0, 1<<32) for i
in range(1000)]
253 yield from run_test(dut
, stimulus_a
, stimulus_b
)
255 print (count
, "random vectors passed")
257 if __name__
== '__main__':
258 dut
= FPADD(width
=32, single_cycle
=True)
259 run_simulation(dut
, testbench(dut
), vcd_name
="test_add.vcd")