1 |
4 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: ../rtl/cordic.v
|
4 |
|
|
//
|
5 |
|
|
// Project: A series of CORDIC related projects
|
6 |
|
|
//
|
7 |
|
|
// Purpose: This file executes a vector rotation on the values
|
8 |
|
|
// (i_xval, i_yval). This vector is rotated left by
|
9 |
|
|
// i_phase. i_phase is given by the angle, in radians, multiplied by
|
10 |
|
|
// 2^32/(2pi). In that fashion, a two pi value is zero just as a zero
|
11 |
|
|
// angle is zero.
|
12 |
|
|
//
|
13 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
14 |
|
|
// Gisselquist Technology, LLC
|
15 |
|
|
//
|
16 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
17 |
|
|
//
|
18 |
|
|
// Copyright (C) 2017, Gisselquist Technology, LLC
|
19 |
|
|
//
|
20 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
21 |
|
|
// modify it under the terms of the GNU General Public License as published
|
22 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
23 |
|
|
// your option) any later version.
|
24 |
|
|
//
|
25 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
26 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
27 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
28 |
|
|
// for more details.
|
29 |
|
|
//
|
30 |
|
|
// You should have received a copy of the GNU General Public License along
|
31 |
|
|
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
32 |
|
|
// target there if the PDF file isn't present.) If not, see
|
33 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
34 |
|
|
//
|
35 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
36 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
37 |
|
|
//
|
38 |
|
|
//
|
39 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
40 |
|
|
//
|
41 |
|
|
//
|
42 |
|
|
`default_nettype none
|
43 |
|
|
//
|
44 |
|
|
module cordic(i_clk, i_reset, i_ce, i_xval, i_yval, i_phase, i_aux,
|
45 |
|
|
o_xval, o_yval, o_aux);
|
46 |
|
|
localparam IW=16, // The number of bits in our inputs
|
47 |
|
|
OW=16, // The number of output bits to produce
|
48 |
|
|
NSTAGES=21,
|
49 |
|
|
XTRA= 5,// Extra bits for internal precision
|
50 |
|
|
WW=21, // Our working bit-width
|
51 |
|
|
PW=25; // Bits in our phase variables
|
52 |
|
|
input wire i_clk, i_reset, i_ce;
|
53 |
|
|
input wire signed [(IW-1):0] i_xval, i_yval;
|
54 |
|
|
input wire [(PW-1):0] i_phase;
|
55 |
|
|
output reg signed [(OW-1):0] o_xval, o_yval;
|
56 |
|
|
input wire i_aux;
|
57 |
|
|
output reg o_aux;
|
58 |
|
|
// First step: expand our input to our working width.
|
59 |
|
|
// This is going to involve extending our input by one
|
60 |
|
|
// (or more) bits in addition to adding any xtra bits on
|
61 |
|
|
// bits on the right. The one bit extra on the left is to
|
62 |
|
|
// allow for any accumulation due to the cordic gain
|
63 |
|
|
// within the algorithm.
|
64 |
|
|
//
|
65 |
|
|
wire signed [(WW-1):0] e_xval, e_yval;
|
66 |
|
|
assign e_xval = { {i_xval[(IW-1)]}, i_xval, {(WW-IW-1){1'b0}} };
|
67 |
|
|
assign e_yval = { {i_yval[(IW-1)]}, i_yval, {(WW-IW-1){1'b0}} };
|
68 |
|
|
|
69 |
|
|
// Declare variables for all of the separate stages
|
70 |
|
|
reg signed [(WW-1):0] xv [0:(NSTAGES)];
|
71 |
|
|
reg signed [(WW-1):0] yv [0:(NSTAGES)];
|
72 |
|
|
reg [(PW-1):0] ph [0:(NSTAGES)];
|
73 |
|
|
|
74 |
|
|
//
|
75 |
|
|
// Handle the auxilliary logic.
|
76 |
|
|
//
|
77 |
|
|
// The auxilliary bit is designed so that you can place a valid bit into
|
78 |
|
|
// the CORDIC function, and see when it comes out. While the bit is
|
79 |
|
|
// allowed to be anything, the requirement of this bit is that it *must*
|
80 |
|
|
// be aligned with the output when done. That is, if i_xval and i_yval
|
81 |
|
|
// are input together with i_aux, then when o_xval and o_yval are set
|
82 |
|
|
// to this value, o_aux *must* contain the value that was in i_aux.
|
83 |
|
|
//
|
84 |
|
|
reg [(NSTAGES):0] ax;
|
85 |
|
|
|
86 |
|
|
always @(posedge i_clk)
|
87 |
|
|
if (i_reset)
|
88 |
|
|
ax <= {(NSTAGES+1){1'b0}};
|
89 |
|
|
else if (i_ce)
|
90 |
|
|
ax <= { ax[(NSTAGES-1):0], i_aux };
|
91 |
|
|
|
92 |
|
|
// First stage, get rid of all but 45 degrees
|
93 |
|
|
// The resulting phase needs to be between -45 and 45
|
94 |
|
|
// degrees but in units of normalized phase
|
95 |
|
|
always @(posedge i_clk)
|
96 |
|
|
if (i_reset)
|
97 |
|
|
begin
|
98 |
|
|
xv[0] <= 0;
|
99 |
|
|
yv[0] <= 0;
|
100 |
|
|
ph[0] <= 0;
|
101 |
|
|
end else if (i_ce)
|
102 |
|
|
begin
|
103 |
|
|
// Walk through all possible quick phase shifts necessary
|
104 |
|
|
// to constrain the input to within +/- 45 degrees.
|
105 |
|
|
case(i_phase[(PW-1):(PW-3)])
|
106 |
|
|
3'b000: begin // 0 .. 45, No change
|
107 |
|
|
xv[0] <= e_xval;
|
108 |
|
|
yv[0] <= e_yval;
|
109 |
|
|
ph[0] <= i_phase;
|
110 |
|
|
end
|
111 |
|
|
3'b001: begin // 45 .. 90
|
112 |
|
|
xv[0] <= -e_yval;
|
113 |
|
|
yv[0] <= e_xval;
|
114 |
|
|
ph[0] <= i_phase - 25'h800000;
|
115 |
|
|
end
|
116 |
|
|
3'b010: begin // 90 .. 135
|
117 |
|
|
xv[0] <= -e_yval;
|
118 |
|
|
yv[0] <= e_xval;
|
119 |
|
|
ph[0] <= i_phase - 25'h800000;
|
120 |
|
|
end
|
121 |
|
|
3'b011: begin // 135 .. 180
|
122 |
|
|
xv[0] <= -e_xval;
|
123 |
|
|
yv[0] <= -e_yval;
|
124 |
|
|
ph[0] <= i_phase - 25'h1000000;
|
125 |
|
|
end
|
126 |
|
|
3'b100: begin // 180 .. 225
|
127 |
|
|
xv[0] <= -e_xval;
|
128 |
|
|
yv[0] <= -e_yval;
|
129 |
|
|
ph[0] <= i_phase - 25'h1000000;
|
130 |
|
|
end
|
131 |
|
|
3'b101: begin // 225 .. 270
|
132 |
|
|
xv[0] <= e_yval;
|
133 |
|
|
yv[0] <= -e_xval;
|
134 |
|
|
ph[0] <= i_phase - 25'h1800000;
|
135 |
|
|
end
|
136 |
|
|
3'b110: begin // 270 .. 315
|
137 |
|
|
xv[0] <= e_yval;
|
138 |
|
|
yv[0] <= -e_xval;
|
139 |
|
|
ph[0] <= i_phase - 25'h1800000;
|
140 |
|
|
end
|
141 |
|
|
3'b111: begin // 315 .. 360, No change
|
142 |
|
|
xv[0] <= e_xval;
|
143 |
|
|
yv[0] <= e_yval;
|
144 |
|
|
ph[0] <= i_phase;
|
145 |
|
|
end
|
146 |
|
|
endcase
|
147 |
|
|
end
|
148 |
|
|
|
149 |
|
|
//
|
150 |
|
|
// In many ways, the key to this whole algorithm lies in the angles
|
151 |
|
|
// necessary to do this. These angles are also our basic reason for
|
152 |
|
|
// building this CORDIC in C++: Verilog just can't parameterize this
|
153 |
|
|
// much. Further, these angle's risk becoming unsupportable magic
|
154 |
|
|
// numbers, hence we define these and set them in C++, based upon
|
155 |
|
|
// the needs of our problem, specifically the number of stages and
|
156 |
|
|
// the number of bits required in our phase accumulator
|
157 |
|
|
//
|
158 |
|
|
wire [24:0] cordic_angle [0:(NSTAGES-1)];
|
159 |
|
|
|
160 |
|
|
assign cordic_angle[ 0] = 25'h025_c80a; // 26.565051 deg
|
161 |
|
|
assign cordic_angle[ 1] = 25'h013_f670; // 14.036243 deg
|
162 |
|
|
assign cordic_angle[ 2] = 25'h00a_2223; // 7.125016 deg
|
163 |
|
|
assign cordic_angle[ 3] = 25'h005_161a; // 3.576334 deg
|
164 |
|
|
assign cordic_angle[ 4] = 25'h002_8baf; // 1.789911 deg
|
165 |
|
|
assign cordic_angle[ 5] = 25'h001_45ec; // 0.895174 deg
|
166 |
|
|
assign cordic_angle[ 6] = 25'h000_a2f8; // 0.447614 deg
|
167 |
|
|
assign cordic_angle[ 7] = 25'h000_517c; // 0.223811 deg
|
168 |
|
|
assign cordic_angle[ 8] = 25'h000_28be; // 0.111906 deg
|
169 |
|
|
assign cordic_angle[ 9] = 25'h000_145f; // 0.055953 deg
|
170 |
|
|
assign cordic_angle[10] = 25'h000_0a2f; // 0.027976 deg
|
171 |
|
|
assign cordic_angle[11] = 25'h000_0517; // 0.013988 deg
|
172 |
|
|
assign cordic_angle[12] = 25'h000_028b; // 0.006994 deg
|
173 |
|
|
assign cordic_angle[13] = 25'h000_0145; // 0.003497 deg
|
174 |
|
|
assign cordic_angle[14] = 25'h000_00a2; // 0.001749 deg
|
175 |
|
|
assign cordic_angle[15] = 25'h000_0051; // 0.000874 deg
|
176 |
|
|
assign cordic_angle[16] = 25'h000_0028; // 0.000437 deg
|
177 |
|
|
assign cordic_angle[17] = 25'h000_0014; // 0.000219 deg
|
178 |
|
|
assign cordic_angle[18] = 25'h000_000a; // 0.000109 deg
|
179 |
|
|
assign cordic_angle[19] = 25'h000_0005; // 0.000055 deg
|
180 |
|
|
assign cordic_angle[20] = 25'h000_0002; // 0.000027 deg
|
181 |
|
|
// Std-Dev : 0.00 (Units)
|
182 |
|
|
// Phase Quantization: 0.000001 (Radians)
|
183 |
|
|
// Gain is 1.164435
|
184 |
|
|
// You can annihilate this gain by multiplying by 32'hdbd95b16
|
185 |
|
|
// and right shifting by 32 bits.
|
186 |
|
|
|
187 |
|
|
genvar i;
|
188 |
|
|
generate for(i=0; i<NSTAGES; i=i+1) begin : CORDICops
|
189 |
|
|
always @(posedge i_clk)
|
190 |
|
|
// Here's where we are going to put the actual CORDIC
|
191 |
|
|
// we've been studying and discussing. Everything up to
|
192 |
|
|
// this point has simply been necessary preliminaries.
|
193 |
|
|
if (i_reset)
|
194 |
|
|
begin
|
195 |
|
|
xv[i+1] <= 0;
|
196 |
|
|
yv[i+1] <= 0;
|
197 |
|
|
ph[i+1] <= 0;
|
198 |
|
|
end else if (i_ce)
|
199 |
|
|
begin
|
200 |
|
|
if ((cordic_angle[i] == 0)||(i >= WW))
|
201 |
|
|
begin // Do nothing but move our outputs
|
202 |
|
|
// forward one stage, since we have more
|
203 |
|
|
// stages than valid data
|
204 |
|
|
xv[i+1] <= xv[i];
|
205 |
|
|
yv[i+1] <= yv[i];
|
206 |
|
|
ph[i+1] <= ph[i];
|
207 |
|
|
end else if (ph[i][(PW-1)]) // Negative phase
|
208 |
|
|
begin
|
209 |
|
|
// If the phase is negative, rotate by the
|
210 |
|
|
// CORDIC angle in a clockwise direction.
|
211 |
|
|
xv[i+1] <= xv[i] + (yv[i]>>>(i+1));
|
212 |
|
|
yv[i+1] <= yv[i] - (xv[i]>>>(i+1));
|
213 |
|
|
ph[i+1] <= ph[i] + cordic_angle[i];
|
214 |
|
|
end else begin
|
215 |
|
|
// On the other hand, if the phase is
|
216 |
|
|
// positive ... rotate in the
|
217 |
|
|
// counter-clockwise direction
|
218 |
|
|
xv[i+1] <= xv[i] - (yv[i]>>>(i+1));
|
219 |
|
|
yv[i+1] <= yv[i] + (xv[i]>>>(i+1));
|
220 |
|
|
ph[i+1] <= ph[i] - cordic_angle[i];
|
221 |
|
|
end
|
222 |
|
|
end
|
223 |
|
|
end endgenerate
|
224 |
|
|
|
225 |
|
|
// Round our result towards even
|
226 |
|
|
wire [(WW-1):0] pre_xval, pre_yval;
|
227 |
|
|
|
228 |
|
|
assign pre_xval = xv[NSTAGES] + {{(OW){1'b0}},
|
229 |
|
|
xv[NSTAGES][(WW-OW)],
|
230 |
|
|
{(WW-OW-1){!xv[NSTAGES][WW-OW]}}};
|
231 |
|
|
assign pre_yval = yv[NSTAGES] + {{(OW){1'b0}},
|
232 |
|
|
yv[NSTAGES][(WW-OW)],
|
233 |
|
|
{(WW-OW-1){!yv[NSTAGES][WW-OW]}}};
|
234 |
|
|
|
235 |
|
|
always @(posedge i_clk)
|
236 |
|
|
begin
|
237 |
|
|
o_xval <= pre_xval[(WW-1):(WW-OW)];
|
238 |
|
|
o_yval <= pre_yval[(WW-1):(WW-OW)];
|
239 |
|
|
o_aux <= ax[NSTAGES];
|
240 |
|
|
end
|
241 |
|
|
|
242 |
|
|
// Make Verilator happy with pre_.val
|
243 |
|
|
// verilator lint_off UNUSED
|
244 |
|
|
wire [(2*(WW-OW)-1):0] unused_val;
|
245 |
|
|
assign unused_val = {
|
246 |
|
|
pre_xval[(WW-OW-1):0],
|
247 |
|
|
pre_yval[(WW-OW-1):0]
|
248 |
|
|
};
|
249 |
|
|
// verilator lint_on UNUSED
|
250 |
|
|
endmodule
|