1 # IEEE Floating Point Adder (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
5 from nmigen
import Module
, Signal
, Elaboratable
6 from nmigen
.cli
import main
, verilog
9 from ieee754
.fpcommon
.fpbase
import FPNumIn
, FPNumOut
, FPNumBaseRecord
10 from ieee754
.fpcommon
.fpbase
import FPState
, FPNumBase
11 from ieee754
.fpcommon
.getop
import FPPipeContext
16 def __init__(self
, pspec
, m_extra
):
18 # NOTE: difference between z and oz is that oz is created by
19 # special-cases module(s) and will propagate, along with its
20 # "bypass" signal out_do_z, through the pipeline, *disabling*
21 # all processing of all subsequent stages.
22 self
.a
= FPNumBaseRecord(width
, m_extra
) # operand a
23 self
.b
= FPNumBaseRecord(width
, m_extra
) # operand b
24 self
.z
= FPNumBaseRecord(width
, False) # denormed result
25 self
.oz
= Signal(width
, reset_less
=True) # "finished" (bypass) result
26 self
.out_do_z
= Signal(reset_less
=True) # "bypass" enabled
27 self
.ctx
= FPPipeContext(pspec
)
28 self
.muxid
= self
.ctx
.muxid
39 ret
= [self
.z
.eq(i
.z
), self
.out_do_z
.eq(i
.out_do_z
), self
.oz
.eq(i
.oz
),
40 self
.a
.eq(i
.a
), self
.b
.eq(i
.b
), self
.ctx
.eq(i
.ctx
)]
44 class FPAddDeNormMod(FPState
, Elaboratable
):
46 def __init__(self
, pspec
, m_extra
):
48 self
.m_extra
= m_extra
53 return FPSCData(self
.pspec
, self
.m_extra
)
56 return FPSCData(self
.pspec
, self
.m_extra
)
61 def setup(self
, m
, i
):
62 """ links module to inputs and outputs
64 m
.submodules
.denormalise
= self
65 m
.d
.comb
+= self
.i
.eq(i
)
67 def elaborate(self
, platform
):
69 m
.submodules
.denorm_in_a
= in_a
= FPNumBase(self
.i
.a
)
70 m
.submodules
.denorm_in_b
= in_b
= FPNumBase(self
.i
.b
)
71 #m.submodules.denorm_out_a = self.o.a
72 #m.submodules.denorm_out_b = self.o.b
73 #m.submodules.denorm_out_z = self.o.z
75 with m
.If(~self
.i
.out_do_z
):
76 # XXX hmmm, don't like repeating identical code
77 m
.d
.comb
+= self
.o
.a
.eq(self
.i
.a
)
78 with m
.If(in_a
.exp_n127
):
79 m
.d
.comb
+= self
.o
.a
.e
.eq(self
.i
.a
.N126
) # limit a exponent
81 m
.d
.comb
+= self
.o
.a
.m
[-1].eq(1) # set top mantissa bit
83 m
.d
.comb
+= self
.o
.b
.eq(self
.i
.b
)
84 with m
.If(in_b
.exp_n127
):
85 m
.d
.comb
+= self
.o
.b
.e
.eq(self
.i
.b
.N126
) # limit a exponent
87 m
.d
.comb
+= self
.o
.b
.m
[-1].eq(1) # set top mantissa bit
89 m
.d
.comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)
90 m
.d
.comb
+= self
.o
.z
.eq(self
.i
.z
)
91 m
.d
.comb
+= self
.o
.out_do_z
.eq(self
.i
.out_do_z
)
92 m
.d
.comb
+= self
.o
.oz
.eq(self
.i
.oz
)
97 class FPAddDeNorm(FPState
):
99 def __init__(self
, width
, id_wid
):
100 FPState
.__init
__(self
, "denormalise")
101 self
.mod
= FPAddDeNormMod(width
)
102 self
.out_a
= FPNumBaseRecord(width
)
103 self
.out_b
= FPNumBaseRecord(width
)
105 def setup(self
, m
, i
):
106 """ links module to inputs and outputs
110 m
.d
.sync
+= self
.out_a
.eq(self
.mod
.out_a
)
111 m
.d
.sync
+= self
.out_b
.eq(self
.mod
.out_b
)
114 # Denormalised Number checks