Advertisement
Andrei_M

cordic unit

Dec 10th, 2019
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module atan_const
  2.  (
  3.   input [3:0] itr ,
  4.   output reg [15:0] atan
  5.   );
  6.  
  7. always @ (*)
  8.   case (itr)
  9.     4'h0 : atan=16'h3244 ;
  10.     4'h1 : atan=16'h1dac ;
  11.     4'h2 : atan=16'h0fae;
  12.     4'h3 : atan=16'h07f5;
  13.     4'h4 : atan=16'h03ff;
  14.     4'h5 : atan=16'h0200;
  15.     4'h6 : atan=16'h0100;
  16.     4'h7 : atan=16'h0080;
  17.     4'h8 : atan=16'h0040;
  18.     4'h9 : atan=16'h0020;
  19.     4'h10 : atan=16'h0010;
  20.     4'h11 : atan=16'h0008;
  21.     4'h12 : atan=16'h0004;
  22.     4'h13 : atan=16'h0002;
  23.     4'h14 : atan=16'h0001;
  24.     default : atan=16'h0000 ;
  25.   endcase
  26. endmodule
  27.  
  28. module rgst #( parameter w = 8 )
  29. (
  30.   input clk,
  31.   input rst_b,
  32.   input [w-1:0] d,
  33.   input ld,
  34.   input clr,
  35.   output reg [w-1:0] q
  36. );
  37.    
  38. always @ (posedge clk, negedge rst_b)
  39.   if (!rst_b)
  40.     q <= 1'd0;
  41.   else
  42.     if (clr)
  43.       q <= 1'd0;
  44.   else
  45.     if (ld)
  46.       q <= d;
  47. endmodule
  48.  
  49. module cntr #(parameter w = 8)
  50.   (
  51.   input clk,
  52.   input rst_b,
  53.   input c_up,
  54.   input clr,
  55.   output reg [w-1 :0] q
  56.   );
  57.  
  58. always @ (posedge clk, negedge rst_b)
  59.     if (!rst_b)  
  60.       q <= 1'd0;
  61.     else
  62.       if (clr)
  63.        q <= 1'd0;
  64.       else
  65.         if(c_up)
  66.           q <= q + 1'd1;
  67. endmodule
  68.  
  69. module CU
  70.   (
  71.   input [3:0] itr,
  72.   input bgn, clk, rst_b,
  73.   output reg fin, init, ld
  74.   );
  75.  
  76.   localparam WAIT = 2'd0;
  77.   localparam EXEC = 2'd1;
  78.   localparam END = 2'd2;
  79.  
  80.   reg [1:0] st, st_next;
  81.   always @(*)
  82.     case(st)
  83.       WAIT: if(!bgn) st_next = WAIT;  
  84.             else if(bgn) st_next = EXEC;
  85.              
  86.       EXEC: if(itr == 15) st_next = END;
  87.             else st_next = EXEC;
  88.      
  89.       END: st_next = WAIT;
  90.      
  91.     endcase
  92.    
  93.     always @(*)
  94.       begin
  95.         fin = 1'd0;
  96.         init =  1'd0;
  97.         ld = 1'd0;
  98.        
  99.         case(st)
  100.           WAIT: if(bgn)
  101.           begin
  102.                   ld = 1;
  103.                   init = 1;    
  104.            end
  105.           EXEC: ld = 1;
  106.          
  107.           END: fin = 1;
  108.      
  109.         endcase    
  110.       end  
  111.      
  112.     always @ (posedge clk, negedge rst_b)
  113.     if (!rst_b)  
  114.       st <= WAIT;
  115.     else  
  116.       st <= st_next;
  117. endmodule
  118.  
  119.  
  120. module cordic_unit
  121.   (
  122.     input [15:0] theta,
  123.     input bgn, clk, rst_b,
  124.     output[15:0] cos,
  125.     output fin
  126.   );
  127.  
  128.   wire [15:0] x_out, x_new, y_out, y_new, z_out, z_new, atan;
  129.   wire ld, init;
  130.   wire [3:0] itr;
  131.  
  132.   atan_const a0( .itr(itr), .atan(atan));
  133.   CU a1( .clk(clk), .rst_b(rst_b), .bgn(bgn), .itr(itr), .fin(fin), .ld(ld), .init(init));
  134.  
  135.   cntr #(.w(4)) a2
  136.   (
  137.     .clk(clk),
  138.     .c_up(ld),
  139.     .clr(init),
  140.     .q(itr),
  141.     .rst_b(rst_b)
  142.    
  143.   );
  144.  
  145.   rgst #(.w(16)) a3
  146.   (
  147.     .clk(clk),
  148.     .rst_b(rst_b),
  149.     .clr(0),
  150.     .ld(ld),
  151.     .d(init) ? 16'h26dd : x_new,
  152.     .q(x_out)
  153.   );
  154.  
  155.   rgst #(.w(16)) a4
  156.   (
  157.     .clk(clk),
  158.     .rst_b(rst_b),
  159.     .clr(0),
  160.     .ld(ld),
  161.     .d(init) ? 16'h26dd : y_new,
  162.     .q(y_out)
  163.   );
  164.  
  165.   rgst #(.w(16)) a5
  166.   (
  167.     .clk(clk),
  168.     .rst_b(rst_b),
  169.     .clr(0),
  170.     .ld(ld),
  171.     .d(init) ? 16'h26dd : z_new,
  172.     .q(z_out)
  173.   );
  174.  
  175.   assign x_new = z_out[15] ? x_out + $unsigned($signed(y_out) >>> itr) : x_out - $unsigned($signed(y_out) >>> itr);
  176.   assign y_new = z_out[15] ? y_out - $unsigned($signed(x_out) >>> itr) : y_out + $unsigned($signed(x_out) >>> itr);
  177.   assign z_new = z_out[15] ?
  178.  
  179. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement