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

Subversion Repositories openarty

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 3 dgisselq
//
3
// Filename:    div.v
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7 50 dgisselq
// Purpose:     Provide an Integer divide capability to the Zip CPU.  Provides
8
//              for both signed and unsigned divide.
9 3 dgisselq
//
10 50 dgisselq
// Steps:
11
//      i_rst   The DIVide unit starts in idle.  It can also be placed into an
12
//      idle by asserting the reset input.
13 3 dgisselq
//
14 50 dgisselq
//      i_wr    When i_rst is asserted, a divide begins.  On the next clock:
15
//
16
//        o_busy is set high so everyone else knows we are at work and they can
17
//              wait for us to complete.
18
//
19
//        pre_sign is set to true if we need to do a signed divide.  In this
20
//              case, we take a clock cycle to turn the divide into an unsigned
21
//              divide.
22
//
23
//        o_quotient, a place to store our result, is initialized to all zeros.
24
//
25
//        r_dividend is set to the numerator
26
//
27
//        r_divisor is set to 2^31 * the denominator (shift left by 31, or add
28
//              31 zeros to the right of the number.
29
//
30
//      pre_sign When true (clock cycle after i_wr), a clock cycle is used
31
//              to take the absolute value of the various arguments (r_dividend
32
//              and r_divisor), and to calculate what sign the output result
33
//              should be.
34
//
35
//
36
//      At this point, the divide is has started.  The divide works by walking
37
//      through every shift of the
38
//
39
//                  DIVIDEND    over the
40
//              DIVISOR
41
//
42
//      If the DIVISOR is bigger than the dividend, the divisor is shifted
43
//      right, and nothing is done to the output quotient.
44
//
45
//                  DIVIDEND
46
//               DIVISOR
47
//
48
//      This repeats, until DIVISOR is less than or equal to the divident, as in
49
//
50
//              DIVIDEND
51
//              DIVISOR
52
//
53
//      At this point, if the DIVISOR is less than the dividend, the
54
//      divisor is subtracted from the dividend, and the DIVISOR is again
55
//      shifted to the right.  Further, a '1' bit gets set in the output
56
//      quotient.
57
//
58
//      Once we've done this for 32 clocks, we've accumulated our answer into
59
//      the output quotient, and we can proceed to the next step.  If the
60
//      result will be signed, the next step negates the quotient, otherwise
61
//      it returns the result.
62
//
63
//      On the clock when we are done, o_busy is set to false, and o_valid set
64
//      to true.  (It is a violation of the ZipCPU internal protocol for both
65
//      busy and valid to ever be true on the same clock.  It is also a
66
//      violation for busy to be false with valid true thereafter.)
67
//
68
//
69 3 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
70
//              Gisselquist Technology, LLC
71
//
72 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
73 3 dgisselq
//
74 50 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
75 3 dgisselq
//
76
// This program is free software (firmware): you can redistribute it and/or
77
// modify it under the terms of  the GNU General Public License as published
78
// by the Free Software Foundation, either version 3 of the License, or (at
79
// your option) any later version.
80
//
81
// This program is distributed in the hope that it will be useful, but WITHOUT
82
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
83
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
84
// for more details.
85
//
86 50 dgisselq
// You should have received a copy of the GNU General Public License along
87
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
88
// target there if the PDF file isn't present.)  If not, see
89
// <http://www.gnu.org/licenses/> for a copy.
90
//
91 3 dgisselq
// License:     GPL, v3, as defined and found on www.gnu.org,
92
//              http://www.gnu.org/licenses/gpl.html
93
//
94
//
95 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
96 3 dgisselq
//
97 50 dgisselq
//
98 3 dgisselq
// `include "cpudefs.v"
99
//
100
module  div(i_clk, i_rst, i_wr, i_signed, i_numerator, i_denominator,
101
                o_busy, o_valid, o_err, o_quotient, o_flags);
102 25 dgisselq
        parameter               BW=32, LGBW = 5;
103
        input                   i_clk, i_rst;
104 3 dgisselq
        // Input parameters
105
        input                   i_wr, i_signed;
106
        input   [(BW-1):0]       i_numerator, i_denominator;
107
        // Output parameters
108
        output  reg             o_busy, o_valid, o_err;
109
        output  reg [(BW-1):0]   o_quotient;
110
        output  wire    [3:0]    o_flags;
111
 
112
        // r_busy is an internal busy register.  It will clear one clock
113
        // before we are valid, so it can't be o_busy ...
114
        //
115
        reg                     r_busy;
116
        reg     [(2*BW-2):0]     r_divisor;
117
        reg     [(BW-1):0]       r_dividend;
118
        wire    [(BW):0] diff; // , xdiff[(BW-1):0];
119
        assign  diff = r_dividend - r_divisor[(BW-1):0];
120
        // assign       xdiff= r_dividend - { 1'b0, r_divisor[(BW-1):1] };
121
 
122
        reg             r_sign, pre_sign, r_z, r_c, last_bit;
123
        reg     [(LGBW-1):0]     r_bit;
124
        reg     zero_divisor;
125
 
126 50 dgisselq
        // The Divide logic begins with r_busy.  We use r_busy to determine
127
        // whether or not the divide is in progress, vs being complete.
128
        // Here, we clear r_busy on any reset and set it on i_wr (the request
129
        // do to a divide).  The divide ends when we are on the last bit,
130
        // or equivalently when we discover we are dividing by zero.
131 3 dgisselq
        initial r_busy = 1'b0;
132
        always @(posedge i_clk)
133
                if (i_rst)
134
                        r_busy <= 1'b0;
135
                else if (i_wr)
136
                        r_busy <= 1'b1;
137
                else if ((last_bit)||(zero_divisor))
138
                        r_busy <= 1'b0;
139
 
140 50 dgisselq
        // o_busy is very similar to r_busy, save for some key differences.
141
        // Primary among them is that o_busy needs to (possibly) be true
142
        // for an extra clock after r_busy clears.  This would be that extra
143
        // clock where we negate the result (assuming a signed divide, and that
144
        // the result is supposed to be negative.)  Otherwise, the two are
145
        // identical.
146 3 dgisselq
        initial o_busy = 1'b0;
147
        always @(posedge i_clk)
148
                if (i_rst)
149
                        o_busy <= 1'b0;
150
                else if (i_wr)
151
                        o_busy <= 1'b1;
152
                else if (((last_bit)&&(~r_sign))||(zero_divisor))
153
                        o_busy <= 1'b0;
154
                else if (~r_busy)
155
                        o_busy <= 1'b0;
156
 
157 50 dgisselq
        // If we are asked to divide by zero, we need to halt.  The sooner
158
        // we halt and report the error, the better.  Hence, here we look
159
        // for a zero divisor while being busy.  The always above us will then
160
        // look at this and halt a divide in the middle if we are trying to
161
        // divide by zero.
162
        //
163
        // Note that this works off of the 2BW-1 length vector.  If we can
164
        // simplify that, it should simplify our logic as well.
165
        initial zero_divisor = 1'b0;
166 3 dgisselq
        always @(posedge i_clk)
167 50 dgisselq
                // zero_divisor <= (r_divisor == 0)&&(r_busy);
168
                if (i_rst)
169
                        zero_divisor <= 1'b0;
170
                else if (i_wr)
171
                        zero_divisor <= (i_denominator == 0);
172
                else if (!r_busy)
173
                        zero_divisor <= 1'b0;
174
 
175
        // o_valid is part of the ZipCPU protocol.  It will be set to true
176
        // anytime our answer is valid and may be used by the calling module.
177
        // Indeed, the ZipCPU will halt (and ignore us) once the i_wr has been
178
        // set until o_valid gets set.
179
        //
180
        // Here, we clear o_valid on a reset, and any time we are on the last
181
        // bit while busy (provided the sign is zero, or we are dividing by
182
        // zero).  Since o_valid is self-clearing, we don't need to clear
183
        // it on an i_wr signal.
184
        initial o_valid = 1'b0;
185
        always @(posedge i_clk)
186
                if (i_rst)
187 3 dgisselq
                        o_valid <= 1'b0;
188
                else if (r_busy)
189
                begin
190
                        if ((last_bit)||(zero_divisor))
191 50 dgisselq
                                o_valid <= (zero_divisor)||(!r_sign);
192 3 dgisselq
                end else if (r_sign)
193
                begin
194 50 dgisselq
                        o_valid <= (!zero_divisor); // 1'b1;
195 3 dgisselq
                end else
196
                        o_valid <= 1'b0;
197
 
198 50 dgisselq
        // Division by zero error reporting.  Anytime we detect a zero divisor,
199
        // we set our output error, and then hold it until we are valid and
200
        // everything clears.
201 3 dgisselq
        initial o_err = 1'b0;
202
        always @(posedge i_clk)
203
                if((i_rst)||(o_valid))
204
                        o_err <= 1'b0;
205
                else if (((r_busy)||(r_sign))&&(zero_divisor))
206
                        o_err <= 1'b1;
207
                else
208
                        o_err <= 1'b0;
209
 
210 50 dgisselq
        // r_bit
211
        //
212
        // Keep track of which "bit" of our divide we are on.  This number
213
        // ranges from 31 down to zero.  On any write, we set ourselves to
214
        // 5'h1f.  Otherwise, while we are busy (but not within the pre-sign
215
        // adjustment stage), we subtract one from our value on every clock.
216
        always @(posedge i_clk)
217
                if ((r_busy)&&(!pre_sign))
218
                        r_bit <= r_bit + {(LGBW){1'b1}};
219
                else
220
                        r_bit <= {(LGBW){1'b1}};
221
 
222
        // last_bit
223
        //
224
        // This logic replaces a lot of logic that was inside our giant state
225
        // machine with ... something simpler.  In particular, we'll use this
226
        // logic to determine we are processing our last bit.  The only trick
227
        // is, this bit needs to be set whenever (r_busy) and (r_bit == 0),
228
        // hence we need to set on (r_busy) and (r_bit == 1) so as to be set
229
        // when (r_bit == 0).
230 3 dgisselq
        initial last_bit = 1'b0;
231
        always @(posedge i_clk)
232 50 dgisselq
                if (r_busy)
233
                        last_bit <= (r_bit == {{(LGBW-1){1'b0}},1'b1});
234
                else
235 3 dgisselq
                        last_bit <= 1'b0;
236
 
237 50 dgisselq
        // pre_sign
238
        //
239
        // This is part of the state machine.  pre_sign indicates that we need
240
        // a extra clock to take the absolute value of our inputs.  It need only
241
        // be true for the one clock, and then it must clear itself.
242
        initial pre_sign = 1'b0;
243 3 dgisselq
        always @(posedge i_clk)
244
                if (i_wr)
245
                        pre_sign <= i_signed;
246 50 dgisselq
                else
247
                        pre_sign <= 1'b0;
248
 
249
        // As a result of our operation, we need to set the flags.  The most
250
        // difficult of these is the "Z" flag indicating that the result is
251
        // zero.  Here, we'll use the same logic that sets the low-order
252
        // bit to clear our zero flag, and leave the zero flag set in all
253
        // other cases.  Well ... not quite.  If we need to flip the sign of
254
        // our value, then we can't quite clear the zero flag ... yet.
255
        always @(posedge i_clk)
256
                if((r_busy)&&(r_divisor[(2*BW-2):(BW)] == 0)&&(!diff[BW]))
257
                        // If we are busy, the upper bits of our divisor are
258
                        // zero (i.e., we got the shift right), and the top
259
                        // (carry) bit of the difference is zero (no overflow),
260
                        // then we could subtract our divisor from our dividend
261
                        // and hence we add a '1' to the quotient, while setting
262
                        // the zero flag to false.
263
                        r_z <= 1'b0;
264
                else if ((!r_busy)&&(!r_sign))
265 3 dgisselq
                        r_z <= 1'b1;
266 50 dgisselq
 
267
        // r_dividend
268
        // This is initially the numerator.  On a signed divide, it then becomes
269
        // the absolute value of the numerator.  We'll subtract from this value
270
        // the divisor shifted as appropriate for every output bit we are
271
        // looking for--just as with traditional long division.
272
        always @(posedge i_clk)
273
                if (pre_sign)
274 3 dgisselq
                begin
275 50 dgisselq
                        // If we are doing a signed divide, then take the
276
                        // absolute value of the dividend
277 3 dgisselq
                        if (r_dividend[BW-1])
278
                                r_dividend <= -r_dividend;
279 50 dgisselq
                        // The begin/end block is important so we don't lose
280
                        // the fact that on an else we don't do anything.
281
                end else if((r_busy)&&(r_divisor[(2*BW-2):(BW)]==0)&&(!diff[BW]))
282
                        // This is the condition whereby we set a '1' in our
283
                        // output quotient, and we subtract the (current)
284
                        // divisor from our dividend.  (The difference is
285
                        // already kept in the diff vector above.)
286
                        r_dividend <= diff[(BW-1):0];
287
                else if (!r_busy)
288
                        // Once we are done, and r_busy is no longer high, we'll
289
                        // always accept new values into our dividend.  This
290
                        // guarantees that, when i_wr is set, the new value
291
                        // is already set as desired.
292
                        r_dividend <=  i_numerator;
293
 
294
        initial r_divisor = 0;
295
        always @(posedge i_clk)
296
                if (pre_sign)
297
                begin
298 3 dgisselq
                        if (r_divisor[(2*BW-2)])
299 50 dgisselq
                                r_divisor[(2*BW-2):(BW-1)]
300
                                        <= -r_divisor[(2*BW-2):(BW-1)];
301 3 dgisselq
                end else if (r_busy)
302 50 dgisselq
                        r_divisor <= { 1'b0, r_divisor[(2*BW-2):1] };
303
                else
304
                        r_divisor <= {  i_denominator, {(BW-1){1'b0}} };
305
 
306
        // r_sign
307
        // is a flag for our state machine control(s).  r_sign will be set to
308
        // true any time we are doing a signed divide and the result must be
309
        // negative.  In that case, we take a final logic stage at the end of
310
        // the divide to negate the output.  This flag is what tells us we need
311
        // to do that.  r_busy will be true during the divide, then when r_busy
312
        // goes low, r_sign will be checked, then the idle/reset stage will have
313
        // been reached.  For this reason, we cannot set r_sign unless we are
314
        // up to something.
315
        initial r_sign = 1'b0;
316
        always @(posedge i_clk)
317
                if (pre_sign)
318
                        r_sign <= ((r_divisor[(2*BW-2)])^(r_dividend[(BW-1)]));
319
                else if (r_busy)
320
                        r_sign <= (r_sign)&&(!zero_divisor);
321
                else
322
                        r_sign <= 1'b0;
323
 
324
        always @(posedge i_clk)
325
                if (r_busy)
326 3 dgisselq
                begin
327 50 dgisselq
                        o_quotient <= { o_quotient[(BW-2):0], 1'b0 };
328
                        if ((r_divisor[(2*BW-2):(BW)] == 0)&&(!diff[BW]))
329 3 dgisselq
                        begin
330 50 dgisselq
                                o_quotient[0] <= 1'b1;
331 3 dgisselq
                        end
332
                end else if (r_sign)
333
                        o_quotient <= -o_quotient;
334 50 dgisselq
                else
335
                        o_quotient <= 0;
336 3 dgisselq
 
337
        // Set Carry on an exact divide
338 50 dgisselq
        // Perhaps nothing uses this, but ... well, I suppose we could remove
339
        // this logic eventually, just ... not yet.
340 3 dgisselq
        always @(posedge i_clk)
341
                r_c <= (r_busy)&&((diff == 0)||(r_dividend == 0));
342 50 dgisselq
 
343
        // The last flag: Negative.  This flag is set assuming that the result
344
        // of the divide was negative (i.e., the high order bit is set).  This
345
        // will also be true of an unsigned divide--if the high order bit is
346
        // ever set upon completion.  Indeed, you might argue that there's no
347
        // logic involved.
348
        wire    w_n;
349 3 dgisselq
        assign w_n = o_quotient[(BW-1)];
350
 
351
        assign o_flags = { 1'b0, w_n, r_c, r_z };
352
endmodule

powered by: WebSVN 2.1.0

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