1 |
90 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2013-2023 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
//
|
9 |
|
|
// BSD 3-Clause License
|
10 |
|
|
// Redistribution and use in source and binary forms, with or without
|
11 |
|
|
// modification, are permitted provided that the following conditions are met:
|
12 |
|
|
//
|
13 |
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
14 |
|
|
// list of conditions and the following disclaimer.
|
15 |
|
|
//
|
16 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
17 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
18 |
|
|
// and/or other materials provided with the distribution.
|
19 |
|
|
//
|
20 |
|
|
// 3. Neither the name of the copyright holder nor the names of its
|
21 |
|
|
// contributors may be used to endorse or promote products derived from
|
22 |
|
|
// this software without specific prior written permission.
|
23 |
|
|
//
|
24 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
25 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
26 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
27 |
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
28 |
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
29 |
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
30 |
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
31 |
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
32 |
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
33 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34 |
|
|
//
|
35 |
|
|
// ============================================================================
|
36 |
|
|
|
37 |
|
|
module fpCordic(rst, clk, arctan, ld, phase_i, xval_i, yval_i, xval_o, yval_o, phase_o, done);
|
38 |
|
|
parameter NSTAGES = 54;
|
39 |
|
|
parameter IW = 54;
|
40 |
|
|
parameter OW = 54;
|
41 |
|
|
parameter WW = 60;
|
42 |
|
|
parameter PW = 60;
|
43 |
|
|
parameter INV_GAIN = 54'h26dd3b6a10d798; // 2^54 / gain
|
44 |
|
|
input rst;
|
45 |
|
|
input clk;
|
46 |
|
|
input arctan;
|
47 |
|
|
input ld;
|
48 |
|
|
input [PW-1:0] phase_i;
|
49 |
|
|
input [IW-1:0] xval_i;
|
50 |
|
|
input [IW-1:0] yval_i;
|
51 |
|
|
output reg [OW-1:0] xval_o;
|
52 |
|
|
output reg [OW-1:0] yval_o;
|
53 |
|
|
output reg [PW-1:0] phase_o;
|
54 |
|
|
output done;
|
55 |
|
|
|
56 |
|
|
integer nn;
|
57 |
|
|
wire [WW-1:0] cordic_angle [0:NSTAGES+2];
|
58 |
|
|
assign cordic_angle[0] = 60'h200000000000000; // 45.000000000000000000000000 deg
|
59 |
|
|
assign cordic_angle[1] = 60'h12e4051d9df3080; // 26.565051177077990018915443 deg
|
60 |
|
|
assign cordic_angle[2] = 60'h09fb385b5ee39e8; // 14.036243467926476924390045 deg
|
61 |
|
|
assign cordic_angle[3] = 60'h051111d41ddd9a4; // 7.125016348901797691439697 deg
|
62 |
|
|
assign cordic_angle[4] = 60'h028b0d430e589b0; // 3.576334374997351517322386 deg
|
63 |
|
|
assign cordic_angle[5] = 60'h0145d7e15904628; // 1.789910608246069401161549 deg
|
64 |
|
|
assign cordic_angle[6] = 60'h00a2f61e5c28263; // 0.895173710211074391551733 deg
|
65 |
|
|
assign cordic_angle[7] = 60'h00517c5511d442b; // 0.447614170860553051145558 deg
|
66 |
|
|
assign cordic_angle[8] = 60'h0028be5346d0c33; // 0.223810500368538084492442 deg
|
67 |
|
|
assign cordic_angle[9] = 60'h00145f2ebb30ab3; // 0.111905677066206896141942 deg
|
68 |
|
|
assign cordic_angle[10] = 60'h000a2f980091ba7; // 0.055952891893803667622276 deg
|
69 |
|
|
assign cordic_angle[11] = 60'h000517cc14a80cb; // 0.027976452617003676193175 deg
|
70 |
|
|
assign cordic_angle[12] = 60'h00028be60cdfec6; // 0.013988227142265016386680 deg
|
71 |
|
|
assign cordic_angle[13] = 60'h000145f306c172f; // 0.006994113675352918273187 deg
|
72 |
|
|
assign cordic_angle[14] = 60'h0000a2f9836ae91; // 0.003497056850704011263936 deg
|
73 |
|
|
assign cordic_angle[15] = 60'h0000517cc1b6ba7; // 0.001748528426980449539466 deg
|
74 |
|
|
assign cordic_angle[16] = 60'h000028be60db85f; // 0.000874264213693780258170 deg
|
75 |
|
|
assign cordic_angle[17] = 60'h0000145f306dc81; // 0.000437132106872334565140 deg
|
76 |
|
|
assign cordic_angle[18] = 60'h00000a2f9836e4a; // 0.000218566053439347843853 deg
|
77 |
|
|
assign cordic_angle[19] = 60'h00000517cc1b726; // 0.000109283026720071498863 deg
|
78 |
|
|
assign cordic_angle[20] = 60'h0000028be60db93; // 0.000054641513360085439772 deg
|
79 |
|
|
assign cordic_angle[21] = 60'h00000145f306dc9; // 0.000027320756680048933720 deg
|
80 |
|
|
assign cordic_angle[22] = 60'h000000a2f9836e4; // 0.000013660378340025242742 deg
|
81 |
|
|
assign cordic_angle[23] = 60'h000000517cc1b72; // 0.000006830189170012718780 deg
|
82 |
|
|
assign cordic_angle[24] = 60'h00000028be60db9; // 0.000003415094585006371248 deg
|
83 |
|
|
assign cordic_angle[25] = 60'h000000145f306dc; // 0.000001707547292503187107 deg
|
84 |
|
|
assign cordic_angle[26] = 60'h0000000a2f9836e; // 0.000000853773646251593765 deg
|
85 |
|
|
assign cordic_angle[27] = 60'h0000000517cc1b7; // 0.000000426886823125796935 deg
|
86 |
|
|
assign cordic_angle[28] = 60'h000000028be60db; // 0.000000213443411562898468 deg
|
87 |
|
|
assign cordic_angle[29] = 60'h0000000145f306d; // 0.000000106721705781449234 deg
|
88 |
|
|
assign cordic_angle[30] = 60'h00000000a2f9836; // 0.000000053360852890724617 deg
|
89 |
|
|
assign cordic_angle[31] = 60'h00000000517cc1b; // 0.000000026680426445362308 deg
|
90 |
|
|
assign cordic_angle[32] = 60'h0000000028be60d; // 0.000000013340213222681154 deg
|
91 |
|
|
assign cordic_angle[33] = 60'h00000000145f306; // 0.000000006670106611340577 deg
|
92 |
|
|
assign cordic_angle[34] = 60'h000000000a2f983; // 0.000000003335053305670289 deg
|
93 |
|
|
assign cordic_angle[35] = 60'h000000000517cc1; // 0.000000001667526652835144 deg
|
94 |
|
|
assign cordic_angle[36] = 60'h00000000028be60; // 0.000000000833763326417572 deg
|
95 |
|
|
assign cordic_angle[37] = 60'h000000000145f30; // 0.000000000416881663208786 deg
|
96 |
|
|
assign cordic_angle[38] = 60'h0000000000a2f98; // 0.000000000208440831604393 deg
|
97 |
|
|
assign cordic_angle[39] = 60'h0000000000517cc; // 0.000000000104220415802197 deg
|
98 |
|
|
assign cordic_angle[40] = 60'h000000000028be6; // 0.000000000052110207901098 deg
|
99 |
|
|
assign cordic_angle[41] = 60'h0000000000145f3; // 0.000000000026055103950549 deg
|
100 |
|
|
assign cordic_angle[42] = 60'h00000000000a2f9; // 0.000000000013027551975275 deg
|
101 |
|
|
assign cordic_angle[43] = 60'h00000000000517c; // 0.000000000006513775987637 deg
|
102 |
|
|
assign cordic_angle[44] = 60'h0000000000028be; // 0.000000000003256887993819 deg
|
103 |
|
|
assign cordic_angle[45] = 60'h00000000000145f; // 0.000000000001628443996909 deg
|
104 |
|
|
assign cordic_angle[46] = 60'h000000000000a2f; // 0.000000000000814221998455 deg
|
105 |
|
|
assign cordic_angle[47] = 60'h000000000000517; // 0.000000000000407110999227 deg
|
106 |
|
|
assign cordic_angle[48] = 60'h00000000000028b; // 0.000000000000203555499614 deg
|
107 |
|
|
assign cordic_angle[49] = 60'h000000000000145; // 0.000000000000101777749807 deg
|
108 |
|
|
assign cordic_angle[50] = 60'h0000000000000a2; // 0.000000000000050888874903 deg
|
109 |
|
|
assign cordic_angle[51] = 60'h000000000000051; // 0.000000000000025444437452 deg
|
110 |
|
|
assign cordic_angle[52] = 60'h000000000000028; // 0.000000000000012722218726 deg
|
111 |
|
|
assign cordic_angle[53] = 60'h000000000000014; // 0.000000000000006361109363 deg
|
112 |
|
|
//gain: 1.646760258121065412240114
|
113 |
|
|
//2^54/gain: 10939296367302552.000000000000000000000000
|
114 |
|
|
//0026dd3b6a10d798
|
115 |
|
|
|
116 |
|
|
reg [7:0] cnt;
|
117 |
|
|
wire signed [(WW-1):0] e_xval, e_yval;
|
118 |
|
|
// Declare variables for all of the separate stages
|
119 |
|
|
reg signed [WW-1:0] xv [0:5];
|
120 |
|
|
reg signed [WW-1:0] yv [0:5];
|
121 |
|
|
reg [PW:0] ph [0:5];
|
122 |
|
|
reg signed [WW-1:0] xv5, yv5;
|
123 |
|
|
reg [PW:0] ph5;
|
124 |
|
|
reg [2:0] cr_rot;
|
125 |
|
|
|
126 |
|
|
assign e_xval = { {xval_i[(IW-1)]}, xval_i, {(WW-IW-1){1'b0}} };
|
127 |
|
|
assign e_yval = { {yval_i[(IW-1)]}, yval_i, {(WW-IW-1){1'b0}} };
|
128 |
|
|
|
129 |
|
|
// Round our result towards even
|
130 |
|
|
wire [WW-1:0] pre_xval, pre_yval;
|
131 |
|
|
|
132 |
|
|
assign pre_xval = xv[4] + {{(OW){1'b0}},xv[4][(WW-OW)],{(WW-OW-1){!xv[4][WW-OW]}}};
|
133 |
|
|
assign pre_yval = yv[4] + {{(OW){1'b0}},yv[4][(WW-OW)],{(WW-OW-1){!yv[4][WW-OW]}}};
|
134 |
|
|
|
135 |
|
|
always_ff @(posedge clk, posedge rst)
|
136 |
|
|
if (rst)
|
137 |
|
|
cnt <= 'd0;
|
138 |
|
|
else begin
|
139 |
|
|
if (ld)
|
140 |
|
|
cnt <= 'd0;
|
141 |
|
|
else if (!done)
|
142 |
|
|
cnt <= cnt + 2'd2;
|
143 |
|
|
end
|
144 |
|
|
|
145 |
|
|
assign done = cnt==8'd64;
|
146 |
|
|
|
147 |
|
|
// cnt equals 10 for the first iteration.
|
148 |
|
|
always_comb
|
149 |
|
|
if (arctan ? ~yv[4][WW-1] : ph[4][PW]) // Negative phase
|
150 |
|
|
begin
|
151 |
|
|
// If the phase is negative, rotate by the
|
152 |
|
|
// CORDIC angle in a clockwise direction.
|
153 |
|
|
xv5 = xv[4] + (yv[4] >>> (cnt - 8'd10));
|
154 |
|
|
yv5 = yv[4] - (xv[4] >>> (cnt - 8'd10));
|
155 |
|
|
ph5 = ph[4] + cordic_angle[cnt-8'd10];
|
156 |
|
|
|
157 |
|
|
end
|
158 |
|
|
else begin
|
159 |
|
|
// On the other hand, if the phase is
|
160 |
|
|
// positive ... rotate in the
|
161 |
|
|
// counter-clockwise direction
|
162 |
|
|
xv5 = xv[4] - (yv[4] >>> (cnt - 8'd10));
|
163 |
|
|
yv5 = yv[4] + (xv[4] >>> (cnt - 8'd10));
|
164 |
|
|
ph5 = ph[4] - cordic_angle[cnt-8'd10];
|
165 |
|
|
end
|
166 |
|
|
|
167 |
|
|
always_ff @(posedge clk, posedge rst)
|
168 |
|
|
if (rst) begin
|
169 |
|
|
xval_o <= 'd0;
|
170 |
|
|
yval_o <= 'd0;
|
171 |
|
|
phase_o <= 'd0;
|
172 |
|
|
for (nn = 0; nn < 6; nn = nn + 1) begin
|
173 |
|
|
xv[nn] <= 'd0;
|
174 |
|
|
yv[nn] <= 'd0;
|
175 |
|
|
ph[nn] <= 'd0;
|
176 |
|
|
end
|
177 |
|
|
end
|
178 |
|
|
else begin
|
179 |
|
|
if (ld) begin
|
180 |
|
|
if (arctan) begin
|
181 |
|
|
// First stage, map to within +/- 45 degrees
|
182 |
|
|
case({xval_i[IW-1], yval_i[IW-1]})
|
183 |
|
|
2'b01: begin // Rotate by -315 degrees
|
184 |
|
|
xv[0] <= e_xval - e_yval;
|
185 |
|
|
yv[0] <= e_xval + e_yval;
|
186 |
|
|
ph[0] <= 60'hE00000000000000;
|
187 |
|
|
end
|
188 |
|
|
2'b10: begin // Rotate by -135 degrees
|
189 |
|
|
xv[0] <= -e_xval + e_yval;
|
190 |
|
|
yv[0] <= -e_xval - e_yval;
|
191 |
|
|
ph[0] <= 60'h300000000000000;
|
192 |
|
|
end
|
193 |
|
|
2'b11: begin // Rotate by -225 degrees
|
194 |
|
|
xv[0] <= -e_xval - e_yval;
|
195 |
|
|
yv[0] <= e_xval - e_yval;
|
196 |
|
|
ph[0] <= 60'h500000000000000; // 19'h50000;
|
197 |
|
|
end
|
198 |
|
|
// 2'b00:
|
199 |
|
|
default: begin // Rotate by -45 degrees
|
200 |
|
|
xv[0] <= e_xval + e_yval;
|
201 |
|
|
yv[0] <= -e_xval + e_yval;
|
202 |
|
|
ph[0] <= 60'h100000000000000;
|
203 |
|
|
end
|
204 |
|
|
endcase
|
205 |
|
|
end
|
206 |
|
|
else begin
|
207 |
|
|
cr_rot <= phase_i[(PW-1):(PW-3)];
|
208 |
|
|
case(phase_i[(PW-1):(PW-3)])
|
209 |
|
|
3'b000:
|
210 |
|
|
begin // 0 .. 45, No change 270 .. 360
|
211 |
|
|
xv[0] <= e_xval;
|
212 |
|
|
yv[0] <= e_yval;
|
213 |
|
|
ph[0] <= phase_i;
|
214 |
|
|
end
|
215 |
|
|
3'b001,3'b010:
|
216 |
|
|
begin // 45 .. 90, 90 .. 135
|
217 |
|
|
xv[0] <= -e_yval;
|
218 |
|
|
yv[0] <= e_xval;
|
219 |
|
|
ph[0] <= phase_i - 60'h400000000000000;
|
220 |
|
|
end
|
221 |
|
|
3'b011:
|
222 |
|
|
begin // 135 .. 180, 180 .. 225
|
223 |
|
|
xv[0] <= -e_xval;
|
224 |
|
|
yv[0] <= -e_yval;
|
225 |
|
|
ph[0] <= phase_i - 60'h800000000000000;
|
226 |
|
|
end
|
227 |
|
|
3'b100:
|
228 |
|
|
begin // 180 .. 225
|
229 |
|
|
xv[0] <= -e_xval;
|
230 |
|
|
yv[0] <= -e_yval;
|
231 |
|
|
ph[0] <= phase_i - 60'h800000000000000;
|
232 |
|
|
end
|
233 |
|
|
3'b101,3'b110:
|
234 |
|
|
begin // 225 .. 270, 270 .. 315
|
235 |
|
|
xv[0] <= e_yval;
|
236 |
|
|
yv[0] <= -e_xval;
|
237 |
|
|
ph[0] <= phase_i - 60'hC00000000000000;
|
238 |
|
|
end
|
239 |
|
|
3'b111:
|
240 |
|
|
begin // 315 .. 360, No change
|
241 |
|
|
xv[0] <= e_xval;
|
242 |
|
|
yv[0] <= e_yval;
|
243 |
|
|
ph[0] <= phase_i;
|
244 |
|
|
ph[0][PW] <= 1'b1; // Make phase negative
|
245 |
|
|
end
|
246 |
|
|
endcase
|
247 |
|
|
end
|
248 |
|
|
end
|
249 |
|
|
xv[1] <= ({{60{xv[0][WW-1]}},xv[0]} * INV_GAIN) >>> 8'd54;
|
250 |
|
|
yv[1] <= ({{60{yv[0][WW-1]}},yv[0]} * INV_GAIN) >>> 8'd54;
|
251 |
|
|
ph[1] <= ph[0];
|
252 |
|
|
xv[2] <= xv[1];
|
253 |
|
|
yv[2] <= yv[1];
|
254 |
|
|
ph[2] <= ph[1];
|
255 |
|
|
xv[3] <= xv[2];
|
256 |
|
|
yv[3] <= yv[2];
|
257 |
|
|
ph[3] <= ph[2];
|
258 |
|
|
if (cnt <= 6'd8) begin
|
259 |
|
|
xv[4] <= xv[3];
|
260 |
|
|
yv[4] <= yv[3];
|
261 |
|
|
ph[4] <= ph[3];
|
262 |
|
|
end
|
263 |
|
|
else if (cnt > 6'd8 && cnt < 8'd60) begin
|
264 |
|
|
if (arctan ? ~yv5[WW-1] : ph5[PW]) // Negative phase
|
265 |
|
|
begin
|
266 |
|
|
// If the phase is negative, rotate by the
|
267 |
|
|
// CORDIC angle in a clockwise direction.
|
268 |
|
|
xv[4] <= xv5 + (yv5 >>> (cnt - 8'd9));
|
269 |
|
|
yv[4] <= yv5 - (xv5 >>> (cnt - 8'd9));
|
270 |
|
|
ph[4] <= ph5 + cordic_angle[cnt-8'd9];
|
271 |
|
|
|
272 |
|
|
end
|
273 |
|
|
else begin
|
274 |
|
|
// On the other hand, if the phase is
|
275 |
|
|
// positive ... rotate in the
|
276 |
|
|
// counter-clockwise direction
|
277 |
|
|
xv[4] <= xv5 - (yv5 >>> (cnt - 8'd9));
|
278 |
|
|
yv[4] <= yv5 + (xv5 >>> (cnt - 8'd9));
|
279 |
|
|
ph[4] <= ph5 - cordic_angle[cnt-8'd9];
|
280 |
|
|
end
|
281 |
|
|
end
|
282 |
|
|
else if (cnt==8'd60) begin
|
283 |
|
|
xv[4] <= xv[4];//({{54{xv[4][WW-1]}},xv[4]} * INV_GAIN) >>> 8'd54;
|
284 |
|
|
yv[4] <= yv[4];//({{54{xv[4][WW-1]}},yv[4]} * INV_GAIN) >>> 8'd54;
|
285 |
|
|
ph[4] <= ph[4];
|
286 |
|
|
end
|
287 |
|
|
else if (cnt==8'd62) begin
|
288 |
|
|
xval_o <= pre_xval[(WW-1):(WW-OW)];
|
289 |
|
|
yval_o <= pre_yval[(WW-1):(WW-OW)];
|
290 |
|
|
phase_o <= ph[4];
|
291 |
|
|
end
|
292 |
|
|
/*
|
293 |
|
|
else if (cnt==8'd64) begin
|
294 |
|
|
case(cr_rot)
|
295 |
|
|
3'd0,3'd7:
|
296 |
|
|
begin
|
297 |
|
|
xval_o <= xval_o;
|
298 |
|
|
yval_o <= yval_o;
|
299 |
|
|
phase_o <= phase_o;
|
300 |
|
|
phase_o[PW] <= 1'b0;
|
301 |
|
|
end
|
302 |
|
|
3'd1,3'd2:
|
303 |
|
|
begin
|
304 |
|
|
xval_o <= xval_o;
|
305 |
|
|
yval_o <= yval_o;
|
306 |
|
|
phase_o <= phase_o + 60'h400000000000000;
|
307 |
|
|
end
|
308 |
|
|
3'd3:
|
309 |
|
|
begin
|
310 |
|
|
xval_o <= xval_o;
|
311 |
|
|
yval_o <= yval_o;
|
312 |
|
|
phase_o <= phase_o + 60'h800000000000000;
|
313 |
|
|
end
|
314 |
|
|
3'd4:
|
315 |
|
|
begin
|
316 |
|
|
xval_o <= xval_o;
|
317 |
|
|
yval_o <= yval_o;
|
318 |
|
|
phase_o <= phase_o + 60'h800000000000000;
|
319 |
|
|
end
|
320 |
|
|
3'd5,3'd6:
|
321 |
|
|
begin
|
322 |
|
|
xval_o <= xval_o;
|
323 |
|
|
yval_o <= yval_o;
|
324 |
|
|
phase_o <= phase_o + 60'hC00000000000000;
|
325 |
|
|
end
|
326 |
|
|
endcase
|
327 |
|
|
end
|
328 |
|
|
*/
|
329 |
|
|
end
|
330 |
|
|
|
331 |
|
|
endmodule
|