373f79762425b50b463764a0ea1dd2c50ab9fae2
1 from nmigen
import Module
, Signal
2 from nmigen
.cli
import main
, verilog
4 from fpbase
import FPNum
, FPOp
, Overflow
, FPBase
9 def __init__(self
, width
):
13 self
.in_a
= FPOp(width
)
14 self
.in_b
= FPOp(width
)
15 self
.out_z
= FPOp(width
)
17 def get_fragment(self
, platform
=None):
18 """ creates the HDL code-fragment for FPMUL
25 z
= FPNum(self
.width
, False)
27 tot
= Signal(28) # sticky/round/guard bits, 23 result, 1 overflow
33 with m
.State("get_a"):
35 m
.d
.sync
+= s
.in_a
.ack
.eq(1)
36 with m
.If(s
.in_a
.ack
& in_a
.stb
):
42 with m
.State("get_b"):
44 m
.d
.sync
+= s
.in_b
.ack
.eq(1)
45 with m
.If(s
.in_b
.ack
& in_b
.stb
):
51 with m
.State("unpack"):
52 m
.next
+= "special_cases"
56 a
.e
.eq(a
[23:31] - 127),
57 b
.e
.eq(b
[23:31] - 127),
62 with m
.State("special_cases"):
63 m
.next
= "normalise_a"
64 with m
.If(a
.is_nan() | b
.is_nan()):
72 with m
.Elif(a
.e
.is_inf()):
84 //if a is inf return inf
85 end else if (a_e == 128) begin
89 //if b is zero return NaN
90 if (($signed(b_e) == -127) && (b_m == 0)) begin
97 //if b is inf return inf
98 end else if (b_e == 128) begin
102 //if a is zero return NaN
103 if (($signed(a_e) == -127) && (a_m == 0)) begin
110 //if a is zero return zero
111 end else if (($signed(a_e) == -127) && (a_m == 0)) begin
116 //if b is zero return zero
117 end else if (($signed(b_e) == -127) && (b_m == 0)) begin
123 //Denormalised Number
124 if ($signed(a_e) == -127) begin
129 //Denormalised Number
130 if ($signed(b_e) == -127) begin
135 state <= normalise_a;
142 state <= normalise_b;
162 z_e <= a_e + b_e + 1;
163 product <= a_m * b_m * 4;
169 z_m <= product[49:26];
170 guard <= product[25];
171 round_bit <= product[24];
172 sticky <= (product[23:0] != 0);
173 state <= normalise_1;
178 if (z_m[23] == 0) begin
185 state <= normalise_2;
191 if ($signed(z_e) < -126) begin
196 sticky <= sticky | round_bit;
204 if (guard && (round_bit | sticky | z_m[0])) begin
206 if (z_m == 24'hffffff) begin
215 z[22 : 0] <= z_m[22:0];
216 z[30 : 23] <= z_e[7:0] + 127;
218 if ($signed(z_e) == -126 && z_m[23] == 0) begin
221 //if overflow occurs, return inf
222 if ($signed(z_e) > 127) begin
234 if (s_output_z_stb && output_z_ack) begin