Professional Documents
Culture Documents
author:
release:
cordic.v
Dale Drinkard
08/06/2008
brief:
First Quadrant CORDIC
This is a self contained, configurable CORDIC generator
The user can modify the `defines below to customize the code generation.
This code is for the first quadrant, but is easily extended to the full
circle by first doing a coarse rotation. For example, to compute the
arctan of -y/x, in the second quadrant, feed the cordic function y/x and
then add 90 degrees (or pi/2 if using radian mode) to the result. When
computing sin and cos of an angle, coarse rotate the angle into the first qu
adrant
by subtracting the appropriate number of 90 (or pi/2) increments to get the
angle in
the first quadrant, keep track of this value, feed the cordic the angle. Th
en
simply change the sign of the results based on this stored number.
To use the core comment/uncomment the `defines below. The user can change t
he number
of bits that represent the x,y, and theta values. The core can operate in e
ither radian
or degree mode.
**NOTE** Even though there are allowances for changeing many parameters of t
he code, it is
strongly advised that the user understand the CORDIC algorythm before monkey
ing with these
settings. By default, the core uses 16+sign (17 bit) numbers for x,y, and t
heta, and iterates 16
times in the algorythm. There are two arctan function tables,one for radian
and one for degree
mode. If more iterations or higher precision calculations are desired then
a new arctan table will
need to be computed.
The core will operate in one
of two modes:
ROTATE: In this mode the user supplies a X and Y cartesian vector and an an
gle. The
CORDIC rotator seeks to reduce the angle to zero by rotating the ve
ctor.
To compute the cos and sin of the angle, set the inputs as follows:
y_in = 0;
x_in = `CORDIC_1
theta_in = the input angle
on completion:
y_out = sin
x_out = cos
*/
/* data valid flag
The iterative CORDIC implementations take a predetermined number of clock cyc
les to complete
If the VALID_FLAG is defined the core instantiates a dvalid_in and dvalid_out
signal. This
signal makes no sense in the COMBINATORIAL mode.
*/
// `define VALID_FLAG
/* Angle mode
The CORDIC can work with the angle expressed in radians or degrees
Uncomment the appropriate `define below.
RADIAN_16 uses 16 bit values (+ sign bit for 17 bit accuracy). angle informat
ion
is in the format U(1,15) where bit 16 is the sign bit, bit 15 is the whole num
ber part
and bits [14:0] are the fractional parts.
DEGREE_8_8 uses U(8,8) + a sign bit where bit 16 = the sign bit, [15:8] = the
whole number part
and [7:0] = the fractional.
The user can define other formats by creating a new tanangle function
*/
// `define DEGREE_8_8
`define RADIAN_16
/* Bit accuracy for sin and cos
The X and Y values are computed using a `XY_BITS + sign bit accuracy. The for
mat is assumed to be U(1,15) + sign bit
However, the CORDIC algorythm really doesn't care.
*/
`define XY_BITS
16
// CORDIC inverse
`ifdef PIPELINE
`define GENERATE_LOOP
`endif
`ifdef COMBINATORIAL
`define GENERATE_LOOP
`endif
/* Signed shifter
This module does an arbitrary right shift to implement'
the 1/2^i function on signed numbers
*/
module signed_shifter (
input wire [`ITERATION_BITS-1:0] i,
input wire signed [`XY_BITS:0] D,
output reg signed [`XY_BITS:0] Q );
integer j;
always @ * begin
Q = D;
for(j=0;j<i;j=j+1) Q = (Q >> 1) | (D[`XY_BITS] << `XY_BITS);
end
endmodule
/* Rotator
This module is the heart of the CORDIC computer and implements the CORDIC algo
rithm.
Input values x_i, y_i, and z_i are micro computed based on the iteration step
and the arctan of that step. See the description of the CORDIC algorithm for
details.
*/
module rotator (
input wire clk,
input wire rst,
`ifdef ITERATE
input wire init,
input wire [`ITERATION_BITS:0] iteration,
input wire signed [`THETA_BITS:0] tangle,
`endif
input wire signed [`XY_BITS:0]
x_i,
input wire signed [`XY_BITS:0]
y_i,
input wire signed [`THETA_BITS:0] z_i,
output wire signed [`XY_BITS:0]
x_o,
output wire signed [`XY_BITS:0]
y_o,
output wire signed [`THETA_BITS:0] z_o
);
`ifdef GENERATE_LOOP
parameter integer iteration = 0;
parameter signed [`THETA_BITS:0] tangle = 0;
`endif
>> i);
>> i);
>> i);
>> i);
// theta = 0.001373
`ifdef GENERATE_LOOP
wire signed [`XY_BITS:0] x [`ITERATIONS-1:0];
wire signed [`XY_BITS:0] y [`ITERATIONS-1:0];
wire signed [`THETA_BITS:0] z [`ITERATIONS-1:0];
assign x[0] = x_i;
assign y[0] = y_i;
assign z[0] = theta_i;
assign x_o = x[`ITERATIONS-1];
assign y_o = y[`ITERATIONS-1];
assign theta_o = z[`ITERATIONS-1];
`endif // GENERATE_LOOP
`ifdef VALID_FLAG
wire [`ITERATIONS-1:0] v;
assign valid_out v[`ITERATIONS-1];
always @ (posedge clk or posedge rst)
if (rst) v <= 0;
else begin
v <= v << 1;
v[0] <= valid_in;
end
`endif
`ifdef GENERATE_LOOP
genvar i;
generate for(i=0;i<`ITERATIONS-1;i=i+1) begin
rotator U (clk,rst,x[i],y[i],z[i],x[i+1],y[i+1],z[i+1]);
defparam U.iteration = i;
defparam U.tangle = tanangle(i);
end
endgenerate
`endif
`ifdef ITERATE
reg [`ITERATION_BITS:0] iteration;
wire signed [`XY_BITS:0] x,y,z;
assign x = init ? x_i : x_o;
assign y = init ? y_i : y_o;
assign z = init ? theta_i : theta_o;
always @ (posedge clk or posedge init)
if (init) iteration <= 0;
else iteration <= iteration + 1;
rotator U (clk,rst,init,iteration,tanangle(iteration),x,y,z,x_o,y_o,theta_o);
`endif
endmodule