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

Subversion Repositories ft816float

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

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 8 robfinch
//   \\__/ o\    (C) 2006-2016  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
                                  WID==48 ? 10 :
46
                                  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
                                  WID==48 ? 35 :
57
                                  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 8 robfinch
wire [FX: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 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 8 robfinch
assign ex1 = (xa|a_dn) - (xb|b_dn) + bias + FMSB - 1;
123 6 robfinch
 
124
// check for exponent underflow/overflow
125
wire under = ex1[EMSB+2];       // MSB set = negative exponent
126
wire over = (&ex1[EMSB:0] | ex1[EMSB+1]) & !ex1[EMSB+2];
127
 
128
// Perform divide
129
// could take either 1 or 16 clock cycles
130 8 robfinch
fpdivr8 #(FMSB+2,2) u2 (.clk(clk), .ld(ld), .a({3'b0,fracta}), .b({3'b0,fractb}), .q(divo), .r(), .done(done));
131 6 robfinch
 
132
// determine when a NaN is output
133
wire qNaNOut = (az&bz)|(aInf&bInf);
134
 
135
always @(posedge clk)
136
        if (ce) begin
137
                if (done) begin
138 8 robfinch
                        casex({qNaNOut|aNan|bNan,bInf,bz})
139 6 robfinch
                        3'b1xx:         xo = infXp;     // NaN exponent value
140
                        3'bx1x:         xo = 0;          // divide by inf
141
                        3'bxx1:         xo = infXp;     // divide by zero
142
                        default:        xo = ex1;               // normal or underflow: passthru neg. exp. for normalization
143
                        endcase
144
 
145 8 robfinch
                        casex({aNan,bNan,qNaNOut,bInf,bz})
146
                        5'b1xxxx:       mo = {1'b0,a[FMSB:0],{FMSB+1{1'b0}}};
147
                        5'bx1xxx:       mo = {1'b0,b[FMSB:0],{FMSB+1{1'b0}}};
148
                        5'bxx1xx:               mo = {1'b0,qNaN[FMSB:0]|{aInf,1'b0}|{az,bz},{FMSB+1{1'b0}}};
149
                        5'bxxx1x:               mo = 0;  // div by inf
150
                        5'bxxxx1:               mo = 0;  // div by zero
151
                        default:        mo = divo;      // plain div
152 6 robfinch
                        endcase
153
 
154
                        so              = sa ^ sb;
155
                        sign_exe        = sa & sb;
156
                        overflow        = over;
157
                        underflow       = under;
158
                end
159
        end
160
 
161
endmodule

powered by: WebSVN 2.1.0

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