1 |
258 |
julius |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// or1200_fpu_post_norm_addsub ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// This file is part of the OpenRISC 1200 project ////
|
6 |
|
|
//// http://opencores.org/project,or1k ////
|
7 |
|
|
//// ////
|
8 |
|
|
//// Description ////
|
9 |
|
|
//// post-normalization entity for the addition/subtraction unit ////
|
10 |
|
|
//// ////
|
11 |
|
|
//// To Do: ////
|
12 |
|
|
//// ////
|
13 |
|
|
//// ////
|
14 |
|
|
//// Author(s): ////
|
15 |
|
|
//// - Original design (FPU100) - ////
|
16 |
|
|
//// Jidan Al-eryani, jidan@gmx.net ////
|
17 |
|
|
//// - Conv. to Verilog and inclusion in OR1200 - ////
|
18 |
|
|
//// Julius Baxter, julius@opencores.org ////
|
19 |
|
|
//// ////
|
20 |
|
|
//////////////////////////////////////////////////////////////////////
|
21 |
|
|
//
|
22 |
|
|
// Copyright (C) 2006, 2010
|
23 |
|
|
//
|
24 |
|
|
// This source file may be used and distributed without
|
25 |
|
|
// restriction provided that this copyright statement is not
|
26 |
|
|
// removed from the file and that any derivative work contains
|
27 |
|
|
// the original copyright notice and the associated disclaimer.
|
28 |
|
|
//
|
29 |
|
|
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY
|
30 |
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
31 |
|
|
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
32 |
|
|
// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR
|
33 |
|
|
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
34 |
|
|
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
35 |
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
36 |
|
|
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
37 |
|
|
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
38 |
|
|
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
39 |
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
40 |
|
|
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
41 |
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
42 |
|
|
//
|
43 |
|
|
|
44 |
|
|
module or1200_fpu_post_norm_addsub
|
45 |
|
|
(
|
46 |
|
|
clk_i,
|
47 |
|
|
opa_i,
|
48 |
|
|
opb_i,
|
49 |
|
|
fract_28_i,
|
50 |
|
|
exp_i,
|
51 |
|
|
sign_i,
|
52 |
|
|
fpu_op_i,
|
53 |
|
|
rmode_i,
|
54 |
|
|
output_o,
|
55 |
|
|
ine_o
|
56 |
|
|
);
|
57 |
|
|
|
58 |
|
|
parameter FP_WIDTH = 32;
|
59 |
|
|
parameter MUL_SERIAL = 0; // 0 for parallel multiplier, 1 for serial
|
60 |
|
|
parameter MUL_COUNT = 11; //11 for parallel multiplier, 34 for serial
|
61 |
|
|
parameter FRAC_WIDTH = 23;
|
62 |
|
|
parameter EXP_WIDTH = 8;
|
63 |
|
|
parameter ZERO_VECTOR = 31'd0;
|
64 |
|
|
parameter INF = 31'b1111111100000000000000000000000;
|
65 |
|
|
parameter QNAN = 31'b1111111110000000000000000000000;
|
66 |
|
|
parameter SNAN = 31'b1111111100000000000000000000001;
|
67 |
|
|
|
68 |
|
|
input clk_i;
|
69 |
|
|
input [FP_WIDTH-1:0] opa_i;
|
70 |
|
|
input [FP_WIDTH-1:0] opb_i;
|
71 |
|
|
input [FRAC_WIDTH+4:0] fract_28_i;
|
72 |
|
|
input [EXP_WIDTH-1:0] exp_i;
|
73 |
|
|
input sign_i;
|
74 |
|
|
input fpu_op_i;
|
75 |
|
|
input [1:0] rmode_i;
|
76 |
|
|
output reg [FP_WIDTH-1:0] output_o;
|
77 |
|
|
output reg ine_o;
|
78 |
|
|
|
79 |
|
|
wire [FP_WIDTH-1:0] s_opa_i;
|
80 |
|
|
wire [FP_WIDTH-1:0] s_opb_i;
|
81 |
|
|
wire [FRAC_WIDTH+4:0] s_fract_28_i;
|
82 |
|
|
wire [EXP_WIDTH-1:0] s_exp_i;
|
83 |
|
|
wire s_sign_i;
|
84 |
|
|
wire s_fpu_op_i;
|
85 |
|
|
wire [1:0] s_rmode_i;
|
86 |
|
|
wire [FP_WIDTH-1:0] s_output_o;
|
87 |
|
|
wire s_ine_o;
|
88 |
|
|
wire s_overflow;
|
89 |
|
|
|
90 |
|
|
wire [5:0] s_zeros;
|
91 |
|
|
reg [5:0] s_shr1;
|
92 |
|
|
reg [5:0] s_shl1;
|
93 |
|
|
wire s_shr2, s_carry;
|
94 |
|
|
|
95 |
|
|
wire [9:0] s_exp10;
|
96 |
|
|
reg [EXP_WIDTH:0] s_expo9_1;
|
97 |
|
|
wire [EXP_WIDTH:0] s_expo9_2;
|
98 |
|
|
wire [EXP_WIDTH:0] s_expo9_3;
|
99 |
|
|
|
100 |
|
|
reg [FRAC_WIDTH+4:0] s_fracto28_1;
|
101 |
|
|
wire [FRAC_WIDTH+4:0] s_fracto28_2;
|
102 |
|
|
wire [FRAC_WIDTH+4:0] s_fracto28_rnd;
|
103 |
|
|
|
104 |
|
|
wire s_roundup;
|
105 |
|
|
wire s_sticky;
|
106 |
|
|
|
107 |
|
|
wire s_zero_fract;
|
108 |
|
|
wire s_lost;
|
109 |
|
|
wire s_infa, s_infb;
|
110 |
|
|
wire s_nan_in, s_nan_op, s_nan_a, s_nan_b, s_nan_sign;
|
111 |
|
|
|
112 |
|
|
assign s_opa_i = opa_i;
|
113 |
|
|
assign s_opb_i = opb_i;
|
114 |
|
|
assign s_fract_28_i = fract_28_i;
|
115 |
|
|
assign s_exp_i = exp_i;
|
116 |
|
|
assign s_sign_i = sign_i;
|
117 |
|
|
assign s_fpu_op_i = fpu_op_i;
|
118 |
|
|
assign s_rmode_i = rmode_i;
|
119 |
|
|
|
120 |
|
|
// Output Register
|
121 |
|
|
always @(posedge clk_i)
|
122 |
|
|
begin
|
123 |
|
|
output_o <= s_output_o;
|
124 |
|
|
ine_o <= s_ine_o;
|
125 |
|
|
end
|
126 |
|
|
//*** Stage 1 ****
|
127 |
|
|
// figure out the output exponent and how much the fraction has to be
|
128 |
|
|
// shiftd right/left
|
129 |
|
|
|
130 |
|
|
assign s_carry = s_fract_28_i[27];
|
131 |
|
|
|
132 |
|
|
reg [5:0] lzeroes;
|
133 |
|
|
|
134 |
|
|
always @(s_fract_28_i)
|
135 |
364 |
julius |
casez(s_fract_28_i[26:0]) // synopsys full_case parallel_case
|
136 |
|
|
27'b1??????????????????????????: lzeroes = 0;
|
137 |
|
|
27'b01?????????????????????????: lzeroes = 1;
|
138 |
|
|
27'b001????????????????????????: lzeroes = 2;
|
139 |
|
|
27'b0001???????????????????????: lzeroes = 3;
|
140 |
|
|
27'b00001??????????????????????: lzeroes = 4;
|
141 |
|
|
27'b000001?????????????????????: lzeroes = 5;
|
142 |
|
|
27'b0000001????????????????????: lzeroes = 6;
|
143 |
|
|
27'b00000001???????????????????: lzeroes = 7;
|
144 |
|
|
27'b000000001??????????????????: lzeroes = 8;
|
145 |
|
|
27'b0000000001?????????????????: lzeroes = 9;
|
146 |
|
|
27'b00000000001????????????????: lzeroes = 10;
|
147 |
|
|
27'b000000000001???????????????: lzeroes = 11;
|
148 |
|
|
27'b0000000000001??????????????: lzeroes = 12;
|
149 |
|
|
27'b00000000000001?????????????: lzeroes = 13;
|
150 |
|
|
27'b000000000000001????????????: lzeroes = 14;
|
151 |
|
|
27'b0000000000000001???????????: lzeroes = 15;
|
152 |
|
|
27'b00000000000000001??????????: lzeroes = 16;
|
153 |
|
|
27'b000000000000000001?????????: lzeroes = 17;
|
154 |
|
|
27'b0000000000000000001????????: lzeroes = 18;
|
155 |
|
|
27'b00000000000000000001???????: lzeroes = 19;
|
156 |
|
|
27'b000000000000000000001??????: lzeroes = 20;
|
157 |
|
|
27'b0000000000000000000001?????: lzeroes = 21;
|
158 |
|
|
27'b00000000000000000000001????: lzeroes = 22;
|
159 |
|
|
27'b000000000000000000000001???: lzeroes = 23;
|
160 |
|
|
27'b0000000000000000000000001??: lzeroes = 24;
|
161 |
|
|
27'b00000000000000000000000001?: lzeroes = 25;
|
162 |
|
|
27'b000000000000000000000000001: lzeroes = 26;
|
163 |
|
|
27'b000000000000000000000000000: lzeroes = 27;
|
164 |
258 |
julius |
endcase
|
165 |
|
|
|
166 |
|
|
assign s_zeros = s_fract_28_i[27] ? 0 : lzeroes;
|
167 |
|
|
|
168 |
|
|
// negative flag & large flag & exp
|
169 |
|
|
assign s_exp10 = {2'd0,s_exp_i} + {9'd0,s_carry} - {4'd0,s_zeros};
|
170 |
|
|
|
171 |
|
|
always @(posedge clk_i)
|
172 |
|
|
begin
|
173 |
|
|
if (s_exp10[9] | !(|s_exp10))
|
174 |
|
|
begin
|
175 |
|
|
s_shr1 <= 0;
|
176 |
|
|
s_expo9_1 <= 9'd1;
|
177 |
|
|
|
178 |
|
|
if (|s_exp_i)
|
179 |
|
|
s_shl1 <= s_exp_i[5:0] - 6'd1;
|
180 |
|
|
else
|
181 |
|
|
s_shl1 <= 0;
|
182 |
|
|
|
183 |
|
|
end
|
184 |
|
|
else if (s_exp10[8])
|
185 |
|
|
begin
|
186 |
|
|
s_shr1 <= 0;
|
187 |
|
|
s_shl1 <= 0;
|
188 |
|
|
s_expo9_1 <= 9'b011111111;
|
189 |
|
|
end
|
190 |
|
|
else
|
191 |
|
|
begin
|
192 |
|
|
s_shr1 <= {5'd0,s_carry};
|
193 |
|
|
s_shl1 <= s_zeros;
|
194 |
|
|
s_expo9_1 <= s_exp10[8:0];
|
195 |
|
|
end // else: !if(s_exp10[8])
|
196 |
|
|
end // always @ (posedge clk_i)
|
197 |
|
|
|
198 |
|
|
//-
|
199 |
|
|
// *** Stage 2 ***
|
200 |
|
|
// Shifting the fraction and rounding
|
201 |
|
|
|
202 |
|
|
always @(posedge clk_i)
|
203 |
|
|
if (|s_shr1)
|
204 |
|
|
s_fracto28_1 <= s_fract_28_i >> s_shr1;
|
205 |
|
|
else
|
206 |
|
|
s_fracto28_1 <= s_fract_28_i << s_shl1;
|
207 |
|
|
|
208 |
|
|
assign s_expo9_2 = (s_fracto28_1[27:26]==2'b00) ?
|
209 |
|
|
s_expo9_1 - 9'd1 : s_expo9_1;
|
210 |
|
|
|
211 |
|
|
// round
|
212 |
|
|
//check last bit, before and after right-shift
|
213 |
|
|
assign s_sticky = s_fracto28_1[0] | (s_fract_28_i[0] & s_fract_28_i[27]);
|
214 |
|
|
|
215 |
|
|
assign s_roundup = s_rmode_i==2'b00 ?
|
216 |
|
|
// round to nearset even
|
217 |
|
|
s_fracto28_1[2] & ((s_fracto28_1[1] | s_sticky) |
|
218 |
|
|
s_fracto28_1[3]) :
|
219 |
|
|
s_rmode_i==2'b10 ?
|
220 |
|
|
// round up
|
221 |
|
|
(s_fracto28_1[2] | s_fracto28_1[1] | s_sticky) & !s_sign_i:
|
222 |
|
|
s_rmode_i==2'b11 ?
|
223 |
|
|
// round down
|
224 |
|
|
(s_fracto28_1[2] | s_fracto28_1[1] | s_sticky) & s_sign_i :
|
225 |
|
|
// round to zero(truncate = no rounding)
|
226 |
|
|
0;
|
227 |
|
|
|
228 |
|
|
assign s_fracto28_rnd = s_roundup ?
|
229 |
|
|
s_fracto28_1+28'b0000_0000_0000_0000_0000_0000_1000 :
|
230 |
|
|
s_fracto28_1;
|
231 |
|
|
|
232 |
|
|
// ***Stage 3***
|
233 |
|
|
// right-shift after rounding (if necessary)
|
234 |
|
|
assign s_shr2 = s_fracto28_rnd[27];
|
235 |
|
|
|
236 |
|
|
assign s_expo9_3 = (s_shr2 & s_expo9_2!=9'b011111111) ?
|
237 |
|
|
s_expo9_2 + 9'b000000001 : s_expo9_2;
|
238 |
|
|
|
239 |
|
|
assign s_fracto28_2 = s_shr2 ? {1'b0,s_fracto28_rnd[27:1]} : s_fracto28_rnd;
|
240 |
|
|
|
241 |
|
|
////-
|
242 |
|
|
|
243 |
|
|
assign s_infa = &s_opa_i[30:23];
|
244 |
|
|
assign s_infb = &s_opb_i[30:23];
|
245 |
|
|
|
246 |
|
|
assign s_nan_a = s_infa & (|s_opa_i[22:0]);
|
247 |
|
|
assign s_nan_b = s_infb & (|s_opb_i[22:0]);
|
248 |
|
|
|
249 |
|
|
assign s_nan_in = s_nan_a | s_nan_b;
|
250 |
|
|
|
251 |
|
|
// inf-inf=Nan
|
252 |
|
|
assign s_nan_op = (s_infa & s_infb) &
|
253 |
|
|
(s_opa_i[31] ^ (s_fpu_op_i ^ s_opb_i[31]));
|
254 |
|
|
|
255 |
|
|
assign s_nan_sign = (s_nan_a & s_nan_b) ? s_sign_i :
|
256 |
|
|
s_nan_a ?
|
257 |
|
|
s_opa_i[31] : s_opb_i[31];
|
258 |
|
|
|
259 |
|
|
// check if result is inexact;
|
260 |
|
|
assign s_lost = (s_shr1[0] & s_fract_28_i[0]) |
|
261 |
|
|
(s_shr2 & s_fracto28_rnd[0]) | (|s_fracto28_2[2:0]);
|
262 |
|
|
|
263 |
|
|
assign s_ine_o = (s_lost | s_overflow) & !(s_infa | s_infb);
|
264 |
|
|
|
265 |
|
|
assign s_overflow = s_expo9_3==9'b011111111 & !(s_infa | s_infb);
|
266 |
|
|
|
267 |
|
|
// '1' if fraction result is zero
|
268 |
|
|
assign s_zero_fract = s_zeros==27 & !s_fract_28_i[27];
|
269 |
|
|
|
270 |
|
|
|
271 |
|
|
// Generate result
|
272 |
|
|
assign s_output_o = (s_nan_in | s_nan_op) ?
|
273 |
|
|
{s_nan_sign,QNAN} :
|
274 |
|
|
(s_infa | s_infb) | s_overflow ?
|
275 |
|
|
{s_sign_i,INF} :
|
276 |
|
|
s_zero_fract ?
|
277 |
|
|
{s_sign_i,ZERO_VECTOR} :
|
278 |
|
|
{s_sign_i,s_expo9_3[7:0],s_fracto28_2[25:3]};
|
279 |
|
|
|
280 |
|
|
endmodule // or1200_fpu_post_norm_addsub
|
281 |
|
|
|
282 |
|
|
|