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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DFPDivide.sv] - Blame information for rev 83

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

Line No. Rev Author Line
1 54 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      DFPDivide.sv
9
//    - decimal floating point divider
10
//    - parameterized width
11
//
12
//
13
// BSD 3-Clause License
14
// Redistribution and use in source and binary forms, with or without
15
// modification, are permitted provided that the following conditions are met:
16
//
17
// 1. Redistributions of source code must retain the above copyright notice, this
18
//    list of conditions and the following disclaimer.
19
//
20
// 2. Redistributions in binary form must reproduce the above copyright notice,
21
//    this list of conditions and the following disclaimer in the documentation
22
//    and/or other materials provided with the distribution.
23
//
24
// 3. Neither the name of the copyright holder nor the names of its
25
//    contributors may be used to endorse or promote products derived from
26
//    this software without specific prior written permission.
27
//
28
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
//
39
//      Floating Point Divider
40
//
41
//Properties:
42
//+-inf * +-inf = -+inf    (this is handled by exOver)
43
//+-inf * 0     = QNaN
44
//+-0 / +-0      = QNaN
45
// ============================================================================
46
 
47
import fp::*;
48
 
49
module DFPDivide(rst, clk, ce, ld, op, a, b, o, done, sign_exe, overflow, underflow);
50 55 robfinch
parameter N=33;
51 54 robfinch
// FADD is a constant that makes the divider width a multiple of four and includes eight extra bits.
52
input rst;
53
input clk;
54
input ce;
55
input ld;
56
input op;
57 55 robfinch
input  [N*4+16+4-1:0] a, b;
58
output [(N+1)*4*2+16+4-1:0] o;
59 54 robfinch
output reg done;
60
output sign_exe;
61
output overflow;
62
output underflow;
63
 
64
// registered outputs
65
reg sign_exe=0;
66
reg inf=0;
67
reg     overflow=0;
68
reg     underflow=0;
69
 
70
reg so, sxo;
71
reg [3:0] st;
72
reg [15:0] xo;
73 55 robfinch
reg [(N+1)*4*2-1:0] mo;
74 54 robfinch
assign o = {st,xo,mo};
75
 
76
// constants
77
wire [15:0] infXp = 16'h9999;   // infinite / NaN - all ones
78
// The following is the value for an exponent of zero, with the offset
79
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
80
// The following is a template for a quiet nan. (MSB=1)
81 55 robfinch
wire [N*4-1:0] qNaN  = {4'h1,{(N-1)*4{1'b0}}};
82 54 robfinch
 
83
// variables
84 55 robfinch
wire [(N+2)*4*2-1:0] divo;
85 54 robfinch
 
86
// Operands
87
wire sa, sb;                    // sign bit
88
wire sxa, sxb;
89
wire [15:0] xa, xb;     // exponent bits
90 55 robfinch
wire [N*4-1:0] siga, sigb;
91 54 robfinch
wire a_dn, b_dn;                        // a/b is denormalized
92
wire az, bz;
93
wire aInf, bInf;
94
wire aNan,bNan;
95
wire done1;
96
wire signed [7:0] lzcnt;
97
 
98
// -----------------------------------------------------------
99
// Clock #1
100
// - decode the input operands
101
// - derive basic information
102
// - calculate fraction
103
// -----------------------------------------------------------
104
reg ld1;
105
DFPDecomposeReg u1a (.clk(clk), .ce(ce), .i(a), .sgn(sa), .sx(sxa), .exp(xa), .sig(siga), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
106
DFPDecomposeReg u1b (.clk(clk), .ce(ce), .i(b), .sgn(sb), .sx(sxb), .exp(xb), .sig(sigb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
107
delay #(.WID(1), .DEP(1)) udly1 (.clk(clk), .ce(ce), .i(ld), .o(ld1));
108
 
109
// -----------------------------------------------------------
110
// Clock #2 to N
111
// - calculate fraction
112
// -----------------------------------------------------------
113
wire done3a,done3;
114
// Perform divide
115 55 robfinch
dfdiv #(N+2) u2 (.clk(clk), .ld(ld1), .a({siga,8'b0}), .b({sigb,8'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
116 54 robfinch
wire [7:0] lzcnt_bin = lzcnt[3:0] + (lzcnt[7:4] * 10);
117 55 robfinch
wire [(N+2)*4*2-1:0] divo1 = divo[(N+2)*4*2-1:0] << ({lzcnt_bin,2'b0}+(N*4));//WAS FPWID=128?+44
118 54 robfinch
delay #(.WID(1), .DEP(3)) u3 (.clk(clk), .ce(ce), .i(done1), .o(done3a));
119
assign done3 = done1&done3a;
120
 
121
// -----------------------------------------------------------
122
// Clock #N+1
123
// - calculate exponent
124
// - calculate fraction
125
// - determine when a NaN is output
126
// -----------------------------------------------------------
127
// Compute the exponent.
128
// - correct the exponent for denormalized operands
129
// - adjust the difference by the bias (add 127)
130
// - also factor in the different decimal position for division
131
reg [15:0] ex2, ex1, ex2a, ex2b;        // sum of exponents
132
reg qNaNOut;
133
reg under1, under;
134
reg over1, over;
135
wire [15:0] xapxb, xamxb, xbmxa;
136
wire xapxbc, xamxbc, xbmxac;
137
reg sxo0;
138
 
139
BCDAddN #(.N(4)) u5 (.ci(1'b0), .a(xa), .b(xb), .o(xapxb), .co(xapxbc) );
140
BCDSubN #(.N(4)) u6 (.ci(1'b0), .a(xa), .b(xb), .o(xamxb), .co(xamxbc) );
141
BCDSubN #(.N(4)) u7 (.ci(1'b0), .a(xb), .b(xa), .o(xbmxa), .co(xbmxac) );
142
BCDSubN #(.N(5)) u10 (.ci(1'b0), .a(20'h10000), .b(ex2a), .o(ex2b), .co() );
143
 
144
always @*
145
case ({sxa,sxb})
146
2'b11:  begin ex2a <= xbmxa; sxo0 <= ~xbmxac; over1 <= 1'b0; under1 <= 1'b0; end
147
2'b10:  begin ex2a <= xapxb; sxo0 <= 1'b1; over1 <= xapxbc; under1 <= 1'b0; end
148
2'b01:  begin ex2a <= xapxb; sxo0 <= 1'b0; over1 <= 1'b0; under1 <= xapxbc; end
149
2'b00:  begin ex2a <= xamxb; sxo0 <= ~xamxbc; over1 <= 1'b0; under1 <= 1'b0; end
150
endcase
151
 
152
always @*
153
if (~sxo0 && ~(sa^sb))
154
        ex2 <= ex2b;
155
else
156
        ex2 <= ex2a;
157
 
158
wire [15:0] ex1a, ex1b, ex1d;
159
reg [15:0] ex1c;
160
wire sxoa, sxob, sxoc;
161
 
162
BCDAddN #(.N(4)) u8 (.ci(1'b0), .a(ex2), .b({8'h00,lzcnt}), .o(ex1a), .co(sxoa) );
163
BCDSubN #(.N(4)) u9 (.ci(1'b0), .a(ex2), .b({8'h00,lzcnt}), .o(ex1b), .co(sxob) );
164
BCDSubN #(.N(5)) u11 (.ci(1'b0), .a(20'h10000), .b(ex1c), .o(ex1d), .co() );
165
 
166
always @(posedge clk)
167
case(sxo0)
168
2'd1:   begin ex1c <= ex1b; sxo <= ~sxob; over <= over1; under <= under1; end
169
2'd0:   begin ex1c <= ex1a; sxo <= 1'b0; over <= over1;  under <= under1|sxob; end
170
endcase
171
 
172
always @*
173
if (sxo0 & sxob)        // There was a borrow on a subtract, making the number negative
174
        ex1 <= ex1d;
175
else
176
        ex1 <= ex1c;
177
 
178
 
179
always @(posedge clk)
180
  if (ce) qNaNOut <= (az&bz)|(aInf&bInf);
181
 
182
// -----------------------------------------------------------
183
// Clock #N+3
184
// -----------------------------------------------------------
185
always @(posedge clk)
186
// Simulation likes to see these values reset to zero on reset. Otherwise the
187
// values propagate in sim as X's.
188
if (rst) begin
189
        xo <= 1'd0;
190
        mo <= 1'd0;
191
        so <= 1'd0;
192
        sign_exe <= 1'd0;
193
        overflow <= 1'd0;
194
        underflow <= 1'd0;
195
        done <= 1'b1;
196
end
197
else if (ce) begin
198
  done <= 1'b0;
199
        if (done3&done1) begin
200
          done <= 1'b1;
201
 
202
                casez({qNaNOut|aNan|bNan,bInf,bz,over,under})
203
                5'b1????:               xo <= infXp;    // NaN exponent value
204
                5'b01???:               xo <= 1'd0;             // divide by inf
205
                5'b001??:               xo <= infXp;    // divide by zero
206
                5'b0001?:               xo <= infXp;    // overflow
207
                5'b00001:               xo <= 1'd0;             // underflow
208
                default:                xo <= ex1;      // normal or underflow: passthru neg. exp. for normalization
209
                endcase
210
 
211
                casez({aNan,bNan,qNaNOut,bInf,bz,over,aInf&bInf,az&bz})
212 55 robfinch
                8'b1???????:  begin mo <= {4'h1,a[N*4-1:0],{(N+1)*4-1{1'b0}}}; st[3] <= 1'b1; end
213
                8'b01??????:  begin mo <= {4'h1,b[N*4-1:0],{(N+1)*4-1{1'b0}}}; st[3] <= 1'b1; end
214
                8'b001?????:    begin mo <= {4'h1,qNaN[N*4-1:0]|{aInf,1'b0}|{az,bz},{(N+1)*4-1{1'b0}}}; st[3] <= 1'b1; end
215
                8'b0001????:    begin mo <= {(N+1)*4*2-1{1'd0}};        st[3] <= 1'b0; end      // div by inf
216
                8'b00001???:    begin mo <= {(N+1)*4*2-1{1'd0}};        st[3] <= 1'b0; end      // div by zero
217
                8'b000001??:    begin mo <= {(N+1)*4*2-1{1'd0}};        st[3] <= 1'b0; end      // Inf exponent
218
                8'b0000001?:    begin mo <= {4'h1,qNaN|`QINFDIV,{(N+1)*4-1{1'b0}}};     st[3] <= 1'b1; end      // infinity / infinity
219
                8'b00000001:    begin mo <= {4'h1,qNaN|`QZEROZERO,{(N+1)*4-1{1'b0}}};   st[3] <= 1'b1; end      // zero / zero
220
                default:                begin mo <= divo1[(N+2)*4*2-1:8];       st[3] <= 1'b0; end      // plain div
221 54 robfinch
                endcase
222
 
223
                st[0] <= sxo;
224
                st[1] <= aInf;
225
                st[2] <= ~(sa ^ sb);
226
                so              <= ~(sa ^ sb);
227
                sign_exe        <= sa & sb;
228
                overflow        <= over;
229
                underflow       <= under;
230
        end
231
end
232
 
233
endmodule
234
 
235
module DFPDividenr(rst, clk, ce, ld, op, a, b, o, rm, done, sign_exe, inf, overflow, underflow);
236 55 robfinch
parameter N=33;
237 54 robfinch
input rst;
238
input clk;
239
input ce;
240
input ld;
241
input op;
242 55 robfinch
input  [N*4+16+4-1:0] a, b;
243
output [N*4+16+4-1:0] o;
244 54 robfinch
input [2:0] rm;
245
output sign_exe;
246
output done;
247
output inf;
248
output overflow;
249
output underflow;
250
 
251 55 robfinch
wire [(N+1)*4*2+16+4-1:0] o1;
252 54 robfinch
wire sign_exe1, inf1, overflow1, underflow1;
253 55 robfinch
wire [N*4+16+4-1+4:0] fpn0;
254 54 robfinch
wire done1, done1a;
255
 
256 55 robfinch
DFPDivide    #(.N(N)) u1 (rst, clk, ce, ld, op, a, b, o1, done1, sign_exe1, overflow1, underflow1);
257
DFPNormalize #(.N(N)) u2(.clk(clk), .ce(ce), .under_i(underflow1), .i(o1), .o(fpn0) );
258
DFPRound     #(.N(N)) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
259 54 robfinch
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
260
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
261
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
262
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
263
delay   #(.WID(1),.DEP(11))   u8(.clk(clk), .ce(ce), .i(done1), .o(done1a));
264
assign done = done1&done1a;
265
 
266
endmodule
267
 

powered by: WebSVN 2.1.0

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