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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v7/] [rtl/] [common/] [FT64_divider.v] - Blame information for rev 60

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 60 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2013-2018  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)
72
        state <= IDLE;
73
else
74
        case(state)
75
        IDLE:
76
                if (ld)
77
                        state <= DIV;
78
        DIV:
79
                if (dvByZr)
80
                        state <= DONE;
81
                else if (cnt_done)
82
                        state <= DONE;
83
        DONE:
84
                state <= IDLE;
85
        default:        state <= IDLE;
86
        endcase
87
 
88
always @(posedge clk)
89
if (rst)
90
        cnt <= 8'h00;
91
else begin
92
        if (abort)
93
          cnt <= 8'd00;
94
        else if (ld)
95
                cnt <= WID+1;
96
        else if (!cnt_done)
97
                cnt <= cnt - 8'd1;
98
end
99
 
100
always @(posedge clk)
101
if (rst)
102
        dvByZr <= 1'b0;
103
else begin
104
        if (ld)
105
                dvByZr <= b=={WID{1'b0}};
106
end
107
 
108
always @(posedge clk)
109
if (rst) begin
110
        bb <= {WID{1'b0}};
111
        q <= {WID{1'b0}};
112
        r <= {WID{1'b0}};
113
        qo <= {WID{1'b0}};
114
        ro <= {WID{1'b0}};
115
end
116
else
117
begin
118
 
119
case(state)
120
IDLE:
121
        if (ld) begin
122
                if (sgn) begin
123
                        q <= a[WID-1] ? -a : a;
124
                        bb <= b[WID-1] ? -b : b;
125
                        so <= a[WID-1] ^ b[WID-1];
126
                end
127
                else if (sgnus) begin
128
                        q <= a[WID-1] ? -a : a;
129
      bb <= b;
130
      so <= a[WID-1];
131
                end
132
                else begin
133
                        q <= a;
134
                        bb <= b;
135
                        so <= 1'b0;
136
                        $display("bb=%d", b);
137
                end
138
                r <= {WID{1'b0}};
139
        end
140
DIV:
141
        if (!cnt_done && !dvByZr) begin
142
                $display("cnt:%d r1=%h q[63:0]=%h", cnt,r1,q);
143
                q <= {q[WID-2:0],b0};
144
                r <= {r1,q[WID-1]};
145
        end
146
        else begin
147
                $display("cnt:%d r1=%h q[63:0]=%h", cnt,r1,q);
148
                if (sgn|sgnus) begin
149
                        if (so) begin
150
                                qo <= dvByZr ? {1'b1,{WID-1{1'b0}}} : -q;
151
                                ro <= dvByZr ? {1'b1,{WID-1{1'b0}}} : -r[WID:1];
152
                        end
153
                        else begin
154
                                qo <= dvByZr ? {WID-1{1'b1}} : q;
155
                                ro <= dvByZr ? {WID-1{1'b1}} : r[WID:1];
156
                        end
157
                end
158
                else begin
159
                        qo <= dvByZr ? {WID-1{1'b1}} : q;
160
                        ro <= dvByZr ? {WID-1{1'b1}} : r[WID:1];
161
                end
162
        end
163
default: ;
164
endcase
165
end
166
 
167
endmodule
168
 
169
module FT64_divider_tb();
170
parameter WID=64;
171
reg rst;
172
reg clk;
173
reg ld;
174
wire done;
175
wire [WID-1:0] qo,ro;
176
 
177
initial begin
178
        clk = 1;
179
        rst = 0;
180
        #100 rst = 1;
181
        #100 rst = 0;
182
        #100 ld = 1;
183
        #150 ld = 0;
184
end
185
 
186
always #10 clk = ~clk;  //  50 MHz
187
 
188
 
189
FT64_divider #(WID) u1
190
(
191
        .rst(rst),
192
        .clk(clk),
193
        .ld(ld),
194
        .sgn(1'b1),
195
        .isDivi(1'b0),
196
        .a(64'd10005),
197
        .b(64'd27),
198
        .imm(64'd123),
199
        .qo(qo),
200
        .ro(ro),
201
        .dvByZr(),
202
        .done(done)
203
);
204
 
205
endmodule
206
 

powered by: WebSVN 2.1.0

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