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