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

Subversion Repositories serial_div_uu

[/] [serial_div_uu/] [web_uploads/] [serial_divide_uu.v] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 root
//---------------------------------------------------------------------------
2
// serial_divide_uu.v  -- Serial division module
3
//
4
//
5
// Description: See description below (which suffices for IP core
6
//                                     specification document.)
7
//
8
// Copyright (C) 2002 John Clayton and OPENCORES.ORG (this Verilog version)
9
//
10
// This source file may be used and distributed without restriction provided
11
// that this copyright statement is not removed from the file and that any
12
// derivative work contains the original copyright notice and the associated
13
// disclaimer.
14
//
15
// This source file is free software; you can redistribute it and/or modify
16
// it under the terms of the GNU Lesser General Public License as published
17
// by the Free Software Foundation;  either version 2.1 of the License, or
18
// (at your option) any later version.
19
//
20
// This source is distributed in the hope that it will be useful, but WITHOUT
21
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
22
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
23
// License for more details.
24
//
25
// You should have received a copy of the GNU Lesser General Public License
26
// along with this source.
27
// If not, download it from http://www.opencores.org/lgpl.shtml
28
//
29
//-----------------------------------------------------------------------------
30
//
31
// Author: John Clayton
32
// Date  : Jan. 30, 2003
33
// Update: Jan. 30, 2003  Copied this file from "vga_crosshair.v"
34
//                        Stripped out extraneous stuff.
35
// Update: Mar. 14, 2003  Added S_PP parameter, made some simple changes to
36
//                        implement quotient leading zero "skip" feature.
37
// Update: Mar. 24, 2003  Updated comments to improve readability.
38
//
39
//-----------------------------------------------------------------------------
40
// Description:
41
//
42
// This module performs a division operation serially, producing one bit of the
43
// answer per clock cycle.  The dividend and the divisor are both taken to be
44
// unsigned quantities.  The divider is conceived as an integer divider (as
45
// opposed to a divider for fractional quantities) but the user can configure
46
// the divider to divide fractional quantities as long as the position of the
47
// binary point is carefully monitored.
48
//
49
// The widths of the signals are configurable by parameters, as follows:
50
//
51
// M_PP = Bit width of the dividend
52
// N_PP = Bit width of the divisor
53
// R_PP = Remainder bits desired
54
// S_PP = Skipped quotient bits
55
//
56
// The skipped quotient bits parameter provides a way to prevent the divider
57
// from calculating the full M_PP+R_PP output bits, in case some of the leading
58
// bits are already known to be zero.  This is the case, for example, when
59
// dividing two quantities to obtain a result that is a fraction between 0 and 1
60
// (as when measuring PWM signals).  In that case the integer portion of the
61
// quotient is always zero, and therefore it need not be calculated.
62
//
63
// The divide operation is begun by providing a pulse on the divide_i input.
64
// The quotient is provided (M_PP+R_PP-S_PP) clock cycles later.
65
// The divide_i pulse stores the input parameters in registers, so they do
66
// not need to be maintained at the inputs throughout the operation of the module.
67
// If a divide_i pulse is given to the serial_divide_uu module during the time
68
// when it is already working on a previous divide operation, it will abort the
69
// operation it was doing, and begin working on the new one.
70
//
71
// The user is responsible for treating the results correctly.  The position
72
// of the binary point is not given, but it is understood that the integer part
73
// of the result is the M_PP most significant bits of the quotient output.
74
// The remaining R_PP least significant bits are the fractional part.
75
//
76
// This is illustrated graphically:
77
//
78
//     [ M_PP bits ][    R_PP bits]
79
//     [ S_PP bits    ][quotient_o]
80
//
81
// The quotient will consist of whatever bits are left after removing the S_PP
82
// most significant bits from the (M_PP+R_PP) result bits.
83
//
84
// Attempting to divide by zero will simply produce a result of all ones.
85
// This core is so simple, that no checking for this condition is provided.
86
// If the user is concerned about a possible divide by zero condition, he should
87
// compare the divisor to zero and flag that condition himself!
88
//
89
// The COUNT_WIDTH_PP parameter must be sized so that 2^COUNT_WIDTH_PP-1 is >=
90
// M_PP+R_PP-S_PP-1.  The unit terminates the divide operation when the count
91
// is equal to M_PP+R_PP-S_PP-1.
92
// 
93
// The HELD_OUTPUT_PP parameter causes the unit to keep its output result in
94
// a register other than the one which it uses to compute the quotient.  This
95
// is useful for applications where the divider is used repeatedly and the
96
// previous divide result (quotient) must be stable during the computation of the
97
// next divide result.  Using the additional output register does incur some
98
// additional utilization of resources.
99
//
100
//-----------------------------------------------------------------------------
101
 
102
 
103
module serial_divide_uu (
104
  clk_i,
105
  clk_en_i,
106
  rst_i,
107
  divide_i,
108
  dividend_i,
109
  divisor_i,
110
  quotient_o,
111
  done_o
112
  );
113
 
114
parameter M_PP = 16;           // Size of dividend
115
parameter N_PP = 8;            // Size of divisor
116
parameter R_PP = 0;            // Size of remainder
117
parameter S_PP = 0;            // Skip this many bits (known leading zeros)
118
parameter COUNT_WIDTH_PP = 5;  // 2^COUNT_WIDTH_PP-1 >= (M_PP+R_PP-S_PP-1)
119
parameter HELD_OUTPUT_PP = 0;  // Set to 1 if stable output should be held
120
                               // from previous operation, during current
121
                               // operation.  Using this option will increase
122
                               // the resource utilization (costs extra
123
                               // d-flip-flops.)
124
 
125
// I/O declarations
126
input  clk_i;                           //
127
input  clk_en_i;
128
input  rst_i;                           // synchronous reset
129
input  divide_i;                        // starts division operation
130
input  [M_PP-1:0] dividend_i;           //
131
input  [N_PP-1:0] divisor_i;            //
132
output [M_PP+R_PP-S_PP-1:0] quotient_o; //
133
output done_o;                          // indicates completion of operation
134
 
135
//reg  [M_PP+R_PP-1:0] quotient_o;
136
reg  done_o;
137
 
138
// Internal signal declarations
139
 
140
reg  [M_PP+R_PP-1:0] grand_dividend;
141
reg  [M_PP+N_PP+R_PP-2:0] grand_divisor;
142
reg  [M_PP+R_PP-S_PP-1:0] quotient;
143
reg  [M_PP+R_PP-1:0] quotient_reg;       // Used exclusively for the held output
144
reg  [COUNT_WIDTH_PP-1:0] divide_count;
145
 
146
wire [M_PP+N_PP+R_PP-1:0] subtract_node; // Subtract node has extra "sign" bit
147
wire [M_PP+R_PP-1:0]      quotient_node; // Shifted version of quotient
148
wire [M_PP+N_PP+R_PP-2:0]  divisor_node; // Shifted version of grand divisor
149
 
150
//--------------------------------------------------------------------------
151
// Module code
152
 
153
// Serial dividing module
154
always @(posedge clk_i)
155
begin
156
  if (rst_i)
157
  begin
158
    grand_dividend <= 0;
159
    grand_divisor <= 0;
160
    divide_count <= 0;
161
    quotient <= 0;
162
    done_o <= 0;
163
  end
164
  else if (clk_en_i)
165
  begin
166
    done_o <= 0;
167
    if (divide_i)       // Start a new division
168
    begin
169
      quotient <= 0;
170
      divide_count <= 0;
171
      // dividend placed initially so that remainder bits are zero...
172
      grand_dividend <= dividend_i << R_PP;
173
      // divisor placed initially for a 1 bit overlap with dividend...
174
      // But adjust it back by S_PP, to account for bits that are known
175
      // to be leading zeros in the quotient.
176
      grand_divisor  <= divisor_i << (N_PP+R_PP-S_PP-1);
177
    end
178
    else if (divide_count == M_PP+R_PP-S_PP-1)
179
    begin
180
      if (~done_o) quotient <= quotient_node;      // final shift...
181
      if (~done_o) quotient_reg <= quotient_node;  // final shift (held output)
182
      done_o <= 1;                                 // Indicate done, just sit
183
    end
184
    else                // Division in progress
185
    begin
186
      // If the subtraction yields a positive result, then store that result
187
      if (~subtract_node[M_PP+N_PP+R_PP-1]) grand_dividend <= subtract_node;
188
      // If the subtraction yields a positive result, then a 1 bit goes into 
189
      // the quotient, via a shift register
190
      quotient <= quotient_node;
191
      // shift the grand divisor to the right, to cut it in half next clock cycle
192
      grand_divisor <= divisor_node;
193
      // Advance the counter
194
      divide_count <= divide_count + 1;
195
    end
196
  end  // End of else if clk_en_i
197
end // End of always block
198
 
199
assign subtract_node = {1'b0,grand_dividend} - {1'b0,grand_divisor};
200
assign quotient_node =
201
  {quotient[M_PP+R_PP-S_PP-2:0],~subtract_node[M_PP+N_PP+R_PP-1]};
202
assign divisor_node  = {1'b0,grand_divisor[M_PP+N_PP+R_PP-2:1]};
203
 
204
assign quotient_o = (HELD_OUTPUT_PP == 0)?quotient:quotient_reg;
205
 
206
endmodule
207
 

powered by: WebSVN 2.1.0

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