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

Subversion Repositories rtf65002

[/] [rtf65002/] [trunk/] [rtl/] [verilog/] [mult_div.v] - Blame information for rev 30

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

Line No. Rev Author Line
1 12 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2013  Robert Finch, Stratford
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@opencores.org
6
//       ||
7
//
8
// rtf65002.v
9
//  - 32 bit CPU multiplier/divider
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
// ============================================================================
25
//
26 30 robfinch
`define SUPPORT_DIVMOD          1'b1
27
 
28 12 robfinch
`define MUL             4'd8
29
`define MULS    4'd9
30
`define DIV             4'd10
31
`define DIVS    4'd11
32
`define MOD             4'd12
33
`define MODS    4'd13
34
 
35
module mult_div(rst, clk, ld, op, a, b, p, q, r, done);
36
parameter IDLE=3'd0;
37
parameter MULT=3'd1;
38
parameter FIX_SIGN=3'd2;
39
parameter DIV=3'd3;
40
input rst;
41
input clk;
42
input ld;
43
input [3:0] op;
44
input [31:0] a;
45
input [31:0] b;
46
output reg [63:0] p;
47
output reg [31:0] q;
48
output reg [31:0] r;
49
output done;
50
 
51
reg [31:0] aa, bb;
52
reg res_sgn;
53
 
54
reg [2:0] state;
55
 
56
assign done = state==IDLE;
57
wire [31:0] diff = r - bb;
58
wire [31:0] pa = a[31] ? -a : a;
59
reg [5:0] cnt;
60
 
61
always @(posedge clk)
62
if (rst)
63
state <= IDLE;
64
else begin
65
case(state)
66
IDLE:
67
        if (ld) begin
68
                cnt <= 6'd32;
69
                case(op)
70
                `MUL:
71
                        begin
72
                                aa <= a;
73
                                bb <= b;
74
                                res_sgn <= 1'b0;
75
                                state <= MULT;
76
                        end
77
                `MULS:
78
                        begin
79
                                aa <= a[31] ? -a : a;
80
                                bb <= b[31] ? -b : b;
81
                                res_sgn <= a[31] ^ b[31];
82
                                state <= MULT;
83
                        end
84 30 robfinch
`ifdef SUPPORT_DIVMOD
85 12 robfinch
                `DIV,`MOD:
86
                        begin
87
                                aa <= a;
88
                                bb <= b;
89
                                q <= a[30:0];
90
                                r <= a[31];
91
                                res_sgn <= 1'b0;
92
                                state <= DIV;
93
                        end
94
                `DIVS,`MODS:
95
                        begin
96
                                aa <= a[31] ? -a : a;
97
                                bb <= b[31] ? -b : b;
98
                                q <= pa[30:0];
99
                                r <= pa[31];
100
                                res_sgn <= a[31] ^ b[31];
101
                                state <= DIV;
102
                        end
103 30 robfinch
`endif
104 12 robfinch
                default:
105
                        state <= IDLE;
106
                endcase
107
        end
108
MULT:
109
        begin
110
                state <= res_sgn ? FIX_SIGN : IDLE;
111
                p <= aa * bb;
112
        end
113 30 robfinch
`ifdef SUPPORT_DIVMOD
114 12 robfinch
DIV:
115
        begin
116
                q <= {q[30:0],~diff[31]};
117
                if (cnt==6'd0) begin
118
                        state <= res_sgn ? FIX_SIGN : IDLE;
119
                        if (diff[31])
120
                                r <= r[30:0];
121
                        else
122
                                r <= diff[30:0];
123
                end
124
                else begin
125
                        if (diff[31])
126
                                r <= {r[30:0],q[31]};
127
                        else
128
                                r <= {diff[30:0],q[31]};
129
                end
130
                cnt <= cnt - 6'd1;
131
        end
132 30 robfinch
`endif
133 12 robfinch
 
134
FIX_SIGN:
135
        begin
136
                state <= IDLE;
137
                if (res_sgn) begin
138
                        p <= -p;
139
                        q <= -q;
140
                        r <= -r;
141
                end
142
        end
143
default:        state <= IDLE;
144
endcase
145
end
146
 
147
endmodule
148
 
149
module multdiv_tb();
150
reg rst;
151
reg clk;
152
reg ld;
153
 
154
initial begin
155
        #0 clk = 1'b0;
156
        #0 rst = 1'b0;
157
        #10 rst = 1'b1;
158
        #10 rst = 1'b0;
159
        #10 ld = 1'b1;
160
        #20 ld = 1'b0;
161
end
162
 
163
always #10 clk = ~clk;
164
 
165
mult_div umd1 (
166
        .rst(rst),
167
        .clk(clk),
168
        .ld(ld),
169
        .op(`DIV),
170
        .a(32'h12345678),
171
        .b(32'd10),
172
        .p(),
173
        .q(),
174
        .r(),
175
        .done()
176
);
177
 
178
endmodule

powered by: WebSVN 2.1.0

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