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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DFPAddsub96.sv] - Blame information for rev 75

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

Line No. Rev Author Line
1 75 robfinch
`timescale 1ns / 1ps
2
// ============================================================================
3
//        __
4
//   \\__/ o\    (C) 2020-2022  Robert Finch, Waterloo
5
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch@finitron.ca
7
//       ||
8
//
9
//      DFPAddsub96.sv
10
//
11
// BSD 3-Clause License
12
// Redistribution and use in source and binary forms, with or without
13
// modification, are permitted provided that the following conditions are met:
14
//
15
// 1. Redistributions of source code must retain the above copyright notice, this
16
//    list of conditions and the following disclaimer.
17
//
18
// 2. Redistributions in binary form must reproduce the above copyright notice,
19
//    this list of conditions and the following disclaimer in the documentation
20
//    and/or other materials provided with the distribution.
21
//
22
// 3. Neither the name of the copyright holder nor the names of its
23
//    contributors may be used to endorse or promote products derived from
24
//    this software without specific prior written permission.
25
//
26
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// ============================================================================
38
 
39
import DFPPkg::*;
40
 
41
module DFPAddsub96(clk, ce, rm, op, a, b, o);
42
input clk;
43
input ce;
44
input [2:0] rm;
45
input op;
46
input DFP96 a;
47
input DFP96 b;
48
output DFP96UD o;
49
localparam N=25;                        // number of BCD digits
50
localparam RIP_STAGES = 3;
51
 
52
parameter TRUE = 1'b1;
53
parameter FALSE = 1'b0;
54
 
55
DFP96U au;
56
DFP96U bu;
57
 
58
DFPUnpack96 u00 (a, au);
59
DFPUnpack96 u01 (b, bu);
60
 
61
reg [(N+1)*4-1:0] oaa10;
62
reg [(N+1)*4-1:0] obb10;
63
wire [(N+1)*4-1:0] oss10;
64
wire oss10c;
65
 
66
BCDAdd8NClk #(.N((N+2)/2)) ubcdadn1
67
(
68
        .clk(clk),
69
        .a({8'h00,oaa10}),
70
        .b({8'h00,obb10}),
71
        .o(oss10),
72
        .ci(1'b0),
73
        .co(oss10c)
74
);
75
 
76
wire [(N+1)*4-1:0] odd10;
77
wire odd10c;
78
 
79
BCDSub8NClk #(.N((N+2)/2)) ubcdsdn1
80
(
81
        .clk(clk),
82
        .a({8'h00,oaa10}),
83
        .b({8'h00,obb10}),
84
        .o(odd10),
85
        .ci(1'b0),
86
        .co(odd10c)
87
);
88
 
89
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90
// Clock #1
91
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
92
reg op1;
93
reg az, bz;
94
always_ff @(posedge clk)
95
        op1 <= op;
96
always_ff @(posedge clk)
97
        az <= au.sig==100'd0 && au.exp==12'd0;
98
always_ff @(posedge clk)
99
        bz <= bu.sig==100'd0 && bu.exp==12'd0;
100
 
101
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102
// Clock #2
103
//
104
// Figure out which operation is really needed an add or subtract ?
105
// If the signs are the same, use the orignal op,
106
// otherwise flip the operation
107
//  a +  b = add,+
108
//  a + -b = sub, so of larger
109
// -a +  b = sub, so of larger
110
// -a + -b = add,-
111
//  a -  b = sub, so of larger
112
//  a - -b = add,+
113
// -a -  b = add,-
114
// -a - -b = sub, so of larger
115
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116
reg realOp2;
117
reg op2;
118
reg [13:0] xa2, xb2;
119
reg az2, bz2;
120
reg xa_gt_xb2;
121
reg [N*4-1:0] siga2, sigb2;
122
reg sigeq, siga_gt_sigb;
123
reg expeq;
124
reg sxo2;
125
 
126
always_ff @(posedge clk)
127
  if (ce) realOp2 = op1 ^ au.sign ^ bu.sign;
128
always_ff @(posedge clk)
129
  if (ce) op2 <= op1;
130
always_ff @(posedge clk)
131
  if (ce) xa2 <= au.exp;
132
always_ff @(posedge clk)
133
  if (ce) xb2 <= bu.exp;
134
always_ff @(posedge clk)
135
  if (ce) siga2 <= au.sig;
136
always_ff @(posedge clk)
137
  if (ce) sigb2 <= bu.sig;
138
always_ff @(posedge clk)
139
  if (ce) az2 <= az;
140
always_ff @(posedge clk)
141
  if (ce) bz2 <= bz;
142
always_ff @(posedge clk)
143
  if (ce)
144
        xa_gt_xb2 <= au.exp > bu.exp;
145
 
146
always_ff @(posedge clk)
147
  if (ce) sigeq <= au.sig==bu.sig;
148
always_ff @(posedge clk)
149
  if (ce) siga_gt_sigb <= au.sig > bu.sig;
150
always_ff @(posedge clk)
151
  if (ce) expeq <= au.exp==bu.exp;
152
 
153
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154
// Clock #3
155
//
156
// Find out if the result will be zero.
157
// Determine which fraction to denormalize
158
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159
//
160
reg [11:0] xa3, xb3;
161
reg resZero3;
162
wire xaInf3, xbInf3;
163
reg xa_gt_xb3;
164
reg a_gt_b3;
165
reg op3;
166
wire sa3, sb3;
167
wire [2:0] rm3;
168
reg [N*4-1:0] mfs3;
169
 
170
always_ff @(posedge clk)
171
  if (ce) resZero3 <= (realOp2 & expeq & sigeq) ||      // subtract, same magnitude
172
                           (az2 & bz2);               // both a,b zero
173
always_ff @(posedge clk)
174
  if (ce) xa3 <= xa2;
175
always_ff @(posedge clk)
176
  if (ce) xb3 <= xb2;
177
always_ff @(posedge clk)
178
  if (ce) xa_gt_xb3 <= xa_gt_xb2;
179
always_ff @(posedge clk)
180
  if (ce) a_gt_b3 <= xa_gt_xb2 | (expeq & siga_gt_sigb);
181
always_ff @(posedge clk)
182
  if (ce) op3 <= op2;
183
always_ff @(posedge clk)
184
  if (ce) mfs3 = xa_gt_xb2 ? sigb2 : siga2;
185
 
186
ft_delay #(.WID(1), .DEP(2)) udly3c (.clk(clk), .ce(ce), .i(au.sign), .o(sa3));
187
ft_delay #(.WID(1), .DEP(2)) udly3d (.clk(clk), .ce(ce), .i(bu.sign), .o(sb3));
188
ft_delay #(.WID(3), .DEP(3)) udly3e (.clk(clk), .ce(ce), .i(rm), .o(rm3));
189
ft_delay #(.WID(1), .DEP(2)) udly3f (.clk(clk), .ce(ce), .i(aInf), .o(aInf3));
190
ft_delay #(.WID(1), .DEP(2)) udly3g (.clk(clk), .ce(ce), .i(bInf), .o(bInf3));
191
 
192
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
193
// Clock #4
194
//
195
// Compute output exponent
196
//
197
// The output exponent is the larger of the two exponents,
198
// unless a subtract operation is in progress and the two
199
// numbers are equal, in which case the exponent should be
200
// zero.
201
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
202
 
203
reg [11:0] xa4, xb4;
204
reg [11:0] xo4;
205
reg xa_gt_xb4;
206
 
207
always_ff @(posedge clk)
208
  if (ce) xa4 <= xa3;
209
always_ff @(posedge clk)
210
  if (ce) xb4 <= xb3;
211
always_ff @(posedge clk)
212
        if (ce) xo4 <= resZero3 ? 12'd0 : xa_gt_xb3 ? xa3 : xb3;
213
always_ff @(posedge clk)
214
  if (ce) xa_gt_xb4 <= xa_gt_xb3;
215
 
216
// Compute output sign
217
reg so4;
218
always_comb
219
        case ({resZero3,sa3,op3,sb3})   // synopsys full_case parallel_case
220
        4'b0000: so4 <= 0;                      // + + + = +
221
        4'b0001: so4 <= !a_gt_b3;       // + + - = sign of larger
222
        4'b0010: so4 <= !a_gt_b3;       // + - + = sign of larger
223
        4'b0011: so4 <= 0;                      // + - - = +
224
        4'b0100: so4 <= a_gt_b3;                // - + + = sign of larger
225
        4'b0101: so4 <= 1;                      // - + - = -
226
        4'b0110: so4 <= 1;                      // - - + = -
227
        4'b0111: so4 <= a_gt_b3;                // - - - = sign of larger
228
        4'b1000: so4 <= 0;                      //  A +  B, sign = +
229
        4'b1001: so4 <= (rm3==3'd3);            //  A + -B, sign = + unless rounding down
230
        4'b1010: so4 <= (rm3==3'd3);            //  A - B, sign = + unless rounding down
231
        4'b1011: so4 <= 0;                      // A - -B, sign = +
232
        4'b1100: so4 <= (rm3==3'd3);            // -A -  -B, sign = + unless rounding down
233
        4'b1101: so4 <= 1;                      // -A + -B, sign = -
234
        4'b1110: so4 <= 1;                      // -A - +B, sign = -
235
        4'b1111: so4 <= (rm3==3'd3);            // A - B, sign = + unless rounding down
236
        endcase
237
 
238
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
239
// Clock #5
240
//
241
// Compute the difference in exponents, provides shift amount
242
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
243
reg [11:0] xdiff5;
244
always_ff @(posedge clk)
245
  if (ce) xdiff5 <= xa_gt_xb4 ? xa4 - xb4 : xb4 - xa4;
246
 
247
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
248
// Clock #6
249
//
250
// Compute the difference in exponents, provides shift amount
251
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
252
// If the difference in the exponent is 24 or greater (assuming 24 nybble dfp or
253
// less) then all of the bits will be shifted out to zero. There is no need to
254
// keep track of a difference more than 24.
255
reg [6:0] xdif6;
256
wire [N*4-1:0] mfs6;
257
always_ff @(posedge clk)
258
  if (ce) xdif6 <= xdiff5 > N ? N : xdiff5[6:0];
259
ft_delay #(.WID(N*4), .DEP(3)) udly6a (.clk(clk), .ce(ce), .i(mfs3), .o(mfs6));
260
 
261
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
262
// Clock #7
263
//
264
// Determine the sticky bit. The sticky bit is the bitwise or of all the bits
265
// being shifted out the right side. The sticky bit is computed here to
266
// reduce the number of regs required.
267
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
268
reg sticky6;
269
wire sticky7;
270
wire [7:0] xdif7;
271
wire [N*4-1:0] mfs7;
272
wire [8:0] xdif6a = {xdif6,2'b00};      // *4
273
integer n;
274
always @*
275
begin
276
        sticky6 = 1'b0;
277
        for (n = 0; n < N*4; n = n + 4)
278
                if (n <= xdif6a)
279
                        sticky6 = sticky6| mfs6[n]|mfs6[n+1]|mfs6[n+2]|mfs6[n+3];       // non-zero nybble
280
end
281
 
282
// register inputs to shifter and shift
283
delay1 #(1)  d16(.clk(clk), .ce(ce), .i(sticky6), .o(sticky7) );
284
delay1 #(9)  d15(.clk(clk), .ce(ce), .i(xdif6a),   .o(xdif7) );
285
delay1 #(N*4) d14(.clk(clk), .ce(ce), .i(mfs6),    .o(mfs7) );
286
 
287
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
288
// Clock #8
289
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
290
reg [(N+1)*4-1:0] md8;
291
wire [N*4-1:0] siga8, sigb8;
292
wire xa_gt_xb8;
293
wire a_gt_b8;
294
always_ff @(posedge clk)
295
  if (ce) md8 <= ({mfs7,4'b0} >> xdif7)|sticky7; // xdif7 is a multiple of four
296
 
297
// sync control signals
298
ft_delay #(.WID(1), .DEP(4)) udly8a (.clk(clk), .ce(ce), .i(xa_gt_xb4), .o(xa_gt_xb8));
299
ft_delay #(.WID(1), .DEP(5)) udly8b (.clk(clk), .ce(ce), .i(a_gt_b3), .o(a_gt_b8));
300
ft_delay #(.WID(N*4), .DEP(6)) udly8d (.clk(clk), .ce(ce), .i(siga2), .o(siga8));
301
ft_delay #(.WID(N*4), .DEP(6)) udly8e (.clk(clk), .ce(ce), .i(sigb2), .o(sigb8));
302
ft_delay #(.WID(1), .DEP(5)) udly8j (.clk(clk), .ce(ce), .i(op3), .o(op8));
303
 
304
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
305
// Clock #9
306
// Sort operands and perform add/subtract
307
// addition can generate an extra bit, subtract can't go negative
308
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
309
reg [(N+1)*4-1:0] oa9, ob9;
310
reg a_gt_b9;
311
always_ff @(posedge clk)
312
  if (ce) oa9 <= xa_gt_xb8 ? {siga8,4'b0} : md8;
313
always_ff @(posedge clk)
314
  if (ce) ob9 <= xa_gt_xb8 ? md8 : {sigb8,4'b0};
315
always_ff @(posedge clk)
316
  if (ce) a_gt_b9 <= a_gt_b8;
317
 
318
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
319
// Clock #10
320
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
321
wire realOp10;
322
reg [11:0] xo10;
323
 
324
always_ff @(posedge clk)
325
  if (ce) oaa10 <= a_gt_b9 ? oa9 : ob9;
326
always_ff @(posedge clk)
327
  if (ce) obb10 <= a_gt_b9 ? ob9 : oa9;
328
ft_delay #(.WID(1), .DEP(8)) udly10a (.clk(clk), .ce(ce), .i(realOp2), .o(realOp10));
329
ft_delay #(.WID(12), .DEP(6)) udly10b (.clk(clk), .ce(ce), .i(xo4), .o(xo10));
330
 
331
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
332
// Clock #11
333
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
334
wire [(N+1)*4-1:0] mab11;
335
wire mab11c;
336
wire [N*4-1:0] siga11, sigb11;
337
wire abInf11;
338
wire aNan11, bNan11;
339
wire xoinf11;
340
wire op11;
341
 
342
ft_delay #(.WID(1), .DEP(8+RIP_STAGES)) udly11a (.clk(clk), .ce(ce), .i(aInf3&bInf3), .o(abInf11));
343
ft_delay #(.WID(1), .DEP(10+RIP_STAGES)) udly11c (.clk(clk), .ce(ce), .i(au.nan), .o(aNan11));
344
ft_delay #(.WID(1), .DEP(10+RIP_STAGES)) udly11d (.clk(clk), .ce(ce), .i(bu.nan), .o(bNan11));
345
ft_delay #(.WID(1), .DEP(3+RIP_STAGES)) udly11e (.clk(clk), .ce(ce), .i(op8), .o(op11));
346
ft_delay #(.WID(N*4), .DEP(3+RIP_STAGES)) udly11f (.clk(clk), .ce(ce), .i(siga8), .o(siga11));
347
ft_delay #(.WID(N*4), .DEP(3+RIP_STAGES)) udly11g (.clk(clk), .ce(ce), .i(sigb8), .o(sigb11));
348
ft_delay #(.WID(1), .DEP(1+RIP_STAGES)) udly11h (.clk(clk), .ce(ce), .i(xo10==14'h2FFF), .o(xoinf11));
349
ft_delay #(.WID((N+1)*4+1), .DEP(1+RIP_STAGES)) udly11i (.clk(clk), .ce(ce), .i(realOp10 ? {odd10c,odd10} : {oss10c,oss10}), .o({mab11c,mab11}));
350
 
351
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
352
// Clock #12+RIP_STAGES
353
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
354
reg [(N+1)*4*2-1:0] mo12;       // mantissa output
355
reg nan12;
356
reg qnan12;
357
reg infinity12;
358
wire sxo11;
359
wire so11;
360
ft_delay #(.WID(1), .DEP(9)) udly12a (.clk(clk), .ce(ce), .i(sxo2), .o(sxo11));
361
ft_delay #(.WID(1), .DEP(7)) udly12b (.clk(clk), .ce(ce), .i(so4), .o(so11));
362
 
363
always_ff @(posedge clk)
364
if (ce)
365
        nan12 <= aNan11|bNan11;
366
 
367
always_ff @(posedge clk)
368
if (ce) begin
369
        infinity12 <= 1'b0;
370
        qnan12 <= 1'b0;
371
        casez({abInf11,aNan11,bNan11,xoinf11})
372
        4'b1???:        // inf +/- inf - generate QNaN on subtract, inf on add
373
                if (op11) begin
374
                        mo12 <= {4'h9,{(N+1)*4*2-4{1'd0}}};
375
                        qnan12 <= 1'b1;
376
                end
377
                else begin
378
                        mo12 <= {(N+1)*2{4'h9}};
379
                        infinity12 <= 1'b1;
380
                end
381
        4'b01??:        mo12 <= {4'b0,siga11[87:0],{(N+1)*4{1'd0}}};
382
        4'b001?:        mo12 <= {4'b0,sigb11[87:0],{(N+1)*4{1'd0}}};
383
        4'b0001:        begin mo12 <= {(N+1)*4*2{1'd0}}; infinity12 <= 1'b1; end
384
        default:        mo12 <= {3'b0,mab11c,mab11,{N*4{1'd0}}};        // mab has an extra lead bit and four trailing bits
385
        endcase
386
end
387
 
388
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
389
// Clock #13
390
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
391
wire so;                        // sign output
392
wire [13:0] xo; // de normalized exponent output
393
wire [(N+1)*4*2-1:0] mo;        // mantissa output
394
 
395
ft_delay #(.WID(1), .DEP(1)) u13c (.clk(clk), .ce(ce), .i(nan12), .o(o.nan) );
396
ft_delay #(.WID(1), .DEP(1)) u13d (.clk(clk), .ce(ce), .i(qnan12), .o(o.qnan) );
397
ft_delay #(.WID(1), .DEP(1)) u13e (.clk(clk), .ce(ce), .i(infinity12), .o(o.infinity) );
398
ft_delay #(.WID(1), .DEP(9)) udly13a (.clk(clk), .ce(ce), .i(so4), .o(o.sign));
399
ft_delay #(.WID(12), .DEP(3)) udly13b (.clk(clk), .ce(ce), .i(xo10), .o(o.exp));
400
ft_delay #(.WID((N+1)*4*2), .DEP(1)) u13f (.clk(clk), .ce(ce), .i(mo12), .o(o.sig));
401
ft_delay #(.WID(1), .DEP(1)) udly13g (.clk(clk), .ce(ce), .i(1'b0), .o(o.snan));
402
 
403
endmodule
404
 
405
 
406
module DFPAddsub96nr(clk, ce, rm, op, a, b, o);
407
input clk;              // system clock
408
input ce;               // core clock enable
409
input [2:0] rm; // rounding mode
410
input op;               // operation 0 = add, 1 = subtract
411
input DFP96 a;  // operand a
412
input DFP96 b;  // operand b
413
output DFP96 o; // output
414
 
415
wire DFP96UD o1;
416
wire DFP96UN fpn0;
417
 
418
DFPAddsub96             u1 (clk, ce, rm, op, a, b, o1);
419
DFPNormalize96  u2(.clk(clk), .ce(ce), .under_i(1'b0), .i(o1), .o(fpn0) );
420
DFPRound96              u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
421
 
422
endmodule

powered by: WebSVN 2.1.0

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