Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module main#(parameter n = 8)(output reg[64*n-1:0] out,input[64*n-1:0] in1,input[64*n-1:0] in2);
- wire [64*n-1:0] out_wire;
- wire [64*n-1:0] out1;
- wire [64*n-1:0] out2;
- reg [64*n-1:0] Temp;
- genvar i;
- butterfly#(n)bt1(out1,in1);
- butterfly#(n)bt2(out2,in2);
- function [63:0] mul;
- input [63:0] in1;
- input [63:0] in2;
- reg [127:0] out;
- begin
- out[127:64]= in1[31:0]*in2[31:0]-in1[63:32]*in2[63:32];
- out[63:0]= in1[31:0]*in2[63:32]+in2[31:0]*in1[63:32];
- mul[63:32] = out[105:74]; //real
- mul[31:0] = out[41:10]; // complex
- end
- endfunction
- generate
- for(i=0;i<n;i=i+1)
- begin
- always@(out1 or out2)
- begin
- Temp = Temp>>64;
- Temp[64*n-1:64*n-64] = mul(out1[64*(i+1)-1:64*i], out2[64*(i+1)-1:64*i]);
- end
- end
- endgenerate
- ibutterfly#(n)(out_wire,Temp);
- always@(out_wire)
- out = out_wire;
- endmodule
- module butterfly#(parameter n = 8)(output reg[64*n-1:0] out,input[64*n-1:0] in);
- //parameter n = 8;
- integer i ;
- genvar j;
- reg [32*n-1:0] in_odd;
- reg [32*n-1:0] in_even;
- // reg [32*n-1:0] out_odd ;
- // reg [32*n-1:0] out_even;
- wire [32*n-1:0] out_odd_wire ;
- wire [32*n-1:0] out_even_wire;
- reg [64*n-1:0] tmp_in ;
- always@(in or n)
- begin
- tmp_in = in;
- for(i=0;i<n/2;i=i+1)
- begin
- in_odd = in_odd>>64;
- in_even = in_even>>64;
- in_even[32*n-1:32*n-64] = tmp_in[63:0];
- in_odd[32*n-1:32*n-64] = tmp_in[127:64];
- tmp_in = tmp_in>>128;
- end
- end
- generate
- if(n>1)
- begin
- butterfly#(n/2) but1(out_odd_wire,in_odd);
- butterfly#(n/2) but2(out_even_wire,in_even);
- initial
- $display("%d n is n",n);
- for(j=0;j<n/2;j=j+1)
- begin
- always@(out_even_wire or out_even_wire)
- begin
- out = out >> 64;
- $display("%d is the angel",j);
- out[64*n-1:64*n-64] = add(out_even_wire[64*(j+1)-1:64*j], mul(cordic(360*j/n),out_odd_wire[64*(j+1)-1:64*j]));
- out[32*n-1:32*n-64] = sub(out_even_wire[64*(j+1)-1:64*j] , mul(cordic(360*j/n),out_odd_wire[64*(j+1)-1:64*j]));
- end
- end
- end
- else if(n==1)
- always@(in)
- begin
- $display("%d n is n",n);
- out = in;
- end
- endgenerate
- function [63:0] add;
- input [63:0] in1;
- input [63:0] in2;
- begin
- add[63:32] = in1[63:32]+in2[63:32];
- add[31:0] = in1[31:0]+in2[31:0];
- end
- endfunction
- function [63:0] sub;
- input [63:0] in1;
- input [63:0] in2;
- begin
- sub[63:32] = in1[63:32]-in2[63:32];
- sub[31:0] = in1[31:0]-in2[31:0];
- end
- endfunction
- function [63:0] mul;
- input [63:0] in1;
- input [63:0] in2;
- reg [127:0] out;
- begin
- out[127:64]= in1[31:0]*in2[31:0]-in1[63:32]*in2[63:32];
- out[63:0]= in1[31:0]*in2[63:32]+in2[31:0]*in1[63:32];
- mul[63:32] = out[105:74]; //real
- mul[31:0] = out[41:10]; // complex
- end
- endfunction
- function [63:0] cordic;
- input [31:0] A;
- reg [31:0] sin,cos;
- integer signed x, y, z,x1,y1;
- integer AT[19:0];
- integer a,i;
- begin
- AT[0] = 2949120;
- AT[1] = 1740967;
- AT[2] = 919879;
- AT[3] = 466945;
- AT[4] = 234378;
- AT[5] = 117303;
- AT[6] = 58666;
- AT[7] = 29334;
- AT[8] = 14667;
- AT[9] = 7333;
- AT[10] = 3666;
- AT[11] = 1833;
- AT[12] = 916;
- AT[13] = 458;
- AT[14] = 229;
- AT[15] = 114;
- AT[16] = 57;
- AT[17] = 28;
- AT[18] = 14;
- AT[19] = 7;
- i = 0;
- a = A << 16;
- x = 39796;
- z = 0;
- y = 0;
- for(i=0;i<20;i=i+1)
- begin
- if(z > a)
- begin
- x1 = x - (y >>> i);
- y1 = y + (x >>> i);
- z = z - AT[i];
- end
- else
- begin
- x1 = x + (y >>> i);
- y1 = y - (x >>> i);
- z = z + AT[i];
- end
- x = x1;
- y = y1;
- end
- sin = y[31] ? -y : y;
- cos = x[31] ? -x : x;
- cordic[41:32] = cos[15:4];
- cordic[63:60] = 0;
- cordic[59:42] = cos[31:16];
- cordic[9:0] = sin[15:4];
- cordic[25:10] = sin[31:16];
- cordic[31:26] = 0;
- end
- endfunction
- endmodule
- module ibutterfly#(parameter n = 8)(output reg[64*n-1:0] out,input[64*n-1:0] in);
- //parameter n = 8;
- integer i ;
- genvar j;
- reg [31:0] shift;
- reg [31:0] log_in = n;
- reg [32*n-1:0] in_odd;
- reg [32*n-1:0] in_even;
- // reg [32*n-1:0] out_odd ;
- // reg [32*n-1:0] out_even;
- wire [32*n-1:0] out_odd_wire ;
- wire [32*n-1:0] out_even_wire;
- reg [64*n-1:0] tmp_in ;
- always@(in or n)
- begin
- tmp_in = in;
- for(i=0;i<n/2;i=i+1)
- begin
- in_odd = in_odd>>64;
- in_even = in_even>>64;
- in_even[32*n-1:32*n-64] = tmp_in[63:0];
- in_odd[32*n-1:32*n-64] = tmp_in[127:64];
- tmp_in = tmp_in>>128;
- end
- end
- generate
- if(n>1)
- begin
- ibutterfly#(n/2) ibut1(out_odd_wire,in_odd);
- ibutterfly#(n/2) ibut2(out_even_wire,in_even);
- initial
- $display("%d n is n",n);
- for(j=0;j<n/2;j=j+1)
- begin
- always@(out_even_wire or out_even_wire)
- begin
- shift = log(log_in);
- out = out >> 64;
- $display("%d is the angel",j);
- out[64*n-1:64*n-64] = sub(out_even_wire[64*(j+1)-1:64*j], mul(cordic(360*j/n),out_odd_wire[64*(j+1)-1:64*j]));
- out[32*n-1:32*n-64] = add(out_even_wire[64*(j+1)-1:64*j] , mul(cordic(360*j/n),out_odd_wire[64*(j+1)-1:64*j]));
- out[64*n-1:64*n-32] = out[64*n-1:64*n-32]>>shift;
- out[64*n-33:64*n-64] = out[64*n-33:64*n-64]>>shift;
- out[32*n-1:32*n-32] = out[32*n-1:32*n-32]>>shift;
- out[32*n-33:32*n-64] = out[32*n-33:32*n-64]>>shift;
- end
- end
- end
- else if(n==1)
- always@(in)
- begin
- $display("%d n is n",n);
- out = in;
- end
- endgenerate
- function [31:0] log;
- input [31:0] in;
- integer i;
- reg counter;
- begin
- counter = 0;
- for(i=0;i<32;i=i+1)
- begin
- if(in!=0)
- counter = counter+1;
- in = in>>1;
- end
- log = counter;
- end
- endfunction
- function [63:0] add;
- input [63:0] in1;
- input [63:0] in2;
- begin
- add[63:32] = in1[63:32]+in2[63:32];
- add[31:0] = in1[31:0]+in2[31:0];
- end
- endfunction
- function [63:0] sub;
- input [63:0] in1;
- input [63:0] in2;
- begin
- sub[63:32] = in1[63:32]-in2[63:32];
- sub[31:0] = in1[31:0]-in2[31:0];
- end
- endfunction
- function [63:0] mul;
- input [63:0] in1;
- input [63:0] in2;
- reg [127:0] out;
- begin
- out[127:64]= in1[31:0]*in2[31:0]-in1[63:32]*in2[63:32];
- out[63:0]= in1[31:0]*in2[63:32]+in2[31:0]*in1[63:32];
- mul[63:32] = out[105:74]; //real
- mul[31:0] = out[41:10]; // complex
- end
- endfunction
- function [63:0] cordic;
- input [31:0] A;
- reg [31:0] sin,cos;
- integer signed x, y, z,x1,y1;
- integer AT[19:0];
- integer a,i;
- begin
- AT[0] = 2949120;
- AT[1] = 1740967;
- AT[2] = 919879;
- AT[3] = 466945;
- AT[4] = 234378;
- AT[5] = 117303;
- AT[6] = 58666;
- AT[7] = 29334;
- AT[8] = 14667;
- AT[9] = 7333;
- AT[10] = 3666;
- AT[11] = 1833;
- AT[12] = 916;
- AT[13] = 458;
- AT[14] = 229;
- AT[15] = 114;
- AT[16] = 57;
- AT[17] = 28;
- AT[18] = 14;
- AT[19] = 7;
- i = 0;
- a = A << 16;
- x = 39796;
- z = 0;
- y = 0;
- for(i=0;i<20;i=i+1)
- begin
- if(z > a)
- begin
- x1 = x - (y >>> i);
- y1 = y + (x >>> i);
- z = z - AT[i];
- end
- else
- begin
- x1 = x + (y >>> i);
- y1 = y - (x >>> i);
- z = z + AT[i];
- end
- x = x1;
- y = y1;
- end
- sin = y[31] ? -y : y;
- cos = x[31] ? -x : x;
- cordic[41:32] = cos[15:4];
- cordic[63:60] = 0;
- cordic[59:42] = cos[31:16];
- cordic[9:0] = sin[15:4];
- cordic[25:10] = sin[31:16];
- cordic[31:26] = 0;
- end
- endfunction
- endmodule
- module test;
- reg [64*8-1:0] in;
- wire [64*8-1:0] out;
- butterfly #(8)b(out,in);
- initial
- begin
- in = 256'd0;
- #10
- in = 512'd565833983613456589907598696096983653424;
- end
- endmodule
Add Comment
Please, Sign In to add comment