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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [DivGoldschmidt.v] - Blame information for rev 15

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

Line No. Rev Author Line
1 14 robfinch
`timescale 1ns / 1ps
2
// ============================================================================
3
//        __
4
//   \\__/ o\    (C) 2017-2018  Robert Finch, Waterloo
5
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch<remove>@finitron.ca
7
//       ||
8
//
9
//      DivGoldschmidt.v
10
//              
11
//
12
// This source file is free software: you can redistribute it and/or modify 
13
// it under the terms of the GNU Lesser General Public License as published 
14
// by the Free Software Foundation, either version 3 of the License, or     
15
// (at your option) any later version.                                      
16
//                                                                          
17
// This source file is distributed in the hope that it will be useful,      
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
20
// GNU General Public License for more details.                             
21
//                                                                          
22
// You should have received a copy of the GNU General Public License        
23
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
24
//                                                                          
25
//
26
// ============================================================================
27
//
28
module DivGoldschmidt(rst, clk, ld, a, b, q, f0, done, lzcnt);
29
parameter WID=32;
30
parameter WHOLE=16;
31
parameter POINTS=16;
32
parameter LEFT=1'b1;
33
localparam SIZE=WID+WHOLE;
34
localparam POINTS2 = POINTS+WHOLE;
35
input rst;
36
input clk;
37
input ld;
38
input [WID-1:0] a;
39
input [WID-1:0] b;
40
output reg [WID*2-1:0] q;
41
output reg [SIZE-1:0] f0;
42
output reg done;
43
output reg [7:0] lzcnt;
44
parameter IDLE = 2'd0;
45
parameter DIV = 2'd1;
46
parameter DONE = 2'd2;
47
parameter DIV2 = 2'd3;
48
 
49
integer n;
50
// Scale D so it is between 0 < D < 1 (shift)
51
reg [SIZE-1:0] F;
52
reg [SIZE*3-1:0] N, D;
53
wire [SIZE*3-1:0] N1, D1;
54
assign N1 = N * F;
55
assign D1 = D * F;
56
reg [1:0] state = IDLE;
57
reg [7:0] count = 0;
58
reg [7:0] lzcnt2;
59
wire [7:0] shft;
60
 
61
// Count the leading zeros on the b side input. Determines how much
62
// shifting is required.
63
always @*
64
begin
65
        lzcnt2 = 8'd0;
66
        if (b[WID-1]==1'b0)
67
                for (n = WID-2; n >= 0; n = n - 1)
68
                        if(b[n] && lzcnt2==8'd0)
69
                                lzcnt2 = (WID-1)-n;
70
end
71
 
72
 
73
// Count the leading zeros in the output. the float divider uses this.
74
always @*
75
begin
76
        lzcnt = 8'd0;
77
        if (q[WID*2-1]==1'b0)
78
                for (n = WID*2-2; n >= 0; n = n - 1)
79
                        if(q[n] && lzcnt==8'd0)
80
                                lzcnt = (WID*2-1)-n;
81
end
82
 
83
wire shift_left = lzcnt2 > WHOLE;
84
assign shft = shift_left ? lzcnt2-WHOLE : WHOLE-lzcnt2;
85
//assign done = (state==IDLE && !ld)||state==DONE;
86
 
87
always @(posedge clk)
88
if (rst) begin
89
        done <= 1'b0;
90
        count <= 6'd0;
91
        state <= IDLE;
92
end
93
else begin
94
        done <= 1'b0;
95
case(state)
96
IDLE:
97
        begin
98
                if (ld) begin
99
                        // Shifting the numerator and denomintor right or left using a barrel
100
                        // or funnel shifter is what gives Goldschmidt a lot of it's performance.
101
                        // Most of the divide is being performed by shifting.
102
                        // For most floating point numbers shifting left isn't required as the
103
                        // number is always between 1.0 and 2.0. Instead typically only a single
104
                        // shift to the right is required. For fixed point numbers however, we
105
                        // probably want to be able to shift left, hence the LEFT parameter.
106
                        // With no left shifting the only impact is for denormal numbers which
107
                        // take longer for the divide to converge.
108
                        if (shift_left) begin
109
                                if (LEFT) begin
110
                                        N <= {16'd0,a,{WHOLE{1'b0}}} << shft;
111
                                        D <= {16'd0,b,{WHOLE{1'd0}}} << shft;
112
                                        F <= {16'd2,{POINTS2{1'b0}}} - ({b,{WHOLE{1'd0}}} << shft);
113
                                        f0 <= {16'd2,{POINTS2{1'b0}}} - ({b,{WHOLE{1'd0}}} << shft);
114
                                end
115
                                else begin
116
                                        N <= {16'd0,a,{WHOLE{1'b0}}};
117
                                        D <= {16'd0,b,{WHOLE{1'd0}}};
118
                                        F <= {16'd2,{POINTS2{1'b0}}} - ({b,{WHOLE{1'd0}}});
119
                                        f0 <= {16'd2,{POINTS2{1'b0}}} - ({b,{WHOLE{1'd0}}});
120
                                end
121
                        end
122
                        else begin
123
                                N <= {16'd0,a,{WHOLE{1'b0}}} >> shft;
124
                                D <= {16'd0,b,{WHOLE{1'd0}}} >> shft;
125
                                F <= {16'd2,{POINTS2{1'b0}}} - ({b,{WHOLE{1'd0}}} >> shft);
126
                                f0 <= {16'd2,{POINTS2{1'b0}}} - ({b,{WHOLE{1'd0}}} >> shft);
127
                        end
128
                        count <= 0;
129
                        state <= DIV;
130
                end
131
        end
132
DIV:
133
        begin
134
                $display("C: %d N: %x D: %x F: %x", count, N,D,F);
135
                N <= N1[SIZE*3-1:POINTS2] + N1[POINTS2-1];
136
                D <= D1[SIZE*3-1:POINTS2] + D1[POINTS2-1];
137
                F <= {16'd2,{POINTS2{1'd0}}} - (D1[SIZE*3-1:POINTS2] + D1[POINTS2-1]);
138
//              q <= N1[SIZE*2-1:POINTS2] + N1[POINTS2-1];
139
                if (D[SIZE*3-1:0]=={2'h1,{POINTS2{1'd0}}})
140
                        state <= DONE;
141
                count <= count + 1;
142
        end
143
DONE:
144
        begin
145
                done <= 1'b1;
146
                q <= N[SIZE*3-1:0];
147
                state <= IDLE;
148
        end
149
endcase
150
end
151
 
152
endmodule
153
 
154
module G_divider_tb();
155
parameter WID=4;
156
reg rst;
157
reg clk;
158
reg ld;
159
wire done;
160
wire [WID*2-1:0] qo;
161
wire [7:0] f0;
162
reg [3:0] state;
163
reg [3:0] a, b;
164
reg [7:0] count;
165
 
166
initial begin
167
        clk = 1;
168
        rst = 0;
169
        #100 rst = 1;
170
        #100 rst = 0;
171
        #100 ld = 1;
172
        #150 ld = 0;
173
end
174
 
175
always #10 clk = ~clk;  //  50 MHz
176
 
177
always @(posedge clk)
178
if (rst) begin
179
        state <= 3'd0;
180
        count = 0;
181
end
182
else begin
183
case(state)
184
3'd0:
185
        begin
186
                ld <= 1;
187
                a <= count[7:4];
188
                b <= count[3:0];
189
        end
190
3'd1:
191
        if (done) begin
192
                $display("C: %x Q: %x  f: %x", count, qo, f0);
193
                state <= 3'd2;
194
        end
195
3'd2:
196
        begin
197
                count <= count + 8'd1;
198
                state <= 3'd0;
199
        end
200
endcase
201
end
202
 
203
DivGoldschmidt #(.WID(WID),.WHOLE(1),.POINTS(3)) u00
204
(
205
        .rst(rst),
206
        .clk(clk),
207
        .ld(ld),
208
//      .sgn(1'b1),
209
//      .isDivi(1'b0),
210
        .a(a),
211
        .b(b),
212
//      .imm(64'd123),
213
        .q(qo),
214
        .f0(f0),
215
//      .ro(ro),
216
//      .dvByZr(),
217
        .left_right(),
218
        .shift(),
219
        .done(done)
220
);
221
 
222
endmodule
223
 

powered by: WebSVN 2.1.0

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