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

Subversion Repositories ft816float

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

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
localparam MSB = WID-1;
35
localparam EMSB = WID==128 ? 14 :
36
                  WID==96 ? 14 :
37
                  WID==80 ? 14 :
38
                  WID==64 ? 10 :
39
                                  WID==52 ? 10 :
40
                                  WID==48 ? 11 :
41
                                  WID==44 ? 10 :
42
                                  WID==42 ? 10 :
43
                                  WID==40 ?  9 :
44
                                  WID==32 ?  7 :
45
                                  WID==24 ?  6 : 4;
46
localparam FMSB = WID==128 ? 111 :
47
                  WID==96 ? 79 :
48
                  WID==80 ? 63 :
49
                  WID==64 ? 51 :
50
                                  WID==52 ? 39 :
51
                                  WID==48 ? 34 :
52
                                  WID==44 ? 31 :
53
                                  WID==42 ? 29 :
54
                                  WID==40 ? 28 :
55
                                  WID==32 ? 22 :
56
                                  WID==24 ? 15 : 9;
57
 
58
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
59
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
60
 
61
input clk;              // system clock
62
input ce;               // core clock enable
63
input [2:0] rm;  // rounding mode
64
input op;               // operation 0 = add, 1 = subtract
65
input [WID-1:0] a;       // operand a
66
input [WID-1:0] b;       // operand b
67
output [EX:0] o; // output
68
 
69
wire so;                        // sign output
70
wire [EMSB:0] xo;        // de normalized exponent output
71
reg [FX:0] mo;   // mantissa output
72
 
73
assign o = {so,xo,mo};
74
 
75
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
76
// Clock edge #1
77
// - Decompose inputs into more digestible values.
78
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
79
wire [WID-1:0] a1;
80
wire [WID-1:0] b1;
81
wire sa1, sb1;
82
wire [EMSB:0] xa1, xb1;
83
wire [FMSB:0] ma1, mb1;
84
wire [FMSB+1:0] fracta1, fractb1;
85
wire adn1, bdn1;                // a,b denormalized ?
86
wire xaInf1, xbInf1;
87
wire aInf1, bInf1;
88
wire aNan1, bNan1;
89
wire az1, bz1;  // operand a,b is zero
90
wire op1;
91
 
92
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) );
93
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) );
94
delay1 #(1)  dop1(.clk(clk), .ce(ce), .i(op), .o(op1) );
95
 
96
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
97
// Clock edge #2
98
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
99
reg xabeq2;
100
reg mabeq2;
101
reg anbz2;
102
reg xabInf2;
103
reg anbInf2;
104
wire [EMSB:0] xa2, xb2;
105
wire [FMSB:0] ma2, mb2;
106
// operands sign,exponent,mantissa
107
wire [FMSB+1:0] fracta2, fractb2;
108
wire az2, bz2;  // operand a,b is zero
109
reg xa_gt_xb2;
110
reg var2;
111
reg [EMSB:0] xad2;
112
reg [EMSB:0] xbd2;
113
reg realOp2;
114
 
115
delay1 #(EMSB+1)  dxa2(.clk(clk), .ce(ce), .i(xa1), .o(xa2) );
116
delay1 #(EMSB+1)  dxb2(.clk(clk), .ce(ce), .i(xb1), .o(xb2) );
117
delay1 #(FMSB+1)  dma2(.clk(clk), .ce(ce), .i(ma1), .o(ma2) );
118
delay1 #(FMSB+1)  dmb2(.clk(clk), .ce(ce), .i(mb1), .o(mb2) );
119
delay1 #(1)  daz2(.clk(clk), .ce(ce), .i(az1), .o(az2) );
120
delay1 #(1)  dbz2(.clk(clk), .ce(ce), .i(bz1), .o(bz2) );
121
delay1 #(FMSB+2)  dfracta2(.clk(clk), .ce(ce), .i(fracta1), .o(fracta2) );
122
delay1 #(FMSB+2)  dfractb2(.clk(clk), .ce(ce), .i(fractb1), .o(fractb2) );
123
 
124
always @(posedge clk)
125
        if (ce) xa_gt_xb2 <= xa1 > xb1;
126
always @(posedge clk)
127
        if (ce) var2 <= (xa1==xb1 && ma1 > mb1);
128
always @(posedge clk)
129
        if (ce) xad2 <= xa1|adn1;       // operand a exponent, compensated for denormalized numbers
130
always @(posedge clk)
131
        if (ce) xbd2 <= xb1|bdn1;       // operand b exponent, compensated for denormalized numbers
132
always @(posedge clk)
133
        if (ce) xabeq2 <= xa1==xb1;
134
always @(posedge clk)
135
        if (ce) mabeq2 <= ma1==mb1;
136
always @(posedge clk)
137
        if (ce) anbz2 <= az1 & bz1;
138
always @(posedge clk)
139
        if (ce) xabInf2 <= xaInf1 & xbInf1;
140
always @(posedge clk)
141
        if (ce) anbInf2 <= aInf1 & bInf1;
142
 
143
// Figure out which operation is really needed an add or
144
// subtract ?
145
// If the signs are the same, use the orignal op,
146
// otherwise flip the operation
147
//  a +  b = add,+
148
//  a + -b = sub, so of larger
149
// -a +  b = sub, so of larger
150
// -a + -b = add,-
151
//  a -  b = sub, so of larger
152
//  a - -b = add,+
153
// -a -  b = add,-
154
// -a - -b = sub, so of larger
155
always @(posedge clk)
156
        if (ce) realOp2 <= op1 ^ sa1 ^ sb1;
157
 
158
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
159
// Clock edge #3
160
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
161
wire [EMSB:0] xa3, xb3;
162
wire xa_gt_xb3;
163
reg x_gt_b3;
164
wire xabInf3;
165
wire sa3,sb3;
166
wire op3;
167
wire [2:0] rm3;
168
reg [EMSB:0] xdiff3;
169
// which has greater magnitude ? Used for sign calc
170
reg a_gt_b3;
171
reg resZero3;
172
reg [FMSB+1:0] mfs3;
173
 
174
delay1 #(EMSB+1)  dxa3(.clk(clk), .ce(ce), .i(xa2), .o(xa3));
175
delay1 #(EMSB+1)  dxb3(.clk(clk), .ce(ce), .i(xb2), .o(xb3));
176
delay1 #(1) dxabInf2(.clk(clk), .ce(ce), .i(xabInf2), .o(xabInf3));
177
delay1 #(1) dxagtxb2(.clk(clk), .ce(ce), .i(xa_gt_xb2), .o(xa_gt_xb3));
178
delay2 #(1) dsa2(.clk(clk), .ce(ce), .i(sa1), .o(sa3));
179
delay2 #(1) dsb2(.clk(clk), .ce(ce), .i(sb1), .o(sb3));
180
delay2 #(1) dop2(.clk(clk), .ce(ce), .i(op1), .o(op3));
181
delay3 #(3) drm2(.clk(clk), .ce(ce), .i(rm), .o(rm3));
182
 
183
always @(posedge clk)
184
        if (ce) a_gt_b3 <= xa_gt_xb2 || var2;
185
// Find out if the result will be zero.
186
always @(posedge clk)
187
        if (ce) resZero3 <= (realOp2 & xabeq2 & mabeq2) |       anbz2;  // subtract, same magnitude,    both a,b zero
188
 
189
// Compute the difference in exponents, provides shift amount
190
always @(posedge clk)
191
        if (ce) xdiff3 <= xa_gt_xb2 ? xad2 - xbd2 : xbd2 - xad2;
192
// determine which fraction to denormalize
193
always @(posedge clk)
194
        if (ce) mfs3 <= xa_gt_xb2 ? fractb2 : fracta2;
195
 
196
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
197
// Clock edge #4
198
// Compute output exponent
199
//
200
// The output exponent is the larger of the two exponents, unless a subtract
201
// operation is in progress and the two numbers are equal, in which case the
202
// exponent should be zero.
203
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
204
reg [EMSB:0] xdif4;
205
wire [FMSB+1:0] mfs4;
206
reg [EMSB:0] xo4;        // de normalized exponent output
207
reg so4;
208
 
209
always @(posedge clk)
210
        if (ce) xo4 <= xabInf3 ? xa3 : resZero3 ? {EMSB+1{1'b0}} : xa_gt_xb3 ? xa3 : xb3;
211
 
212
// Compute output sign
213
always @(posedge clk)
214
if (ce)
215
        case ({resZero3,sa3,op3,sb3})   // synopsys full_case parallel_case
216
        4'b0000: so4 <= 0;                       // + + + = +
217
        4'b0001: so4 <= !a_gt_b3;       // + + - = sign of larger
218
        4'b0010: so4 <= !a_gt_b3;       // + - + = sign of larger
219
        4'b0011: so4 <= 0;                       // + - - = +
220
        4'b0100: so4 <= a_gt_b3;                // - + + = sign of larger
221
        4'b0101: so4 <= 1;                      // - + - = -
222
        4'b0110: so4 <= 1;                      // - - + = -
223
        4'b0111: so4 <= a_gt_b3;                // - - - = sign of larger
224
        4'b1000: so4 <= 0;                       //  A +  B, sign = +
225
        4'b1001: so4 <= rm3==3'd3;              //  A + -B, sign = + unless rounding down
226
        4'b1010: so4 <= rm3==3'd3;              //  A -  B, sign = + unless rounding down
227
        4'b1011: so4 <= 0;                       // +A - -B, sign = +
228
        4'b1100: so4 <= rm3==3'd3;              // -A +  B, sign = + unless rounding down
229
        4'b1101: so4 <= 1;                      // -A + -B, sign = -
230
        4'b1110: so4 <= 1;                      // -A - +B, sign = -
231
        4'b1111: so4 <= rm3==3'd3;              // -A - -B, sign = + unless rounding down
232
        endcase
233
 
234
always @(posedge clk)
235
if (ce) xdif4 <= xdiff3 > FMSB+3 ? FMSB+3 : xdiff3;
236
delay1 #(FMSB+2) dmsf3(.clk(clk), .ce(ce), .i(mfs3), .o(mfs4));
237
 
238
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
239
// Clock edge #5
240
// Determine the sticky bit
241
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
242
wire [EMSB:0] xdif5;
243
wire [FMSB+1:0] mfs5;
244
wire sticky, sticky5;
245
 
246
// register inputs to shifter and shift
247
delay1 #(1)      dstky4(.clk(clk), .ce(ce), .i(sticky), .o(sticky5) );
248
delay1 #(EMSB+1) dxdif4(.clk(clk), .ce(ce), .i(xdif4), .o(xdif5) );
249
delay1 #(FMSB+2) dmsf4(.clk(clk), .ce(ce), .i(mfs4), .o(mfs5));
250
 
251
generate
252
begin
253
if (WID==128)
254
    redor128 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
255
else if (WID==96)
256
    redor96 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
257
else if (WID==80)
258
    redor80 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
259
else if (WID==64)
260
    redor64 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
261
else if (WID==40)
262
    redor40 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
263
else if (WID==32)
264
    redor32 u1 (.a(xdif4), .b({mfs4,2'b0}), .o(sticky) );
265
end
266
endgenerate
267
 
268
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
269
// Clock edge #6
270
// Shift (denormalize)
271
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
272
reg [FMSB+3:0] md6;
273
wire xa_gt_xb6;
274
wire [FMSB+1:0] fracta6, fractb6;
275
 
276
delay3 #(1) dxagtxb5(.clk(clk), .ce(ce), .i(xa_gt_xb3), .o(xa_gt_xb6));
277
delay4 #(FMSB+2)  dfracta5(.clk(clk), .ce(ce), .i(fracta2), .o(fracta6) );
278
delay4 #(FMSB+2)  dfractb5(.clk(clk), .ce(ce), .i(fractb2), .o(fractb6) );
279
 
280
always @(posedge clk)
281
        if (ce) md6 <= ({mfs5,2'b0} >> xdif5)|sticky5;
282
 
283
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
284
// Clock edge #7
285
// Sort operands
286
// addition can generate an extra bit, subtract can't go negative
287
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
288
reg [FMSB+3:0] oa7;
289
reg [FMSB+3:0] ob7;
290
wire a_gt_b7;
291
 
292
delay4 #(1) dagtb5(.clk(clk), .ce(ce), .i(a_gt_b3), .o(a_gt_b7));
293
 
294
always @(posedge clk)
295
        if (ce) oa7 <= xa_gt_xb6 ? {fracta6,2'b0} : md6;
296
always @(posedge clk)
297
        if (ce) ob7 <= xa_gt_xb6 ? md6 : {fractb6,2'b0};
298
 
299
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
300
// Clock edge #8
301
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
302
reg [FMSB+3:0] oaa8;
303
reg [FMSB+3:0] obb8;
304
wire [EMSB:0] xo8;
305
wire realOp8;
306
vtdl #(.WID(1)) drealop7 (.clk(clk), .ce(ce), .a(4'd5), .d(realOp2), .q(realOp8));
307
vtdl #(.WID(EMSB+1)) dxo7(.clk(clk), .ce(ce), .a(4'd3), .d(xo4), .q(xo8));
308
always @(posedge clk)
309
        if (ce) oaa8 <= a_gt_b7 ? oa7 : ob7;
310
always @(posedge clk)
311
        if (ce) obb8 <= a_gt_b7 ? ob7 : oa7;
312
 
313
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
314
// Clock edge #9
315
// perform add/subtract
316
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
317
reg [FMSB+4:0] mab9;
318
wire anbInf9;
319
wire aNan9, bNan9;
320
wire op9;
321
wire [FMSB+1:0] fracta9, fractb9;
322
wire xo9;
323
reg xinf9;
324
 
325
vtdl #(1) danbInf7(.clk(clk), .ce(ce), .a(4'd6), .d(anbInf2), .q(anbInf9));
326
vtdl #(1) danan8(.clk(clk), .ce(ce), .a(4'd7), .d(aNan1), .q(aNan9));
327
vtdl #(1) dbnan8(.clk(clk), .ce(ce), .a(4'd7), .d(bNan1), .q(bNan9));
328
vtdl #(1) dop6(.clk(clk), .ce(ce), .a(4'd5), .d(op3), .q(op9));
329
delay3 #(FMSB+2)  dfracta8(.clk(clk), .ce(ce), .i(fracta6), .o(fracta9) );
330
delay3 #(FMSB+2)  dfractb8(.clk(clk), .ce(ce), .i(fractb6), .o(fractb9) );
331
 
332
always @(posedge clk)
333
        if (ce) mab9 <= realOp8 ? oaa8 - obb8 : oaa8 + obb8;
334
always @(posedge clk)
335
        if (ce) xinf9 <= xo8 == {EMSB+1{1'b1}};
336
 
337
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
338
// Clock edge #10
339
// Final outputs
340
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
341
vtdl #(1) dso6(.clk(clk), .ce(ce), .a(4'd5), .d(so4), .q(so));
342
vtdl #(.WID(EMSB+1)) dxo6(.clk(clk), .ce(ce), .a(4'd1), .d(xo8), .q(xo));
343
 
344
always @(posedge clk)
345
if (ce)
346
        casez({xinf9,anbInf9,aNan9,bNan9})
347
        4'b1???:        mo <= 1'd0;             // exponent hit infinity -> force mantissa to zero
348
        4'b01??:        mo <= {1'b0,op9,{FMSB-1{1'b0}},op9,{FMSB{1'b0}}};       // inf +/- inf - generate QNaN on subtract, inf on add
349
        4'b001?:        mo <= {1'b0,fracta9[FMSB+1:0],{FMSB{1'b0}}};
350
        4'b0001:        mo <= {1'b0,fractb9[FMSB+1:0],{FMSB{1'b0}}};
351
        default:        mo <= {mab9,{FMSB-1{1'b0}}};    // mab has an extra lead bit and two trailing bits
352
        endcase
353
 
354
endmodule
355
 
356
module fpAddsubnr_L10(clk, ce, rm, op, a, b, o);
357
parameter WID = 128;
358
localparam MSB = WID-1;
359
localparam EMSB = WID==128 ? 14 :
360
                  WID==96 ? 14 :
361
                  WID==80 ? 14 :
362
                  WID==64 ? 10 :
363
                                  WID==52 ? 10 :
364
                                  WID==48 ? 11 :
365
                                  WID==44 ? 10 :
366
                                  WID==42 ? 10 :
367
                                  WID==40 ?  9 :
368
                                  WID==32 ?  7 :
369
                                  WID==24 ?  6 : 4;
370
localparam FMSB = WID==128 ? 111 :
371
                  WID==96 ? 79 :
372
                  WID==80 ? 63 :
373
                  WID==64 ? 51 :
374
                                  WID==52 ? 39 :
375
                                  WID==48 ? 34 :
376
                                  WID==44 ? 31 :
377
                                  WID==42 ? 29 :
378
                                  WID==40 ? 28 :
379
                                  WID==32 ? 22 :
380
                                  WID==24 ? 15 : 9;
381
 
382
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
383
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
384
 
385
input clk;              // system clock
386
input ce;               // core clock enable
387
input [2:0] rm;  // rounding mode
388
input op;               // operation 0 = add, 1 = subtract
389
input [MSB:0] a; // operand a
390
input [MSB:0] b; // operand b
391
output [MSB:0] o;        // output
392
 
393
wire [EX:0] o1;
394
wire [MSB+3:0] fpn0;
395
 
396
fpAddsub_L10  #(WID) u1 (clk, ce, rm, op, a, b, o1);
397
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(1'b0), .i(o1), .o(fpn0) );
398
fpRoundReg  #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
399
 
400
endmodule

powered by: WebSVN 2.1.0

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