Re: generate loop
here i posted my code, it consists three modules they are signed_shifter, rotater and cordic which is the top entity.....my doubt is in last of cordic module....
module signed_shifter (
input wire [`ITERATION_BITS-1:0] i,
input wire signed [`XY_BITS:0] D,
output reg signed [`XY_BITS:0] Q );
integer j;
always @ * begin
Q = D;
for(j=0;j<i;j=j+1) Q = (Q >> 1) | (D[`XY_BITS] << `XY_BITS);
end
endmodule
module rotator (
input wire clk,
input wire rst,
input wire signed [`XY_BITS:0] x_i,
input wire signed [`XY_BITS:0] y_i,
input wire signed [`THETA_BITS:0] z_i,
output wire signed [`XY_BITS:0] x_o,
output wire signed [`XY_BITS:0] y_o,
output wire signed [`THETA_BITS:0] z_o
);
parameter integer iteration = 0;
parameter signed [`THETA_BITS:0] tangle = 0;
reg signed [`XY_BITS:0] x_1;
reg signed [`XY_BITS:0] y_1;
reg signed [`THETA_BITS:0] z_1;
wire signed [`XY_BITS:0] x_i_shifted;
wire signed [`XY_BITS:0] y_i_shifted;
signed_shifter x_shifter(iteration,x_i,x_i_shifted);
signed_shifter y_shifter(iteration,y_i,y_i_shifted);
always @ *
if (rst) begin
x_1 <= 0;
y_1 <= 0;
z_1 <= 0;
end
else begin
if (z_i < 0) begin
x_1 <= x_i + y_i_shifted; //shifter(y_1,i); //(y_1 >> i);
y_1 <= y_i - x_i_shifted; //shifter(x_1,i); //(x_1 >> i);
z_1 <= z_i + tangle;
end
else begin
x_1 <= x_i - y_i_shifted; //shifter(y_1,i); //(y_1 >> i);
y_1 <= y_i + x_i_shifted; //shifter(x_1,i); //(x_1 >> i);
z_1 <= z_i - tangle;
end
end
assign x_o = x_1;
assign y_o = y_1;
assign z_o = z_1;
endmodule
//Top Entity
module cordic (
input wire clk,
input wire rst,
input wire signed [`XY_BITS:0] x_i,
input wire signed [`XY_BITS:0] y_i,
input wire signed [`THETA_BITS:0] theta_i,
output wire signed [`XY_BITS:0] x_o,
output wire signed [`XY_BITS:0] y_o,
output wire signed [`THETA_BITS:0] theta_o
);
function [`THETA_BITS:0] tanangle;
input [3:0] i;
begin
case (i)
0: tanangle = 17'd11520; // theta = 45.000000
1: tanangle = 17'd6800; // theta = 22.500000
2: tanangle = 17'd3593; // theta = 11.250000
3: tanangle = 17'd1824; // theta = 5.625000
4: tanangle = 17'd915; // theta = 2.812500
5: tanangle = 17'd458; // theta = 1.406250
6: tanangle = 17'd229; // theta = 0.703125
7: tanangle = 17'd114; // theta = 0.351562
8: tanangle = 17'd57; // theta = 0.175781
9: tanangle = 17'd28; // theta = 0.087891
10: tanangle = 17'd14; // theta = 0.043945
11: tanangle = 17'd7; // theta = 0.021973
12: tanangle = 17'd3; // theta = 0.010986
13: tanangle = 17'd1; // theta = 0.005493
14: tanangle = 17'd0; // theta = 0.002747
15: tanangle = 17'd0; // theta = 0.001373
endcase
end
endfunction
wire signed [`XY_BITS:0] x [`ITERATIONS-1:0];
wire signed [`XY_BITS:0] y [`ITERATIONS-1:0];
wire signed [`THETA_BITS:0] z [`ITERATIONS-1:0];
assign x[0] = x_i;
assign y[0] = y_i;
assign z[0] = theta_i;
assign x_o = x[`ITERATIONS-1];
assign y_o = y[`ITERATIONS-1];
assign theta_o = z[`ITERATIONS-1];
genvar i;
generate
for(i=0;i<`ITERATIONS-1;i=i+1) begin:Loop1
rotator U (clk,rst,x,y,z,x[i+1],y[i+1],z[i+1]);
defparam U.iteration = i;
defparam U.tangle = tanangle(i);
end
endgenerate
endmodule