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

Subversion Repositories thor

[/] [thor/] [trunk/] [rtl/] [verilog/] [Thor_divider.v] - Blame information for rev 42

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 robfinch
// ============================================================================
2
//        __
3 13 robfinch
//   \\__/ o\    (C) 2013,2015  Robert Finch, Stratford
4 3 robfinch
//    \  __ /    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
// Thor Superscaler
23
// Thor_divider.v
24
//  - 64 bit divider
25
//
26
// ============================================================================
27
//
28 13 robfinch
module Thor_divider(rst, clk, ld, abort, sgn, isDivi, a, b, imm, qo, ro, dvByZr, done, idle);
29 3 robfinch
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 13 robfinch
input abort;
37 3 robfinch
input sgn;
38
input isDivi;
39
input [WID-1:0] a;
40
input [WID-1:0] b;
41
input [WID-1:0] imm;
42
output [WID-1:0] qo;
43
reg [WID-1:0] qo;
44
output [WID-1:0] ro;
45
reg [WID-1:0] ro;
46
output done;
47 13 robfinch
output idle;
48 3 robfinch
output dvByZr;
49
reg dvByZr;
50
 
51
reg [WID-1:0] aa,bb;
52
reg so;
53
reg [2:0] state;
54
reg [7:0] cnt;
55
wire cnt_done = cnt==8'd0;
56 42 robfinch
assign done = state==DONE||(state==IDLE && !ld);
57 13 robfinch
assign idle = state==IDLE;
58 3 robfinch
reg ce1;
59
reg [WID-1:0] q;
60
reg [WID:0] r;
61
wire b0 = bb <= r;
62
wire [WID-1:0] r1 = b0 ? r - bb : r;
63
 
64
initial begin
65
    q = 64'd0;
66
    r = 64'd0;
67
    qo = 64'd0;
68
    ro = 64'd0;
69
end
70
 
71
always @(posedge clk)
72
if (rst) begin
73
        aa <= {WID{1'b0}};
74
        bb <= {WID{1'b0}};
75
        q <= {WID{1'b0}};
76
        r <= {WID{1'b0}};
77
        qo <= {WID{1'b0}};
78
        ro <= {WID{1'b0}};
79
        cnt <= 8'd0;
80 42 robfinch
        dvByZr <= 1'b0;
81 3 robfinch
        state <= IDLE;
82
end
83
else
84
begin
85 13 robfinch
if (abort)
86
    cnt <= 8'd00;
87
else if (!cnt_done)
88 3 robfinch
        cnt <= cnt - 8'd1;
89
 
90
case(state)
91
IDLE:
92
        if (ld) begin
93
                if (sgn) begin
94
                        q <= a[WID-1] ? -a : a;
95
                        bb <= isDivi ? (imm[WID-1] ? -imm : imm) :(b[WID-1] ? -b : b);
96
                        so <= isDivi ? a[WID-1] ^ imm[WID-1] : a[WID-1] ^ b[WID-1];
97
                end
98
                else begin
99
                        q <= a;
100
                        bb <= isDivi ? imm : b;
101
                        so <= 1'b0;
102
                        $display("bb=%d", isDivi ? imm : b);
103
                end
104
                dvByZr <= isDivi ? imm=={WID{1'b0}} : b=={WID{1'b0}};
105
                r <= {WID{1'b0}};
106
                cnt <= WID+1;
107
                state <= DIV;
108
        end
109
DIV:
110
        if (!cnt_done) begin
111
                $display("cnt:%d r1=%h q[63:0]=%h", cnt,r1,q);
112
                q <= {q[WID-2:0],b0};
113
                r <= {r1,q[WID-1]};
114
        end
115
        else begin
116
                $display("cnt:%d r1=%h q[63:0]=%h", cnt,r1,q);
117
                if (sgn) begin
118
                        if (so) begin
119
                                qo <= -q;
120
                                ro <= -r[WID:1];
121
                        end
122
                        else begin
123
                                qo <= q;
124
                                ro <= r[WID:1];
125
                        end
126
                end
127
                else begin
128
                        qo <= q;
129
                        ro <= r[WID:1];
130
                end
131
                state <= DONE;
132
        end
133
DONE:
134
        state <= IDLE;
135
endcase
136
end
137
 
138
endmodule
139
 
140
module Thor_divider_tb();
141
parameter WID=64;
142
reg rst;
143
reg clk;
144
reg ld;
145
wire done;
146
wire [WID-1:0] qo,ro;
147
 
148
initial begin
149
        clk = 1;
150
        rst = 0;
151
        #100 rst = 1;
152
        #100 rst = 0;
153
        #100 ld = 1;
154
        #150 ld = 0;
155
end
156
 
157
always #10 clk = ~clk;  //  50 MHz
158
 
159
 
160
Thor_divider #(WID) u1
161
(
162
        .rst(rst),
163
        .clk(clk),
164
        .ld(ld),
165
        .sgn(1'b1),
166
        .isDivi(1'b0),
167
        .a(64'd10005),
168
        .b(64'd27),
169
        .imm(64'd123),
170
        .qo(qo),
171
        .ro(ro),
172
        .dvByZr(),
173
        .done(done)
174
);
175
 
176
endmodule
177
 

powered by: WebSVN 2.1.0

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