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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [rtl/] [common/] [FT64_divider.v] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2013-2017  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// This source file is free software: you can redistribute it and/or modify 
9
// it under the terms of the GNU Lesser General Public License as published 
10
// by the Free Software Foundation, either version 3 of the License, or     
11
// (at your option) any later version.                                      
12
//                                                                          
13
// This source file is distributed in the hope that it will be useful,      
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
16
// GNU General Public License for more details.                             
17
//                                                                          
18
// You should have received a copy of the GNU General Public License        
19
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
20
//
21
//
22
// FT64 Superscaler
23
// FT64_divider.v
24
//  - 64 bit divider
25
//
26
// ============================================================================
27
//
28
module FT64_divider(rst, clk, ld, abort, sgn, sgnus, a, b, qo, ro, dvByZr, done, idle);
29
parameter WID=64;
30
parameter DIV=3'd3;
31
parameter IDLE=3'd4;
32
parameter DONE=3'd5;
33
input clk;
34
input rst;
35
input ld;
36
input abort;
37
input sgn;
38
input sgnus;
39
input [WID-1:0] a;
40
input [WID-1:0] b;
41
output [WID-1:0] qo;
42
reg [WID-1:0] qo;
43
output [WID-1:0] ro;
44
reg [WID-1:0] ro;
45
output done;
46
output idle;
47
output dvByZr;
48
reg dvByZr;
49
 
50
reg [WID-1:0] bb;
51
reg so;
52
reg [2:0] state;
53
reg [7:0] cnt;
54
wire cnt_done = cnt==8'd0;
55
assign done = state==DONE||(state==IDLE && !ld);
56
assign idle = state==IDLE;
57
reg ce1;
58
reg [WID-1:0] q;
59
reg [WID:0] r;
60
wire b0 = bb <= r;
61
wire [WID-1:0] r1 = b0 ? r - bb : r;
62
 
63
initial begin
64
    q = 64'd0;
65
    r = 64'd0;
66
    qo = 64'd0;
67
    ro = 64'd0;
68
end
69
 
70
always @(posedge clk)
71
if (rst) begin
72
        bb <= {WID{1'b0}};
73
        q <= {WID{1'b0}};
74
        r <= {WID{1'b0}};
75
        qo <= {WID{1'b0}};
76
        ro <= {WID{1'b0}};
77
        cnt <= 8'd0;
78
        dvByZr <= 1'b0;
79
        state <= IDLE;
80
end
81
else
82
begin
83
if (abort)
84
    cnt <= 8'd00;
85
else if (!cnt_done)
86
        cnt <= cnt - 8'd1;
87
 
88
case(state)
89
IDLE:
90
        if (ld) begin
91
                if (sgn) begin
92
                        q <= a[WID-1] ? -a : a;
93
                        bb <= b[WID-1] ? -b : b;
94
                        so <= a[WID-1] ^ b[WID-1];
95
                end
96
                else if (sgnus) begin
97
                        q <= a[WID-1] ? -a : a;
98
            bb <= b;
99
            so <= a[WID-1];
100
                end
101
                else begin
102
                        q <= a;
103
                        bb <= b;
104
                        so <= 1'b0;
105
                        $display("bb=%d", b);
106
                end
107
                dvByZr <= b=={WID{1'b0}};
108
                r <= {WID{1'b0}};
109
                cnt <= WID+1;
110
                state <= DIV;
111
        end
112
DIV:
113
        if (!cnt_done) begin
114
                $display("cnt:%d r1=%h q[63:0]=%h", cnt,r1,q);
115
                q <= {q[WID-2:0],b0};
116
                r <= {r1,q[WID-1]};
117
                if (dvByZr)
118
                        cnt <= 8'h00;
119
        end
120
        else begin
121
                $display("cnt:%d r1=%h q[63:0]=%h", cnt,r1,q);
122
                if (sgn|sgnus) begin
123
                        if (so) begin
124
                                qo <= dvByZr ? {1'b1,{WID-1{1'b0}}} : -q;
125
                                ro <= dvByZr ? {1'b1,{WID-1{1'b0}}} : -r[WID:1];
126
                        end
127
                        else begin
128
                                qo <= dvByZr ? {WID-1{1'b1}} : q;
129
                                ro <= dvByZr ? {WID-1{1'b1}} : r[WID:1];
130
                        end
131
                end
132
                else begin
133
                        qo <= dvByZr ? {WID-1{1'b1}} : q;
134
                        ro <= dvByZr ? {WID-1{1'b1}} : r[WID:1];
135
                end
136
                state <= DONE;
137
        end
138
DONE:
139
        state <= IDLE;
140
default: state <= IDLE;
141
endcase
142
end
143
 
144
endmodule
145
 
146
module FT64_divider_tb();
147
parameter WID=64;
148
reg rst;
149
reg clk;
150
reg ld;
151
wire done;
152
wire [WID-1:0] qo,ro;
153
 
154
initial begin
155
        clk = 1;
156
        rst = 0;
157
        #100 rst = 1;
158
        #100 rst = 0;
159
        #100 ld = 1;
160
        #150 ld = 0;
161
end
162
 
163
always #10 clk = ~clk;  //  50 MHz
164
 
165
 
166
FT64_divider #(WID) u1
167
(
168
        .rst(rst),
169
        .clk(clk),
170
        .ld(ld),
171
        .sgn(1'b1),
172
        .isDivi(1'b0),
173
        .a(64'd10005),
174
        .b(64'd27),
175
        .imm(64'd123),
176
        .qo(qo),
177
        .ro(ro),
178
        .dvByZr(),
179
        .done(done)
180
);
181
 
182
endmodule
183
 

powered by: WebSVN 2.1.0

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