20 资产
以下是我自己编写的代码,然而仿真结果并不理想,求助大神以及同行交流
module cordic#(parameter DATA_WIDTH=12)
(
input clk,
input rst_n,
input ena,
input [DATA_WIDTH-1:0] x_in,
input [DATA_WIDTH-1:0] y_in,
output reg [DATA_WIDTH-1:0] phase_out,
output reg [DATA_WIDTH-1:0] r,
output reg [DATA_WIDTH-1:0] eps
);
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 [DATA_WIDTH-1:0] x8,y8,z8;
reg [DATA_WIDTH-1:0] x9,y9,z9;
reg [DATA_WIDTH-1:0] x10,y10,z10;
reg [DATA_WIDTH-1:0] x11,y11,z11;
reg [DATA_WIDTH-1:0] x_in_reg;
reg [DATA_WIDTH-1:0] y_in_reg;
reg [DATA_WIDTH-1:0] z_in_reg;
//预处理
always @(posedge clk) //or negedge rst_n)
begin
if(ena)
begin
case({y_in[DATA_WIDTH-1],x_in[DATA_WIDTH-1]})
2'b00:
begin
y_in_reg<=y_in;
x_in_reg<=x_in;
end
2'b01:
begin
y_in_reg<=y_in;
x_in_reg<=x_in-12'b100000000000;
end
2'b10:
begin
y_in_reg<=y_in-12'b100000000000;
x_in_reg<=x_in-12'b100000000000;
end
2'b11:
begin
y_in_reg<=y_in-12'b100000000000;
x_in_reg<=x_in;
end
default;
endcase
end
end
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x0<=12'b0;
y0<=12'b0;
z0<=12'b0;
end
else
if(ena)
begin
x0 <= x_in_reg; //0.60725*2^7
y0 <= y_in_reg;
z0 <= 12'b0;
end
end
//leve1
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x1<=12'b0;
y1<=12'b0;
z1<=12'b0;
end
else
if(ena)
if(y0[DATA_WIDTH-1] ==1'b1)
begin
x1 <= x0-y0; //0.60725*2^7
y1 <= y0+x0;
z1 <= z0-12'h200;
end
else
begin
x1 <= x0+y0; //0.60725*2^7
y1 <= y0-x0;
z1 <= z0+12'h200;
end
end
//leve2
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x2<=12'b0;
y2<=12'b0;
z2<=12'b0;
end
else
if(ena)
if(y1[DATA_WIDTH-1] ==1'b1)
begin
x2 <= x1-{{1{y1[DATA_WIDTH-1]}},y1[DATA_WIDTH-1:1]}; //0.60725*2^7
y2 <= y1+{{1{x1[DATA_WIDTH-1]}},x1[DATA_WIDTH-1:1]};
z2 <= z1-12'h12e;
end
else
begin
x2 <= x1+{{1{y1[DATA_WIDTH-1]}},y1[DATA_WIDTH-1:1]}; //0.60725*2^7
y2 <= y1-{{1{x1[DATA_WIDTH-1]}},x1[DATA_WIDTH-1:1]};
z2 <= z1+12'h12e;
end
end
//leve3
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x3<=12'b0;
y3<=12'b0;
z3<=12'b0;
end
else
if(ena)
if(y2[DATA_WIDTH-1] ==1'b1)
begin
x3 <= x2-{{2{y2[DATA_WIDTH-1]}},y2[DATA_WIDTH-1:2]}; //0.60725*2^7
y3 <= y2+{{2{x2[DATA_WIDTH-1]}},x2[DATA_WIDTH-1:2]};
z3 <= z2-12'h09f;
end
else
begin
x3 <= x2+{{2{y2[DATA_WIDTH-1]}},y2[DATA_WIDTH-1:2]}; //0.60725*2^7
y3 <= y2-{{2{x2[DATA_WIDTH-1]}},x2[DATA_WIDTH-1:2]};
z3 <= z2+12'h09f;
end
end
//leve4
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x4<=12'b0;
y4<=12'b0;
z4<=12'b0;
end
else
if(ena)
if(y3[DATA_WIDTH-1] ==1'b1)
begin
x4<= x3-{{3{y3[DATA_WIDTH-1]}},y3[DATA_WIDTH-1:3]}; //0.60725*2^7
y4 <= y3+{{3{x3[DATA_WIDTH-1]}},x3[DATA_WIDTH-1:3]};
z4 <= z3-12'h051;
end
else
begin
x4 <= x3+{{3{y3[DATA_WIDTH-1]}},y3[DATA_WIDTH-1:3]}; //0.60725*2^7
y4 <= y3-{{3{x3[DATA_WIDTH-1]}},x3[DATA_WIDTH-1:3]};
z4 <= z3+12'h051;
end
end
//leve5
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x5<=12'b0;
y5<=12'b0;
z5<=12'b0;
end
else
if(ena)
if(y4[DATA_WIDTH-1] ==1'b1)
begin
x5<= x4-{{4{y4[DATA_WIDTH-1]}},y4[DATA_WIDTH-1:4]}; //0.60725*2^7
y5 <= y4+{{4{x4[DATA_WIDTH-1]}},x4[DATA_WIDTH-1:4]};
z5 <= z4-12'h028;
end
else
begin
x5 <= x4+{{4{y4[DATA_WIDTH-1]}},y4[DATA_WIDTH-1:4]}; //0.60725*2^7
y5<= y4-{{4{x4[DATA_WIDTH-1]}},x4[DATA_WIDTH-1:4]};
z5 <= z4+12'h028;
end
end
//leve6
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x6<=12'b0;
y6<=12'b0;
z6<=12'b0;
end
else
if(ena)
if(y5[DATA_WIDTH-1] ==1'b1)
begin
x6 <= x5-{{5{y5[DATA_WIDTH-1]}},y5[DATA_WIDTH-1:5]}; //0.60725*2^7
y6 <= y5+{{5{x5[DATA_WIDTH-1]}},x5[DATA_WIDTH-1:5]};
z6 <= z5-12'h014;
end
else
begin
x6 <= x5+{{5{y5[DATA_WIDTH-1]}},y5[DATA_WIDTH-1:5]}; //0.60725*2^7
y6 <= y5-{{5{x5[DATA_WIDTH-1]}},x5[DATA_WIDTH-1:5]};
z6 <= z5+12'h014;
end
end
//leve7
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x7<=12'b0;
y7<=12'b0;
z7<=12'b0;
end
else
if(ena)
if(y6[DATA_WIDTH-1] ==1'b1)
begin
x7 <= x6-{{6{y6[DATA_WIDTH-1]}},y6[DATA_WIDTH-1:6]}; //0.60725*2^7
y7 <= y6+{{6{x6[DATA_WIDTH-1]}},x6[DATA_WIDTH-1:6]};
z7 <= z6-12'h00a;
end
else
begin
x7 <= x6+{{6{y6[DATA_WIDTH-1]}},y6[DATA_WIDTH-1:6]}; //0.60725*2^7
y7 <= y6-{{6{x6[DATA_WIDTH-1]}},x6[DATA_WIDTH-1:6]};
z7 <= z6+12'h00a;
end
end
//leve8
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x8<=12'b0;
y8<=12'b0;
z8<=12'b0;
end
else
if(ena)
if(y7[DATA_WIDTH-1] ==1'b1)
begin
x8 <= x7-{{7{y7[DATA_WIDTH-1]}},y7[DATA_WIDTH-1:7]}; //0.60725*2^7
y8 <= y7+{{7{x7[DATA_WIDTH-1]}},x7[DATA_WIDTH-1:7]};
z8 <= z7-12'h005;
end
else
begin
x8 <= x7+{{7{y7[DATA_WIDTH-1]}},y7[DATA_WIDTH-1:7]}; //0.60725*2^7
y8 <= y7-{{7{x7[DATA_WIDTH-1]}},x7[DATA_WIDTH-1:7]};
z8 <= z7+12'h005;
end
end
//leve9
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x9<=12'b0;
y9<=12'b0;
z9<=12'b0;
end
else
if(ena)
if(y8[DATA_WIDTH-1] ==1'b1)
begin
x9 <= x8-{{8{y8[DATA_WIDTH-1]}},y8[DATA_WIDTH-1:8]}; //0.60725*2^7
y9 <= y8+{{8{x8[DATA_WIDTH-1]}},x8[DATA_WIDTH-1:8]};
z9 <= z8-12'h002;
end
else
begin
x9 <= x8+{{8{y8[DATA_WIDTH-1]}},y8[DATA_WIDTH-1:8]}; //0.60725*2^7
y9 <= y8-{{8{x8[DATA_WIDTH-1]}},x8[DATA_WIDTH-1:8]};
z9 <= z8+12'h002;
end
end
//level0
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x10<=12'b0;
y10<=12'b0;
z10<=12'b0;
end
else
if(ena)
if(y9[DATA_WIDTH-1] ==1'b1)
begin
x10 <= x9-{{9{y9[DATA_WIDTH-1]}},y9[DATA_WIDTH-1:9]}; //0.60725*2^7
y10 <= y9+{{9{x9[DATA_WIDTH-1]}},x9[DATA_WIDTH-1:9]};
z10 <= z10-12'h001;
end
else
begin
x10 <= x9+{{9{y9[DATA_WIDTH-1]}},y9[DATA_WIDTH-1:9]}; //0.60725*2^7
y10 <= y9-{{9{x9[DATA_WIDTH-1]}},x9[DATA_WIDTH-1:9]};
z10 <= z9+12'h001;
end
end
//level1
always @ (posedge clk,negedge rst_n)
begin
if(!rst_n)
begin
x11<=12'b0;
y11<=12'b0;
z11<=12'b0;
end
else
if(ena)
if(y10[DATA_WIDTH-1] ==1'b1)
begin
x11 <= x10-{{10{y10[DATA_WIDTH-1]}},y10[DATA_WIDTH-1:10]}; //0.60725*2^7
y11 <= y10+{{10{x10[DATA_WIDTH-1]}},x10[DATA_WIDTH-1:10]};
z11 <= z10-12'h000;
end
else
begin
x11 <= x10+{{10{y10[DATA_WIDTH-1]}},y10[DATA_WIDTH-1:10]}; //0.60725*2^7
y11 <= y10-{{10{x10[DATA_WIDTH-1]}},x10[DATA_WIDTH-1:10]};
z11 <= z10+12'h000;
end
end
//后处理
always @(posedge clk)//,negedge rst_n)
begin
if(ena)
begin
case({y_in[DATA_WIDTH-1],x_in[DATA_WIDTH-1]})
2'b00:
begin
r<= x11;
eps<=y11;
phase_out<=z11;
end
2'b01:
begin
r<= x11;
eps<=y11;
phase_out<=z10+12'b100000000000;
end
2'b10:
begin
r<= x11;
eps<=y11;
phase_out<=z10+12'b100000000000;
end
2'b11:
begin
r<= x11;
eps<=y11;
phase_out<=z10+12'b100000000000;
end
default;
endcase
end
end
endmodule
我来回答