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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpDiv.v] - Blame information for rev 89

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

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

powered by: WebSVN 2.1.0

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