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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [cpu/] [div.v] - Blame information for rev 32

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

Line No. Rev Author Line
1 3 dgisselq
///////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    div.v
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     Provide an Integer divide capability to the Zip CPU.
8
//
9
//
10
// Creator:     Dan Gisselquist, Ph.D.
11
//              Gisselquist Technology, LLC
12
//
13
///////////////////////////////////////////////////////////////////////////////
14
//
15
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
16
//
17
// This program is free software (firmware): you can redistribute it and/or
18
// modify it under the terms of  the GNU General Public License as published
19
// by the Free Software Foundation, either version 3 of the License, or (at
20
// your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful, but WITHOUT
23
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
24
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
// for more details.
26
//
27
// License:     GPL, v3, as defined and found on www.gnu.org,
28
//              http://www.gnu.org/licenses/gpl.html
29
//
30
//
31
///////////////////////////////////////////////////////////////////////////////
32
//
33
// `include "cpudefs.v"
34
//
35
module  div(i_clk, i_rst, i_wr, i_signed, i_numerator, i_denominator,
36
                o_busy, o_valid, o_err, o_quotient, o_flags);
37 25 dgisselq
        parameter               BW=32, LGBW = 5;
38
        input                   i_clk, i_rst;
39 3 dgisselq
        // Input parameters
40
        input                   i_wr, i_signed;
41
        input   [(BW-1):0]       i_numerator, i_denominator;
42
        // Output parameters
43
        output  reg             o_busy, o_valid, o_err;
44
        output  reg [(BW-1):0]   o_quotient;
45
        output  wire    [3:0]    o_flags;
46
 
47
        // r_busy is an internal busy register.  It will clear one clock
48
        // before we are valid, so it can't be o_busy ...
49
        //
50
        reg                     r_busy;
51
        reg     [(2*BW-2):0]     r_divisor;
52
        reg     [(BW-1):0]       r_dividend;
53
        wire    [(BW):0] diff; // , xdiff[(BW-1):0];
54
        assign  diff = r_dividend - r_divisor[(BW-1):0];
55
        // assign       xdiff= r_dividend - { 1'b0, r_divisor[(BW-1):1] };
56
 
57
        reg             r_sign, pre_sign, r_z, r_c, last_bit;
58
        reg     [(LGBW-1):0]     r_bit;
59
 
60
        reg     zero_divisor;
61
        initial zero_divisor = 1'b0;
62
        always @(posedge i_clk)
63
                zero_divisor <= (r_divisor == 0)&&(r_busy);
64
 
65
        initial r_busy = 1'b0;
66
        always @(posedge i_clk)
67
                if (i_rst)
68
                        r_busy <= 1'b0;
69
                else if (i_wr)
70
                        r_busy <= 1'b1;
71
                else if ((last_bit)||(zero_divisor))
72
                        r_busy <= 1'b0;
73
 
74
        initial o_busy = 1'b0;
75
        always @(posedge i_clk)
76
                if (i_rst)
77
                        o_busy <= 1'b0;
78
                else if (i_wr)
79
                        o_busy <= 1'b1;
80
                else if (((last_bit)&&(~r_sign))||(zero_divisor))
81
                        o_busy <= 1'b0;
82
                else if (~r_busy)
83
                        o_busy <= 1'b0;
84
 
85
        always @(posedge i_clk)
86
                if ((i_rst)||(i_wr))
87
                        o_valid <= 1'b0;
88
                else if (r_busy)
89
                begin
90
                        if ((last_bit)||(zero_divisor))
91
                                o_valid <= (zero_divisor)||(~r_sign);
92
                end else if (r_sign)
93
                begin
94
                        o_valid <= (~zero_divisor); // 1'b1;
95
                end else
96
                        o_valid <= 1'b0;
97
 
98
        initial o_err = 1'b0;
99
        always @(posedge i_clk)
100
                if((i_rst)||(o_valid))
101
                        o_err <= 1'b0;
102
                else if (((r_busy)||(r_sign))&&(zero_divisor))
103
                        o_err <= 1'b1;
104
                else
105
                        o_err <= 1'b0;
106
 
107
        initial last_bit = 1'b0;
108
        always @(posedge i_clk)
109
                if ((i_wr)||(pre_sign)||(i_rst))
110
                        last_bit <= 1'b0;
111
                else if (r_busy)
112
                        last_bit <= (r_bit == {{(LGBW-1){1'b0}},1'b1});
113
 
114
        always @(posedge i_clk)
115
                // if (i_rst) r_busy <= 1'b0;
116
                // else
117
                if (i_wr)
118
                begin
119
                        //
120
                        // Set our values upon an initial command.  Here's
121
                        // where we come in and start.
122
                        //
123
                        // r_busy <= 1'b1;
124
                        //
125
                        o_quotient <= 0;
126
                        r_bit <= {(LGBW){1'b1}};
127
                        r_divisor <= {  i_denominator, {(BW-1){1'b0}} };
128
                        r_dividend <=  i_numerator;
129
                        r_sign <= 1'b0;
130
                        pre_sign <= i_signed;
131
                        r_z <= 1'b1;
132
                end else if (pre_sign)
133
                begin
134
                        //
135
                        // Note that we only come in here, for one clock, if
136
                        // our initial value may have been signed.  If we are
137
                        // doing an unsigned divide, we then skip this step.
138
                        //
139
                        r_sign <= ((r_divisor[(2*BW-2)])^(r_dividend[(BW-1)]));
140
                        // Negate our dividend if necessary so that it becomes
141
                        // a magnitude only value
142
                        if (r_dividend[BW-1])
143
                                r_dividend <= -r_dividend;
144
                        // Do the same with the divisor--rendering it into
145
                        // a magnitude only.
146
                        if (r_divisor[(2*BW-2)])
147
                                r_divisor[(2*BW-2):(BW-1)] <= -r_divisor[(2*BW-2):(BW-1)];
148
                        //
149
                        // We only do this stage for a single clock, so go on
150
                        // with the rest of the divide otherwise.
151
                        pre_sign <= 1'b0;
152
                end else if (r_busy)
153
                begin
154
                        // While the divide is taking place, we examine each bit
155
                        // in turn here.
156
                        //
157
                        r_bit <= r_bit + {(LGBW){1'b1}}; // r_bit = r_bit - 1;
158
                        r_divisor <= { 1'b0, r_divisor[(2*BW-2):1] };
159
                        if (|r_divisor[(2*BW-2):(BW)])
160
                        begin
161
                        end else if (diff[BW])
162
                        begin
163
                                // 
164
                                // diff = r_dividend - r_divisor[(BW-1):0];
165
                                //
166
                                // If this value was negative, there wasn't
167
                                // enough value in the dividend to support
168
                                // pulling off a bit.  We'll move down a bit
169
                                // therefore and try again.
170
                                //
171
                        end else begin
172
                                //
173
                                // Put a '1' into our output accumulator.
174
                                // Subtract the divisor from the dividend,
175
                                // and then move on to the next bit
176
                                //
177
                                r_dividend <= diff[(BW-1):0];
178
                                o_quotient[r_bit[(LGBW-1):0]] <= 1'b1;
179
                                r_z <= 1'b0;
180
                        end
181
                        r_sign <= (r_sign)&&(~zero_divisor);
182
                end else if (r_sign)
183
                begin
184
                        r_sign <= 1'b0;
185
                        o_quotient <= -o_quotient;
186
                end
187
 
188
        // Set Carry on an exact divide
189
        wire    w_n;
190
        always @(posedge i_clk)
191
                r_c <= (r_busy)&&((diff == 0)||(r_dividend == 0));
192
        assign w_n = o_quotient[(BW-1)];
193
 
194
        assign o_flags = { 1'b0, w_n, r_c, r_z };
195
endmodule

powered by: WebSVN 2.1.0

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