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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpDiv.v] - Blame information for rev 36

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

Line No. Rev Author Line
1 29 robfinch
// ============================================================================
2
//        __
3 34 robfinch
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4 29 robfinch
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      fpDiv.v
9
//    - floating point divider
10 34 robfinch
//    - parameterized FPWIDth
11 29 robfinch
//    - IEEE 754 representation
12
//
13
//
14
// This source file is free software: you can redistribute it and/or modify 
15
// it under the terms of the GNU Lesser General Public License as published 
16
// by the Free Software Foundation, either version 3 of the License, or     
17
// (at your option) any later version.                                      
18
//                                                                          
19
// This source file is distributed in the hope that it will be useful,      
20
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
21
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
22
// GNU General Public License for more details.                             
23
//                                                                          
24
// You should have received a copy of the GNU General Public License        
25
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
26
//                                                                          
27
//      Floating Point Multiplier / Divider
28
//
29
//Properties:
30
//+-inf * +-inf = -+inf    (this is handled by exOver)
31
//+-inf * 0     = QNaN
32
//+-0 / +-0      = QNaN
33
// ============================================================================
34
 
35
`include "fpConfig.sv"
36 34 robfinch
`include "fpDefines.v"
37 29 robfinch
//`define GOLDSCHMIDT   1'b1
38
 
39
module fpDiv(rst, clk, clk4x, ce, ld, op, a, b, o, done, sign_exe, overflow, underflow);
40
 
41 32 robfinch
parameter FPWID = 64;
42 29 robfinch
`include "fpSize.sv"
43 32 robfinch
// FADD is a constant that makes the divider width a multiple of four and includes eight extra bits.                    
44
localparam FADD = FPWID==128 ? 9 :
45
                                  FPWID==96 ? 9 :
46
                                  FPWID==84 ? 9 :
47
                                  FPWID==80 ? 9 :
48
                                  FPWID==64 ? 13 :
49 34 robfinch
                                  FPWID==52 ? 13 :
50 32 robfinch
                                  FPWID==48 ? 10 :
51
                                  FPWID==44 ? 9 :
52
                                  FPWID==42 ? 11 :
53
                                  FPWID==40 ? 8 :
54
                                  FPWID==32 ? 10 :
55
                                  FPWID==24 ? 9 : 11;
56 29 robfinch
 
57
input rst;
58
input clk;
59
input clk4x;
60
input ce;
61
input ld;
62
input op;
63
input [MSB:0] a, b;
64
output [EX:0] o;
65
output done;
66
output sign_exe;
67
output overflow;
68
output underflow;
69
 
70
// registered outputs
71
reg sign_exe=0;
72
reg inf=0;
73
reg     overflow=0;
74
reg     underflow=0;
75
 
76
reg so;
77
reg [EMSB:0] xo;
78
reg [FX:0] mo;
79
assign o = {so,xo,mo};
80
 
81
// constants
82
wire [EMSB:0] infXp = {EMSB+1{1'b1}};    // infinite / NaN - all ones
83
// The following is the value for an exponent of zero, with the offset
84
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
85
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};        //2^0 exponent
86
// The following is a template for a quiet nan. (MSB=1)
87
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
88
 
89
// variables
90 35 robfinch
reg [EMSB+2:0] ex1;      // sum of exponents
91 29 robfinch
`ifndef GOLDSCHMIDT
92
wire [(FMSB+FADD)*2-1:0] divo;
93
`else
94
wire [(FMSB+5)*2-1:0] divo;
95
`endif
96
 
97
// Operands
98
wire sa, sb;                    // sign bit
99
wire [EMSB:0] xa, xb;    // exponent bits
100
wire [FMSB+1:0] fracta, fractb;
101
wire a_dn, b_dn;                        // a/b is denormalized
102
wire az, bz;
103
wire aInf, bInf;
104
wire aNan,bNan;
105
wire done1;
106
wire signed [7:0] lzcnt;
107
 
108
// -----------------------------------------------------------
109
// - decode the input operands
110
// - derive basic information
111
// - calculate exponent
112
// - calculate fraction
113
// -----------------------------------------------------------
114
 
115 35 robfinch
wire ld1;
116
fpDecompReg #(FPWID) u1a (.clk(clk), .ce(ce), .i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
117
fpDecompReg #(FPWID) u1b (.clk(clk), .ce(ce), .i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
118
delay1 #(1) u5 (.clk(clk), .ce(ce), .i(ld), .o(ld1));
119 29 robfinch
 
120
// Compute the exponent.
121
// - correct the exponent for denormalized operands
122
// - adjust the difference by the bias (add 127)
123
// - also factor in the different decimal position for division
124 35 robfinch
reg [EMSB+2:0] ex1a;
125
always @(posedge clk)
126
  if (ce) ex1a = (xa|a_dn) - (xb|b_dn) + bias;
127
 
128 29 robfinch
`ifndef GOLDSCHMIDT
129 35 robfinch
always @(posedge clk)
130
  if (ce) ex1 = ex1a + FMSB + (FADD-1) - lzcnt - 8'd1;
131 29 robfinch
`else
132 35 robfinch
  if (ce) ex1 = ex1a + FMSB - lzcnt + 8'd4;
133 29 robfinch
`endif
134
 
135 35 robfinch
 
136 29 robfinch
// check for exponent underflow/overflow
137
wire under = ex1[EMSB+2];       // MSB set = negative exponent
138
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
139
 
140
// Perform divide
141 32 robfinch
// Divider width must be a multiple of four
142 29 robfinch
`ifndef GOLDSCHMIDT
143 35 robfinch
`ifdef DIV_RADIX4
144
fpdivr4 #(FMSB+FADD) u2 (.clk(clk), .ld(ld1), .a({3'b0,fracta,8'b0}), .b({3'b0,fractb,8'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
145
`endif
146
`ifdef DIV_RADIX16
147
fpdivr16 #(FMSB+FADD) u2 (.clk(clk), .ld(ld1), .a({3'b0,fracta,8'b0}), .b({3'b0,fractb,8'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
148
`endif
149 29 robfinch
//fpdivr2 #(FMSB+FADD) u2 (.clk4x(clk4x), .ld(ld), .a({3'b0,fracta,8'b0}), .b({3'b0,fractb,8'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
150 35 robfinch
reg [(FMSB+FADD)*2-1:0] divo1, divo2;
151
reg [7:0] lzcnts;
152 34 robfinch
always @(posedge clk)
153 35 robfinch
  if (ce) lzcnts = lzcnt - 2;
154
always @(posedge clk)
155
  if (ce) divo2 = divo[(FMSB+FADD)*2-1:0];
156
 
157
always @(posedge clk)
158
  if (ce) divo1 = divo2 << lzcnts;
159 29 robfinch
`else
160 34 robfinch
DivGoldschmidt #(.FPWID(FMSB+6),.WHOLE(1),.POINTS(FMSB+5))
161 35 robfinch
        u2 (.rst(rst), .clk(clk), .ld(ld1), .a({fracta,4'b0}), .b({fractb,4'b0}), .q(divo), .done(done1), .lzcnt(lzcnt));
162
reg [(FMSB+6)*2+1:0] divo1, divo2;
163 34 robfinch
always @(posedge clk)
164 35 robfinch
  if (ce) divo2 = divo;
165
always @(posedge clk)
166 34 robfinch
  if (ce) divo1 =
167 35 robfinch
          lzcnt > 8'd5 ? divo2 << (lzcnt-8'd6) :
168
          divo2 >> (8'd6-lzcnt);
169 34 robfinch
          ;
170 29 robfinch
`endif
171 35 robfinch
delay2 #(1) u3 (.clk(clk), .ce(ce), .i(done1), .o(done2));
172
delay3 #(1) u4 (.clk(clk), .ce(ce), .i(done1), .o(done));
173 29 robfinch
 
174
 
175
// determine when a NaN is output
176
wire qNaNOut = (az&bz)|(aInf&bInf);
177
 
178
always @(posedge clk)
179
// Simulation likes to see these values reset to zero on reset. Otherwise the
180
// values propagate in sim as X's.
181
if (rst) begin
182
        xo <= 1'd0;
183
        mo <= 1'd0;
184
        so <= 1'd0;
185
        sign_exe <= 1'd0;
186
        overflow <= 1'd0;
187
        underflow <= 1'd0;
188
end
189
else if (ce) begin
190 34 robfinch
                if (done2) begin
191 29 robfinch
                        casez({qNaNOut|aNan|bNan,bInf,bz,over,under})
192
                        5'b1????:               xo <= infXp;    // NaN exponent value
193
                        5'b01???:               xo <= 1'd0;             // divide by inf
194
                        5'b001??:               xo <= infXp;    // divide by zero
195
                        5'b0001?:               xo <= infXp;    // overflow
196
                        5'b00001:               xo <= 1'd0;             // underflow
197
                        default:                xo <= ex1;      // normal or underflow: passthru neg. exp. for normalization
198
                        endcase
199
 
200
                        casez({aNan,bNan,qNaNOut,bInf,bz,over,aInf&bInf,az&bz})
201 34 robfinch
                        8'b1???????:  mo <= {1'b1,1'b1,a[FMSB-1:0],{FMSB+1{1'b0}}};
202
                        8'b01??????:  mo <= {1'b1,1'b1,b[FMSB-1:0],{FMSB+1{1'b0}}};
203 29 robfinch
                        8'b001?????:    mo <= {1'b1,qNaN[FMSB:0]|{aInf,1'b0}|{az,bz},{FMSB+1{1'b0}}};
204
                        8'b0001????:    mo <= 1'd0;     // div by inf
205
                        8'b00001???:    mo <= 1'd0;     // div by zero
206
                        8'b000001??:    mo <= 1'd0;     // Inf exponent
207
                        8'b0000001?:    mo <= {1'b1,qNaN|`QINFDIV,{FMSB+1{1'b0}}};      // infinity / infinity
208
                        8'b00000001:    mo <= {1'b1,qNaN|`QZEROZERO,{FMSB+1{1'b0}}};    // zero / zero
209
`ifndef GOLDSCHMIDT
210
                        default:                mo <= divo1[(FMSB+FADD)*2-1:(FADD-2)*2-2];      // plain div
211
`else
212
                        default:                mo <= divo1[(FMSB+6)*2+1:2];    // plain div
213
`endif
214
                        endcase
215
 
216
                        so              <= sa ^ sb;
217
                        sign_exe        <= sa & sb;
218
                        overflow        <= over;
219
                        underflow       <= under;
220
                end
221
        end
222
 
223
endmodule
224
 
225
module fpDivnr(rst, clk, clk4x, ce, ld, op, a, b, o, rm, done, sign_exe, inf, overflow, underflow);
226 32 robfinch
parameter FPWID=64;
227 29 robfinch
`include "fpSize.sv"
228
 
229
input rst;
230
input clk;
231
input clk4x;
232
input ce;
233
input ld;
234
input op;
235
input  [MSB:0] a, b;
236
output [MSB:0] o;
237
input [2:0] rm;
238
output sign_exe;
239
output done;
240
output inf;
241
output overflow;
242
output underflow;
243
 
244
wire [EX:0] o1;
245
wire sign_exe1, inf1, overflow1, underflow1;
246
wire [MSB+3:0] fpn0;
247
wire done1;
248
 
249
fpDiv       #(FPWID) u1 (rst, clk, clk4x, ce, ld, op, a, b, o1, done1, sign_exe1, overflow1, underflow1);
250 32 robfinch
fpNormalize #(FPWID) u2(.clk(clk), .ce(ce), .under_i(underflow1), .i(o1), .o(fpn0) );
251 34 robfinch
fpRound                 #(FPWID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
252 29 robfinch
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
253
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
254
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
255
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
256 34 robfinch
delay2            #(1)   u8(.clk(clk), .ce(ce), .i(done1), .o(done));
257 29 robfinch
endmodule
258
 

powered by: WebSVN 2.1.0

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