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

Subversion Repositories thor

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

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

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

powered by: WebSVN 2.1.0

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