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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpAddsub.v] - Blame information for rev 66

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

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

powered by: WebSVN 2.1.0

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