OpenCores
URL https://opencores.org/ocsvn/ft816float/ft816float/trunk

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpAddsub_L10.v] - Blame information for rev 75

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 robfinch
`timescale 1ns / 1ps
2
// ============================================================================
3
//        __
4
//   \\__/ o\    (C) 2006-2019  Robert Finch, Waterloo
5
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch<remove>@finitron.ca
7
//       ||
8
//
9
//      fpAddsub_L10.v
10
//    - floating point adder/subtracter
11
//    - ten cycle latency
12
//    - can issue every clock cycle
13
//    - parameterized width
14
//    - IEEE 754 representation
15
//
16
//
17
// This source file is free software: you can redistribute it and/or modify 
18
// it under the terms of the GNU Lesser General Public License as published 
19
// by the Free Software Foundation, either version 3 of the License, or     
20
// (at your option) any later version.                                      
21
//                                                                          
22
// This source file is distributed in the hope that it will be useful,      
23
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
24
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
25
// GNU General Public License for more details.                             
26
//                                                                          
27
// You should have received a copy of the GNU General Public License        
28
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
29
//                                                                          
30
// ============================================================================
31
 
32
module fpAddsub_L10(clk, ce, rm, op, a, b, o);
33
parameter WID = 128;
34 26 robfinch
`include "fpSize.sv"
35 19 robfinch
 
36
input clk;              // system clock
37
input ce;               // core clock enable
38
input [2:0] rm;  // rounding mode
39
input op;               // operation 0 = add, 1 = subtract
40
input [WID-1:0] a;       // operand a
41
input [WID-1:0] b;       // operand b
42
output [EX:0] o; // output
43
 
44
wire so;                        // sign output
45
wire [EMSB:0] xo;        // de normalized exponent output
46
reg [FX:0] mo;   // mantissa output
47
 
48
assign o = {so,xo,mo};
49
 
50
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
51
// Clock edge #1
52
// - Decompose inputs into more digestible values.
53
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
54
wire [WID-1:0] a1;
55
wire [WID-1:0] b1;
56
wire sa1, sb1;
57
wire [EMSB:0] xa1, xb1;
58
wire [FMSB:0] ma1, mb1;
59
wire [FMSB+1:0] fracta1, fractb1;
60
wire adn1, bdn1;                // a,b denormalized ?
61
wire xaInf1, xbInf1;
62
wire aInf1, bInf1;
63
wire aNan1, bNan1;
64
wire az1, bz1;  // operand a,b is zero
65
wire op1;
66
 
67
fpDecompReg #(WID) u1a (.clk(clk), .ce(ce), .i(a), .o(a1), .sgn(sa1), .exp(xa1), .man(ma1), .fract(fracta1), .xz(adn1), .vz(az1), .xinf(xaInf1), .inf(aInf1), .nan(aNan1) );
68
fpDecompReg #(WID) u1b (.clk(clk), .ce(ce), .i(b), .o(b1), .sgn(sb1), .exp(xb1), .man(mb1), .fract(fractb1), .xz(bdn1), .vz(bz1), .xinf(xbInf1), .inf(bInf1), .nan(bNan1) );
69
delay1 #(1)  dop1(.clk(clk), .ce(ce), .i(op), .o(op1) );
70
 
71
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
72
// Clock edge #2
73
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
74
reg xabeq2;
75
reg mabeq2;
76
reg anbz2;
77
reg xabInf2;
78
reg anbInf2;
79
wire [EMSB:0] xa2, xb2;
80
wire [FMSB:0] ma2, mb2;
81
// operands sign,exponent,mantissa
82
wire [FMSB+1:0] fracta2, fractb2;
83
wire az2, bz2;  // operand a,b is zero
84
reg xa_gt_xb2;
85
reg var2;
86
reg [EMSB:0] xad2;
87
reg [EMSB:0] xbd2;
88
reg realOp2;
89
 
90
delay1 #(EMSB+1)  dxa2(.clk(clk), .ce(ce), .i(xa1), .o(xa2) );
91
delay1 #(EMSB+1)  dxb2(.clk(clk), .ce(ce), .i(xb1), .o(xb2) );
92
delay1 #(FMSB+1)  dma2(.clk(clk), .ce(ce), .i(ma1), .o(ma2) );
93
delay1 #(FMSB+1)  dmb2(.clk(clk), .ce(ce), .i(mb1), .o(mb2) );
94
delay1 #(1)  daz2(.clk(clk), .ce(ce), .i(az1), .o(az2) );
95
delay1 #(1)  dbz2(.clk(clk), .ce(ce), .i(bz1), .o(bz2) );
96
delay1 #(FMSB+2)  dfracta2(.clk(clk), .ce(ce), .i(fracta1), .o(fracta2) );
97
delay1 #(FMSB+2)  dfractb2(.clk(clk), .ce(ce), .i(fractb1), .o(fractb2) );
98
 
99
always @(posedge clk)
100
        if (ce) xa_gt_xb2 <= xa1 > xb1;
101
always @(posedge clk)
102
        if (ce) var2 <= (xa1==xb1 && ma1 > mb1);
103
always @(posedge clk)
104
        if (ce) xad2 <= xa1|adn1;       // operand a exponent, compensated for denormalized numbers
105
always @(posedge clk)
106
        if (ce) xbd2 <= xb1|bdn1;       // operand b exponent, compensated for denormalized numbers
107
always @(posedge clk)
108
        if (ce) xabeq2 <= xa1==xb1;
109
always @(posedge clk)
110
        if (ce) mabeq2 <= ma1==mb1;
111
always @(posedge clk)
112
        if (ce) anbz2 <= az1 & bz1;
113
always @(posedge clk)
114
        if (ce) xabInf2 <= xaInf1 & xbInf1;
115
always @(posedge clk)
116
        if (ce) anbInf2 <= aInf1 & bInf1;
117
 
118
// Figure out which operation is really needed an add or
119
// subtract ?
120
// If the signs are the same, use the orignal op,
121
// otherwise flip the operation
122
//  a +  b = add,+
123
//  a + -b = sub, so of larger
124
// -a +  b = sub, so of larger
125
// -a + -b = add,-
126
//  a -  b = sub, so of larger
127
//  a - -b = add,+
128
// -a -  b = add,-
129
// -a - -b = sub, so of larger
130
always @(posedge clk)
131
        if (ce) realOp2 <= op1 ^ sa1 ^ sb1;
132
 
133
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
134
// Clock edge #3
135
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
136
wire [EMSB:0] xa3, xb3;
137
wire xa_gt_xb3;
138
reg x_gt_b3;
139
wire xabInf3;
140
wire sa3,sb3;
141
wire op3;
142
wire [2:0] rm3;
143
reg [EMSB:0] xdiff3;
144
// which has greater magnitude ? Used for sign calc
145
reg a_gt_b3;
146
reg resZero3;
147
reg [FMSB+1:0] mfs3;
148
 
149
delay1 #(EMSB+1)  dxa3(.clk(clk), .ce(ce), .i(xa2), .o(xa3));
150
delay1 #(EMSB+1)  dxb3(.clk(clk), .ce(ce), .i(xb2), .o(xb3));
151
delay1 #(1) dxabInf2(.clk(clk), .ce(ce), .i(xabInf2), .o(xabInf3));
152
delay1 #(1) dxagtxb2(.clk(clk), .ce(ce), .i(xa_gt_xb2), .o(xa_gt_xb3));
153
delay2 #(1) dsa2(.clk(clk), .ce(ce), .i(sa1), .o(sa3));
154
delay2 #(1) dsb2(.clk(clk), .ce(ce), .i(sb1), .o(sb3));
155
delay2 #(1) dop2(.clk(clk), .ce(ce), .i(op1), .o(op3));
156
delay3 #(3) drm2(.clk(clk), .ce(ce), .i(rm), .o(rm3));
157
 
158
always @(posedge clk)
159
        if (ce) a_gt_b3 <= xa_gt_xb2 || var2;
160
// Find out if the result will be zero.
161
always @(posedge clk)
162
        if (ce) resZero3 <= (realOp2 & xabeq2 & mabeq2) |       anbz2;  // subtract, same magnitude,    both a,b zero
163
 
164
// Compute the difference in exponents, provides shift amount
165
always @(posedge clk)
166
        if (ce) xdiff3 <= xa_gt_xb2 ? xad2 - xbd2 : xbd2 - xad2;
167
// determine which fraction to denormalize
168
always @(posedge clk)
169
        if (ce) mfs3 <= xa_gt_xb2 ? fractb2 : fracta2;
170
 
171
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
172
// Clock edge #4
173
// Compute output exponent
174
//
175
// The output exponent is the larger of the two exponents, unless a subtract
176
// operation is in progress and the two numbers are equal, in which case the
177
// exponent should be zero.
178
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
179
reg [EMSB:0] xdif4;
180
wire [FMSB+1:0] mfs4;
181
reg [EMSB:0] xo4;        // de normalized exponent output
182
reg so4;
183
 
184
always @(posedge clk)
185
        if (ce) xo4 <= xabInf3 ? xa3 : resZero3 ? {EMSB+1{1'b0}} : xa_gt_xb3 ? xa3 : xb3;
186
 
187
// Compute output sign
188
always @(posedge clk)
189
if (ce)
190
        case ({resZero3,sa3,op3,sb3})   // synopsys full_case parallel_case
191
        4'b0000: so4 <= 0;                       // + + + = +
192
        4'b0001: so4 <= !a_gt_b3;       // + + - = sign of larger
193
        4'b0010: so4 <= !a_gt_b3;       // + - + = sign of larger
194
        4'b0011: so4 <= 0;                       // + - - = +
195
        4'b0100: so4 <= a_gt_b3;                // - + + = sign of larger
196
        4'b0101: so4 <= 1;                      // - + - = -
197
        4'b0110: so4 <= 1;                      // - - + = -
198
        4'b0111: so4 <= a_gt_b3;                // - - - = sign of larger
199
        4'b1000: so4 <= 0;                       //  A +  B, sign = +
200
        4'b1001: so4 <= rm3==3'd3;              //  A + -B, sign = + unless rounding down
201
        4'b1010: so4 <= rm3==3'd3;              //  A -  B, sign = + unless rounding down
202
        4'b1011: so4 <= 0;                       // +A - -B, sign = +
203
        4'b1100: so4 <= rm3==3'd3;              // -A +  B, sign = + unless rounding down
204
        4'b1101: so4 <= 1;                      // -A + -B, sign = -
205
        4'b1110: so4 <= 1;                      // -A - +B, sign = -
206
        4'b1111: so4 <= rm3==3'd3;              // -A - -B, sign = + unless rounding down
207
        endcase
208
 
209
always @(posedge clk)
210
if (ce) xdif4 <= xdiff3 > FMSB+3 ? FMSB+3 : xdiff3;
211
delay1 #(FMSB+2) dmsf3(.clk(clk), .ce(ce), .i(mfs3), .o(mfs4));
212
 
213
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
214
// Clock edge #5
215
// Determine the sticky bit
216
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
217
wire [EMSB:0] xdif5;
218
wire [FMSB+1:0] mfs5;
219
wire sticky, sticky5;
220
 
221
// register inputs to shifter and shift
222
delay1 #(1)      dstky4(.clk(clk), .ce(ce), .i(sticky), .o(sticky5) );
223
delay1 #(EMSB+1) dxdif4(.clk(clk), .ce(ce), .i(xdif4), .o(xdif5) );
224
delay1 #(FMSB+2) dmsf4(.clk(clk), .ce(ce), .i(mfs4), .o(mfs5));
225
 
226
generate
227
begin
228
if (WID==128)
229
    redor128 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
230
else if (WID==96)
231
    redor96 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
232 26 robfinch
else if (WID==84)
233
    redor84 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
234 19 robfinch
else if (WID==80)
235
    redor80 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
236
else if (WID==64)
237
    redor64 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
238
else if (WID==40)
239
    redor40 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
240
else if (WID==32)
241
    redor32 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
242
end
243
endgenerate
244
 
245
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
246
// Clock edge #6
247
// Shift (denormalize)
248
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
249
reg [FMSB+3:0] md6;
250
wire xa_gt_xb6;
251
wire [FMSB+1:0] fracta6, fractb6;
252
 
253
delay3 #(1) dxagtxb5(.clk(clk), .ce(ce), .i(xa_gt_xb3), .o(xa_gt_xb6));
254
delay4 #(FMSB+2)  dfracta5(.clk(clk), .ce(ce), .i(fracta2), .o(fracta6) );
255
delay4 #(FMSB+2)  dfractb5(.clk(clk), .ce(ce), .i(fractb2), .o(fractb6) );
256
 
257
always @(posedge clk)
258
        if (ce) md6 <= ({mfs5,2'b0} >> xdif5)|sticky5;
259
 
260
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
261
// Clock edge #7
262
// Sort operands
263
// addition can generate an extra bit, subtract can't go negative
264
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
265
reg [FMSB+3:0] oa7;
266
reg [FMSB+3:0] ob7;
267
wire a_gt_b7;
268
 
269
delay4 #(1) dagtb5(.clk(clk), .ce(ce), .i(a_gt_b3), .o(a_gt_b7));
270
 
271
always @(posedge clk)
272
        if (ce) oa7 <= xa_gt_xb6 ? {fracta6,2'b0} : md6;
273
always @(posedge clk)
274
        if (ce) ob7 <= xa_gt_xb6 ? md6 : {fractb6,2'b0};
275
 
276
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
277
// Clock edge #8
278
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
279
reg [FMSB+3:0] oaa8;
280
reg [FMSB+3:0] obb8;
281
wire [EMSB:0] xo8;
282
wire realOp8;
283
vtdl #(.WID(1)) drealop7 (.clk(clk), .ce(ce), .a(4'd5), .d(realOp2), .q(realOp8));
284
vtdl #(.WID(EMSB+1)) dxo7(.clk(clk), .ce(ce), .a(4'd3), .d(xo4), .q(xo8));
285
always @(posedge clk)
286
        if (ce) oaa8 <= a_gt_b7 ? oa7 : ob7;
287
always @(posedge clk)
288
        if (ce) obb8 <= a_gt_b7 ? ob7 : oa7;
289
 
290
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
291
// Clock edge #9
292
// perform add/subtract
293
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
294
reg [FMSB+4:0] mab9;
295
wire anbInf9;
296
wire aNan9, bNan9;
297
wire op9;
298
wire [FMSB+1:0] fracta9, fractb9;
299
wire xo9;
300
reg xinf9;
301
 
302
vtdl #(1) danbInf7(.clk(clk), .ce(ce), .a(4'd6), .d(anbInf2), .q(anbInf9));
303
vtdl #(1) danan8(.clk(clk), .ce(ce), .a(4'd7), .d(aNan1), .q(aNan9));
304
vtdl #(1) dbnan8(.clk(clk), .ce(ce), .a(4'd7), .d(bNan1), .q(bNan9));
305
vtdl #(1) dop6(.clk(clk), .ce(ce), .a(4'd5), .d(op3), .q(op9));
306
delay3 #(FMSB+2)  dfracta8(.clk(clk), .ce(ce), .i(fracta6), .o(fracta9) );
307
delay3 #(FMSB+2)  dfractb8(.clk(clk), .ce(ce), .i(fractb6), .o(fractb9) );
308
 
309
always @(posedge clk)
310
        if (ce) mab9 <= realOp8 ? oaa8 - obb8 : oaa8 + obb8;
311
always @(posedge clk)
312
        if (ce) xinf9 <= xo8 == {EMSB+1{1'b1}};
313
 
314
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
315
// Clock edge #10
316
// Final outputs
317
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
318
vtdl #(1) dso6(.clk(clk), .ce(ce), .a(4'd5), .d(so4), .q(so));
319
vtdl #(.WID(EMSB+1)) dxo6(.clk(clk), .ce(ce), .a(4'd1), .d(xo8), .q(xo));
320
 
321
always @(posedge clk)
322
if (ce)
323 20 robfinch
        casez({anbInf9,aNan9,bNan9,xinf9})
324
        4'b1???:        mo <= {1'b0,op9,{FMSB-1{1'b0}},op9,{FMSB{1'b0}}};       // inf +/- inf - generate QNaN on subtract, inf on add
325
        4'b01??:        mo <= {1'b0,fracta9[FMSB+1:0],{FMSB{1'b0}}};
326
        4'b001?:        mo <= {1'b0,fractb9[FMSB+1:0],{FMSB{1'b0}}};
327
        4'b0001:        mo <= 1'd0;             // exponent hit infinity -> force mantissa to zero
328 19 robfinch
        default:        mo <= {mab9,{FMSB-1{1'b0}}};    // mab has an extra lead bit and two trailing bits
329
        endcase
330
 
331
endmodule
332
 
333
module fpAddsubnr_L10(clk, ce, rm, op, a, b, o);
334
parameter WID = 128;
335 26 robfinch
`include "fpSize.sv"
336 19 robfinch
 
337
input clk;              // system clock
338
input ce;               // core clock enable
339
input [2:0] rm;  // rounding mode
340
input op;               // operation 0 = add, 1 = subtract
341
input [MSB:0] a; // operand a
342
input [MSB:0] b; // operand b
343
output [MSB:0] o;        // output
344
 
345
wire [EX:0] o1;
346
wire [MSB+3:0] fpn0;
347
 
348
fpAddsub_L10  #(WID) u1 (clk, ce, rm, op, a, b, o1);
349
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(1'b0), .i(o1), .o(fpn0) );
350
fpRoundReg  #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
351
 
352
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.