Turned the add_1 verilog state into nmigen
[ieee754fpu.git] / src / add / nmigen_add_experiment.py
1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
3 # 2013-12-12
4
5 from nmigen import Module, Signal, Cat
6 from nmigen.cli import main
7
8
9 class FPADD:
10 def __init__(self, width):
11 self.width = width
12
13 self.in_a = Signal(width)
14 self.in_a_stb = Signal()
15 self.in_a_ack = Signal()
16
17 self.in_b = Signal(width)
18 self.in_b_stb = Signal()
19 self.in_b_ack = Signal()
20
21 self.out_z = Signal(width)
22 self.out_z_stb = Signal()
23 self.out_z_ack = Signal()
24
25 s_out_z_stb = Signal()
26 s_out_z = Signal(width)
27 s_in_a_ack = Signal()
28 s_in_b_ack = Signal()
29
30 def get_fragment(self, platform):
31 m = Module()
32
33 # Latches
34 a = Signal(self.width)
35 b = Signal(self.width)
36 z = Signal(self.width)
37
38 # Mantissa
39 a_m = Signal(27) # ??? seems to be 1 bit extra??
40 b_m = Signal(27) # ??? seems to be 1 bit extra??
41 z_m = Signal(24)
42
43 # Exponent
44 a_e = Signal(10)
45 b_e = Signal(10)
46 z_e = Signal(10)
47
48 # Sign
49 a_s = Signal()
50 b_s = Signal()
51 z_s = Signal()
52
53 guard = Signal()
54 round_bit = Signal()
55 sticky = Signal()
56
57 tot = Signal(28)
58
59 with m.FSM() as fsm:
60
61 # ******
62 # gets operand a
63
64 with m.State("get_a"):
65 with m.If((self.in_a_ack) & (self.in_a_stb)):
66 m.next = "get_b"
67 m.d.sync += [
68 a.eq(self.in_a),
69 self.in_a_ack.eq(0)
70 ]
71 with m.Else():
72 m.d.sync += self.in_a_ack.eq(1)
73
74 # ******
75 # gets operand b
76
77 with m.State("get_b"):
78 with m.If((self.in_b_ack) & (self.in_b_stb)):
79 m.next = "get_a"
80 m.d.sync += [
81 b.eq(self.in_b),
82 self.in_b_ack.eq(0)
83 ]
84 with m.Else():
85 m.d.sync += self.in_b_ack.eq(1)
86
87 # ******
88 # unpacks operands into sign, mantissa and exponent
89
90 with m.State("unpack"):
91 m.next = "special_cases"
92 m.d.sync += [
93 # mantissa
94 a_m.eq(Cat(0, 0, 0, a[0:23])),
95 b_m.eq(Cat(0, 0, 0, b[0:23])),
96 # exponent (take off exponent bias, here)
97 a_e.eq(Cat(a[23:31]) - 127),
98 b_e.eq(Cat(b[23:31]) - 127),
99 # sign
100 a_s.eq(Cat(a[31])),
101 b_s.eq(Cat(b[31]))
102 ]
103
104 # ******
105 # special cases: NaNs, infs, zeros, denormalised
106
107 with m.State("special_cases"):
108
109 # if a is NaN or b is NaN return NaN
110 with m.If(((a_e == 128) & (a_m != 0)) | \
111 ((b_e == 128) & (b_m != 0))):
112 m.next = "put_z"
113 m.d.sync += [
114 z[31].eq(1), # sign: 1
115 z[23:31].eq(255), # exp: 0b11111...
116 z[22].eq(1), # mantissa top bit: 1
117 z[0:22].eq(0) # mantissa rest: 0b0000...
118 ]
119
120 # if a is inf return inf (or NaN)
121 with m.Elif(a_e == 128):
122 m.next = "put_z"
123 m.d.sync += [
124 z[31].eq(a_s), # sign: a_s
125 z[23:31].eq(255), # exp: 0b11111...
126 z[0:23].eq(0) # mantissa rest: 0b0000...
127 ]
128 # if a is inf and signs don't match return NaN
129 with m.If((b_e == 128) & (a_s != b_s)):
130 m.d.sync += [
131 z[31].eq(b_s), # sign: b_s
132 z[23:31].eq(255), # exp: 0b11111...
133 z[22].eq(1), # mantissa top bit: 1
134 z[0:22].eq(0) # mantissa rest: 0b0000...
135 ]
136 # if b is inf return inf
137 with m.Elif(b_e == 128):
138 m.next = "put_z"
139 m.d.sync += [
140 z[31].eq(b_s), # sign: b_s
141 z[23:31].eq(255), # exp: 0b11111...
142 z[0:23].eq(0) # mantissa rest: 0b0000...
143 ]
144
145 # if a is zero and b zero return signed-a/b
146 with m.Elif(((a_e == -127) & (a_m == 0)) & \
147 ((b_e == -127) & (b_m == 0))):
148 m.next = "put_z"
149 m.d.sync += [
150 z[31].eq(a_s & b_s), # sign: a/b_s
151 z[23:31].eq(b_e[0:8] + 127), # exp: b_e (plus bias)
152 z[0:23].eq(b_m[3:26]) # mantissa: b_m top bits
153 ]
154
155 # if a is zero return b
156 with m.Elif((a_e == -127) & (a_m == 0)):
157 m.next = "put_z"
158 m.d.sync += [
159 z[31].eq(b_s), # sign: a/b_s
160 z[23:31].eq(b_e[0:8] + 127), # exp: b_e (plus bias)
161 z[0:23].eq(b_m[3:26]) # mantissa: b_m top bits
162 ]
163
164 # if b is zero return a
165 with m.Elif((b_e == -127) & (b_m == 0)):
166 m.next = "put_z"
167 m.d.sync += [
168 z[31].eq(a_s), # sign: a/b_s
169 z[23:31].eq(a_e[0:8] + 127), # exp: a_e (plus bias)
170 z[0:23].eq(a_m[3:26]) # mantissa: a_m top bits
171 ]
172
173 # Denormalised Number checks
174 with m.Else():
175 m.next = "align"
176 # denormalise a check
177 with m.If(a_e == -127):
178 m.d.sync += a_e.eq(-126) # limit a exponent
179 with m.Else():
180 m.d.sync += a_m[26].eq(1) # set highest mantissa bit
181 # denormalise b check
182 with m.If(b_e == -127):
183 m.d.sync += b_e.eq(-126) # limit b exponent
184 with m.Else():
185 m.d.sync += b_m[26].eq(1) # set highest mantissa bit
186
187 # ******
188 # First stage of add
189
190 with m.State("add_0"):
191 m.next = "add_1"
192 m.d.sync += z_e.eq(a_e)
193 # same-sign (both negative or both positive) add mantissas
194 with m.If(a_s == b_s):
195 m.d.sync += [
196 tot.eq(a_m + b_m),
197 z_s.eq(a_s)
198 ]
199 # a mantissa greater than b, use a
200 with m.Else(a_m >= b_m):
201 m.d.sync += [
202 tot.eq(a_m - b_m),
203 z_s.eq(a_s)
204 ]
205 # b mantissa greater than a, use b
206 with m.Else():
207 m.sync += [
208 tot.eq(b_m - a_m),
209 z_s.eq(b_s)
210 ]
211
212 with m.State("add_1"):
213 m.next = "normalise_1"
214
215 with m.If(tot[27]):
216 m.d.sync += [
217 z_m.eq(tot[4:27]),
218 guard.eq(tot[3]),
219 round_bit.eq(tot[2]),
220 sticky.eq(tot[1] | tot[0]),
221 z_e.eq(z_e + 1)
222 ]
223
224 with m.Else():
225 m.d.sync += [
226 z_m.eq(tot[3:26]),
227 guard.eq(tot[2]),
228 round_bit.eq(tot[1]),
229 sticky.eq(tot[0])
230 ]
231 return m
232
233 """
234 always @(posedge clk)
235 begin
236
237 case(state)
238
239 get_a:
240 begin
241 s_in_a_ack <= 1;
242 if (s_in_a_ack && in_a_stb) begin
243 a <= in_a;
244 s_in_a_ack <= 0;
245 state <= get_b;
246 end
247 end
248
249 get_b:
250 begin
251 s_in_b_ack <= 1;
252 if (s_in_b_ack && in_b_stb) begin
253 b <= in_b;
254 s_in_b_ack <= 0;
255 state <= unpack;
256 end
257 end
258
259 unpack:
260 begin
261 a_m <= {a[22 : 0], 3'd0};
262 b_m <= {b[22 : 0], 3'd0};
263 a_e <= a[30 : 23] - 127;
264 b_e <= b[30 : 23] - 127;
265 a_s <= a[31];
266 b_s <= b[31];
267 state <= special_cases;
268 end
269
270 special_cases:
271 begin
272 //if a is NaN or b is NaN return NaN
273 if ((a_e == 128 && a_m != 0) || (b_e == 128 && b_m != 0)) begin
274 z[31] <= 1;
275 z[30:23] <= 255;
276 z[22] <= 1;
277 z[21:0] <= 0;
278 state <= put_z;
279 //if a is inf return inf
280 end else if (a_e == 128) begin
281 z[31] <= a_s;
282 z[30:23] <= 255;
283 z[22:0] <= 0;
284 //if a is inf and signs don't match return nan
285 if ((b_e == 128) && (a_s != b_s)) begin
286 z[31] <= b_s;
287 z[30:23] <= 255;
288 z[22] <= 1;
289 z[21:0] <= 0;
290 end
291 state <= put_z;
292 //if b is inf return inf
293 end else if (b_e == 128) begin
294 z[31] <= b_s;
295 z[30:23] <= 255;
296 z[22:0] <= 0;
297 state <= put_z;
298 //if a is zero return b
299 end else if ((($signed(a_e) == -127) && (a_m == 0)) && (($signed(b_e) == -127) && (b_m == 0))) begin
300 z[31] <= a_s & b_s;
301 z[30:23] <= b_e[7:0] + 127;
302 z[22:0] <= b_m[26:3];
303 state <= put_z;
304 //if a is zero return b
305 end else if (($signed(a_e) == -127) && (a_m == 0)) begin
306 z[31] <= b_s;
307 z[30:23] <= b_e[7:0] + 127;
308 z[22:0] <= b_m[26:3];
309 state <= put_z;
310 //if b is zero return a
311 end else if (($signed(b_e) == -127) && (b_m == 0)) begin
312 z[31] <= a_s;
313 z[30:23] <= a_e[7:0] + 127;
314 z[22:0] <= a_m[26:3];
315 state <= put_z;
316 end else begin
317 //Denormalised Number
318 if ($signed(a_e) == -127) begin
319 a_e <= -126;
320 end else begin
321 a_m[26] <= 1;
322 end
323 //Denormalised Number
324 if ($signed(b_e) == -127) begin
325 b_e <= -126;
326 end else begin
327 b_m[26] <= 1;
328 end
329 state <= align;
330 end
331 end
332
333 align:
334 begin
335 if ($signed(a_e) > $signed(b_e)) begin
336 b_e <= b_e + 1;
337 b_m <= b_m >> 1;
338 b_m[0] <= b_m[0] | b_m[1];
339 end else if ($signed(a_e) < $signed(b_e)) begin
340 a_e <= a_e + 1;
341 a_m <= a_m >> 1;
342 a_m[0] <= a_m[0] | a_m[1];
343 end else begin
344 state <= add_0;
345 end
346 end
347
348 add_0:
349 begin
350 z_e <= a_e;
351 if (a_s == b_s) begin
352 tot <= a_m + b_m;
353 z_s <= a_s;
354 end else begin
355 if (a_m >= b_m) begin
356 tot <= a_m - b_m;
357 z_s <= a_s;
358 end else begin
359 tot <= b_m - a_m;
360 z_s <= b_s;
361 end
362 end
363 state <= add_1;
364 end
365
366 add_1:
367 begin
368 if (tot[27]) begin
369 z_m <= tot[27:4];
370 guard <= tot[3];
371 round_bit <= tot[2];
372 sticky <= tot[1] | tot[0];
373 z_e <= z_e + 1;
374 end else begin
375 z_m <= tot[26:3];
376 guard <= tot[2];
377 round_bit <= tot[1];
378 sticky <= tot[0];
379 end
380 state <= normalise_1;
381 end
382
383 normalise_1:
384 begin
385 if (z_m[23] == 0 && $signed(z_e) > -126) begin
386 z_e <= z_e - 1;
387 z_m <= z_m << 1;
388 z_m[0] <= guard;
389 guard <= round_bit;
390 round_bit <= 0;
391 end else begin
392 state <= normalise_2;
393 end
394 end
395
396 normalise_2:
397 begin
398 if ($signed(z_e) < -126) begin
399 z_e <= z_e + 1;
400 z_m <= z_m >> 1;
401 guard <= z_m[0];
402 round_bit <= guard;
403 sticky <= sticky | round_bit;
404 end else begin
405 state <= round;
406 end
407 end
408
409 round:
410 begin
411 if (guard && (round_bit | sticky | z_m[0])) begin
412 z_m <= z_m + 1;
413 if (z_m == 24'hffffff) begin
414 z_e <=z_e + 1;
415 end
416 end
417 state <= pack;
418 end
419
420 pack:
421 begin
422 z[22 : 0] <= z_m[22:0];
423 z[30 : 23] <= z_e[7:0] + 127;
424 z[31] <= z_s;
425 if ($signed(z_e) == -126 && z_m[23] == 0) begin
426 z[30 : 23] <= 0;
427 end
428 if ($signed(z_e) == -126 && z_m[23:0] == 24'h0) begin
429 z[31] <= 1'b0; // FIX SIGN BUG: -a + a = +0.
430 end
431 //if overflow occurs, return inf
432 if ($signed(z_e) > 127) begin
433 z[22 : 0] <= 0;
434 z[30 : 23] <= 255;
435 z[31] <= z_s;
436 end
437 state <= put_z;
438 end
439
440 put_z:
441 begin
442 s_out_z_stb <= 1;
443 s_out_z <= z;
444 if (s_out_z_stb && out_z_ack) begin
445 s_out_z_stb <= 0;
446 state <= get_a;
447 end
448 end
449
450 endcase
451
452 if (rst == 1) begin
453 state <= get_a;
454 s_in_a_ack <= 0;
455 s_in_b_ack <= 0;
456 s_out_z_stb <= 0;
457 end
458
459 end
460 assign in_a_ack = s_in_a_ack;
461 assign in_b_ack = s_in_b_ack;
462 assign out_z_stb = s_out_z_stb;
463 assign out_z = s_out_z;
464
465 endmodule
466 """
467
468 if __name__ == "__main__":
469 alu = FPADD(width=32)
470 main(alu, ports=[
471 alu.in_a, alu.in_a_stb, alu.in_a_ack,
472 alu.in_b, alu.in_b_stb, alu.in_b_ack,
473 alu.out_z, alu.out_z_stb, alu.out_z_ack,
474 ])