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

Subversion Repositories ft816float

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

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 10 robfinch
//   \\__/ o\    (C) 2006-2018  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 6 robfinch
module fpDiv(clk, ce, ld, a, b, o, done, sign_exe, overflow, underflow);
37
 
38 8 robfinch
parameter WID = 128;
39 6 robfinch
localparam MSB = WID-1;
40 8 robfinch
localparam EMSB = WID==128 ? 14 :
41
                  WID==96 ? 14 :
42
                  WID==80 ? 14 :
43 6 robfinch
                  WID==64 ? 10 :
44
                                  WID==52 ? 10 :
45 10 robfinch
                                  WID==48 ? 11 :
46 6 robfinch
                                  WID==44 ? 10 :
47
                                  WID==42 ? 10 :
48
                                  WID==40 ?  9 :
49
                                  WID==32 ?  7 :
50
                                  WID==24 ?  6 : 4;
51 8 robfinch
localparam FMSB = WID==128 ? 111 :
52
                  WID==96 ? 79 :
53
                  WID==80 ? 63 :
54 6 robfinch
                  WID==64 ? 51 :
55
                                  WID==52 ? 39 :
56 10 robfinch
                                  WID==48 ? 34 :
57 6 robfinch
                                  WID==44 ? 31 :
58
                                  WID==42 ? 29 :
59
                                  WID==40 ? 28 :
60
                                  WID==32 ? 22 :
61
                                  WID==24 ? 15 : 9;
62
 
63 8 robfinch
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
64
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
65 6 robfinch
 
66
input clk;
67
input ce;
68
input ld;
69
input [MSB:0] a, b;
70 8 robfinch
output [EX:0] o;
71 6 robfinch
output done;
72
output sign_exe;
73
output overflow;
74
output underflow;
75
 
76
// registered outputs
77
reg sign_exe;
78
reg inf;
79
reg     overflow;
80
reg     underflow;
81
 
82
reg so;
83
reg [EMSB:0] xo;
84 8 robfinch
reg [FX:0] mo;
85 6 robfinch
assign o = {so,xo,mo};
86
 
87
// constants
88
wire [EMSB:0] infXp = {EMSB+1{1'b1}};    // infinite / NaN - all ones
89
// The following is the value for an exponent of zero, with the offset
90
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
91
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};        //2^0 exponent
92
// The following is a template for a quiet nan. (MSB=1)
93
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
94
 
95
// variables
96
wire [EMSB+2:0] ex1;     // sum of exponents
97 10 robfinch
wire [(FMSB+9)*2-1:0] divo;
98 6 robfinch
 
99
// Operands
100
wire sa, sb;                    // sign bit
101
wire [EMSB:0] xa, xb;    // exponent bits
102
wire [FMSB+1:0] fracta, fractb;
103
wire a_dn, b_dn;                        // a/b is denormalized
104
wire az, bz;
105
wire aInf, bInf;
106 8 robfinch
wire aNan,bNan;
107 10 robfinch
wire done1;
108
wire [7:0] lzcnt;
109 6 robfinch
 
110
// -----------------------------------------------------------
111
// - decode the input operands
112
// - derive basic information
113
// - calculate exponent
114
// - calculate fraction
115
// -----------------------------------------------------------
116
 
117 8 robfinch
fpDecomp #(WID) u1a (.i(a), .sgn(sa), .exp(xa), .fract(fracta), .xz(a_dn), .vz(az), .inf(aInf), .nan(aNan) );
118
fpDecomp #(WID) u1b (.i(b), .sgn(sb), .exp(xb), .fract(fractb), .xz(b_dn), .vz(bz), .inf(bInf), .nan(bNan) );
119 6 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 10 robfinch
assign ex1 = (xa|a_dn) - (xb|b_dn) + bias + FMSB + 9 - lzcnt;
125 6 robfinch
 
126
// check for exponent underflow/overflow
127
wire under = ex1[EMSB+2];       // MSB set = negative exponent
128
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
129
 
130
// Perform divide
131
// could take either 1 or 16 clock cycles
132 10 robfinch
fpdivr8 #(FMSB+9,2) u2 (.clk(clk), .ld(ld), .a({fracta,7'b0}), .b({fractb,7'b0}), .q(divo), .r(), .done(done1), .lzcnt(lzcnt));
133
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(done1), .o(done));
134 6 robfinch
 
135 10 robfinch
 
136 6 robfinch
// determine when a NaN is output
137
wire qNaNOut = (az&bz)|(aInf&bInf);
138 10 robfinch
wire [(FMSB+9)*2-1:0] divo1 = divo[(FMSB+9)*2-1:0] << (lzcnt-2);
139 6 robfinch
 
140
always @(posedge clk)
141
        if (ce) begin
142 10 robfinch
                if (done1) begin
143
                        casez({qNaNOut|aNan|bNan,bInf,bz,over,under})
144
                        5'b1????:               xo = infXp;     // NaN exponent value
145
                        5'b01???:               xo = 0;          // divide by inf
146
                        5'b001??:               xo = infXp;     // divide by zero
147
                        5'b0001?:               xo = infXp;     // overflow
148
                        5'b00001:               xo = 0;          // underflow
149
                        default:                xo = ex1;       // normal or underflow: passthru neg. exp. for normalization
150 6 robfinch
                        endcase
151
 
152 10 robfinch
                        casez({aNan,bNan,qNaNOut,bInf,bz,over})
153
                        6'b1?????:      mo = {1'b1,a[FMSB:0],{FMSB+1{1'b0}}};
154
                        6'b01????:      mo = {1'b1,b[FMSB:0],{FMSB+1{1'b0}}};
155
                        6'b001???:              mo = {1'b1,qNaN[FMSB:0]|{aInf,1'b0}|{az,bz},{FMSB+1{1'b0}}};
156
                        6'b0001??:              mo = 0;  // div by inf
157
                        6'b00001?:              mo = 0;  // div by zero
158
                        6'b000001:              mo = 0;  // Inf exponent
159
                        default:                mo = divo1[(FMSB+9)*2-1:14];    // plain div
160 6 robfinch
                        endcase
161
 
162
                        so              = sa ^ sb;
163
                        sign_exe        = sa & sb;
164
                        overflow        = over;
165
                        underflow       = under;
166
                end
167
        end
168
 
169
endmodule
170 10 robfinch
 
171
module fpDivnr(clk, ce, ld, a, b, o, rm, done, sign_exe, inf, overflow, underflow);
172
parameter WID=32;
173
localparam MSB = WID-1;
174
localparam EMSB = WID==128 ? 14 :
175
                  WID==96 ? 14 :
176
                  WID==80 ? 14 :
177
                  WID==64 ? 10 :
178
                                  WID==52 ? 10 :
179
                                  WID==48 ? 11 :
180
                                  WID==44 ? 10 :
181
                                  WID==42 ? 10 :
182
                                  WID==40 ?  9 :
183
                                  WID==32 ?  7 :
184
                                  WID==24 ?  6 : 4;
185
localparam FMSB = WID==128 ? 111 :
186
                  WID==96 ? 79 :
187
                  WID==80 ? 63 :
188
                  WID==64 ? 51 :
189
                                  WID==52 ? 39 :
190
                                  WID==48 ? 34 :
191
                                  WID==44 ? 31 :
192
                                  WID==42 ? 29 :
193
                                  WID==40 ? 28 :
194
                                  WID==32 ? 22 :
195
                                  WID==24 ? 15 : 9;
196
 
197
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
198
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
199
input clk;
200
input ce;
201
input ld;
202
input  [MSB:0] a, b;
203
output [MSB:0] o;
204
input [2:0] rm;
205
output sign_exe;
206
output done;
207
output inf;
208
output overflow;
209
output underflow;
210
 
211
wire [EX:0] o1;
212
wire sign_exe1, inf1, overflow1, underflow1;
213
wire [MSB+3:0] fpn0;
214
wire done1;
215
 
216
fpDiv       #(WID) u1 (clk, ce, ld, a, b, o1, done1, sign_exe1, overflow1, underflow1);
217
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(underflow1), .i(o1), .o(fpn0) );
218
fpRoundReg  #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
219
delay2      #(1)   u4(.clk(clk), .ce(ce), .i(sign_exe1), .o(sign_exe));
220
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
221
delay2      #(1)   u6(.clk(clk), .ce(ce), .i(overflow1), .o(overflow));
222
delay2      #(1)   u7(.clk(clk), .ce(ce), .i(underflow1), .o(underflow));
223
delay2          #(1)   u8(.clk(clk), .ce(ce), .i(done1), .o(done));
224
endmodule
225
 

powered by: WebSVN 2.1.0

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