xinxin3219 发表于 2014-6-30 17:13:42

cordic计算sin,cos出错

万能的坛子,请教下这个正弦值在过零点和峰值点峰谷点的值是怎么出现异常的~{:cry:}
代码如下:
module cordic
#(parameter DATA_WIDTH=8)
(
    input clk,
    input rst_n,
    input ena,
    input phase_in,
    output reg sin_out,
    output reg cos_out,
    output reg eps
);

localparam PIPELINE=8;

reg phase_in_reg;
reg x0,y0,z0;
reg x1,y1,z1;
reg x2,y2,z2;
reg x3,y3,z3;
reg x4,y4,z4;
reg x5,y5,z5;
reg x6,y6,z6;
reg x7,y7,z7;

reg quadrant ;
integer i;

always @(posedge clk,negedge rst_n)
begin
    if(!rst_n)
      phase_in_reg <= 8'b0;
    else
      if(ena)
      begin
            case(phase_in)//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

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==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==1'b0)// if z0 is still > 0
      begin
            x2 <= x1-{y1,y1};//concatation   why not shift operation
            y2 <= y1+{x1,x1};//make MSB stable
            z2 <=z1-8'h12;//26deg
      end
      else
      begin
            x2<= x1+{y1,y1};
            y2<= y1-{x1,x1};
            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==1'b0)
      begin
            x3 <= x2-{{2{y2}},y2};//{y2,y2,y2}
            y3 <= y2+{{2{x2}},x2};
            z3 <=z2-8'h09;//14deg
      end
      else
      begin
            x3<= x2+{{2{y2}},y2};
            y3<= y2-{{2{x2}},x2};
            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==1'b0)
      begin
            x4 <= x3-{{3{y3}},y3};
            y4 <= y3+{{3{x3}},x3};
            z4 <= z3-8'h04;//7deg
      end
      else
      begin
            x4<= x3+{{3{y3}},y3};
            y4<= y3-{{3{x3}},x3};
            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==1'b0)
      begin
            x5 <= x4-{{4{y4}},y4};
            y5 <= y4+{{4{x4}},x4};
            z5 <= z4-8'h02;//4deg
      end
      else
      begin
            x5<= x4+{{4{y4}},y4};
            y5<= y4-{{4{x4}},x4};
            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==1'b0)
      begin
            x6 <= x5-{{5{y5}},y5};
            y6 <= y5+{{5{x5}},x5};
            z6 <= z5-8'h01;//2deg
      end
      else
      begin
            x6<= x5+{{5{y5}},y5};
            y6<= y5-{{5{x5}},x5};
            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==1'b0)
      begin
            x7 <= x6-{{6{y6}},y6};
            y7 <= y6+{{6{x6}},x6};
            z7 <= z6-8'h00;//2deg
      end
      else
      begin
            x7<= x6+{{6{y6}},y6};
            y7<= y6-{{6{x6}},x6};
            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<=quadrant;
                quadrant<=phase_in;
            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)//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

huabutterfly 发表于 2014-6-30 20:39:37

抢沙发                     

bigZ 发表于 2014-6-30 20:46:35

LZ的分析仪是什么型号?

linjpxt 发表于 2014-6-30 23:47:10

Eps是什么啊

xinxin3219 发表于 2014-7-1 10:30:38

bigZ 发表于 2014-6-30 20:46
LZ的分析仪是什么型号?

modelsim6.5b

xinxin3219 发表于 2014-7-1 10:30:56

linjpxt 发表于 2014-6-30 23:47
Eps是什么啊

剩余的旋转角度
页: [1]
查看完整版本: cordic计算sin,cos出错