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

Subversion Repositories raptor64

[/] [raptor64/] [trunk/] [rtl/] [verilog/] [Raptor64Div.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
// (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
parameter DONE=4'd5;
28
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
        state <= IDLE;
65
end
66
else
67
begin
68
if (!cnt_done)
69
        cnt <= cnt - 8'd1;
70
 
71
case(state)
72
IDLE:
73
        if (ld) begin
74
                if (sgn) begin
75
                        q <= a[63] ? -a : a;
76
                        bb <= isDivi ? (imm[63] ? -imm : imm) :(b[63] ? -b : b);
77
                        so <= isDivi ? a[63] ^ imm[63] : a[63] ^ b[63];
78
                end
79
                else begin
80
                        q <= a;
81
                        bb <= isDivi ? imm : b;
82
                        so <= 1'b0;
83
                end
84
                dvByZr <= isDivi ? imm==64'd0 : b==64'd0;
85
                r <= 64'd0;
86
                cnt <= 8'd65;
87
                state <= DIV;
88
        end
89
DIV:
90
        if (!cnt_done) begin
91
                $display("cnt:%d r1=%h q[63:0]=%h", cnt,r1,q);
92
                q <= {q[62:0],b0};
93
                r <= {r1,q[63]};
94
        end
95
        else begin
96
                $display("cnt:%d r1=%h q[63:0]=%h", cnt,r1,q);
97
                if (sgn) begin
98
                        if (so) begin
99
                                qo <= -q;
100
                                ro <= -r[64:1];
101
                        end
102
                        else begin
103
                                qo <= q;
104
                                ro <= r[64:1];
105
                        end
106
                end
107
                else begin
108
                        qo <= q;
109
                        ro <= r[64:1];
110
                end
111
                state <= DONE;
112
        end
113
DONE:
114
        state <= IDLE;
115
endcase
116
end
117
 
118
endmodule
119
 
120
module Raptor64Div_tb();
121
 
122
reg rst;
123
reg clk;
124
reg ld;
125
wire done;
126
wire [63:0] qo,ro;
127
 
128
initial begin
129
        clk = 1;
130
        rst = 0;
131
        #100 rst = 1;
132
        #100 rst = 0;
133
        #100 ld = 1;
134
        #150 ld = 0;
135
end
136
 
137
always #10 clk = ~clk;  //  50 MHz
138
 
139
 
140
Raptor64Div u1
141
(
142
        .rst(rst),
143
        .clk(clk),
144
        .ld(ld),
145
        .sgn(1'b1),
146
        .isDivi(1'b0),
147
        .a(64'd10005),
148
        .b(64'd27),
149
        .imm(64'd123),
150
        .qo(qo),
151
        .ro(ro),
152
        .dvByZr(),
153
        .done(done)
154
);
155
 
156
endmodule
157
 

powered by: WebSVN 2.1.0

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