add use of FPState, not being used yet
[ieee754fpu.git] / src / add / nmigen_div_experiment.py
1 # IEEE Floating Point Divider (Single Precision)
2 # Copyright (C) Jonathan P Dawson 2013
3 # 2013-12-12
4
5 from nmigen import Module, Signal, Const, Cat
6 from nmigen.cli import main, verilog
7
8 from fpbase import FPNumIn, FPNumOut, FPOp, Overflow, FPBase
9 from nmigen_add_experiment import FPState
10
11 class Div:
12 def __init__(self, width):
13 self.width = width
14 self.quot = Signal(width) # quotient
15 self.dor = Signal(width) # divisor
16 self.dend = Signal(width) # dividend
17 self.rem = Signal(width) # remainder
18 self.count = Signal(7) # loop count
19
20 self.czero = Const(0, width)
21
22 def reset(self, m):
23 m.d.sync += [
24 self.quot.eq(self.czero),
25 self.rem.eq(self.czero),
26 self.count.eq(Const(0, 7))
27 ]
28
29
30 class FPDIV(FPBase):
31
32 def __init__(self, width):
33 FPBase.__init__(self)
34 self.width = width
35
36 self.in_a = FPOp(width)
37 self.in_b = FPOp(width)
38 self.out_z = FPOp(width)
39
40 self.states = []
41
42 def add_state(self, state):
43 self.states.append(state)
44 return state
45
46 def get_fragment(self, platform=None):
47 """ creates the HDL code-fragment for FPDiv
48 """
49 m = Module()
50
51 # Latches
52 a = FPNumIn(None, self.width, False)
53 b = FPNumIn(None, self.width, False)
54 z = FPNumOut(self.width, False)
55
56 div = Div(a.m_width*2 + 3) # double the mantissa width plus g/r/sticky
57
58 of = Overflow()
59 m.submodules.in_a = a
60 m.submodules.in_b = b
61 m.submodules.z = z
62 m.submodules.of = of
63
64 with m.FSM() as fsm:
65
66 # ******
67 # gets operand a
68
69 with m.State("get_a"):
70 self.get_op(m, self.in_a, a, "get_b")
71
72 # ******
73 # gets operand b
74
75 with m.State("get_b"):
76 self.get_op(m, self.in_b, b, "special_cases")
77
78 # ******
79 # special cases: NaNs, infs, zeros, denormalised
80 # NOTE: some of these are unique to div. see "Special Operations"
81 # https://steve.hollasch.net/cgindex/coding/ieeefloat.html
82
83 with m.State("special_cases"):
84
85 # if a is NaN or b is NaN return NaN
86 with m.If(a.is_nan | b.is_nan):
87 m.next = "put_z"
88 m.d.sync += z.nan(1)
89
90 # if a is Inf and b is Inf return NaN
91 with m.Elif(a.is_inf & b.is_inf):
92 m.next = "put_z"
93 m.d.sync += z.nan(1)
94
95 # if a is inf return inf (or NaN if b is zero)
96 with m.Elif(a.is_inf):
97 m.next = "put_z"
98 m.d.sync += z.inf(a.s ^ b.s)
99
100 # if b is inf return zero
101 with m.Elif(b.is_inf):
102 m.next = "put_z"
103 m.d.sync += z.zero(a.s ^ b.s)
104
105 # if a is zero return zero (or NaN if b is zero)
106 with m.Elif(a.is_zero):
107 m.next = "put_z"
108 # if b is zero return NaN
109 with m.If(b.is_zero):
110 m.d.sync += z.nan(1)
111 with m.Else():
112 m.d.sync += z.zero(a.s ^ b.s)
113
114 # if b is zero return Inf
115 with m.Elif(b.is_zero):
116 m.next = "put_z"
117 m.d.sync += z.inf(a.s ^ b.s)
118
119 # Denormalised Number checks
120 with m.Else():
121 m.next = "normalise_a"
122 self.denormalise(m, a)
123 self.denormalise(m, b)
124
125 # ******
126 # normalise_a
127
128 with m.State("normalise_a"):
129 self.op_normalise(m, a, "normalise_b")
130
131 # ******
132 # normalise_b
133
134 with m.State("normalise_b"):
135 self.op_normalise(m, b, "divide_0")
136
137 # ******
138 # First stage of divide. initialise state
139
140 with m.State("divide_0"):
141 m.next = "divide_1"
142 m.d.sync += [
143 z.s.eq(a.s ^ b.s), # sign
144 z.e.eq(a.e - b.e), # exponent
145 div.dend.eq(a.m<<(a.m_width+3)), # 3 bits for g/r/sticky
146 div.dor.eq(b.m),
147 ]
148 div.reset(m)
149
150 # ******
151 # Second stage of divide.
152
153 with m.State("divide_1"):
154 m.next = "divide_2"
155 m.d.sync += [
156 div.quot.eq(div.quot << 1),
157 div.rem.eq(Cat(div.dend[-1], div.rem[0:])),
158 div.dend.eq(div.dend << 1),
159 ]
160
161 # ******
162 # Third stage of divide.
163 # This stage ends by jumping out to divide_3
164 # However it defaults to jumping to divide_1 (which comes back here)
165
166 with m.State("divide_2"):
167 with m.If(div.rem >= div.dor):
168 m.d.sync += [
169 div.quot[0].eq(1),
170 div.rem.eq(div.rem - div.dor),
171 ]
172 with m.If(div.count == div.width-2):
173 m.next = "divide_3"
174 with m.Else():
175 m.next = "divide_1"
176 m.d.sync += [
177 div.count.eq(div.count + 1),
178 ]
179
180 # ******
181 # Fourth stage of divide.
182
183 with m.State("divide_3"):
184 m.next = "normalise_1"
185 m.d.sync += [
186 z.m.eq(div.quot[3:]),
187 of.guard.eq(div.quot[2]),
188 of.round_bit.eq(div.quot[1]),
189 of.sticky.eq(div.quot[0] | (div.rem != 0))
190 ]
191
192 # ******
193 # First stage of normalisation.
194
195 with m.State("normalise_1"):
196 self.normalise_1(m, z, of, "normalise_2")
197
198 # ******
199 # Second stage of normalisation.
200
201 with m.State("normalise_2"):
202 self.normalise_2(m, z, of, "round")
203
204 # ******
205 # rounding stage
206
207 with m.State("round"):
208 self.roundz(m, z, of.roundz)
209 m.next = "corrections"
210
211 # ******
212 # correction stage
213
214 with m.State("corrections"):
215 self.corrections(m, z, "pack")
216
217 # ******
218 # pack stage
219
220 with m.State("pack"):
221 self.pack(m, z, "put_z")
222
223 # ******
224 # put_z stage
225
226 with m.State("put_z"):
227 self.put_z(m, z, self.out_z, "get_a")
228
229 return m
230
231
232 if __name__ == "__main__":
233 alu = FPDIV(width=32)
234 main(alu, ports=alu.in_a.ports() + alu.in_b.ports() + alu.out_z.ports())
235
236
237 # works... but don't use, just do "python fname.py convert -t v"
238 #print (verilog.convert(alu, ports=[
239 # ports=alu.in_a.ports() + \
240 # alu.in_b.ports() + \
241 # alu.out_z.ports())