Guest User

Untitled

a guest
Jun 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. module main#(parameter n = 8)(output reg[64*n-1:0] out,input[64*n-1:0] in1,input[64*n-1:0] in2);
  2.  
  3. wire [64*n-1:0] out_wire;
  4. wire [64*n-1:0] out1;
  5. wire [64*n-1:0] out2;
  6. reg [64*n-1:0] Temp;
  7. genvar i;
  8.  
  9. butterfly#(n)bt1(out1,in1);
  10. butterfly#(n)bt2(out2,in2);
  11. function [63:0] mul;
  12.  
  13. input [63:0] in1;
  14. input [63:0] in2;
  15. reg [127:0] out;
  16.  
  17. begin
  18. out[127:64]= in1[31:0]*in2[31:0]-in1[63:32]*in2[63:32];
  19. out[63:0]= in1[31:0]*in2[63:32]+in2[31:0]*in1[63:32];
  20.  
  21. mul[63:32] = out[105:74]; //real
  22. mul[31:0] = out[41:10]; // complex
  23. end
  24.  
  25. endfunction
  26.  
  27. generate
  28. for(i=0;i<n;i=i+1)
  29. begin
  30. always@(out1 or out2)
  31. begin
  32. Temp = Temp>>64;
  33. Temp[64*n-1:64*n-64] = mul(out1[64*(i+1)-1:64*i], out2[64*(i+1)-1:64*i]);
  34. end
  35. end
  36. endgenerate
  37.  
  38. ibutterfly#(n)(out_wire,Temp);
  39.  
  40. always@(out_wire)
  41. out = out_wire;
  42. endmodule
  43.  
  44. module butterfly#(parameter n = 8)(output reg[64*n-1:0] out,input[64*n-1:0] in);
  45.  
  46. //parameter n = 8;
  47. integer i ;
  48. genvar j;
  49.  
  50. reg [32*n-1:0] in_odd;
  51. reg [32*n-1:0] in_even;
  52.  
  53. // reg [32*n-1:0] out_odd ;
  54. // reg [32*n-1:0] out_even;
  55.  
  56. wire [32*n-1:0] out_odd_wire ;
  57. wire [32*n-1:0] out_even_wire;
  58.  
  59. reg [64*n-1:0] tmp_in ;
  60.  
  61.  
  62. always@(in or n)
  63. begin
  64.  
  65. tmp_in = in;
  66.  
  67. for(i=0;i<n/2;i=i+1)
  68. begin
  69.  
  70. in_odd = in_odd>>64;
  71. in_even = in_even>>64;
  72.  
  73. in_even[32*n-1:32*n-64] = tmp_in[63:0];
  74. in_odd[32*n-1:32*n-64] = tmp_in[127:64];
  75. tmp_in = tmp_in>>128;
  76.  
  77. end
  78.  
  79. end
  80.  
  81. generate
  82. if(n>1)
  83. begin
  84. butterfly#(n/2) but1(out_odd_wire,in_odd);
  85. butterfly#(n/2) but2(out_even_wire,in_even);
  86. initial
  87. $display("%d n is n",n);
  88. for(j=0;j<n/2;j=j+1)
  89. begin
  90.  
  91. always@(out_even_wire or out_even_wire)
  92. begin
  93. out = out >> 64;
  94. $display("%d is the angel",j);
  95. 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]));
  96. 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]));
  97. end
  98. end
  99. end
  100. else if(n==1)
  101. always@(in)
  102. begin
  103. $display("%d n is n",n);
  104. out = in;
  105. end
  106. endgenerate
  107.  
  108. function [63:0] add;
  109.  
  110. input [63:0] in1;
  111. input [63:0] in2;
  112. begin
  113. add[63:32] = in1[63:32]+in2[63:32];
  114. add[31:0] = in1[31:0]+in2[31:0];
  115. end
  116. endfunction
  117.  
  118. function [63:0] sub;
  119.  
  120. input [63:0] in1;
  121. input [63:0] in2;
  122. begin
  123. sub[63:32] = in1[63:32]-in2[63:32];
  124. sub[31:0] = in1[31:0]-in2[31:0];
  125. end
  126. endfunction
  127.  
  128. function [63:0] mul;
  129.  
  130. input [63:0] in1;
  131. input [63:0] in2;
  132. reg [127:0] out;
  133.  
  134. begin
  135. out[127:64]= in1[31:0]*in2[31:0]-in1[63:32]*in2[63:32];
  136. out[63:0]= in1[31:0]*in2[63:32]+in2[31:0]*in1[63:32];
  137.  
  138. mul[63:32] = out[105:74]; //real
  139. mul[31:0] = out[41:10]; // complex
  140. end
  141.  
  142. endfunction
  143.  
  144. function [63:0] cordic;
  145. input [31:0] A;
  146. reg [31:0] sin,cos;
  147.  
  148. integer signed x, y, z,x1,y1;
  149. integer AT[19:0];
  150. integer a,i;
  151. begin
  152. AT[0] = 2949120;
  153. AT[1] = 1740967;
  154. AT[2] = 919879;
  155. AT[3] = 466945;
  156. AT[4] = 234378;
  157. AT[5] = 117303;
  158. AT[6] = 58666;
  159. AT[7] = 29334;
  160. AT[8] = 14667;
  161. AT[9] = 7333;
  162. AT[10] = 3666;
  163. AT[11] = 1833;
  164. AT[12] = 916;
  165. AT[13] = 458;
  166. AT[14] = 229;
  167. AT[15] = 114;
  168. AT[16] = 57;
  169. AT[17] = 28;
  170. AT[18] = 14;
  171. AT[19] = 7;
  172.  
  173. i = 0;
  174. a = A << 16;
  175. x = 39796;
  176. z = 0;
  177. y = 0;
  178. for(i=0;i<20;i=i+1)
  179. begin
  180. if(z > a)
  181. begin
  182. x1 = x - (y >>> i);
  183. y1 = y + (x >>> i);
  184. z = z - AT[i];
  185. end
  186. else
  187. begin
  188. x1 = x + (y >>> i);
  189. y1 = y - (x >>> i);
  190. z = z + AT[i];
  191. end
  192. x = x1;
  193. y = y1;
  194. end
  195. sin = y[31] ? -y : y;
  196. cos = x[31] ? -x : x;
  197. cordic[41:32] = cos[15:4];
  198. cordic[63:60] = 0;
  199. cordic[59:42] = cos[31:16];
  200. cordic[9:0] = sin[15:4];
  201. cordic[25:10] = sin[31:16];
  202. cordic[31:26] = 0;
  203. end
  204. endfunction
  205.  
  206.  
  207. endmodule
  208.  
  209. module ibutterfly#(parameter n = 8)(output reg[64*n-1:0] out,input[64*n-1:0] in);
  210.  
  211. //parameter n = 8;
  212. integer i ;
  213. genvar j;
  214.  
  215. reg [31:0] shift;
  216. reg [31:0] log_in = n;
  217.  
  218. reg [32*n-1:0] in_odd;
  219. reg [32*n-1:0] in_even;
  220.  
  221. // reg [32*n-1:0] out_odd ;
  222. // reg [32*n-1:0] out_even;
  223.  
  224. wire [32*n-1:0] out_odd_wire ;
  225. wire [32*n-1:0] out_even_wire;
  226.  
  227. reg [64*n-1:0] tmp_in ;
  228.  
  229.  
  230. always@(in or n)
  231. begin
  232.  
  233. tmp_in = in;
  234.  
  235. for(i=0;i<n/2;i=i+1)
  236. begin
  237.  
  238. in_odd = in_odd>>64;
  239. in_even = in_even>>64;
  240.  
  241. in_even[32*n-1:32*n-64] = tmp_in[63:0];
  242. in_odd[32*n-1:32*n-64] = tmp_in[127:64];
  243. tmp_in = tmp_in>>128;
  244.  
  245. end
  246.  
  247. end
  248.  
  249. generate
  250. if(n>1)
  251. begin
  252. ibutterfly#(n/2) ibut1(out_odd_wire,in_odd);
  253. ibutterfly#(n/2) ibut2(out_even_wire,in_even);
  254. initial
  255. $display("%d n is n",n);
  256. for(j=0;j<n/2;j=j+1)
  257. begin
  258.  
  259. always@(out_even_wire or out_even_wire)
  260. begin
  261. shift = log(log_in);
  262. out = out >> 64;
  263. $display("%d is the angel",j);
  264. 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]));
  265. 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]));
  266. out[64*n-1:64*n-32] = out[64*n-1:64*n-32]>>shift;
  267. out[64*n-33:64*n-64] = out[64*n-33:64*n-64]>>shift;
  268. out[32*n-1:32*n-32] = out[32*n-1:32*n-32]>>shift;
  269. out[32*n-33:32*n-64] = out[32*n-33:32*n-64]>>shift;
  270. end
  271. end
  272. end
  273. else if(n==1)
  274. always@(in)
  275. begin
  276. $display("%d n is n",n);
  277. out = in;
  278. end
  279. endgenerate
  280.  
  281. function [31:0] log;
  282. input [31:0] in;
  283. integer i;
  284. reg counter;
  285. begin
  286. counter = 0;
  287. for(i=0;i<32;i=i+1)
  288. begin
  289. if(in!=0)
  290. counter = counter+1;
  291. in = in>>1;
  292. end
  293. log = counter;
  294. end
  295. endfunction
  296.  
  297.  
  298. function [63:0] add;
  299.  
  300. input [63:0] in1;
  301. input [63:0] in2;
  302. begin
  303. add[63:32] = in1[63:32]+in2[63:32];
  304. add[31:0] = in1[31:0]+in2[31:0];
  305. end
  306. endfunction
  307.  
  308. function [63:0] sub;
  309.  
  310. input [63:0] in1;
  311. input [63:0] in2;
  312. begin
  313. sub[63:32] = in1[63:32]-in2[63:32];
  314. sub[31:0] = in1[31:0]-in2[31:0];
  315. end
  316. endfunction
  317.  
  318. function [63:0] mul;
  319.  
  320. input [63:0] in1;
  321. input [63:0] in2;
  322. reg [127:0] out;
  323.  
  324. begin
  325. out[127:64]= in1[31:0]*in2[31:0]-in1[63:32]*in2[63:32];
  326. out[63:0]= in1[31:0]*in2[63:32]+in2[31:0]*in1[63:32];
  327.  
  328. mul[63:32] = out[105:74]; //real
  329. mul[31:0] = out[41:10]; // complex
  330. end
  331.  
  332. endfunction
  333.  
  334. function [63:0] cordic;
  335. input [31:0] A;
  336. reg [31:0] sin,cos;
  337.  
  338. integer signed x, y, z,x1,y1;
  339. integer AT[19:0];
  340. integer a,i;
  341. begin
  342. AT[0] = 2949120;
  343. AT[1] = 1740967;
  344. AT[2] = 919879;
  345. AT[3] = 466945;
  346. AT[4] = 234378;
  347. AT[5] = 117303;
  348. AT[6] = 58666;
  349. AT[7] = 29334;
  350. AT[8] = 14667;
  351. AT[9] = 7333;
  352. AT[10] = 3666;
  353. AT[11] = 1833;
  354. AT[12] = 916;
  355. AT[13] = 458;
  356. AT[14] = 229;
  357. AT[15] = 114;
  358. AT[16] = 57;
  359. AT[17] = 28;
  360. AT[18] = 14;
  361. AT[19] = 7;
  362.  
  363. i = 0;
  364. a = A << 16;
  365. x = 39796;
  366. z = 0;
  367. y = 0;
  368. for(i=0;i<20;i=i+1)
  369. begin
  370. if(z > a)
  371. begin
  372. x1 = x - (y >>> i);
  373. y1 = y + (x >>> i);
  374. z = z - AT[i];
  375. end
  376. else
  377. begin
  378. x1 = x + (y >>> i);
  379. y1 = y - (x >>> i);
  380. z = z + AT[i];
  381. end
  382. x = x1;
  383. y = y1;
  384. end
  385. sin = y[31] ? -y : y;
  386. cos = x[31] ? -x : x;
  387. cordic[41:32] = cos[15:4];
  388. cordic[63:60] = 0;
  389. cordic[59:42] = cos[31:16];
  390. cordic[9:0] = sin[15:4];
  391. cordic[25:10] = sin[31:16];
  392. cordic[31:26] = 0;
  393. end
  394. endfunction
  395.  
  396.  
  397. endmodule
  398.  
  399. module test;
  400. reg [64*8-1:0] in;
  401. wire [64*8-1:0] out;
  402. butterfly #(8)b(out,in);
  403. initial
  404. begin
  405. in = 256'd0;
  406. #10
  407. in = 512'd565833983613456589907598696096983653424;
  408. end
  409. endmodule
Add Comment
Please, Sign In to add comment