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 fpbase
import FPState
10 from ieee754
.fpcommon
.postcalc
import FPAddStage1Data
11 from .add0
import FPAddStage0Data
14 class FPAddStage1Mod(FPState
, Elaboratable
):
15 """ Second stage of add: preparation for normalisation.
16 detects when tot sum is too big (tot[27] is kinda a carry bit)
19 def __init__(self
, width
, id_wid
):
26 return FPAddStage0Data(self
.width
, self
.id_wid
)
29 return FPAddStage1Data(self
.width
, self
.id_wid
)
34 def setup(self
, m
, i
):
35 """ links module to inputs and outputs
37 m
.submodules
.add1
= self
38 m
.submodules
.add1_out_overflow
= self
.o
.of
40 m
.d
.comb
+= self
.i
.eq(i
)
42 def elaborate(self
, platform
):
44 m
.d
.comb
+= self
.o
.z
.eq(self
.i
.z
)
45 # tot[-1] (MSB) gets set when the sum overflows. shift result down
46 with m
.If(~self
.i
.out_do_z
):
47 with m
.If(self
.i
.tot
[-1]):
49 self
.o
.z
.m
.eq(self
.i
.tot
[4:]),
50 self
.o
.of
.m0
.eq(self
.i
.tot
[4]),
51 self
.o
.of
.guard
.eq(self
.i
.tot
[3]),
52 self
.o
.of
.round_bit
.eq(self
.i
.tot
[2]),
53 self
.o
.of
.sticky
.eq(self
.i
.tot
[1] | self
.i
.tot
[0]),
54 self
.o
.z
.e
.eq(self
.i
.z
.e
+ 1)
56 # tot[-1] (MSB) zero case
59 self
.o
.z
.m
.eq(self
.i
.tot
[3:]),
60 self
.o
.of
.m0
.eq(self
.i
.tot
[3]),
61 self
.o
.of
.guard
.eq(self
.i
.tot
[2]),
62 self
.o
.of
.round_bit
.eq(self
.i
.tot
[1]),
63 self
.o
.of
.sticky
.eq(self
.i
.tot
[0])
66 m
.d
.comb
+= self
.o
.out_do_z
.eq(self
.i
.out_do_z
)
67 m
.d
.comb
+= self
.o
.oz
.eq(self
.i
.oz
)
68 m
.d
.comb
+= self
.o
.mid
.eq(self
.i
.mid
)
73 class FPAddStage1(FPState
):
75 def __init__(self
, width
, id_wid
):
76 FPState
.__init
__(self
, "add_1")
77 self
.mod
= FPAddStage1Mod(width
)
78 self
.out_z
= FPNumBase(width
, False)
79 self
.out_of
= Overflow()
80 self
.norm_stb
= Signal()
82 def setup(self
, m
, i
):
83 """ links module to inputs and outputs
87 m
.d
.sync
+= self
.norm_stb
.eq(0) # sets to zero when not in add1 state
89 m
.d
.sync
+= self
.out_of
.eq(self
.mod
.out_of
)
90 m
.d
.sync
+= self
.out_z
.eq(self
.mod
.out_z
)
91 m
.d
.sync
+= self
.norm_stb
.eq(1)
94 m
.next
= "normalise_1"