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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64/] [rtl/] [common/] [FT64_divider.v] - Blame information for rev 43

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 43 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
        end
118
        else begin
119
                $display("cnt:%d r1=%h q[63:0]=%h", cnt,r1,q);
120
                if (sgn|sgnus) begin
121
                        if (so) begin
122
                                qo <= -q;
123
                                ro <= -r[WID:1];
124
                        end
125
                        else begin
126
                                qo <= q;
127
                                ro <= r[WID:1];
128
                        end
129
                end
130
                else begin
131
                        qo <= q;
132
                        ro <= r[WID:1];
133
                end
134
                state <= DONE;
135
        end
136
DONE:
137
        state <= IDLE;
138
default: state <= IDLE;
139
endcase
140
end
141
 
142
endmodule
143
 
144
module FT64_divider_tb();
145
parameter WID=64;
146
reg rst;
147
reg clk;
148
reg ld;
149
wire done;
150
wire [WID-1:0] qo,ro;
151
 
152
initial begin
153
        clk = 1;
154
        rst = 0;
155
        #100 rst = 1;
156
        #100 rst = 0;
157
        #100 ld = 1;
158
        #150 ld = 0;
159
end
160
 
161
always #10 clk = ~clk;  //  50 MHz
162
 
163
 
164
FT64_divider #(WID) u1
165
(
166
        .rst(rst),
167
        .clk(clk),
168
        .ld(ld),
169
        .sgn(1'b1),
170
        .isDivi(1'b0),
171
        .a(64'd10005),
172
        .b(64'd27),
173
        .imm(64'd123),
174
        .qo(qo),
175
        .ro(ro),
176
        .dvByZr(),
177
        .done(done)
178
);
179
 
180
endmodule
181
 

powered by: WebSVN 2.1.0

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