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