|
万能的坛子,请教下这个正弦值在过零点和峰值点峰谷点的值是怎么出现异常的~
代码如下:
module cordic
#(parameter DATA_WIDTH=8)
(
input clk,
input rst_n,
input ena,
input [DATA_WIDTH-1:0] phase_in,
output reg [DATA_WIDTH-1:0] sin_out,
output reg [DATA_WIDTH-1:0] cos_out,
output reg [DATA_WIDTH-1:0] eps
);
localparam PIPELINE=8;
reg [DATA_WIDTH-1:0] phase_in_reg;
reg [DATA_WIDTH-1:0] x0,y0,z0;
reg [DATA_WIDTH-1:0] x1,y1,z1;
reg [DATA_WIDTH-1:0] x2,y2,z2;
reg [DATA_WIDTH-1:0] x3,y3,z3;
reg [DATA_WIDTH-1:0] x4,y4,z4;
reg [DATA_WIDTH-1:0] x5,y5,z5;
reg [DATA_WIDTH-1:0] x6,y6,z6;
reg [DATA_WIDTH-1:0] x7,y7,z7;
reg [1:0] quadrant [PIPELINE:0];
integer i;
always @(posedge clk,negedge rst_n)
begin
if(!rst_n)
phase_in_reg <= 8'b0;
else
if(ena)
begin
case(phase_in[7:6])//phase_in/360*256,so phase_in is discrete 360/256=1.40625 degree
2'b00: phase_in_reg <= phase_in; //when phase_in <90 degree,phase_in =phase_in
2'b01: phase_in_reg <= phase_in-8'h40;//when phase_in >=90 degree,phase_in-90
2'b10: phase_in_reg <= phase_in-8'h80;//when phase_in >=180 degree,phase_in-180
2'b11: phase_in_reg <= phase_in-8'hc0;//when phase_in >=270 degree,phase_in-270
default:;
endcase
end
end // this program is intend to turn phase_in into [0,90]
always @ (posedge clk,negedge rst_n)//circul cordic
begin
if(!rst_n)
begin
x0<=8'b0;
y0<=8'b0;
z0<=8'b0;
end
else
if(ena)
begin
x0 <= 8'h4D; //0.60725*2^7,gian K
y0 <= 8'h00;
z0 <= phase_in_reg;//start values of x0,y0,z0,
end
end
//level 1
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x1 <=8'b0;
y1 <=8'b0;
z1 <=8'b0;
end
else
if(ena)
if(z0[7]==1'b0)// if MSB of z0 is not 1,z0 is positive,of course it is true
begin
x1 <= x0-y0;
y1 <= y0+x0;
z1 <=z0-8'h20; //45deg
end
else
begin //z0 is defined as positive value
x1<=x0+y0;
y1<=y0-x0;
z1<=z0+8'h20;
end
end
//level 2
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x2 <=8'b0;
y2 <=8'b0;
z2 <=8'b0;
end
else
if(ena)
if(z1[7]==1'b0)// if z0 is still > 0
begin
x2 <= x1-{y1[DATA_WIDTH-1],y1[DATA_WIDTH-1:1]};//concatation why not shift operation
y2 <= y1+{x1[DATA_WIDTH-1],x1[DATA_WIDTH-1:1]};//make MSB stable
z2 <=z1-8'h12;//26deg
end
else
begin
x2<= x1+{y1[DATA_WIDTH-1],y1[DATA_WIDTH-1:1]};
y2<= y1-{x1[DATA_WIDTH-1],x1[DATA_WIDTH-1:1]};
z2<= z1+8'h12;
end
end
//level 3
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x3 <=8'b0;
y3 <=8'b0;
z3 <=8'b0;
end
else
if(ena)
if(z2[7]==1'b0)
begin
x3 <= x2-{{2{y2[DATA_WIDTH-1]}},y2[DATA_WIDTH-1:2]};//{y2[DATA_WIDTH-1],y2[DATA_WIDTH-1],y2[DATA_WIDTH-1:2]}
y3 <= y2+{{2{x2[DATA_WIDTH-1]}},x2[DATA_WIDTH-1:2]};
z3 <=z2-8'h09;//14deg
end
else
begin
x3<= x2+{{2{y2[DATA_WIDTH-1]}},y2[DATA_WIDTH-1:2]};
y3<= y2-{{2{x2[DATA_WIDTH-1]}},x2[DATA_WIDTH-1:2]};
z3<= z2+8'h09;
end
end
//level 4
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x4 <=8'b0;
y4 <=8'b0;
z4 <=8'b0;
end
else
if(ena)
if(z3[7]==1'b0)
begin
x4 <= x3-{{3{y3[DATA_WIDTH-1]}},y3[DATA_WIDTH-1:3]};
y4 <= y3+{{3{x3[DATA_WIDTH-1]}},x3[DATA_WIDTH-1:3]};
z4 <= z3-8'h04;//7deg
end
else
begin
x4<= x3+{{3{y3[DATA_WIDTH-1]}},y3[DATA_WIDTH-1:3]};
y4<= y3-{{3{x3[DATA_WIDTH-1]}},x3[DATA_WIDTH-1:3]};
z4<= z3+8'h04;
end
end
//level 5
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x5 <=8'b0;
y5 <=8'b0;
z5 <=8'b0;
end
else
if(ena)
if(z4[7]==1'b0)
begin
x5 <= x4-{{4{y4[DATA_WIDTH-1]}},y4[DATA_WIDTH-1:4]};
y5 <= y4+{{4{x4[DATA_WIDTH-1]}},x4[DATA_WIDTH-1:4]};
z5 <= z4-8'h02;//4deg
end
else
begin
x5<= x4+{{4{y4[DATA_WIDTH-1]}},y4[DATA_WIDTH-1:4]};
y5<= y4-{{4{x4[DATA_WIDTH-1]}},x4[DATA_WIDTH-1:4]};
z5<= z4+8'h02;
end
end
//level 6
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x6 <=8'b0;
y6 <=8'b0;
z6 <=8'b0;
end
else
if(ena)
if(z5[7]==1'b0)
begin
x6 <= x5-{{5{y5[DATA_WIDTH-1]}},y5[DATA_WIDTH-1:5]};
y6 <= y5+{{5{x5[DATA_WIDTH-1]}},x5[DATA_WIDTH-1:5]};
z6 <= z5-8'h01;//2deg
end
else
begin
x6<= x5+{{5{y5[DATA_WIDTH-1]}},y5[DATA_WIDTH-1:5]};
y6<= y5-{{5{x5[DATA_WIDTH-1]}},x5[DATA_WIDTH-1:5]};
z6<= z5+8'h01;
end
end
//level 7
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x7 <=8'b0;
y7 <=8'b0;
z7 <=8'b0;
end
else
if(ena)
if(z6[7]==1'b0)
begin
x7 <= x6-{{6{y6[DATA_WIDTH-1]}},y6[DATA_WIDTH-1:6]};
y7 <= y6+{{6{x6[DATA_WIDTH-1]}},x6[DATA_WIDTH-1:6]};
z7 <= z6-8'h00;//2deg
end
else
begin
x7<= x6+{{6{y6[DATA_WIDTH-1]}},y6[DATA_WIDTH-1:6]};
y7<= y6-{{6{x6[DATA_WIDTH-1]}},x6[DATA_WIDTH-1:6]};
z7<= z6+8'h00;
end
end
//-------
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
for(i=0;i<=PIPELINE;i=i+1)
quadrant<=2'b00;
else
if(ena)
begin
for(i=0;i<PIPELINE;i=i+1)
quadrant[i+1]<=quadrant;
quadrant[0]<=phase_in[7:6];
end
end //remark the quadrant of the first eight phases_in
//------------
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
sin_out <= 8'b0;
cos_out <= 8'b0;
eps <= 8'b0;
end
else
if(ena)
case(quadrant[7])//if the phase is in first quadrant,the sin(X)=sin(A),cos(X)=cos(X)
2'b00:
begin
sin_out <= y7;
cos_out <= x7;
eps <= z7;
end
2'b01:
begin //if the phase is in second quadrant,the sin(X)=sin(A+90)=cosA,cos(X)=cos(A+90)=-sinA
sin_out <= x7;
cos_out <= ~(y7)+1'b1;
eps <= z7;
end
2'b10:
begin //if the phase is in third quadrant,the sin(X)=sin(A+180)=-sinA,cos(X)=cos(A+180)=-cosA
sin_out <= ~(y7)+1'b1;
cos_out <= ~(x7)+1'b1;
eps <= z7;
end
2'b11: //if the phase is in forth quadrant,the sin(X)=sin(A+270)=-cosA,cos(X)=cos(A+270)=cosA
begin
sin_out <= ~(x7)+1'b1;
cos_out <= y7;
eps <= z7;
end
endcase
end
endmodule |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|