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

Subversion Repositories ft816float

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

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 78 robfinch
BCDSubtract #(N+2) ubcdsubn1
80
(
81
        .clk(clk),
82
        .a({8'h00,oaa10}),
83
        .b({8'h00,obb10}),
84
        .o(odd10),
85
        .co(odd10c)
86
);
87
/*
88 75 robfinch
BCDSub8NClk #(.N((N+2)/2)) ubcdsdn1
89
(
90
        .clk(clk),
91
        .a({8'h00,oaa10}),
92
        .b({8'h00,obb10}),
93
        .o(odd10),
94
        .ci(1'b0),
95
        .co(odd10c)
96
);
97 78 robfinch
*/
98 75 robfinch
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99
// Clock #1
100
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101
reg op1;
102
reg az, bz;
103
always_ff @(posedge clk)
104
        op1 <= op;
105
always_ff @(posedge clk)
106
        az <= au.sig==100'd0 && au.exp==12'd0;
107
always_ff @(posedge clk)
108
        bz <= bu.sig==100'd0 && bu.exp==12'd0;
109
 
110
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111
// Clock #2
112
//
113
// Figure out which operation is really needed an add or subtract ?
114
// If the signs are the same, use the orignal op,
115
// otherwise flip the operation
116
//  a +  b = add,+
117
//  a + -b = sub, so of larger
118
// -a +  b = sub, so of larger
119
// -a + -b = add,-
120
//  a -  b = sub, so of larger
121
//  a - -b = add,+
122
// -a -  b = add,-
123
// -a - -b = sub, so of larger
124
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125
reg realOp2;
126
reg op2;
127
reg [13:0] xa2, xb2;
128
reg az2, bz2;
129
reg xa_gt_xb2;
130
reg [N*4-1:0] siga2, sigb2;
131
reg sigeq, siga_gt_sigb;
132
reg expeq;
133
 
134
always_ff @(posedge clk)
135
  if (ce) realOp2 = op1 ^ au.sign ^ bu.sign;
136
always_ff @(posedge clk)
137
  if (ce) op2 <= op1;
138
always_ff @(posedge clk)
139
  if (ce) xa2 <= au.exp;
140
always_ff @(posedge clk)
141
  if (ce) xb2 <= bu.exp;
142
always_ff @(posedge clk)
143
  if (ce) siga2 <= au.sig;
144
always_ff @(posedge clk)
145
  if (ce) sigb2 <= bu.sig;
146
always_ff @(posedge clk)
147
  if (ce) az2 <= az;
148
always_ff @(posedge clk)
149
  if (ce) bz2 <= bz;
150
always_ff @(posedge clk)
151
  if (ce)
152
        xa_gt_xb2 <= au.exp > bu.exp;
153
 
154
always_ff @(posedge clk)
155
  if (ce) sigeq <= au.sig==bu.sig;
156
always_ff @(posedge clk)
157
  if (ce) siga_gt_sigb <= au.sig > bu.sig;
158
always_ff @(posedge clk)
159
  if (ce) expeq <= au.exp==bu.exp;
160
 
161
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
162
// Clock #3
163
//
164
// Find out if the result will be zero.
165
// Determine which fraction to denormalize
166
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
167
//
168
reg [11:0] xa3, xb3;
169
reg resZero3;
170
reg xa_gt_xb3;
171
reg a_gt_b3;
172
reg op3;
173
wire sa3, sb3;
174
wire [2:0] rm3;
175
reg [N*4-1:0] mfs3;
176
 
177
always_ff @(posedge clk)
178
  if (ce) resZero3 <= (realOp2 & expeq & sigeq) ||      // subtract, same magnitude
179
                           (az2 & bz2);               // both a,b zero
180
always_ff @(posedge clk)
181
  if (ce) xa3 <= xa2;
182
always_ff @(posedge clk)
183
  if (ce) xb3 <= xb2;
184
always_ff @(posedge clk)
185
  if (ce) xa_gt_xb3 <= xa_gt_xb2;
186
always_ff @(posedge clk)
187
  if (ce) a_gt_b3 <= xa_gt_xb2 | (expeq & siga_gt_sigb);
188
always_ff @(posedge clk)
189
  if (ce) op3 <= op2;
190
always_ff @(posedge clk)
191
  if (ce) mfs3 = xa_gt_xb2 ? sigb2 : siga2;
192
 
193
ft_delay #(.WID(1), .DEP(2)) udly3c (.clk(clk), .ce(ce), .i(au.sign), .o(sa3));
194
ft_delay #(.WID(1), .DEP(2)) udly3d (.clk(clk), .ce(ce), .i(bu.sign), .o(sb3));
195
ft_delay #(.WID(3), .DEP(3)) udly3e (.clk(clk), .ce(ce), .i(rm), .o(rm3));
196 78 robfinch
ft_delay #(.WID(1), .DEP(3)) udly3f (.clk(clk), .ce(ce), .i(au.infinity), .o(aInf3));
197
ft_delay #(.WID(1), .DEP(3)) udly3g (.clk(clk), .ce(ce), .i(bu.infinity), .o(bInf3));
198 75 robfinch
 
199
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
200
// Clock #4
201
//
202
// Compute output exponent
203
//
204
// The output exponent is the larger of the two exponents,
205
// unless a subtract operation is in progress and the two
206
// numbers are equal, in which case the exponent should be
207
// zero.
208
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
209
 
210
reg [11:0] xa4, xb4;
211
reg [11:0] xo4;
212
reg xa_gt_xb4;
213
 
214
always_ff @(posedge clk)
215
  if (ce) xa4 <= xa3;
216
always_ff @(posedge clk)
217
  if (ce) xb4 <= xb3;
218
always_ff @(posedge clk)
219
        if (ce) xo4 <= resZero3 ? 12'd0 : xa_gt_xb3 ? xa3 : xb3;
220
always_ff @(posedge clk)
221
  if (ce) xa_gt_xb4 <= xa_gt_xb3;
222
 
223
// Compute output sign
224
reg so4;
225
always_comb
226
        case ({resZero3,sa3,op3,sb3})   // synopsys full_case parallel_case
227
        4'b0000: so4 <= 0;                      // + + + = +
228
        4'b0001: so4 <= !a_gt_b3;       // + + - = sign of larger
229
        4'b0010: so4 <= !a_gt_b3;       // + - + = sign of larger
230
        4'b0011: so4 <= 0;                      // + - - = +
231
        4'b0100: so4 <= a_gt_b3;                // - + + = sign of larger
232
        4'b0101: so4 <= 1;                      // - + - = -
233
        4'b0110: so4 <= 1;                      // - - + = -
234
        4'b0111: so4 <= a_gt_b3;                // - - - = sign of larger
235
        4'b1000: so4 <= 0;                      //  A +  B, sign = +
236
        4'b1001: so4 <= (rm3==3'd3);            //  A + -B, sign = + unless rounding down
237
        4'b1010: so4 <= (rm3==3'd3);            //  A - B, sign = + unless rounding down
238
        4'b1011: so4 <= 0;                      // A - -B, sign = +
239
        4'b1100: so4 <= (rm3==3'd3);            // -A -  -B, sign = + unless rounding down
240
        4'b1101: so4 <= 1;                      // -A + -B, sign = -
241
        4'b1110: so4 <= 1;                      // -A - +B, sign = -
242
        4'b1111: so4 <= (rm3==3'd3);            // A - B, sign = + unless rounding down
243
        endcase
244
 
245
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
246
// Clock #5
247
//
248
// Compute the difference in exponents, provides shift amount
249
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
250
reg [11:0] xdiff5;
251
always_ff @(posedge clk)
252
  if (ce) xdiff5 <= xa_gt_xb4 ? xa4 - xb4 : xb4 - xa4;
253
 
254
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
255
// Clock #6
256
//
257
// Compute the difference in exponents, provides shift amount
258
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
259
// If the difference in the exponent is 24 or greater (assuming 24 nybble dfp or
260
// less) then all of the bits will be shifted out to zero. There is no need to
261
// keep track of a difference more than 24.
262
reg [6:0] xdif6;
263
wire [N*4-1:0] mfs6;
264
always_ff @(posedge clk)
265
  if (ce) xdif6 <= xdiff5 > N ? N : xdiff5[6:0];
266
ft_delay #(.WID(N*4), .DEP(3)) udly6a (.clk(clk), .ce(ce), .i(mfs3), .o(mfs6));
267
 
268
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
269
// Clock #7
270
//
271
// Determine the sticky bit. The sticky bit is the bitwise or of all the bits
272
// being shifted out the right side. The sticky bit is computed here to
273
// reduce the number of regs required.
274
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
275
reg sticky6;
276
wire sticky7;
277
wire [7:0] xdif7;
278
wire [N*4-1:0] mfs7;
279
wire [8:0] xdif6a = {xdif6,2'b00};      // *4
280
integer n;
281
always @*
282
begin
283
        sticky6 = 1'b0;
284
        for (n = 0; n < N*4; n = n + 4)
285
                if (n <= xdif6a)
286
                        sticky6 = sticky6| mfs6[n]|mfs6[n+1]|mfs6[n+2]|mfs6[n+3];       // non-zero nybble
287
end
288
 
289
// register inputs to shifter and shift
290
delay1 #(1)  d16(.clk(clk), .ce(ce), .i(sticky6), .o(sticky7) );
291
delay1 #(9)  d15(.clk(clk), .ce(ce), .i(xdif6a),   .o(xdif7) );
292
delay1 #(N*4) d14(.clk(clk), .ce(ce), .i(mfs6),    .o(mfs7) );
293
 
294
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
295
// Clock #8
296
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
297
reg [(N+1)*4-1:0] md8;
298
wire [N*4-1:0] siga8, sigb8;
299
wire xa_gt_xb8;
300
wire a_gt_b8;
301
always_ff @(posedge clk)
302
  if (ce) md8 <= ({mfs7,4'b0} >> xdif7)|sticky7; // xdif7 is a multiple of four
303
 
304
// sync control signals
305
ft_delay #(.WID(1), .DEP(4)) udly8a (.clk(clk), .ce(ce), .i(xa_gt_xb4), .o(xa_gt_xb8));
306
ft_delay #(.WID(1), .DEP(5)) udly8b (.clk(clk), .ce(ce), .i(a_gt_b3), .o(a_gt_b8));
307
ft_delay #(.WID(N*4), .DEP(6)) udly8d (.clk(clk), .ce(ce), .i(siga2), .o(siga8));
308
ft_delay #(.WID(N*4), .DEP(6)) udly8e (.clk(clk), .ce(ce), .i(sigb2), .o(sigb8));
309
ft_delay #(.WID(1), .DEP(5)) udly8j (.clk(clk), .ce(ce), .i(op3), .o(op8));
310
 
311
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
312
// Clock #9
313
// Sort operands and perform add/subtract
314
// addition can generate an extra bit, subtract can't go negative
315
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
316
reg [(N+1)*4-1:0] oa9, ob9;
317
reg a_gt_b9;
318
always_ff @(posedge clk)
319
  if (ce) oa9 <= xa_gt_xb8 ? {siga8,4'b0} : md8;
320
always_ff @(posedge clk)
321
  if (ce) ob9 <= xa_gt_xb8 ? md8 : {sigb8,4'b0};
322
always_ff @(posedge clk)
323
  if (ce) a_gt_b9 <= a_gt_b8;
324
 
325
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
326
// Clock #10
327
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
328
wire realOp10;
329
reg [11:0] xo10;
330
 
331
always_ff @(posedge clk)
332
  if (ce) oaa10 <= a_gt_b9 ? oa9 : ob9;
333
always_ff @(posedge clk)
334
  if (ce) obb10 <= a_gt_b9 ? ob9 : oa9;
335
ft_delay #(.WID(1), .DEP(8)) udly10a (.clk(clk), .ce(ce), .i(realOp2), .o(realOp10));
336
ft_delay #(.WID(12), .DEP(6)) udly10b (.clk(clk), .ce(ce), .i(xo4), .o(xo10));
337
 
338
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
339
// Clock #11
340
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
341
wire [(N+1)*4-1:0] mab11;
342
wire mab11c;
343
wire [N*4-1:0] siga11, sigb11;
344
wire abInf11;
345
wire aNan11, bNan11;
346
wire xoinf11;
347
wire op11;
348
 
349
ft_delay #(.WID(1), .DEP(8+RIP_STAGES)) udly11a (.clk(clk), .ce(ce), .i(aInf3&bInf3), .o(abInf11));
350
ft_delay #(.WID(1), .DEP(10+RIP_STAGES)) udly11c (.clk(clk), .ce(ce), .i(au.nan), .o(aNan11));
351
ft_delay #(.WID(1), .DEP(10+RIP_STAGES)) udly11d (.clk(clk), .ce(ce), .i(bu.nan), .o(bNan11));
352
ft_delay #(.WID(1), .DEP(3+RIP_STAGES)) udly11e (.clk(clk), .ce(ce), .i(op8), .o(op11));
353
ft_delay #(.WID(N*4), .DEP(3+RIP_STAGES)) udly11f (.clk(clk), .ce(ce), .i(siga8), .o(siga11));
354
ft_delay #(.WID(N*4), .DEP(3+RIP_STAGES)) udly11g (.clk(clk), .ce(ce), .i(sigb8), .o(sigb11));
355
ft_delay #(.WID(1), .DEP(1+RIP_STAGES)) udly11h (.clk(clk), .ce(ce), .i(xo10==14'h2FFF), .o(xoinf11));
356
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}));
357
 
358
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
359
// Clock #12+RIP_STAGES
360
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
361
reg [(N+1)*4*2-1:0] mo12;       // mantissa output
362
reg nan12;
363
reg qnan12;
364
reg infinity12;
365
wire so11;
366
ft_delay #(.WID(1), .DEP(7)) udly12b (.clk(clk), .ce(ce), .i(so4), .o(so11));
367
 
368
always_ff @(posedge clk)
369
if (ce)
370
        nan12 <= aNan11|bNan11;
371
 
372
always_ff @(posedge clk)
373
if (ce) begin
374
        infinity12 <= 1'b0;
375
        qnan12 <= 1'b0;
376
        casez({abInf11,aNan11,bNan11,xoinf11})
377
        4'b1???:        // inf +/- inf - generate QNaN on subtract, inf on add
378
                if (op11) begin
379
                        mo12 <= {4'h9,{(N+1)*4*2-4{1'd0}}};
380
                        qnan12 <= 1'b1;
381
                end
382
                else begin
383
                        mo12 <= {(N+1)*2{4'h9}};
384
                        infinity12 <= 1'b1;
385
                end
386
        4'b01??:        mo12 <= {4'b0,siga11[87:0],{(N+1)*4{1'd0}}};
387
        4'b001?:        mo12 <= {4'b0,sigb11[87:0],{(N+1)*4{1'd0}}};
388
        4'b0001:        begin mo12 <= {(N+1)*4*2{1'd0}}; infinity12 <= 1'b1; end
389
        default:        mo12 <= {3'b0,mab11c,mab11,{N*4{1'd0}}};        // mab has an extra lead bit and four trailing bits
390
        endcase
391
end
392
 
393
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
394
// Clock #13
395
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
396
 
397
ft_delay #(.WID(1), .DEP(1)) u13c (.clk(clk), .ce(ce), .i(nan12), .o(o.nan) );
398
ft_delay #(.WID(1), .DEP(1)) u13d (.clk(clk), .ce(ce), .i(qnan12), .o(o.qnan) );
399
ft_delay #(.WID(1), .DEP(1)) u13e (.clk(clk), .ce(ce), .i(infinity12), .o(o.infinity) );
400
ft_delay #(.WID(1), .DEP(9)) udly13a (.clk(clk), .ce(ce), .i(so4), .o(o.sign));
401
ft_delay #(.WID(12), .DEP(3)) udly13b (.clk(clk), .ce(ce), .i(xo10), .o(o.exp));
402
ft_delay #(.WID((N+1)*4*2), .DEP(1)) u13f (.clk(clk), .ce(ce), .i(mo12), .o(o.sig));
403
ft_delay #(.WID(1), .DEP(1)) udly13g (.clk(clk), .ce(ce), .i(1'b0), .o(o.snan));
404
 
405
endmodule
406
 
407
 
408
module DFPAddsub96nr(clk, ce, rm, op, a, b, o);
409
input clk;              // system clock
410
input ce;               // core clock enable
411
input [2:0] rm; // rounding mode
412
input op;               // operation 0 = add, 1 = subtract
413
input DFP96 a;  // operand a
414
input DFP96 b;  // operand b
415
output DFP96 o; // output
416
 
417
wire DFP96UD o1;
418
wire DFP96UN fpn0;
419
 
420
DFPAddsub96             u1 (clk, ce, rm, op, a, b, o1);
421
DFPNormalize96  u2(.clk(clk), .ce(ce), .under_i(1'b0), .i(o1), .o(fpn0) );
422
DFPRound96              u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
423
 
424
endmodule

powered by: WebSVN 2.1.0

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