module cordic(clk, phi, cos, sin);
parameter W = 13, W_Z = 14;
input
clk;
input [W_Z-1:0] phi;
output[W-1:0]
cos, sin;
reg [W-1:0] cos, sin;
reg [W-1:0] x[8:0], y[8:0];
reg [W_Z-1:0] z[7:0];
always @(posedge clk)begin
x[0] <= 13'h4D;
// 修正CORDIC算法的比例因子,An的倒数
y[0] <= 13'h00;
z[0] <= phi;
// 旋转45度
if(z[0][W_Z-1]) begin
x[1] <= x[0] + y[0];
y[1] <= y[0] - x[0];
z[1] <= z[0] + 14'h65;
end
else begin
x[1] <= x[0] - y[0];
y[1] <= y[0] + x[0];
z[1] <= z[0] - 14'h65;
end
// 旋转26.57度
if(z[1][W_Z-1])begin
x[2] <= x[1] + {{1{y[1][W-1]}}, y[1][W-1:1]};
y[2] <= y[1] - {{1{x[1][W-1]}}, x[1][W-1:1]};
z[2] <= z[1] + 14'h3B;
end
else begin
x[2] <= x[1] - {{1{y[1][W-1]}}, y[1][W-1:1]};
y[2] <= y[1] + {{1{x[1][W-1]}}, x[1][W-1:1]};
z[2] <= z[1] - 14'h3B;
end
// 旋转14.04度
if(z[2][W_Z-1])begin
x[3] <= x[2] + {{2{y[2][W-1]}}, y[2][W-1:2]};
y[3] <= y[2] - {{2{x[2][W-1]}}, x[2][W-1:2]};
z[3] <= z[2] + 14'h1F;
end
else begin
x[3] <= x[2] - {{2{y[2][W-1]}}, y[2][W-1:2]};
y[3] <= y[2] + {{2{x[2][W-1]}}, x[2][W-1:2]};
z[3] <= z[2] - 14'h1F;
end
// 旋转7.13度
if(z[3][W_Z-1])begin
x[4] <= x[3] + {{3{y[3][W-1]}}, y[3][W-1:3]};
y[4] <= y[3] - {{3{x[3][W-1]}}, x[3][W-1:3]};
z[4] <= z[3] + 14'h10;
end
else begin
x[4] <= x[3] - {{3{y[3][W-1]}}, y[3][W-1:3]};
y[4] <= y[3] + {{3{x[3][W-1]}}, x[3][W-1:3]};
z[4] <= z[3] - 14'h10;
end
// 旋转3.58度
if(z[4][W_Z-1])begin
x[5] <= x[4] + {{4{y[4][W-1]}}, y[4][W-1:4]};
y[5] <= y[4] - {{4{x[4][W-1]}}, x[4][W-1:4]};
z[5] <= z[4] + 14'h8;
end
else begin
x[5] <= x[4] - {{4{y[4][W-1]}}, y[4][W-1:4]};
y[5] <= y[4] + {{4{x[4][W-1]}}, x[4][W-1:4]};
z[5] <= z[4] - 14'h8;
end
// 旋转1.79度
if(z[5][W_Z-1])begin
x[6] <= x[5] + {{5{y[5][W-1]}}, y[5][W-1:5]};
y[6] <= y[5] - {{5{x[5][W-1]}}, x[5][W-1:5]};
z[6] <= z[5] + 14'h4;
end
else begin
x[6] <= x[5] - {{5{y[5][W-1]}}, y[5][W-1:5]};
y[6] <= y[5] + {{5{x[5][W-1]}}, x[5][W-1:5]};
z[6] <= z[5] - 14'h4;
end
// 旋转0.90度
if(z[6][W_Z-1])begin
x[7] <= x[6] + {{6{y[6][W-1]}}, y[6][W-1:6]};
y[7] <= y[6] - {{6{x[6][W-1]}},
x[6][W-1:6]};
z[7] <= z[6] + 14'h2;
end
else begin
x[7] <= x[6] - {{6{y[6][W-1]}}, y[6][W-1:6]};
y[7] <= y[6] + {{6{x[6][W-1]}}, x[6][W-1:6]};
z[7] <= z[6] - 14'h2;
end
// 旋转0.45度
if(z[7][W_Z-1])begin
x[8] <= x[7] + {{7{y[7][W-1]}}, y[7][W-1:7]};
y[8] <= y[7] - {{7{x[7][W-1]}}, x[7][W-1:7]};
end
else begin
x[8] <= x[7] - {{7{y[7][W-1]}}, y[7][W-1:7]};
y[8] <= y[7] + {{7{x[7][W-1]}}, x[7][W-1:7]};
end
cos <= x[8];
sin <= y[8];
end
endmodule |