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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpSqrt.v] - Blame information for rev 77

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

Line No. Rev Author Line
1 29 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2018-2019  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      fpSqrt.v
9
//    - floating point square root
10
//    - parameterized width
11
//    - 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
// ============================================================================
30
 
31
`include "fpConfig.sv"
32
`include "fp_defines.v"
33
 
34
module fpSqrt(rst, clk, ce, ld, a, o, done, sqrinf, sqrneg);
35
parameter FPWID = 32;
36
`include "fpSize.sv"
37
localparam pShiftAmt =
38
        FPWID==80 ? 48 :
39
        FPWID==64 ? 36 :
40
        FPWID==32 ? 7 : (FMSB+1-16);
41
input rst;
42
input clk;
43
input ce;
44
input ld;
45
input [MSB:0] a;
46
output reg [EX:0] o;
47
output done;
48
output sqrinf;
49
output sqrneg;
50
 
51
// registered outputs
52
reg sign_exe;
53
reg inf;
54
reg     overflow;
55
reg     underflow;
56
 
57
wire so;
58
wire [EMSB:0] xo;
59
wire [FX:0] mo;
60
 
61
// constants
62
wire [EMSB:0] infXp = {EMSB+1{1'b1}};    // infinite / NaN - all ones
63
// The following is the value for an exponent of zero, with the offset
64
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
65
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};        //2^0 exponent
66
// The following is a template for a quiet nan. (MSB=1)
67
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
68
 
69
// variables
70
wire [EMSB+2:0] ex1;     // sum of exponents
71
wire [FX:0] sqrto;
72
 
73
// Operands
74
wire sa;                        // sign bit
75
wire [EMSB:0] xa;        // exponent bits
76
wire [FMSB+1:0] fracta;
77
wire a_dn;                      // a/b is denormalized
78
wire az;
79
wire aInf;
80
wire aNan;
81
wire done1;
82
wire [7:0] lzcnt;
83
wire [MSB:0] aa;
84
 
85
// -----------------------------------------------------------
86
// - decode the input operand
87
// - derive basic information
88
// - calculate exponent
89
// - calculate fraction
90
// -----------------------------------------------------------
91
 
92
fpDecompReg #(FPWID) u1
93
(
94
        .clk(clk),
95
        .ce(ce),
96
        .i(a),
97
        .o(aa),
98
        .sgn(sa),
99
        .exp(xa),
100
        .fract(fracta),
101
        .xz(a_dn),
102
        .vz(az),
103
        .inf(aInf),
104
        .nan(aNan)
105
);
106
 
107
assign ex1 = xa + 8'd1;
108
assign so = 1'b0;                               // square root of positive numbers only
109
assign xo = (ex1 >> 1) + (bias >> 1);   // divide by 2 cuts the bias in half, so 1/2 of it is added back in.
110
assign mo = aNan ? {1'b1,aa[FMSB:0],{FMSB+1{1'b0}}} : (sqrto << pShiftAmt);
111
assign sqrinf = aInf;
112
assign sqrneg = !az & so;
113
 
114
wire [FMSB+2:0] fracta1 = ex1[0] ? {1'b0,fracta} << 1 : {2'b0,fracta};
115
 
116
wire ldd;
117
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(ld), .o(ldd));
118
 
119
isqrt #(FX+1) u2
120
(
121
        .rst(rst),
122
        .clk(clk),
123
        .ce(ce),
124
        .ld(ldd),
125
        .a({1'b0,fracta1,{FMSB+1{1'b0}}}),
126
        .o(sqrto),
127
        .done(done)
128
);
129
 
130
always @*
131
casez({aNan,sqrinf,sqrneg})
132
3'b1??: o <= {sa,xa,mo};
133
3'b01?: o <= {sa,1'b1,qNaN|`QSQRTINF,{FMSB+1{1'b0}}};
134
3'b001: o <= {sa,1'b1,qNaN|`QSQRTNEG,{FMSB+1{1'b0}}};
135
default:        o <= {so,xo,mo};
136
endcase
137
 
138
 
139
endmodule
140
 
141
module fpSqrtnr(rst, clk, ce, ld, a, o, rm, done, inf, sqrinf, sqrneg);
142
parameter FPWID=32;
143
`include "fpSize.sv"
144
 
145
input rst;
146
input clk;
147
input ce;
148
input ld;
149
input  [MSB:0] a;
150
output [MSB:0] o;
151
input [2:0] rm;
152
output done;
153
output inf;
154
output sqrinf;
155
output sqrneg;
156
 
157
wire [EX:0] o1;
158
wire inf1;
159
wire [MSB+3:0] fpn0;
160
wire done1;
161
 
162
fpSqrt      #(FPWID) u1 (rst, clk, ce, ld, a, o1, done1, sqrinf, sqrneg);
163
fpNormalize #(FPWID) u2(.clk(clk), .ce(ce), .under_i(1'b0), .i(o1), .o(fpn0) );
164
fpRound  #(FPWID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
165
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
166 34 robfinch
vtdl            #(1)   u8(.clk(clk), .ce(ce), .a(4'd10), .d(done1), .q(done));
167 29 robfinch
endmodule
168
 

powered by: WebSVN 2.1.0

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