1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Cat
, Mux
6 from nmigen
.lib
.coding
import PriorityEncoder
7 from nmigen
.cli
import main
, verilog
9 from fpbase
import FPNumIn
, FPNumOut
, FPOp
, Overflow
, FPBase
, FPNumBase
10 from fpbase
import MultiShiftRMerge
, Trigger
11 #from fpbase import FPNumShiftMultiRight
13 class FPState(FPBase
):
14 def __init__(self
, state_from
):
15 self
.state_from
= state_from
17 def set_inputs(self
, inputs
):
19 for k
,v
in inputs
.items():
22 def set_outputs(self
, outputs
):
23 self
.outputs
= outputs
24 for k
,v
in outputs
.items():
29 def __init__(self
, width
):
30 self
.in_op
= FPOp(width
)
31 self
.out_op
= Signal(width
)
32 self
.out_decode
= Signal(reset_less
=True)
34 def elaborate(self
, platform
):
36 m
.d
.comb
+= self
.out_decode
.eq((self
.in_op
.ack
) & (self
.in_op
.stb
))
37 m
.submodules
.get_op_in
= self
.in_op
38 #m.submodules.get_op_out = self.out_op
39 with m
.If(self
.out_decode
):
41 self
.out_op
.eq(self
.in_op
.v
),
46 class FPGetOp(FPState
):
50 def __init__(self
, in_state
, out_state
, in_op
, width
):
51 FPState
.__init
__(self
, in_state
)
52 self
.out_state
= out_state
53 self
.mod
= FPGetOpMod(width
)
55 self
.out_op
= Signal(width
)
56 self
.out_decode
= Signal(reset_less
=True)
58 def setup(self
, m
, in_op
):
59 """ links module to inputs and outputs
61 setattr(m
.submodules
, self
.state_from
, self
.mod
)
62 m
.d
.comb
+= self
.mod
.in_op
.copy(in_op
)
63 #m.d.comb += self.out_op.eq(self.mod.out_op)
64 m
.d
.comb
+= self
.out_decode
.eq(self
.mod
.out_decode
)
67 with m
.If(self
.out_decode
):
68 m
.next
= self
.out_state
71 self
.out_op
.eq(self
.mod
.out_op
)
74 m
.d
.sync
+= self
.in_op
.ack
.eq(1)
77 class FPGet2OpMod(Trigger
):
78 def __init__(self
, width
):
79 Trigger
.__init
__(self
)
80 self
.in_op1
= Signal(width
, reset_less
=True)
81 self
.in_op2
= Signal(width
, reset_less
=True)
82 self
.out_op1
= FPNumIn(None, width
)
83 self
.out_op2
= FPNumIn(None, width
)
85 def elaborate(self
, platform
):
86 m
= Trigger
.elaborate(self
, platform
)
87 #m.submodules.get_op_in = self.in_op
88 m
.submodules
.get_op1_out
= self
.out_op1
89 m
.submodules
.get_op2_out
= self
.out_op2
90 with m
.If(self
.trigger
):
92 self
.out_op1
.decode(self
.in_op1
),
93 self
.out_op2
.decode(self
.in_op2
),
98 class FPGet2Op(FPState
):
102 def __init__(self
, in_state
, out_state
, in_op1
, in_op2
, width
):
103 FPState
.__init
__(self
, in_state
)
104 self
.out_state
= out_state
105 self
.mod
= FPGet2OpMod(width
)
108 self
.out_op1
= FPNumIn(None, width
)
109 self
.out_op2
= FPNumIn(None, width
)
110 self
.in_stb
= Signal(reset_less
=True)
111 self
.out_ack
= Signal(reset_less
=True)
112 self
.out_decode
= Signal(reset_less
=True)
114 def setup(self
, m
, in_op1
, in_op2
, in_stb
, in_ack
):
115 """ links module to inputs and outputs
117 m
.submodules
.get_ops
= self
.mod
118 m
.d
.comb
+= self
.mod
.in_op1
.eq(in_op1
)
119 m
.d
.comb
+= self
.mod
.in_op2
.eq(in_op2
)
120 m
.d
.comb
+= self
.mod
.stb
.eq(in_stb
)
121 m
.d
.comb
+= self
.out_ack
.eq(self
.mod
.ack
)
122 m
.d
.comb
+= self
.out_decode
.eq(self
.mod
.trigger
)
123 m
.d
.comb
+= in_ack
.eq(self
.mod
.ack
)
126 with m
.If(self
.out_decode
):
127 m
.next
= self
.out_state
130 #self.out_op1.v.eq(self.mod.out_op1.v),
131 #self.out_op2.v.eq(self.mod.out_op2.v),
132 self
.out_op1
.copy(self
.mod
.out_op1
),
133 self
.out_op2
.copy(self
.mod
.out_op2
)
136 m
.d
.sync
+= self
.mod
.ack
.eq(1)
139 class FPAddSpecialCasesMod
:
140 """ special cases: NaNs, infs, zeros, denormalised
141 NOTE: some of these are unique to add. see "Special Operations"
142 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
145 def __init__(self
, width
):
146 self
.in_a
= FPNumBase(width
)
147 self
.in_b
= FPNumBase(width
)
148 self
.out_z
= FPNumOut(width
, False)
149 self
.out_do_z
= Signal(reset_less
=True)
151 def setup(self
, m
, in_a
, in_b
, out_do_z
):
152 """ links module to inputs and outputs
154 m
.submodules
.specialcases
= self
155 m
.d
.comb
+= self
.in_a
.copy(in_a
)
156 m
.d
.comb
+= self
.in_b
.copy(in_b
)
157 m
.d
.comb
+= out_do_z
.eq(self
.out_do_z
)
159 def elaborate(self
, platform
):
162 m
.submodules
.sc_in_a
= self
.in_a
163 m
.submodules
.sc_in_b
= self
.in_b
164 m
.submodules
.sc_out_z
= self
.out_z
167 m
.d
.comb
+= s_nomatch
.eq(self
.in_a
.s
!= self
.in_b
.s
)
170 m
.d
.comb
+= m_match
.eq(self
.in_a
.m
== self
.in_b
.m
)
172 # if a is NaN or b is NaN return NaN
173 with m
.If(self
.in_a
.is_nan | self
.in_b
.is_nan
):
174 m
.d
.comb
+= self
.out_do_z
.eq(1)
175 m
.d
.comb
+= self
.out_z
.nan(0)
177 # XXX WEIRDNESS for FP16 non-canonical NaN handling
180 ## if a is zero and b is NaN return -b
181 #with m.If(a.is_zero & (a.s==0) & b.is_nan):
182 # m.d.comb += self.out_do_z.eq(1)
183 # m.d.comb += z.create(b.s, b.e, Cat(b.m[3:-2], ~b.m[0]))
185 ## if b is zero and a is NaN return -a
186 #with m.Elif(b.is_zero & (b.s==0) & a.is_nan):
187 # m.d.comb += self.out_do_z.eq(1)
188 # m.d.comb += z.create(a.s, a.e, Cat(a.m[3:-2], ~a.m[0]))
190 ## if a is -zero and b is NaN return -b
191 #with m.Elif(a.is_zero & (a.s==1) & b.is_nan):
192 # m.d.comb += self.out_do_z.eq(1)
193 # m.d.comb += z.create(a.s & b.s, b.e, Cat(b.m[3:-2], 1))
195 ## if b is -zero and a is NaN return -a
196 #with m.Elif(b.is_zero & (b.s==1) & a.is_nan):
197 # m.d.comb += self.out_do_z.eq(1)
198 # m.d.comb += z.create(a.s & b.s, a.e, Cat(a.m[3:-2], 1))
200 # if a is inf return inf (or NaN)
201 with m
.Elif(self
.in_a
.is_inf
):
202 m
.d
.comb
+= self
.out_do_z
.eq(1)
203 m
.d
.comb
+= self
.out_z
.inf(self
.in_a
.s
)
204 # if a is inf and signs don't match return NaN
205 with m
.If(self
.in_b
.exp_128
& s_nomatch
):
206 m
.d
.comb
+= self
.out_z
.nan(0)
208 # if b is inf return inf
209 with m
.Elif(self
.in_b
.is_inf
):
210 m
.d
.comb
+= self
.out_do_z
.eq(1)
211 m
.d
.comb
+= self
.out_z
.inf(self
.in_b
.s
)
213 # if a is zero and b zero return signed-a/b
214 with m
.Elif(self
.in_a
.is_zero
& self
.in_b
.is_zero
):
215 m
.d
.comb
+= self
.out_do_z
.eq(1)
216 m
.d
.comb
+= self
.out_z
.create(self
.in_a
.s
& self
.in_b
.s
,
220 # if a is zero return b
221 with m
.Elif(self
.in_a
.is_zero
):
222 m
.d
.comb
+= self
.out_do_z
.eq(1)
223 m
.d
.comb
+= self
.out_z
.create(self
.in_b
.s
, self
.in_b
.e
,
226 # if b is zero return a
227 with m
.Elif(self
.in_b
.is_zero
):
228 m
.d
.comb
+= self
.out_do_z
.eq(1)
229 m
.d
.comb
+= self
.out_z
.create(self
.in_a
.s
, self
.in_a
.e
,
232 # if a equal to -b return zero (+ve zero)
233 with m
.Elif(s_nomatch
& m_match
& (self
.in_a
.e
== self
.in_b
.e
)):
234 m
.d
.comb
+= self
.out_do_z
.eq(1)
235 m
.d
.comb
+= self
.out_z
.zero(0)
237 # Denormalised Number checks
239 m
.d
.comb
+= self
.out_do_z
.eq(0)
245 def __init__(self
, id_wid
):
248 self
.in_mid
= Signal(id_wid
, reset_less
=True)
249 self
.out_mid
= Signal(id_wid
, reset_less
=True)
255 if self
.id_wid
is not None:
256 m
.d
.sync
+= self
.out_mid
.eq(self
.in_mid
)
259 class FPAddSpecialCases(FPState
, FPID
):
260 """ special cases: NaNs, infs, zeros, denormalised
261 NOTE: some of these are unique to add. see "Special Operations"
262 https://steve.hollasch.net/cgindex/coding/ieeefloat.html
265 def __init__(self
, width
, id_wid
):
266 FPState
.__init
__(self
, "special_cases")
267 FPID
.__init
__(self
, id_wid
)
268 self
.mod
= FPAddSpecialCasesMod(width
)
269 self
.out_z
= FPNumOut(width
, False)
270 self
.out_do_z
= Signal(reset_less
=True)
272 def setup(self
, m
, in_a
, in_b
, in_mid
):
273 """ links module to inputs and outputs
275 self
.mod
.setup(m
, in_a
, in_b
, self
.out_do_z
)
276 if self
.in_mid
is not None:
277 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
281 with m
.If(self
.out_do_z
):
282 m
.d
.sync
+= self
.out_z
.v
.eq(self
.mod
.out_z
.v
) # only take the output
285 m
.next
= "denormalise"
288 class FPAddDeNormMod(FPState
):
290 def __init__(self
, width
):
291 self
.in_a
= FPNumBase(width
)
292 self
.in_b
= FPNumBase(width
)
293 self
.out_a
= FPNumBase(width
)
294 self
.out_b
= FPNumBase(width
)
296 def elaborate(self
, platform
):
298 m
.submodules
.denorm_in_a
= self
.in_a
299 m
.submodules
.denorm_in_b
= self
.in_b
300 m
.submodules
.denorm_out_a
= self
.out_a
301 m
.submodules
.denorm_out_b
= self
.out_b
302 # hmmm, don't like repeating identical code
303 m
.d
.comb
+= self
.out_a
.copy(self
.in_a
)
304 with m
.If(self
.in_a
.exp_n127
):
305 m
.d
.comb
+= self
.out_a
.e
.eq(self
.in_a
.N126
) # limit a exponent
307 m
.d
.comb
+= self
.out_a
.m
[-1].eq(1) # set top mantissa bit
309 m
.d
.comb
+= self
.out_b
.copy(self
.in_b
)
310 with m
.If(self
.in_b
.exp_n127
):
311 m
.d
.comb
+= self
.out_b
.e
.eq(self
.in_b
.N126
) # limit a exponent
313 m
.d
.comb
+= self
.out_b
.m
[-1].eq(1) # set top mantissa bit
318 class FPAddDeNorm(FPState
, FPID
):
320 def __init__(self
, width
, id_wid
):
321 FPState
.__init
__(self
, "denormalise")
322 FPID
.__init
__(self
, id_wid
)
323 self
.mod
= FPAddDeNormMod(width
)
324 self
.out_a
= FPNumBase(width
)
325 self
.out_b
= FPNumBase(width
)
327 def setup(self
, m
, in_a
, in_b
, in_mid
):
328 """ links module to inputs and outputs
330 m
.submodules
.denormalise
= self
.mod
331 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
332 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
333 if self
.in_mid
is not None:
334 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
338 # Denormalised Number checks
340 m
.d
.sync
+= self
.out_a
.copy(self
.mod
.out_a
)
341 m
.d
.sync
+= self
.out_b
.copy(self
.mod
.out_b
)
344 class FPAddAlignMultiMod(FPState
):
346 def __init__(self
, width
):
347 self
.in_a
= FPNumBase(width
)
348 self
.in_b
= FPNumBase(width
)
349 self
.out_a
= FPNumIn(None, width
)
350 self
.out_b
= FPNumIn(None, width
)
351 self
.exp_eq
= Signal(reset_less
=True)
353 def elaborate(self
, platform
):
354 # This one however (single-cycle) will do the shift
359 m
.submodules
.align_in_a
= self
.in_a
360 m
.submodules
.align_in_b
= self
.in_b
361 m
.submodules
.align_out_a
= self
.out_a
362 m
.submodules
.align_out_b
= self
.out_b
364 # NOTE: this does *not* do single-cycle multi-shifting,
365 # it *STAYS* in the align state until exponents match
367 # exponent of a greater than b: shift b down
368 m
.d
.comb
+= self
.exp_eq
.eq(0)
369 m
.d
.comb
+= self
.out_a
.copy(self
.in_a
)
370 m
.d
.comb
+= self
.out_b
.copy(self
.in_b
)
371 agtb
= Signal(reset_less
=True)
372 altb
= Signal(reset_less
=True)
373 m
.d
.comb
+= agtb
.eq(self
.in_a
.e
> self
.in_b
.e
)
374 m
.d
.comb
+= altb
.eq(self
.in_a
.e
< self
.in_b
.e
)
376 m
.d
.comb
+= self
.out_b
.shift_down(self
.in_b
)
377 # exponent of b greater than a: shift a down
379 m
.d
.comb
+= self
.out_a
.shift_down(self
.in_a
)
380 # exponents equal: move to next stage.
382 m
.d
.comb
+= self
.exp_eq
.eq(1)
386 class FPAddAlignMulti(FPState
, FPID
):
388 def __init__(self
, width
, id_wid
):
389 FPID
.__init
__(self
, id_wid
)
390 FPState
.__init
__(self
, "align")
391 self
.mod
= FPAddAlignMultiMod(width
)
392 self
.out_a
= FPNumIn(None, width
)
393 self
.out_b
= FPNumIn(None, width
)
394 self
.exp_eq
= Signal(reset_less
=True)
396 def setup(self
, m
, in_a
, in_b
, in_mid
):
397 """ links module to inputs and outputs
399 m
.submodules
.align
= self
.mod
400 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
401 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
402 #m.d.comb += self.out_a.copy(self.mod.out_a)
403 #m.d.comb += self.out_b.copy(self.mod.out_b)
404 m
.d
.comb
+= self
.exp_eq
.eq(self
.mod
.exp_eq
)
405 if self
.in_mid
is not None:
406 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
410 m
.d
.sync
+= self
.out_a
.copy(self
.mod
.out_a
)
411 m
.d
.sync
+= self
.out_b
.copy(self
.mod
.out_b
)
412 with m
.If(self
.exp_eq
):
416 class FPAddAlignSingleMod
:
418 def __init__(self
, width
):
420 self
.in_a
= FPNumBase(width
)
421 self
.in_b
= FPNumBase(width
)
422 self
.out_a
= FPNumIn(None, width
)
423 self
.out_b
= FPNumIn(None, width
)
425 def elaborate(self
, platform
):
426 """ Aligns A against B or B against A, depending on which has the
427 greater exponent. This is done in a *single* cycle using
428 variable-width bit-shift
430 the shifter used here is quite expensive in terms of gates.
431 Mux A or B in (and out) into temporaries, as only one of them
432 needs to be aligned against the other
436 m
.submodules
.align_in_a
= self
.in_a
437 m
.submodules
.align_in_b
= self
.in_b
438 m
.submodules
.align_out_a
= self
.out_a
439 m
.submodules
.align_out_b
= self
.out_b
441 # temporary (muxed) input and output to be shifted
442 t_inp
= FPNumBase(self
.width
)
443 t_out
= FPNumIn(None, self
.width
)
444 espec
= (len(self
.in_a
.e
), True)
445 msr
= MultiShiftRMerge(self
.in_a
.m_width
, espec
)
446 m
.submodules
.align_t_in
= t_inp
447 m
.submodules
.align_t_out
= t_out
448 m
.submodules
.multishift_r
= msr
450 ediff
= Signal(espec
, reset_less
=True)
451 ediffr
= Signal(espec
, reset_less
=True)
452 tdiff
= Signal(espec
, reset_less
=True)
453 elz
= Signal(reset_less
=True)
454 egz
= Signal(reset_less
=True)
456 # connect multi-shifter to t_inp/out mantissa (and tdiff)
457 m
.d
.comb
+= msr
.inp
.eq(t_inp
.m
)
458 m
.d
.comb
+= msr
.diff
.eq(tdiff
)
459 m
.d
.comb
+= t_out
.m
.eq(msr
.m
)
460 m
.d
.comb
+= t_out
.e
.eq(t_inp
.e
+ tdiff
)
461 m
.d
.comb
+= t_out
.s
.eq(t_inp
.s
)
463 m
.d
.comb
+= ediff
.eq(self
.in_a
.e
- self
.in_b
.e
)
464 m
.d
.comb
+= ediffr
.eq(self
.in_b
.e
- self
.in_a
.e
)
465 m
.d
.comb
+= elz
.eq(self
.in_a
.e
< self
.in_b
.e
)
466 m
.d
.comb
+= egz
.eq(self
.in_a
.e
> self
.in_b
.e
)
468 # default: A-exp == B-exp, A and B untouched (fall through)
469 m
.d
.comb
+= self
.out_a
.copy(self
.in_a
)
470 m
.d
.comb
+= self
.out_b
.copy(self
.in_b
)
471 # only one shifter (muxed)
472 #m.d.comb += t_out.shift_down_multi(tdiff, t_inp)
473 # exponent of a greater than b: shift b down
475 m
.d
.comb
+= [t_inp
.copy(self
.in_b
),
477 self
.out_b
.copy(t_out
),
478 self
.out_b
.s
.eq(self
.in_b
.s
), # whoops forgot sign
480 # exponent of b greater than a: shift a down
482 m
.d
.comb
+= [t_inp
.copy(self
.in_a
),
484 self
.out_a
.copy(t_out
),
485 self
.out_a
.s
.eq(self
.in_a
.s
), # whoops forgot sign
490 class FPAddAlignSingle(FPState
, FPID
):
492 def __init__(self
, width
, id_wid
):
493 FPState
.__init
__(self
, "align")
494 FPID
.__init
__(self
, id_wid
)
495 self
.mod
= FPAddAlignSingleMod(width
)
496 self
.out_a
= FPNumIn(None, width
)
497 self
.out_b
= FPNumIn(None, width
)
499 def setup(self
, m
, in_a
, in_b
, in_mid
):
500 """ links module to inputs and outputs
502 m
.submodules
.align
= self
.mod
503 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
504 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
505 if self
.in_mid
is not None:
506 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
510 # NOTE: could be done as comb
511 m
.d
.sync
+= self
.out_a
.copy(self
.mod
.out_a
)
512 m
.d
.sync
+= self
.out_b
.copy(self
.mod
.out_b
)
516 class FPAddStage0Mod
:
518 def __init__(self
, width
):
519 self
.in_a
= FPNumBase(width
)
520 self
.in_b
= FPNumBase(width
)
521 self
.in_z
= FPNumBase(width
, False)
522 self
.out_z
= FPNumBase(width
, False)
523 self
.out_tot
= Signal(self
.out_z
.m_width
+ 4, reset_less
=True)
525 def elaborate(self
, platform
):
527 m
.submodules
.add0_in_a
= self
.in_a
528 m
.submodules
.add0_in_b
= self
.in_b
529 m
.submodules
.add0_out_z
= self
.out_z
531 m
.d
.comb
+= self
.out_z
.e
.eq(self
.in_a
.e
)
533 # store intermediate tests (and zero-extended mantissas)
534 seq
= Signal(reset_less
=True)
535 mge
= Signal(reset_less
=True)
536 am0
= Signal(len(self
.in_a
.m
)+1, reset_less
=True)
537 bm0
= Signal(len(self
.in_b
.m
)+1, reset_less
=True)
538 m
.d
.comb
+= [seq
.eq(self
.in_a
.s
== self
.in_b
.s
),
539 mge
.eq(self
.in_a
.m
>= self
.in_b
.m
),
540 am0
.eq(Cat(self
.in_a
.m
, 0)),
541 bm0
.eq(Cat(self
.in_b
.m
, 0))
543 # same-sign (both negative or both positive) add mantissas
546 self
.out_tot
.eq(am0
+ bm0
),
547 self
.out_z
.s
.eq(self
.in_a
.s
)
549 # a mantissa greater than b, use a
552 self
.out_tot
.eq(am0
- bm0
),
553 self
.out_z
.s
.eq(self
.in_a
.s
)
555 # b mantissa greater than a, use b
558 self
.out_tot
.eq(bm0
- am0
),
559 self
.out_z
.s
.eq(self
.in_b
.s
)
564 class FPAddStage0(FPState
, FPID
):
565 """ First stage of add. covers same-sign (add) and subtract
566 special-casing when mantissas are greater or equal, to
567 give greatest accuracy.
570 def __init__(self
, width
, id_wid
):
571 FPState
.__init
__(self
, "add_0")
572 FPID
.__init
__(self
, id_wid
)
573 self
.mod
= FPAddStage0Mod(width
)
574 self
.out_z
= FPNumBase(width
, False)
575 self
.out_tot
= Signal(self
.out_z
.m_width
+ 4, reset_less
=True)
577 def setup(self
, m
, in_a
, in_b
, in_mid
):
578 """ links module to inputs and outputs
580 m
.submodules
.add0
= self
.mod
581 m
.d
.comb
+= self
.mod
.in_a
.copy(in_a
)
582 m
.d
.comb
+= self
.mod
.in_b
.copy(in_b
)
583 if self
.in_mid
is not None:
584 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
588 # NOTE: these could be done as combinatorial (merge add0+add1)
589 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
590 m
.d
.sync
+= self
.out_tot
.eq(self
.mod
.out_tot
)
594 class FPAddStage1Mod(FPState
):
595 """ Second stage of add: preparation for normalisation.
596 detects when tot sum is too big (tot[27] is kinda a carry bit)
599 def __init__(self
, width
):
600 self
.out_norm
= Signal(reset_less
=True)
601 self
.in_z
= FPNumBase(width
, False)
602 self
.in_tot
= Signal(self
.in_z
.m_width
+ 4, reset_less
=True)
603 self
.out_z
= FPNumBase(width
, False)
604 self
.out_of
= Overflow()
606 def elaborate(self
, platform
):
608 #m.submodules.norm1_in_overflow = self.in_of
609 #m.submodules.norm1_out_overflow = self.out_of
610 #m.submodules.norm1_in_z = self.in_z
611 #m.submodules.norm1_out_z = self.out_z
612 m
.d
.comb
+= self
.out_z
.copy(self
.in_z
)
613 # tot[27] gets set when the sum overflows. shift result down
614 with m
.If(self
.in_tot
[-1]):
616 self
.out_z
.m
.eq(self
.in_tot
[4:]),
617 self
.out_of
.m0
.eq(self
.in_tot
[4]),
618 self
.out_of
.guard
.eq(self
.in_tot
[3]),
619 self
.out_of
.round_bit
.eq(self
.in_tot
[2]),
620 self
.out_of
.sticky
.eq(self
.in_tot
[1] | self
.in_tot
[0]),
621 self
.out_z
.e
.eq(self
.in_z
.e
+ 1)
626 self
.out_z
.m
.eq(self
.in_tot
[3:]),
627 self
.out_of
.m0
.eq(self
.in_tot
[3]),
628 self
.out_of
.guard
.eq(self
.in_tot
[2]),
629 self
.out_of
.round_bit
.eq(self
.in_tot
[1]),
630 self
.out_of
.sticky
.eq(self
.in_tot
[0])
635 class FPAddStage1(FPState
, FPID
):
637 def __init__(self
, width
, id_wid
):
638 FPState
.__init
__(self
, "add_1")
639 FPID
.__init
__(self
, id_wid
)
640 self
.mod
= FPAddStage1Mod(width
)
641 self
.out_z
= FPNumBase(width
, False)
642 self
.out_of
= Overflow()
643 self
.norm_stb
= Signal()
645 def setup(self
, m
, in_tot
, in_z
, in_mid
):
646 """ links module to inputs and outputs
648 m
.submodules
.add1
= self
.mod
649 m
.submodules
.add1_out_overflow
= self
.out_of
651 m
.d
.comb
+= self
.mod
.in_z
.copy(in_z
)
652 m
.d
.comb
+= self
.mod
.in_tot
.eq(in_tot
)
654 m
.d
.sync
+= self
.norm_stb
.eq(0) # sets to zero when not in add1 state
656 if self
.in_mid
is not None:
657 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
661 m
.d
.sync
+= self
.out_of
.copy(self
.mod
.out_of
)
662 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
663 m
.d
.sync
+= self
.norm_stb
.eq(1)
664 m
.next
= "normalise_1"
667 class FPNorm1ModSingle
:
669 def __init__(self
, width
):
671 self
.out_norm
= Signal(reset_less
=True)
672 self
.in_z
= FPNumBase(width
, False)
673 self
.in_of
= Overflow()
674 self
.out_z
= FPNumBase(width
, False)
675 self
.out_of
= Overflow()
677 def setup(self
, m
, in_z
, in_of
, out_z
):
678 """ links module to inputs and outputs
680 m
.submodules
.normalise_1
= self
682 m
.d
.comb
+= self
.in_z
.copy(in_z
)
683 m
.d
.comb
+= self
.in_of
.copy(in_of
)
685 m
.d
.comb
+= out_z
.copy(self
.out_z
)
687 def elaborate(self
, platform
):
690 mwid
= self
.out_z
.m_width
+2
691 pe
= PriorityEncoder(mwid
)
692 m
.submodules
.norm_pe
= pe
694 m
.submodules
.norm1_out_z
= self
.out_z
695 m
.submodules
.norm1_out_overflow
= self
.out_of
696 m
.submodules
.norm1_in_z
= self
.in_z
697 m
.submodules
.norm1_in_overflow
= self
.in_of
699 in_z
= FPNumBase(self
.width
, False)
701 m
.submodules
.norm1_insel_z
= in_z
702 m
.submodules
.norm1_insel_overflow
= in_of
704 espec
= (len(in_z
.e
), True)
705 ediff_n126
= Signal(espec
, reset_less
=True)
706 msr
= MultiShiftRMerge(mwid
, espec
)
707 m
.submodules
.multishift_r
= msr
709 m
.d
.comb
+= in_z
.copy(self
.in_z
)
710 m
.d
.comb
+= in_of
.copy(self
.in_of
)
711 # initialise out from in (overridden below)
712 m
.d
.comb
+= self
.out_z
.copy(in_z
)
713 m
.d
.comb
+= self
.out_of
.copy(in_of
)
714 # normalisation increase/decrease conditions
715 decrease
= Signal(reset_less
=True)
716 increase
= Signal(reset_less
=True)
717 m
.d
.comb
+= decrease
.eq(in_z
.m_msbzero
& in_z
.exp_gt_n126
)
718 m
.d
.comb
+= increase
.eq(in_z
.exp_lt_n126
)
721 # *sigh* not entirely obvious: count leading zeros (clz)
722 # with a PriorityEncoder: to find from the MSB
723 # we reverse the order of the bits.
724 temp_m
= Signal(mwid
, reset_less
=True)
725 temp_s
= Signal(mwid
+1, reset_less
=True)
726 clz
= Signal((len(in_z
.e
), True), reset_less
=True)
727 # make sure that the amount to decrease by does NOT
728 # go below the minimum non-INF/NaN exponent
729 limclz
= Mux(in_z
.exp_sub_n126
> pe
.o
, pe
.o
,
732 # cat round and guard bits back into the mantissa
733 temp_m
.eq(Cat(in_of
.round_bit
, in_of
.guard
, in_z
.m
)),
734 pe
.i
.eq(temp_m
[::-1]), # inverted
735 clz
.eq(limclz
), # count zeros from MSB down
736 temp_s
.eq(temp_m
<< clz
), # shift mantissa UP
737 self
.out_z
.e
.eq(in_z
.e
- clz
), # DECREASE exponent
738 self
.out_z
.m
.eq(temp_s
[2:]), # exclude bits 0&1
739 self
.out_of
.m0
.eq(temp_s
[2]), # copy of mantissa[0]
740 # overflow in bits 0..1: got shifted too (leave sticky)
741 self
.out_of
.guard
.eq(temp_s
[1]), # guard
742 self
.out_of
.round_bit
.eq(temp_s
[0]), # round
745 with m
.Elif(increase
):
746 temp_m
= Signal(mwid
+1, reset_less
=True)
748 temp_m
.eq(Cat(in_of
.sticky
, in_of
.round_bit
, in_of
.guard
,
750 ediff_n126
.eq(in_z
.N126
- in_z
.e
),
751 # connect multi-shifter to inp/out mantissa (and ediff)
753 msr
.diff
.eq(ediff_n126
),
754 self
.out_z
.m
.eq(msr
.m
[3:]),
755 self
.out_of
.m0
.eq(temp_s
[3]), # copy of mantissa[0]
756 # overflow in bits 0..1: got shifted too (leave sticky)
757 self
.out_of
.guard
.eq(temp_s
[2]), # guard
758 self
.out_of
.round_bit
.eq(temp_s
[1]), # round
759 self
.out_of
.sticky
.eq(temp_s
[0]), # sticky
760 self
.out_z
.e
.eq(in_z
.e
+ ediff_n126
),
766 class FPNorm1ModMulti
:
768 def __init__(self
, width
, single_cycle
=True):
770 self
.in_select
= Signal(reset_less
=True)
771 self
.out_norm
= Signal(reset_less
=True)
772 self
.in_z
= FPNumBase(width
, False)
773 self
.in_of
= Overflow()
774 self
.temp_z
= FPNumBase(width
, False)
775 self
.temp_of
= Overflow()
776 self
.out_z
= FPNumBase(width
, False)
777 self
.out_of
= Overflow()
779 def elaborate(self
, platform
):
782 m
.submodules
.norm1_out_z
= self
.out_z
783 m
.submodules
.norm1_out_overflow
= self
.out_of
784 m
.submodules
.norm1_temp_z
= self
.temp_z
785 m
.submodules
.norm1_temp_of
= self
.temp_of
786 m
.submodules
.norm1_in_z
= self
.in_z
787 m
.submodules
.norm1_in_overflow
= self
.in_of
789 in_z
= FPNumBase(self
.width
, False)
791 m
.submodules
.norm1_insel_z
= in_z
792 m
.submodules
.norm1_insel_overflow
= in_of
794 # select which of temp or in z/of to use
795 with m
.If(self
.in_select
):
796 m
.d
.comb
+= in_z
.copy(self
.in_z
)
797 m
.d
.comb
+= in_of
.copy(self
.in_of
)
799 m
.d
.comb
+= in_z
.copy(self
.temp_z
)
800 m
.d
.comb
+= in_of
.copy(self
.temp_of
)
801 # initialise out from in (overridden below)
802 m
.d
.comb
+= self
.out_z
.copy(in_z
)
803 m
.d
.comb
+= self
.out_of
.copy(in_of
)
804 # normalisation increase/decrease conditions
805 decrease
= Signal(reset_less
=True)
806 increase
= Signal(reset_less
=True)
807 m
.d
.comb
+= decrease
.eq(in_z
.m_msbzero
& in_z
.exp_gt_n126
)
808 m
.d
.comb
+= increase
.eq(in_z
.exp_lt_n126
)
809 m
.d
.comb
+= self
.out_norm
.eq(decrease | increase
) # loop-end
813 self
.out_z
.e
.eq(in_z
.e
- 1), # DECREASE exponent
814 self
.out_z
.m
.eq(in_z
.m
<< 1), # shift mantissa UP
815 self
.out_z
.m
[0].eq(in_of
.guard
), # steal guard (was tot[2])
816 self
.out_of
.guard
.eq(in_of
.round_bit
), # round (was tot[1])
817 self
.out_of
.round_bit
.eq(0), # reset round bit
818 self
.out_of
.m0
.eq(in_of
.guard
),
821 with m
.Elif(increase
):
823 self
.out_z
.e
.eq(in_z
.e
+ 1), # INCREASE exponent
824 self
.out_z
.m
.eq(in_z
.m
>> 1), # shift mantissa DOWN
825 self
.out_of
.guard
.eq(in_z
.m
[0]),
826 self
.out_of
.m0
.eq(in_z
.m
[1]),
827 self
.out_of
.round_bit
.eq(in_of
.guard
),
828 self
.out_of
.sticky
.eq(in_of
.sticky | in_of
.round_bit
)
834 class FPNorm1Single(FPState
, FPID
):
836 def __init__(self
, width
, id_wid
, single_cycle
=True):
837 FPID
.__init
__(self
, id_wid
)
838 FPState
.__init
__(self
, "normalise_1")
839 self
.mod
= FPNorm1ModSingle(width
)
840 self
.out_norm
= Signal(reset_less
=True)
841 self
.out_z
= FPNumBase(width
)
842 self
.out_roundz
= Signal(reset_less
=True)
844 def setup(self
, m
, in_z
, in_of
, in_mid
):
845 """ links module to inputs and outputs
847 self
.mod
.setup(m
, in_z
, in_of
, self
.out_z
)
849 if self
.in_mid
is not None:
850 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
854 m
.d
.sync
+= self
.out_roundz
.eq(self
.mod
.out_of
.roundz
)
858 class FPNorm1Multi(FPState
, FPID
):
860 def __init__(self
, width
, id_wid
):
861 FPID
.__init
__(self
, id_wid
)
862 FPState
.__init
__(self
, "normalise_1")
863 self
.mod
= FPNorm1ModMulti(width
)
864 self
.stb
= Signal(reset_less
=True)
865 self
.ack
= Signal(reset
=0, reset_less
=True)
866 self
.out_norm
= Signal(reset_less
=True)
867 self
.in_accept
= Signal(reset_less
=True)
868 self
.temp_z
= FPNumBase(width
)
869 self
.temp_of
= Overflow()
870 self
.out_z
= FPNumBase(width
)
871 self
.out_roundz
= Signal(reset_less
=True)
873 def setup(self
, m
, in_z
, in_of
, norm_stb
, in_mid
):
874 """ links module to inputs and outputs
876 self
.mod
.setup(m
, in_z
, in_of
, norm_stb
,
877 self
.in_accept
, self
.temp_z
, self
.temp_of
,
878 self
.out_z
, self
.out_norm
)
880 m
.d
.comb
+= self
.stb
.eq(norm_stb
)
881 m
.d
.sync
+= self
.ack
.eq(0) # sets to zero when not in normalise_1 state
883 if self
.in_mid
is not None:
884 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
888 m
.d
.comb
+= self
.in_accept
.eq((~self
.ack
) & (self
.stb
))
889 m
.d
.sync
+= self
.temp_of
.copy(self
.mod
.out_of
)
890 m
.d
.sync
+= self
.temp_z
.copy(self
.out_z
)
891 with m
.If(self
.out_norm
):
892 with m
.If(self
.in_accept
):
897 m
.d
.sync
+= self
.ack
.eq(0)
899 # normalisation not required (or done).
901 m
.d
.sync
+= self
.ack
.eq(1)
902 m
.d
.sync
+= self
.out_roundz
.eq(self
.mod
.out_of
.roundz
)
905 class FPNormToPack(FPState
, FPID
):
907 def __init__(self
, width
, id_wid
):
908 FPID
.__init
__(self
, id_wid
)
909 FPState
.__init
__(self
, "normalise_1")
912 def setup(self
, m
, in_z
, in_of
, in_mid
):
913 """ links module to inputs and outputs
916 # Normalisation (chained to input in_z+in_of)
917 nmod
= FPNorm1ModSingle(self
.width
)
918 n_out_z
= FPNumBase(self
.width
)
919 n_out_roundz
= Signal(reset_less
=True)
920 nmod
.setup(m
, in_z
, in_of
, n_out_z
)
922 # Rounding (chained to normalisation)
923 rmod
= FPRoundMod(self
.width
)
924 r_out_z
= FPNumBase(self
.width
)
925 rmod
.setup(m
, n_out_z
, n_out_roundz
)
926 m
.d
.comb
+= n_out_roundz
.eq(nmod
.out_of
.roundz
)
927 m
.d
.comb
+= r_out_z
.copy(rmod
.out_z
)
929 # Corrections (chained to rounding)
930 cmod
= FPCorrectionsMod(self
.width
)
931 c_out_z
= FPNumBase(self
.width
)
932 cmod
.setup(m
, r_out_z
)
933 m
.d
.comb
+= c_out_z
.copy(cmod
.out_z
)
935 # Pack (chained to corrections)
936 self
.pmod
= FPPackMod(self
.width
)
937 self
.out_z
= FPNumBase(self
.width
)
938 self
.pmod
.setup(m
, c_out_z
)
941 if self
.in_mid
is not None:
942 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
945 self
.idsync(m
) # copies incoming ID to outgoing
946 m
.d
.sync
+= self
.out_z
.v
.eq(self
.pmod
.out_z
.v
) # outputs packed result
947 m
.next
= "pack_put_z"
952 def __init__(self
, width
):
953 self
.in_roundz
= Signal(reset_less
=True)
954 self
.in_z
= FPNumBase(width
, False)
955 self
.out_z
= FPNumBase(width
, False)
957 def setup(self
, m
, in_z
, roundz
):
958 m
.submodules
.roundz
= self
960 m
.d
.comb
+= self
.in_z
.copy(in_z
)
961 m
.d
.comb
+= self
.in_roundz
.eq(roundz
)
963 def elaborate(self
, platform
):
965 m
.d
.comb
+= self
.out_z
.copy(self
.in_z
)
966 with m
.If(self
.in_roundz
):
967 m
.d
.comb
+= self
.out_z
.m
.eq(self
.in_z
.m
+ 1) # mantissa rounds up
968 with m
.If(self
.in_z
.m
== self
.in_z
.m1s
): # all 1s
969 m
.d
.comb
+= self
.out_z
.e
.eq(self
.in_z
.e
+ 1) # exponent up
973 class FPRound(FPState
, FPID
):
975 def __init__(self
, width
, id_wid
):
976 FPState
.__init
__(self
, "round")
977 FPID
.__init
__(self
, id_wid
)
978 self
.mod
= FPRoundMod(width
)
979 self
.out_z
= FPNumBase(width
)
981 def setup(self
, m
, in_z
, roundz
, in_mid
):
982 """ links module to inputs and outputs
984 self
.mod
.setup(m
, in_z
, roundz
)
986 if self
.in_mid
is not None:
987 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
991 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
992 m
.next
= "corrections"
995 class FPCorrectionsMod
:
997 def __init__(self
, width
):
998 self
.in_z
= FPNumOut(width
, False)
999 self
.out_z
= FPNumOut(width
, False)
1001 def setup(self
, m
, in_z
):
1002 """ links module to inputs and outputs
1004 m
.submodules
.corrections
= self
1005 m
.d
.comb
+= self
.in_z
.copy(in_z
)
1007 def elaborate(self
, platform
):
1009 m
.submodules
.corr_in_z
= self
.in_z
1010 m
.submodules
.corr_out_z
= self
.out_z
1011 m
.d
.comb
+= self
.out_z
.copy(self
.in_z
)
1012 with m
.If(self
.in_z
.is_denormalised
):
1013 m
.d
.comb
+= self
.out_z
.e
.eq(self
.in_z
.N127
)
1017 class FPCorrections(FPState
, FPID
):
1019 def __init__(self
, width
, id_wid
):
1020 FPState
.__init
__(self
, "corrections")
1021 FPID
.__init
__(self
, id_wid
)
1022 self
.mod
= FPCorrectionsMod(width
)
1023 self
.out_z
= FPNumBase(width
)
1025 def setup(self
, m
, in_z
, in_mid
):
1026 """ links module to inputs and outputs
1028 self
.mod
.setup(m
, in_z
)
1029 if self
.in_mid
is not None:
1030 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
1032 def action(self
, m
):
1034 m
.d
.sync
+= self
.out_z
.copy(self
.mod
.out_z
)
1040 def __init__(self
, width
):
1041 self
.in_z
= FPNumOut(width
, False)
1042 self
.out_z
= FPNumOut(width
, False)
1044 def setup(self
, m
, in_z
):
1045 """ links module to inputs and outputs
1047 m
.submodules
.pack
= self
1048 m
.d
.comb
+= self
.in_z
.copy(in_z
)
1050 def elaborate(self
, platform
):
1052 m
.submodules
.pack_in_z
= self
.in_z
1053 with m
.If(self
.in_z
.is_overflowed
):
1054 m
.d
.comb
+= self
.out_z
.inf(self
.in_z
.s
)
1056 m
.d
.comb
+= self
.out_z
.create(self
.in_z
.s
, self
.in_z
.e
, self
.in_z
.m
)
1060 class FPPack(FPState
, FPID
):
1062 def __init__(self
, width
, id_wid
):
1063 FPState
.__init
__(self
, "pack")
1064 FPID
.__init
__(self
, id_wid
)
1065 self
.mod
= FPPackMod(width
)
1066 self
.out_z
= FPNumOut(width
, False)
1068 def setup(self
, m
, in_z
, in_mid
):
1069 """ links module to inputs and outputs
1071 self
.mod
.setup(m
, in_z
)
1072 if self
.in_mid
is not None:
1073 m
.d
.comb
+= self
.in_mid
.eq(in_mid
)
1075 def action(self
, m
):
1077 m
.d
.sync
+= self
.out_z
.v
.eq(self
.mod
.out_z
.v
)
1078 m
.next
= "pack_put_z"
1081 class FPPutZ(FPState
):
1083 def __init__(self
, state
, in_z
, out_z
, in_mid
, out_mid
):
1084 FPState
.__init
__(self
, state
)
1087 self
.in_mid
= in_mid
1088 self
.out_mid
= out_mid
1090 def action(self
, m
):
1091 if self
.in_mid
is not None:
1092 m
.d
.sync
+= self
.out_mid
.eq(self
.in_mid
)
1094 self
.out_z
.v
.eq(self
.in_z
.v
)
1096 with m
.If(self
.out_z
.stb
& self
.out_z
.ack
):
1097 m
.d
.sync
+= self
.out_z
.stb
.eq(0)
1100 m
.d
.sync
+= self
.out_z
.stb
.eq(1)
1103 class FPADDBaseMod(FPID
):
1105 def __init__(self
, width
, id_wid
=None, single_cycle
=False, compact
=True):
1108 * width: bit-width of IEEE754. supported: 16, 32, 64
1109 * id_wid: an identifier that is sync-connected to the input
1110 * single_cycle: True indicates each stage to complete in 1 clock
1111 * compact: True indicates a reduced number of stages
1113 FPID
.__init
__(self
, id_wid
)
1115 self
.single_cycle
= single_cycle
1116 self
.compact
= compact
1118 self
.in_t
= Trigger()
1119 self
.in_a
= Signal(width
)
1120 self
.in_b
= Signal(width
)
1121 self
.out_z
= FPOp(width
)
1125 def add_state(self
, state
):
1126 self
.states
.append(state
)
1129 def get_fragment(self
, platform
=None):
1130 """ creates the HDL code-fragment for FPAdd
1133 m
.submodules
.out_z
= self
.out_z
1134 m
.submodules
.in_t
= self
.in_t
1136 self
.get_compact_fragment(m
, platform
)
1138 self
.get_longer_fragment(m
, platform
)
1140 with m
.FSM() as fsm
:
1142 for state
in self
.states
:
1143 with m
.State(state
.state_from
):
1148 def get_longer_fragment(self
, m
, platform
=None):
1150 get
= self
.add_state(FPGet2Op("get_ops", "special_cases",
1151 self
.in_a
, self
.in_b
, self
.width
))
1152 get
.setup(m
, self
.in_a
, self
.in_b
, self
.in_t
.stb
, self
.in_t
.ack
)
1156 sc
= self
.add_state(FPAddSpecialCases(self
.width
, self
.id_wid
))
1157 sc
.setup(m
, a
, b
, self
.in_mid
)
1159 dn
= self
.add_state(FPAddDeNorm(self
.width
, self
.id_wid
))
1160 dn
.setup(m
, a
, b
, sc
.in_mid
)
1162 if self
.single_cycle
:
1163 alm
= self
.add_state(FPAddAlignSingle(self
.width
, self
.id_wid
))
1164 alm
.setup(m
, dn
.out_a
, dn
.out_b
, dn
.in_mid
)
1166 alm
= self
.add_state(FPAddAlignMulti(self
.width
, self
.id_wid
))
1167 alm
.setup(m
, dn
.out_a
, dn
.out_b
, dn
.in_mid
)
1169 add0
= self
.add_state(FPAddStage0(self
.width
, self
.id_wid
))
1170 add0
.setup(m
, alm
.out_a
, alm
.out_b
, alm
.in_mid
)
1172 add1
= self
.add_state(FPAddStage1(self
.width
, self
.id_wid
))
1173 add1
.setup(m
, add0
.out_tot
, add0
.out_z
, add0
.in_mid
)
1175 if self
.single_cycle
:
1176 n1
= self
.add_state(FPNorm1Single(self
.width
, self
.id_wid
))
1177 n1
.setup(m
, add1
.out_z
, add1
.out_of
, add0
.in_mid
)
1179 n1
= self
.add_state(FPNorm1Multi(self
.width
, self
.id_wid
))
1180 n1
.setup(m
, add1
.out_z
, add1
.out_of
, add1
.norm_stb
, add0
.in_mid
)
1182 rn
= self
.add_state(FPRound(self
.width
, self
.id_wid
))
1183 rn
.setup(m
, n1
.out_z
, n1
.out_roundz
, n1
.in_mid
)
1185 cor
= self
.add_state(FPCorrections(self
.width
, self
.id_wid
))
1186 cor
.setup(m
, rn
.out_z
, rn
.in_mid
)
1188 pa
= self
.add_state(FPPack(self
.width
, self
.id_wid
))
1189 pa
.setup(m
, cor
.out_z
, rn
.in_mid
)
1191 ppz
= self
.add_state(FPPutZ("pack_put_z", pa
.out_z
, self
.out_z
,
1192 pa
.in_mid
, self
.out_mid
))
1194 pz
= self
.add_state(FPPutZ("put_z", sc
.out_z
, self
.out_z
,
1195 pa
.in_mid
, self
.out_mid
))
1197 def get_compact_fragment(self
, m
, platform
=None):
1199 get
= self
.add_state(FPGet2Op("get_ops", "special_cases",
1200 self
.in_a
, self
.in_b
, self
.width
))
1201 get
.setup(m
, self
.in_a
, self
.in_b
, self
.in_t
.stb
, self
.in_t
.ack
)
1205 sc
= self
.add_state(FPAddSpecialCases(self
.width
, self
.id_wid
))
1206 sc
.setup(m
, a
, b
, self
.in_mid
)
1208 dn
= self
.add_state(FPAddDeNorm(self
.width
, self
.id_wid
))
1209 dn
.setup(m
, a
, b
, sc
.in_mid
)
1211 if self
.single_cycle
:
1212 alm
= self
.add_state(FPAddAlignSingle(self
.width
, self
.id_wid
))
1213 alm
.setup(m
, dn
.out_a
, dn
.out_b
, dn
.in_mid
)
1215 alm
= self
.add_state(FPAddAlignMulti(self
.width
, self
.id_wid
))
1216 alm
.setup(m
, dn
.out_a
, dn
.out_b
, dn
.in_mid
)
1218 add0
= self
.add_state(FPAddStage0(self
.width
, self
.id_wid
))
1219 add0
.setup(m
, alm
.out_a
, alm
.out_b
, alm
.in_mid
)
1221 add1
= self
.add_state(FPAddStage1(self
.width
, self
.id_wid
))
1222 add1
.setup(m
, add0
.out_tot
, add0
.out_z
, add0
.in_mid
)
1224 n1
= self
.add_state(FPNormToPack(self
.width
, self
.id_wid
))
1225 n1
.setup(m
, add1
.out_z
, add1
.out_of
, add0
.in_mid
)
1227 ppz
= self
.add_state(FPPutZ("pack_put_z", n1
.out_z
, self
.out_z
,
1228 n1
.in_mid
, self
.out_mid
))
1230 pz
= self
.add_state(FPPutZ("put_z", sc
.out_z
, self
.out_z
,
1231 sc
.in_mid
, self
.out_mid
))
1234 class FPADDBase(FPState
, FPID
):
1236 def __init__(self
, width
, id_wid
=None, single_cycle
=False):
1239 * width: bit-width of IEEE754. supported: 16, 32, 64
1240 * id_wid: an identifier that is sync-connected to the input
1241 * single_cycle: True indicates each stage to complete in 1 clock
1243 FPID
.__init
__(self
, id_wid
)
1244 FPState
.__init
__(self
, "fpadd")
1246 self
.single_cycle
= single_cycle
1247 self
.mod
= FPADDBaseMod(width
, id_wid
, single_cycle
)
1249 self
.in_t
= Trigger()
1250 self
.in_a
= Signal(width
)
1251 self
.in_b
= Signal(width
)
1252 #self.out_z = FPOp(width)
1254 self
.z_done
= Signal(reset_less
=True) # connects to out_z Strobe
1255 self
.in_accept
= Signal(reset_less
=True)
1256 self
.add_stb
= Signal(reset_less
=True)
1257 self
.add_ack
= Signal(reset
=0, reset_less
=True)
1259 def setup(self
, m
, a
, b
, add_stb
, in_mid
, out_z
, out_mid
):
1261 self
.out_mid
= out_mid
1262 m
.d
.comb
+= [self
.in_a
.eq(a
),
1264 self
.mod
.in_a
.eq(self
.in_a
),
1265 self
.mod
.in_b
.eq(self
.in_b
),
1266 self
.in_mid
.eq(in_mid
),
1267 self
.mod
.in_mid
.eq(self
.in_mid
),
1268 self
.z_done
.eq(self
.mod
.out_z
.trigger
),
1269 #self.add_stb.eq(add_stb),
1270 self
.mod
.in_t
.stb
.eq(self
.in_t
.stb
),
1271 self
.in_t
.ack
.eq(self
.mod
.in_t
.ack
),
1272 self
.out_mid
.eq(self
.mod
.out_mid
),
1273 self
.out_z
.v
.eq(self
.mod
.out_z
.v
),
1274 self
.out_z
.stb
.eq(self
.mod
.out_z
.stb
),
1275 self
.mod
.out_z
.ack
.eq(self
.out_z
.ack
),
1278 m
.d
.sync
+= self
.add_stb
.eq(add_stb
)
1279 m
.d
.sync
+= self
.add_ack
.eq(0) # sets to zero when not in active state
1280 #m.d.sync += self.in_t.stb.eq(0)
1282 m
.submodules
.fpadd
= self
.mod
1284 def action(self
, m
):
1286 # in_accept is set on incoming strobe HIGH and ack LOW.
1287 m
.d
.comb
+= self
.in_accept
.eq((~self
.add_ack
) & (self
.add_stb
))
1289 #with m.If(self.in_t.ack):
1290 # m.d.sync += self.in_t.stb.eq(0)
1291 with m
.If(~self
.z_done
):
1292 # not done: test for accepting an incoming operand pair
1293 with m
.If(self
.in_accept
):
1295 self
.add_ack
.eq(1), # acknowledge receipt...
1296 self
.in_t
.stb
.eq(1), # initiate add
1299 m
.d
.sync
+= [self
.add_ack
.eq(0),
1300 self
.in_t
.stb
.eq(0),
1303 # done: acknowledge, and write out id and value
1304 m
.d
.sync
+= [self
.add_ack
.eq(1),
1311 if self
.in_mid
is not None:
1312 m
.d
.sync
+= self
.out_mid
.eq(self
.mod
.out_mid
)
1315 self
.out_z
.v
.eq(self
.mod
.out_z
.v
)
1317 # move to output state on detecting z ack
1318 with m
.If(self
.out_z
.trigger
):
1319 m
.d
.sync
+= self
.out_z
.stb
.eq(0)
1322 m
.d
.sync
+= self
.out_z
.stb
.eq(1)
1326 """ FPADD: stages as follows:
1332 FPAddBase---> FPAddBaseMod
1334 PutZ GetOps->Specials->Align->Add1/2->Norm->Round/Pack->PutZ
1336 FPAddBase is tricky: it is both a stage and *has* stages.
1337 Connection to FPAddBaseMod therefore requires an in stb/ack
1338 and an out stb/ack. Just as with Add1-Norm1 interaction, FPGetOp
1339 needs to be the thing that raises the incoming stb.
1342 def __init__(self
, width
, id_wid
=None, single_cycle
=False):
1345 * width: bit-width of IEEE754. supported: 16, 32, 64
1346 * id_wid: an identifier that is sync-connected to the input
1347 * single_cycle: True indicates each stage to complete in 1 clock
1349 FPID
.__init
__(self
, id_wid
)
1351 self
.id_wid
= id_wid
1352 self
.single_cycle
= single_cycle
1354 self
.in_a
= FPOp(width
)
1355 self
.in_b
= FPOp(width
)
1356 self
.out_z
= FPOp(width
)
1360 def add_state(self
, state
):
1361 self
.states
.append(state
)
1364 def get_fragment(self
, platform
=None):
1365 """ creates the HDL code-fragment for FPAdd
1368 m
.submodules
.in_a
= self
.in_a
1369 m
.submodules
.in_b
= self
.in_b
1370 m
.submodules
.out_z
= self
.out_z
1372 geta
= self
.add_state(FPGetOp("get_a", "get_b",
1373 self
.in_a
, self
.width
))
1374 geta
.setup(m
, self
.in_a
)
1377 getb
= self
.add_state(FPGetOp("get_b", "fpadd",
1378 self
.in_b
, self
.width
))
1379 getb
.setup(m
, self
.in_b
)
1382 ab
= FPADDBase(self
.width
, self
.id_wid
, self
.single_cycle
)
1383 ab
= self
.add_state(ab
)
1384 ab
.setup(m
, a
, b
, getb
.out_decode
, self
.in_mid
,
1385 self
.out_z
, self
.out_mid
)
1387 #pz = self.add_state(FPPutZ("put_z", ab.out_z, self.out_z,
1388 # ab.out_mid, self.out_mid))
1390 with m
.FSM() as fsm
:
1392 for state
in self
.states
:
1393 with m
.State(state
.state_from
):
1399 if __name__
== "__main__":
1401 alu
= FPADD(width
=32, id_wid
=5, single_cycle
=True)
1402 main(alu
, ports
=alu
.in_a
.ports() + \
1403 alu
.in_b
.ports() + \
1404 alu
.out_z
.ports() + \
1405 [alu
.in_mid
, alu
.out_mid
])
1407 alu
= FPADDBase(width
=32, id_wid
=5, single_cycle
=True)
1408 main(alu
, ports
=[alu
.in_a
, alu
.in_b
] + \
1409 alu
.in_t
.ports() + \
1410 alu
.out_z
.ports() + \
1411 [alu
.in_mid
, alu
.out_mid
])
1414 # works... but don't use, just do "python fname.py convert -t v"
1415 #print (verilog.convert(alu, ports=[
1416 # ports=alu.in_a.ports() + \
1417 # alu.in_b.ports() + \
1418 # alu.out_z.ports())