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

Subversion Repositories raptor64

[/] [raptor64/] [trunk/] [rtl/] [verilog/] [Raptor64Div.v] - Blame information for rev 44

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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