store zero-extended a and b in temp signals
[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, verilog
7
8 from fpbase import FPNumIn, FPNumOut, FPOp, Overflow, FPBase, FPNumBase
9
10
11 class FPState(FPBase):
12 def __init__(self, state_from):
13 self.state_from = state_from
14
15 def set_inputs(self, inputs):
16 self.inputs = inputs
17 for k,v in inputs.items():
18 setattr(self, k, v)
19
20 def set_outputs(self, outputs):
21 self.outputs = outputs
22 for k,v in outputs.items():
23 setattr(self, k, v)
24
25
26 class FPGetOpMod:
27 def __init__(self, width):
28 self.in_op = FPOp(width)
29 self.out_op = FPNumIn(self.in_op, width)
30 self.out_decode = Signal(reset_less=True)
31
32 def setup(self, m, in_op, out_op, out_decode):
33 """ links module to inputs and outputs
34 """
35 m.d.comb += self.in_op.copy(in_op)
36 m.d.comb += out_op.v.eq(self.out_op.v)
37 m.d.comb += out_decode.eq(self.out_decode)
38
39 def elaborate(self, platform):
40 m = Module()
41 m.d.comb += self.out_decode.eq((self.in_op.ack) & (self.in_op.stb))
42 #m.submodules.get_op_in = self.in_op
43 m.submodules.get_op_out = self.out_op
44 with m.If(self.out_decode):
45 m.d.comb += [
46 self.out_op.decode(self.in_op.v),
47 ]
48 return m
49
50
51 class FPGetOp(FPState):
52 """ gets operand
53 """
54
55 def __init__(self, in_state, out_state, in_op, width):
56 FPState.__init__(self, in_state)
57 self.out_state = out_state
58 self.mod = FPGetOpMod(width)
59 self.in_op = in_op
60 self.out_op = FPNumIn(in_op, width)
61 self.out_decode = Signal(reset_less=True)
62
63 def action(self, m):
64 with m.If(self.out_decode):
65 m.next = self.out_state
66 m.d.sync += [
67 self.in_op.ack.eq(0),
68 self.out_op.copy(self.mod.out_op)
69 ]
70 with m.Else():
71 m.d.sync += self.in_op.ack.eq(1)
72
73
74 class FPGetOpB(FPState):
75 """ gets operand b
76 """
77
78 def __init__(self, in_b, width):
79 FPState.__init__(self, "get_b")
80 self.in_b = in_b
81 self.b = FPNumIn(self.in_b, width)
82
83 def action(self, m):
84 self.get_op(m, self.in_b, self.b, "special_cases")
85
86
87 class FPAddSpecialCasesMod:
88 """ special cases: NaNs, infs, zeros, denormalised
89 NOTE: some of these are unique to add. see "Special Operations"
90 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
91 """
92
93 def __init__(self, width):
94 self.in_a = FPNumBase(width)
95 self.in_b = FPNumBase(width)
96 self.out_z = FPNumOut(width, False)
97 self.out_do_z = Signal(reset_less=True)
98
99 def setup(self, m, in_a, in_b, out_z, out_do_z):
100 """ links module to inputs and outputs
101 """
102 m.d.comb += self.in_a.copy(in_a)
103 m.d.comb += self.in_b.copy(in_b)
104 m.d.comb += out_z.v.eq(self.out_z.v)
105 m.d.comb += out_do_z.eq(self.out_do_z)
106
107 def elaborate(self, platform):
108 m = Module()
109
110 m.submodules.sc_in_a = self.in_a
111 m.submodules.sc_in_b = self.in_b
112 m.submodules.sc_out_z = self.out_z
113
114 s_nomatch = Signal()
115 m.d.comb += s_nomatch.eq(self.in_a.s != self.in_b.s)
116
117 m_match = Signal()
118 m.d.comb += m_match.eq(self.in_a.m == self.in_b.m)
119
120 # if a is NaN or b is NaN return NaN
121 with m.If(self.in_a.is_nan | self.in_b.is_nan):
122 m.d.comb += self.out_do_z.eq(1)
123 m.d.comb += self.out_z.nan(0)
124
125 # XXX WEIRDNESS for FP16 non-canonical NaN handling
126 # under review
127
128 ## if a is zero and b is NaN return -b
129 #with m.If(a.is_zero & (a.s==0) & b.is_nan):
130 # m.d.comb += self.out_do_z.eq(1)
131 # m.d.comb += z.create(b.s, b.e, Cat(b.m[3:-2], ~b.m[0]))
132
133 ## if b is zero and a is NaN return -a
134 #with m.Elif(b.is_zero & (b.s==0) & a.is_nan):
135 # m.d.comb += self.out_do_z.eq(1)
136 # m.d.comb += z.create(a.s, a.e, Cat(a.m[3:-2], ~a.m[0]))
137
138 ## if a is -zero and b is NaN return -b
139 #with m.Elif(a.is_zero & (a.s==1) & b.is_nan):
140 # m.d.comb += self.out_do_z.eq(1)
141 # m.d.comb += z.create(a.s & b.s, b.e, Cat(b.m[3:-2], 1))
142
143 ## if b is -zero and a is NaN return -a
144 #with m.Elif(b.is_zero & (b.s==1) & a.is_nan):
145 # m.d.comb += self.out_do_z.eq(1)
146 # m.d.comb += z.create(a.s & b.s, a.e, Cat(a.m[3:-2], 1))
147
148 # if a is inf return inf (or NaN)
149 with m.Elif(self.in_a.is_inf):
150 m.d.comb += self.out_do_z.eq(1)
151 m.d.comb += self.out_z.inf(self.in_a.s)
152 # if a is inf and signs don't match return NaN
153 with m.If(self.in_b.exp_128 & s_nomatch):
154 m.d.comb += self.out_z.nan(0)
155
156 # if b is inf return inf
157 with m.Elif(self.in_b.is_inf):
158 m.d.comb += self.out_do_z.eq(1)
159 m.d.comb += self.out_z.inf(self.in_b.s)
160
161 # if a is zero and b zero return signed-a/b
162 with m.Elif(self.in_a.is_zero & self.in_b.is_zero):
163 m.d.comb += self.out_do_z.eq(1)
164 m.d.comb += self.out_z.create(self.in_a.s & self.in_b.s,
165 self.in_b.e,
166 self.in_b.m[3:-1])
167
168 # if a is zero return b
169 with m.Elif(self.in_a.is_zero):
170 m.d.comb += self.out_do_z.eq(1)
171 m.d.comb += self.out_z.create(self.in_b.s, self.in_b.e,
172 self.in_b.m[3:-1])
173
174 # if b is zero return a
175 with m.Elif(self.in_b.is_zero):
176 m.d.comb += self.out_do_z.eq(1)
177 m.d.comb += self.out_z.create(self.in_a.s, self.in_a.e,
178 self.in_a.m[3:-1])
179
180 # if a equal to -b return zero (+ve zero)
181 with m.Elif(s_nomatch & m_match & (self.in_a.e == self.in_b.e)):
182 m.d.comb += self.out_do_z.eq(1)
183 m.d.comb += self.out_z.zero(0)
184
185 # Denormalised Number checks
186 with m.Else():
187 m.d.comb += self.out_do_z.eq(0)
188
189 return m
190
191
192 class FPAddSpecialCases(FPState):
193 """ special cases: NaNs, infs, zeros, denormalised
194 NOTE: some of these are unique to add. see "Special Operations"
195 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
196 """
197
198 def __init__(self, width):
199 FPState.__init__(self, "special_cases")
200 self.mod = FPAddSpecialCasesMod(width)
201 self.out_z = FPNumOut(width, False)
202 self.out_do_z = Signal(reset_less=True)
203
204 def action(self, m):
205 with m.If(self.out_do_z):
206 m.d.sync += self.z.v.eq(self.out_z.v) # only take the output
207 m.next = "put_z"
208 with m.Else():
209 m.next = "denormalise"
210
211
212 class FPAddDeNormMod(FPState):
213
214 def __init__(self, width):
215 self.in_a = FPNumBase(width)
216 self.in_b = FPNumBase(width)
217 self.out_a = FPNumBase(width)
218 self.out_b = FPNumBase(width)
219
220 def setup(self, m, in_a, in_b, out_a, out_b):
221 """ links module to inputs and outputs
222 """
223 m.d.comb += self.in_a.copy(in_a)
224 m.d.comb += self.in_b.copy(in_b)
225 m.d.comb += out_a.copy(self.out_a)
226 m.d.comb += out_b.copy(self.out_b)
227
228 def elaborate(self, platform):
229 m = Module()
230 m.submodules.denorm_in_a = self.in_a
231 m.submodules.denorm_in_b = self.in_b
232 m.submodules.denorm_out_a = self.out_a
233 m.submodules.denorm_out_b = self.out_b
234 # hmmm, don't like repeating identical code
235 m.d.comb += self.out_a.copy(self.in_a)
236 with m.If(self.in_a.exp_n127):
237 m.d.comb += self.out_a.e.eq(self.in_a.N126) # limit a exponent
238 with m.Else():
239 m.d.comb += self.out_a.m[-1].eq(1) # set top mantissa bit
240
241 m.d.comb += self.out_b.copy(self.in_b)
242 with m.If(self.in_b.exp_n127):
243 m.d.comb += self.out_b.e.eq(self.in_b.N126) # limit a exponent
244 with m.Else():
245 m.d.comb += self.out_b.m[-1].eq(1) # set top mantissa bit
246
247 return m
248
249
250 class FPAddDeNorm(FPState):
251
252 def __init__(self, width):
253 FPState.__init__(self, "denormalise")
254 self.mod = FPAddDeNormMod(width)
255 self.out_a = FPNumBase(width)
256 self.out_b = FPNumBase(width)
257
258 def action(self, m):
259 # Denormalised Number checks
260 m.next = "align"
261 m.d.sync += self.a.copy(self.out_a)
262 m.d.sync += self.b.copy(self.out_b)
263
264
265 class FPAddAlignMultiMod(FPState):
266
267 def __init__(self, width):
268 self.in_a = FPNumBase(width)
269 self.in_b = FPNumBase(width)
270 self.out_a = FPNumIn(None, width)
271 self.out_b = FPNumIn(None, width)
272 self.exp_eq = Signal(reset_less=True)
273
274 def setup(self, m, in_a, in_b, out_a, out_b, exp_eq):
275 """ links module to inputs and outputs
276 """
277 m.d.comb += self.in_a.copy(in_a)
278 m.d.comb += self.in_b.copy(in_b)
279 m.d.comb += out_a.copy(self.out_a)
280 m.d.comb += out_b.copy(self.out_b)
281 m.d.comb += exp_eq.eq(self.exp_eq)
282
283 def elaborate(self, platform):
284 # This one however (single-cycle) will do the shift
285 # in one go.
286
287 m = Module()
288
289 #m.submodules.align_in_a = self.in_a
290 #m.submodules.align_in_b = self.in_b
291 m.submodules.align_out_a = self.out_a
292 m.submodules.align_out_b = self.out_b
293
294 # NOTE: this does *not* do single-cycle multi-shifting,
295 # it *STAYS* in the align state until exponents match
296
297 # exponent of a greater than b: shift b down
298 m.d.comb += self.exp_eq.eq(0)
299 m.d.comb += self.out_a.copy(self.in_a)
300 m.d.comb += self.out_b.copy(self.in_b)
301 agtb = Signal(reset_less=True)
302 altb = Signal(reset_less=True)
303 m.d.comb += agtb.eq(self.in_a.e > self.in_b.e)
304 m.d.comb += altb.eq(self.in_a.e < self.in_b.e)
305 with m.If(agtb):
306 m.d.comb += self.out_b.shift_down(self.in_b)
307 # exponent of b greater than a: shift a down
308 with m.Elif(altb):
309 m.d.comb += self.out_a.shift_down(self.in_a)
310 # exponents equal: move to next stage.
311 with m.Else():
312 m.d.comb += self.exp_eq.eq(1)
313 return m
314
315
316 class FPAddAlignMulti(FPState):
317
318 def __init__(self, width):
319 FPState.__init__(self, "align")
320 self.mod = FPAddAlignMultiMod(width)
321 self.out_a = FPNumIn(None, width)
322 self.out_b = FPNumIn(None, width)
323 self.exp_eq = Signal(reset_less=True)
324
325 def action(self, m):
326 m.d.sync += self.a.copy(self.out_a)
327 m.d.sync += self.b.copy(self.out_b)
328 with m.If(self.exp_eq):
329 m.next = "add_0"
330
331
332 class FPAddAlignSingleMod:
333
334 def __init__(self, width):
335 self.in_a = FPNumBase(width)
336 self.in_b = FPNumBase(width)
337 self.out_a = FPNumIn(None, width)
338 self.out_b = FPNumIn(None, width)
339 #self.out_a = FPNumBase(width)
340 #self.out_b = FPNumBase(width)
341
342 def setup(self, m, in_a, in_b, out_a, out_b):
343 """ links module to inputs and outputs
344 """
345 m.d.comb += self.in_a.copy(in_a)
346 m.d.comb += self.in_b.copy(in_b)
347 m.d.comb += out_a.copy(self.out_a)
348 m.d.comb += out_b.copy(self.out_b)
349
350 def elaborate(self, platform):
351 # This one however (single-cycle) will do the shift
352 # in one go.
353
354 m = Module()
355
356 #m.submodules.align_in_a = self.in_a
357 #m.submodules.align_in_b = self.in_b
358 m.submodules.align_out_a = self.out_a
359 m.submodules.align_out_b = self.out_b
360
361 # XXX TODO: the shifter used here is quite expensive
362 # having only one would be better
363
364 ediff = Signal((len(self.in_a.e), True), reset_less=True)
365 ediffr = Signal((len(self.in_a.e), True), reset_less=True)
366 m.d.comb += ediff.eq(self.in_a.e - self.in_b.e)
367 m.d.comb += ediffr.eq(self.in_b.e - self.in_a.e)
368 m.d.comb += self.out_a.copy(self.in_a)
369 m.d.comb += self.out_b.copy(self.in_b)
370 with m.If(ediff > 0):
371 m.d.comb += self.out_b.shift_down_multi(ediff)
372 # exponent of b greater than a: shift a down
373 with m.Elif(ediff < 0):
374 m.d.comb += self.out_a.shift_down_multi(ediffr)
375 return m
376
377
378 class FPAddAlignSingle(FPState):
379
380 def __init__(self, width):
381 FPState.__init__(self, "align")
382 self.mod = FPAddAlignSingleMod(width)
383 self.out_a = FPNumIn(None, width)
384 self.out_b = FPNumIn(None, width)
385
386 def action(self, m):
387 m.d.sync += self.a.copy(self.out_a)
388 m.d.sync += self.b.copy(self.out_b)
389 m.next = "add_0"
390
391
392 class FPAddStage0Mod:
393
394 def __init__(self, width):
395 self.in_a = FPNumBase(width)
396 self.in_b = FPNumBase(width)
397 self.in_z = FPNumBase(width, False)
398 self.out_z = FPNumBase(width, False)
399 self.out_tot = Signal(self.out_z.m_width + 4, reset_less=True)
400
401 def setup(self, m, in_a, in_b, in_z, out_z, out_tot):
402 """ links module to inputs and outputs
403 """
404 m.d.comb += self.in_a.copy(in_a)
405 m.d.comb += self.in_b.copy(in_b)
406 m.d.comb += self.in_z.copy(in_z)
407 m.d.comb += out_z.copy(self.out_z)
408 m.d.comb += out_tot.eq(self.out_tot)
409
410 def elaborate(self, platform):
411 m = Module()
412 m.submodules.add0_in_a = self.in_a
413 m.submodules.add0_in_b = self.in_b
414 #m.submodules.add0_in_z = self.in_z
415 #m.submodules.add0_out_z = self.out_z
416
417 m.d.comb += self.out_z.e.eq(self.in_a.e)
418 # same-sign (both negative or both positive) add mantissas
419 seq = Signal(reset_less=True)
420 mge = Signal(reset_less=True)
421 am0 = Signal(len(self.in_a.m)+1, reset_less=True)
422 bm0 = Signal(len(self.in_b.m)+1, reset_less=True)
423 m.d.comb += [seq.eq(self.in_a.s == self.in_b.s),
424 mge.eq(self.in_a.m >= self.in_b.m),
425 am0.eq(Cat(self.in_a.m, 0)),
426 bm0.eq(Cat(self.in_b.m, 0))
427 ]
428 with m.If(seq):
429 m.d.comb += [
430 self.out_tot.eq(am0 + bm0),
431 self.out_z.s.eq(self.in_a.s)
432 ]
433 # a mantissa greater than b, use a
434 with m.Elif(mge):
435 m.d.comb += [
436 self.out_tot.eq(am0 - bm0),
437 self.out_z.s.eq(self.in_a.s)
438 ]
439 # b mantissa greater than a, use b
440 with m.Else():
441 m.d.comb += [
442 self.out_tot.eq(bm0 - am0),
443 self.out_z.s.eq(self.in_b.s)
444 ]
445 return m
446
447
448 class FPAddStage0(FPState):
449 """ First stage of add. covers same-sign (add) and subtract
450 special-casing when mantissas are greater or equal, to
451 give greatest accuracy.
452 """
453
454 def __init__(self, width):
455 FPState.__init__(self, "add_0")
456 self.mod = FPAddStage0Mod(width)
457 self.out_z = FPNumBase(width, False)
458 self.out_tot = Signal(self.out_z.m_width + 4, reset_less=True)
459
460 def action(self, m):
461 m.next = "add_1"
462 m.d.sync += self.z.copy(self.out_z)
463
464
465 class FPAddStage1Mod(FPState):
466 """ Second stage of add: preparation for normalisation.
467 detects when tot sum is too big (tot[27] is kinda a carry bit)
468 """
469
470 def __init__(self, width):
471 self.out_norm = Signal(reset_less=True)
472 self.in_z = FPNumBase(width, False)
473 self.in_tot = Signal(self.in_z.m_width + 4, reset_less=True)
474 self.out_z = FPNumBase(width, False)
475 self.out_of = Overflow()
476
477 def setup(self, m, in_tot, in_z, out_z, out_of):
478 """ links module to inputs and outputs
479 """
480 m.d.comb += self.in_z.copy(in_z)
481 m.d.comb += self.in_tot.eq(in_tot)
482 m.d.comb += out_z.copy(self.out_z)
483 m.d.comb += out_of.copy(self.out_of)
484
485 def elaborate(self, platform):
486 m = Module()
487 #m.submodules.norm1_in_overflow = self.in_of
488 #m.submodules.norm1_out_overflow = self.out_of
489 #m.submodules.norm1_in_z = self.in_z
490 #m.submodules.norm1_out_z = self.out_z
491 m.d.comb += self.out_z.copy(self.in_z)
492 # tot[27] gets set when the sum overflows. shift result down
493 with m.If(self.in_tot[-1]):
494 m.d.comb += [
495 self.out_z.m.eq(self.in_tot[4:]),
496 self.out_of.m0.eq(self.in_tot[4]),
497 self.out_of.guard.eq(self.in_tot[3]),
498 self.out_of.round_bit.eq(self.in_tot[2]),
499 self.out_of.sticky.eq(self.in_tot[1] | self.in_tot[0]),
500 self.out_z.e.eq(self.in_z.e + 1)
501 ]
502 # tot[27] zero case
503 with m.Else():
504 m.d.comb += [
505 self.out_z.m.eq(self.in_tot[3:]),
506 self.out_of.m0.eq(self.in_tot[3]),
507 self.out_of.guard.eq(self.in_tot[2]),
508 self.out_of.round_bit.eq(self.in_tot[1]),
509 self.out_of.sticky.eq(self.in_tot[0])
510 ]
511 return m
512
513
514 class FPAddStage1(FPState):
515
516 def __init__(self, width):
517 FPState.__init__(self, "add_1")
518 self.mod = FPAddStage1Mod(width)
519 self.out_z = FPNumBase(width, False)
520 self.out_of = Overflow()
521
522 def action(self, m):
523 m.d.sync += self.of.copy(self.out_of)
524 m.d.sync += self.z.copy(self.out_z)
525 m.next = "normalise_1"
526
527
528 class FPNorm1Mod:
529
530 def __init__(self, width):
531 self.out_norm = Signal(reset_less=True)
532 self.in_z = FPNumBase(width, False)
533 self.out_z = FPNumBase(width, False)
534 self.in_of = Overflow()
535 self.out_of = Overflow()
536
537 def setup(self, m, in_z, out_z, in_of, out_of, out_norm):
538 """ links module to inputs and outputs
539 """
540 m.d.comb += self.in_z.copy(in_z)
541 m.d.comb += out_z.copy(self.out_z)
542 m.d.comb += self.in_of.copy(in_of)
543 m.d.comb += out_of.copy(self.out_of)
544 m.d.comb += out_norm.eq(self.out_norm)
545
546 def elaborate(self, platform):
547 m = Module()
548 m.submodules.norm1_in_overflow = self.in_of
549 m.submodules.norm1_out_overflow = self.out_of
550 m.submodules.norm1_in_z = self.in_z
551 m.submodules.norm1_out_z = self.out_z
552 m.d.comb += self.out_z.copy(self.in_z)
553 m.d.comb += self.out_of.copy(self.in_of)
554 m.d.comb += self.out_norm.eq((self.in_z.m_msbzero) & \
555 (self.in_z.exp_gt_n126))
556 with m.If(self.out_norm):
557 m.d.comb += [
558 self.out_z.e.eq(self.in_z.e - 1), # DECREASE exponent
559 self.out_z.m.eq(self.in_z.m << 1), # shift mantissa UP
560 self.out_z.m[0].eq(self.in_of.guard), # steal guard (was tot[2])
561 self.out_of.guard.eq(self.in_of.round_bit), # round (was tot[1])
562 self.out_of.round_bit.eq(0), # reset round bit
563 self.out_of.m0.eq(self.in_of.guard),
564 ]
565
566 return m
567
568
569 class FPNorm1(FPState):
570
571 def __init__(self, width):
572 FPState.__init__(self, "normalise_1")
573 self.mod = FPNorm1Mod(width)
574 self.out_norm = Signal(reset_less=True)
575 self.out_z = FPNumBase(width)
576 self.out_of = Overflow()
577
578 def action(self, m):
579 m.d.sync += self.of.copy(self.out_of)
580 m.d.sync += self.z.copy(self.out_z)
581 with m.If(~self.out_norm):
582 m.next = "normalise_2"
583
584
585 class FPNorm2Mod:
586
587 def __init__(self, width):
588 self.out_norm = Signal(reset_less=True)
589 self.in_z = FPNumBase(width, False)
590 self.out_z = FPNumBase(width, False)
591 self.in_of = Overflow()
592 self.out_of = Overflow()
593
594 def setup(self, m, in_z, out_z, in_of, out_of, out_norm):
595 """ links module to inputs and outputs
596 """
597 m.d.comb += self.in_z.copy(in_z)
598 m.d.comb += out_z.copy(self.out_z)
599 m.d.comb += self.in_of.copy(in_of)
600 m.d.comb += out_of.copy(self.out_of)
601 m.d.comb += out_norm.eq(self.out_norm)
602
603 def elaborate(self, platform):
604 m = Module()
605 m.submodules.norm2_in_overflow = self.in_of
606 m.submodules.norm2_out_overflow = self.out_of
607 m.submodules.norm2_in_z = self.in_z
608 m.submodules.norm2_out_z = self.out_z
609 m.d.comb += self.out_z.copy(self.in_z)
610 m.d.comb += self.out_of.copy(self.in_of)
611 m.d.comb += self.out_norm.eq(self.in_z.exp_lt_n126)
612 with m.If(self.out_norm):
613 m.d.comb += [
614 self.out_z.e.eq(self.in_z.e + 1), # INCREASE exponent
615 self.out_z.m.eq(self.in_z.m >> 1), # shift mantissa DOWN
616 self.out_of.guard.eq(self.in_z.m[0]),
617 self.out_of.m0.eq(self.in_z.m[1]),
618 self.out_of.round_bit.eq(self.in_of.guard),
619 self.out_of.sticky.eq(self.in_of.sticky | self.in_of.round_bit)
620 ]
621
622 return m
623
624
625 class FPNorm2(FPState):
626
627 def __init__(self, width):
628 FPState.__init__(self, "normalise_2")
629 self.mod = FPNorm2Mod(width)
630 self.out_norm = Signal(reset_less=True)
631 self.out_z = FPNumBase(width)
632 self.out_of = Overflow()
633
634 def action(self, m):
635 #m.d.sync += self.of.copy(self.out_of)
636 m.d.sync += self.z.copy(self.out_z)
637 with m.If(~self.out_norm):
638 m.next = "round"
639
640
641 class FPRoundMod:
642
643 def __init__(self, width):
644 self.in_roundz = Signal(reset_less=True)
645 self.in_z = FPNumBase(width, False)
646 self.out_z = FPNumBase(width, False)
647
648 def setup(self, m, in_z, out_z, in_of):
649 """ links module to inputs and outputs
650 """
651 m.d.comb += self.in_z.copy(in_z)
652 m.d.comb += out_z.copy(self.out_z)
653 m.d.comb += self.in_roundz.eq(in_of.roundz)
654
655 def elaborate(self, platform):
656 m = Module()
657 m.d.comb += self.out_z.copy(self.in_z)
658 with m.If(self.in_roundz):
659 m.d.comb += self.out_z.m.eq(self.in_z.m + 1) # mantissa rounds up
660 with m.If(self.in_z.m == self.in_z.m1s): # all 1s
661 m.d.comb += self.out_z.e.eq(self.in_z.e + 1) # exponent up
662 return m
663
664
665 class FPRound(FPState):
666
667 def __init__(self, width):
668 FPState.__init__(self, "round")
669 self.mod = FPRoundMod(width)
670 self.out_z = FPNumBase(width)
671
672 def action(self, m):
673 m.d.sync += self.z.copy(self.out_z)
674 m.next = "corrections"
675
676
677 class FPCorrectionsMod:
678
679 def __init__(self, width):
680 self.in_z = FPNumOut(width, False)
681 self.out_z = FPNumOut(width, False)
682
683 def setup(self, m, in_z, out_z):
684 """ links module to inputs and outputs
685 """
686 m.d.comb += self.in_z.copy(in_z)
687 m.d.comb += out_z.copy(self.out_z)
688
689 def elaborate(self, platform):
690 m = Module()
691 m.submodules.corr_in_z = self.in_z
692 m.submodules.corr_out_z = self.out_z
693 m.d.comb += self.out_z.copy(self.in_z)
694 with m.If(self.in_z.is_denormalised):
695 m.d.comb += self.out_z.e.eq(self.in_z.N127)
696
697 # with m.If(self.in_z.is_overflowed):
698 # m.d.comb += self.out_z.inf(self.in_z.s)
699 # with m.Else():
700 # m.d.comb += self.out_z.create(self.in_z.s, self.in_z.e, self.in_z.m)
701 return m
702
703
704 class FPCorrections(FPState):
705
706 def __init__(self, width):
707 FPState.__init__(self, "corrections")
708 self.mod = FPCorrectionsMod(width)
709 self.out_z = FPNumBase(width)
710
711 def action(self, m):
712 m.d.sync += self.z.copy(self.out_z)
713 m.next = "pack"
714
715
716 class FPPackMod:
717
718 def __init__(self, width):
719 self.in_z = FPNumOut(width, False)
720 self.out_z = FPNumOut(width, False)
721
722 def setup(self, m, in_z, out_z):
723 """ links module to inputs and outputs
724 """
725 m.d.comb += self.in_z.copy(in_z)
726 m.d.comb += out_z.v.eq(self.out_z.v)
727
728 def elaborate(self, platform):
729 m = Module()
730 m.submodules.pack_in_z = self.in_z
731 with m.If(self.in_z.is_overflowed):
732 m.d.comb += self.out_z.inf(self.in_z.s)
733 with m.Else():
734 m.d.comb += self.out_z.create(self.in_z.s, self.in_z.e, self.in_z.m)
735 return m
736
737
738 class FPPack(FPState):
739
740 def __init__(self, width):
741 FPState.__init__(self, "pack")
742 self.mod = FPPackMod(width)
743 self.out_z = FPNumOut(width, False)
744
745 def action(self, m):
746 m.d.sync += self.z.v.eq(self.out_z.v)
747 m.next = "put_z"
748
749
750 class FPPutZ(FPState):
751
752 def action(self, m):
753 self.put_z(m, self.z, self.out_z, "get_a")
754
755
756 class FPADD:
757
758 def __init__(self, width, single_cycle=False):
759 self.width = width
760 self.single_cycle = single_cycle
761
762 self.in_a = FPOp(width)
763 self.in_b = FPOp(width)
764 self.out_z = FPOp(width)
765
766 self.states = []
767
768 def add_state(self, state):
769 self.states.append(state)
770 return state
771
772 def get_fragment(self, platform=None):
773 """ creates the HDL code-fragment for FPAdd
774 """
775 m = Module()
776
777 # Latches
778 z = FPNumOut(self.width, False)
779 m.submodules.fpnum_z = z
780
781 w = z.m_width + 4
782
783 of = Overflow()
784 m.submodules.overflow = of
785
786 geta = self.add_state(FPGetOp("get_a", "get_b",
787 self.in_a, self.width))
788 a = geta.out_op
789 geta.mod.setup(m, self.in_a, geta.out_op, geta.out_decode)
790 m.submodules.get_a = geta.mod
791
792 getb = self.add_state(FPGetOp("get_b", "special_cases",
793 self.in_b, self.width))
794 b = getb.out_op
795 getb.mod.setup(m, self.in_b, getb.out_op, getb.out_decode)
796 m.submodules.get_b = getb.mod
797
798 sc = self.add_state(FPAddSpecialCases(self.width))
799 sc.set_inputs({"a": a, "b": b})
800 sc.set_outputs({"z": z})
801 sc.mod.setup(m, a, b, sc.out_z, sc.out_do_z)
802 m.submodules.specialcases = sc.mod
803
804 dn = self.add_state(FPAddDeNorm(self.width))
805 dn.set_inputs({"a": a, "b": b})
806 #dn.set_outputs({"a": a, "b": b}) # XXX outputs same as inputs
807 dn.mod.setup(m, a, b, dn.out_a, dn.out_b)
808 m.submodules.denormalise = dn.mod
809
810 if self.single_cycle:
811 alm = self.add_state(FPAddAlignSingle(self.width))
812 alm.set_inputs({"a": a, "b": b})
813 alm.set_outputs({"a": a, "b": b}) # XXX outputs same as inputs
814 alm.mod.setup(m, a, b, alm.out_a, alm.out_b)
815 else:
816 alm = self.add_state(FPAddAlignMulti(self.width))
817 alm.set_inputs({"a": a, "b": b})
818 #alm.set_outputs({"a": a, "b": b}) # XXX outputs same as inputs
819 alm.mod.setup(m, a, b, alm.out_a, alm.out_b, alm.exp_eq)
820 m.submodules.align = alm.mod
821
822 add0 = self.add_state(FPAddStage0(self.width))
823 add0.set_inputs({"a": alm.out_a, "b": alm.out_b})
824 add0.set_outputs({"z": z})
825 add0.mod.setup(m, alm.out_a, alm.out_b, z, add0.out_z, add0.out_tot)
826 m.submodules.add0 = add0.mod
827
828 add1 = self.add_state(FPAddStage1(self.width))
829 add1.set_inputs({"tot": add0.out_tot, "z": add0.out_z})
830 add1.set_outputs({"z": z, "of": of}) # XXX Z as output
831 add1.mod.setup(m, add0.out_tot, z, add1.out_z, add1.out_of)
832 m.submodules.add1 = add1.mod
833
834 n1 = self.add_state(FPNorm1(self.width))
835 n1.set_inputs({"z": z, "of": of}) # XXX Z as output
836 n1.set_outputs({"z": z}) # XXX Z as output
837 n1.mod.setup(m, z, n1.out_z, of, n1.out_of, n1.out_norm)
838 m.submodules.normalise_1 = n1.mod
839
840 n2 = self.add_state(FPNorm2(self.width))
841 n2.set_inputs({"z": n1.out_z, "of": n1.out_of})
842 n2.set_outputs({"z": z})
843 n2.mod.setup(m, n1.out_z, n2.out_z, n1.out_of, n2.out_of, n2.out_norm)
844 m.submodules.normalise_2 = n2.mod
845
846 rn = self.add_state(FPRound(self.width))
847 rn.set_inputs({"z": n2.out_z, "of": n2.out_of})
848 rn.set_outputs({"z": z})
849 rn.mod.setup(m, n2.out_z, rn.out_z, of)
850 m.submodules.roundz = rn.mod
851
852 cor = self.add_state(FPCorrections(self.width))
853 cor.set_inputs({"z": z}) # XXX Z as output
854 cor.set_outputs({"z": z}) # XXX Z as output
855 cor.mod.setup(m, z, cor.out_z)
856 m.submodules.corrections = cor.mod
857
858 pa = self.add_state(FPPack(self.width))
859 pa.set_inputs({"z": z}) # XXX Z as output
860 pa.set_outputs({"z": z}) # XXX Z as output
861 pa.mod.setup(m, z, pa.out_z)
862 m.submodules.pack = pa.mod
863
864 pz = self.add_state(FPPutZ("put_z"))
865 pz.set_inputs({"z": z})
866 pz.set_outputs({"out_z": self.out_z})
867
868 with m.FSM() as fsm:
869
870 for state in self.states:
871 with m.State(state.state_from):
872 state.action(m)
873
874 return m
875
876
877 if __name__ == "__main__":
878 alu = FPADD(width=32)
879 main(alu, ports=alu.in_a.ports() + alu.in_b.ports() + alu.out_z.ports())
880
881
882 # works... but don't use, just do "python fname.py convert -t v"
883 #print (verilog.convert(alu, ports=[
884 # ports=alu.in_a.ports() + \
885 # alu.in_b.ports() + \
886 # alu.out_z.ports())