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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [dfdiv2.sv] - Blame information for rev 85

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

Line No. Rev Author Line
1 80 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2022  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      dfdiv2.v
9
//    Decimal Float divider primitive
10
//
11
// BSD 3-Clause License
12
// Redistribution and use in source and binary forms, with or without
13
// modification, are permitted provided that the following conditions are met:
14
//
15
// 1. Redistributions of source code must retain the above copyright notice, this
16
//    list of conditions and the following disclaimer.
17
//
18
// 2. Redistributions in binary form must reproduce the above copyright notice,
19
//    this list of conditions and the following disclaimer in the documentation
20
//    and/or other materials provided with the distribution.
21
//
22
// 3. Neither the name of the copyright holder nor the names of its
23
//    contributors may be used to endorse or promote products derived from
24
//    this software without specific prior written permission.
25
//
26
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// ============================================================================
38
 
39
module dfdiv2(clk, ld, a, b, q, r, done, lzcnt);
40
parameter N=33;
41
localparam FPWID = N*4;
42
parameter RADIX = 10;
43
localparam FPWID1 = FPWID;//((FPWID+2)/3)*3;    // make FPWIDth a multiple of three
44
localparam DMSB = FPWID1-1;
45
input clk;
46
input ld;
47
input [FPWID-1:0] a;
48
input [FPWID-1:0] b;
49
output reg [FPWID*2-1:0] q;
50
output reg [FPWID-1:0] r;
51
output reg done;
52
output reg [7:0] lzcnt; // Leading zero digit count
53
 
54
 
55
reg [2:0] st;
56
parameter IDLE = 3'd0;
57
parameter DDIN1 = 3'd1;
58
parameter DDIN2 = 3'd2;
59
parameter CALC = 3'd3;
60
parameter DDO = 3'd4;
61
parameter DONE = 3'd5;
62
parameter DDIN3 = 3'd6;
63
 
64
reg rstdd;
65
reg ldddi,lddiv,ldddo;
66
reg [1:0] ddidone,ddodone;
67
wire ddadone,ddbdone,divdone,ddoqdone,ddordone;
68
wire [N*4-1:0] dda, ddb;
69
wire [N*4*2-1:0] qdiv;
70
wire [N*4-1:0] rdiv;
71
wire [N*4*2-1:0] qo,qo1;
72
wire [N*4-1:0] ro;
73
 
74
DDBCDToBin #(.WID(N*4)) uddi1
75
(
76
        .rst(rstdd),
77
        .clk(clk),
78
        .ld(ldddi),
79
        .bcd(a),
80
        .bin(dda),
81
        .done(ddadone)
82
);
83
 
84
DDBCDToBin #(.WID(N*4)) uddi2
85
(
86
        .rst(rstdd),
87
        .clk(clk),
88
        .ld(ldddi),
89
        .bcd(b),
90
        .bin(ddb),
91
        .done(ddbdone)
92
);
93
 
94
fpdivr2 #(.FPWID(N*4)) udiv1
95
(
96
        .clk_div(clk),
97
        .ld(lddiv),
98
        .a({{N*4{1'b0}},dda}),
99
        .b({{N{1'b0}},ddb}),
100
        .q(qdiv),
101
        .r(rdiv),
102
        .done(divdone),
103
        .lzcnt()
104
);
105
 
106
 
107
DDBinToBCDFract #(.WID(N*4)) udd3
108
(
109
        .rst(rstdd),
110
        .clk(clk),
111
        .ld(ldddo),
112
        .bin({qdiv[N*4-1:0],4'h0}),
113
        .bcd(qo1),
114
        .done(ddoqdone)
115
);
116
assign qo = {qdiv[N*4+3:N*4],qo1[N*4-1:4]};
117
 
118
DDBinToBCDFract #(.WID(N*4)) udd4
119
(
120
        .rst(rstdd),
121
        .clk(clk),
122
        .ld(ldddo),
123
        .bin(rdiv),
124
        .bcd(ro),
125
        .done(ddordone)
126
);
127
 
128
reg nz;
129
reg [N-1:0] zc;
130
genvar g;
131
generate begin : glzcnt
132
        for (g = N-1; g >= 0; g = g - 1)
133
        always_comb
134
                zc[g] = qo[g*4+3+N*4:g*4+N*4]==0;
135
end
136
endgenerate
137
 
138
integer n;
139
always_comb
140
begin
141
        nz = 1'b0;
142
        lzcnt = 'd0;
143
        for (n = N-1; n >= 0; n = n - 1)
144
        begin
145
                nz = nz | ~zc[n];
146
                if (!nz)
147
                        lzcnt = lzcnt + 1;
148
        end
149
end
150
 
151
always_comb
152
        lddiv <= ddidone==2'b11 && st==DDIN3;
153
always_comb
154
        ldddo <= divdone==1'b1 && st==CALC;
155
 
156
always_ff @(posedge clk)
157
begin
158
        rstdd <= 1'b0;
159
        ldddi <= 1'b0;
160
case(st)
161
IDLE:   ;
162
DDIN1:
163
        begin
164
                ldddi <= 1'b1;
165
                st <= DDIN2;
166
        end
167
DDIN2:
168
        st <= DDIN3;
169
DDIN3:
170
        begin
171
                if (ddadone) ddidone <= ddidone | {ddbdone,ddadone};
172
                if (ddidone==2'b11)
173
                        st <= CALC;
174
        end
175
CALC:
176
        if (divdone)
177
                st <= DDO;
178
DDO:
179
        begin
180
                if (ddoqdone) ddodone <= ddodone | {ddordone,ddoqdone};
181
                q <= qo;
182
                r <= ro;
183
                if (ddodone==2'b11)
184
                        st <= DONE;
185
        end
186
DONE:
187
        begin
188
                done <= 1'b1;
189
        end
190
default:
191
        st <= IDLE;
192
endcase
193
if (ld) begin
194
        done <= 1'b0;
195
        rstdd <= 1'b1;
196
        ddidone <= 'd0;
197
        ddodone <= 'd0;
198
        st <= DDIN1;
199
end
200
end
201
 
202
endmodule
203
 
204
module dfdiv2_tb();
205
 
206
reg clk;
207
reg ld;
208
reg [135:0] a, b;
209
wire [271:0] q;
210
wire [135:0] r;
211
wire [7:0] lzcnt;
212
 
213
initial begin
214
        clk = 1'b0;
215
        ld = 1'b0;
216
        a = 136'h99_99999999_00000000_00000000_00000000;
217
        b = 136'h50_00000000_00000000_00000000_00000000;
218
        #20 ld = 1'b1;
219
        #40 ld = 1'b0;
220
end
221
 
222
always #5 clk = ~clk;
223
 
224
dfdiv2 #(.N(34)) u1 (
225
        .clk(clk),
226
        .ld(ld),
227
        .a(a),
228
        .b(b),
229
        .q(q),
230
        .r(r),
231
        .done(done),
232
        .lzcnt(lzcnt)
233
);
234
endmodule

powered by: WebSVN 2.1.0

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