RenHao

CIC_pretest

Mar 17th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module SET ( clk , rst, en, central, radius, mode, busy, valid, candidate );
  2.  
  3. input clk, rst;
  4. input en;
  5. input [23:0] central; // [23:20] Xa [19:16] Ya ; [15:12] Xb [11:8] Yb ;
  6.                              // remain the rest of it
  7. input [11:0] radius;  // [11:8] ra ; [7:4] rb ; remain the rest of it
  8. input [1:0] mode;
  9.  
  10. output reg busy;    //high > busy
  11. output reg valid;   //high > output
  12. output [7:0] candidate;
  13.  
  14. reg [7:0] candidate_tmp,candidate_tmp1,candidate_tmp2;
  15. reg [3:0]cnt_x,cnt_y;
  16. reg [7:0]X_ptr,Y_ptr;
  17. reg [9:0]r;
  18.  
  19. assign  candidate[7:0] = candidate_tmp[7:0];
  20.  
  21. /* To run all the points */
  22. always@(posedge clk or posedge rst)
  23. begin
  24.     if(rst)
  25.     begin
  26.         cnt_x <= 4'h1;
  27.         cnt_y <= 4'h1;
  28.         busy <= 1'b0;
  29.         valid <= 1'b0;
  30.     end
  31.     else if(en)
  32.     begin
  33.         busy <= 1'b1;
  34.         valid <= 1'b0;
  35.         cnt_x <= 4'h1;
  36.         cnt_y <= 4'h1;
  37.     end
  38.     else
  39.     begin
  40.         if(cnt_x == 4'h8 && cnt_y == 4'h8)
  41.         begin
  42.                 busy <= 1'b0;
  43.                 valid <= 1'b1;
  44.                 cnt_x <= 4'h1;
  45.                 cnt_y <= 4'h1;
  46.         end
  47.        
  48.         else if(cnt_x == 4'h8)
  49.         begin
  50.             cnt_x <= 4'h1;
  51.             cnt_y <= cnt_y + 1;
  52.         end
  53.        
  54.         else if(cnt_y == 4'h8)
  55.         begin
  56.             cnt_x <= cnt_x + 1;
  57.         end
  58.        
  59.         else
  60.             cnt_x <= cnt_x + 1;    
  61.     end
  62. end
  63.  
  64. /* Modes */
  65. always@(posedge clk or posedge rst)
  66. begin
  67.     if(rst)
  68.     begin
  69.         candidate_tmp <= 8'b0;
  70.     end
  71.     else if(en)
  72.     begin
  73.         candidate_tmp <= 8'b0;
  74.         candidate_tmp1 <= 8'b0;
  75.         candidate_tmp2 <= 8'b0;
  76.     //A
  77.         X_ptr[7:4] <=  central[23:20];
  78.         Y_ptr[7:4] <=  central[19:16];
  79.         r[9:5] <= radius[11:8];
  80.     //B
  81.         X_ptr[3:0] <=  central[15:12];
  82.         Y_ptr[3:0] <=  central[11:8];
  83.         r[4:0] <= radius[7:4];
  84.     end
  85.     else
  86.     begin
  87.         case(mode)
  88.             2'b00:begin //set A and set B
  89.                 if( ((X_ptr[7:4]*X_ptr[7:4]-2*X_ptr[7:4]*cnt_x[3:0]+cnt_x[3:0]*cnt_x[3:0])
  90.                     + (Y_ptr[7:4]*Y_ptr[7:4]+cnt_y[3:0]*cnt_y[3:0]-2*cnt_y[3:0]*Y_ptr[7:4])) <= (r[9:5]*r[9:5]) )
  91.                     candidate_tmp <= candidate_tmp+1;
  92.             end
  93.                 // Use a2-2ab+b2 to replace (a-b)2 to avoid negative situation
  94.            
  95.             2'b01:begin //set A & B
  96.                 if( (((
  97.                  (X_ptr[7:4]*X_ptr[7:4]-2*X_ptr[7:4]*cnt_x[3:0]+cnt_x[3:0]*cnt_x[3:0])
  98.                 +(Y_ptr[7:4]*Y_ptr[7:4]+cnt_y[3:0]*cnt_y[3:0]-2*cnt_y[3:0]*Y_ptr[7:4]))) <= (r[9:5]*r[9:5]))
  99.                  && ((((X_ptr[3:0]*X_ptr[3:0]-2*X_ptr[3:0]*cnt_x[3:0]+cnt_x[3:0]*cnt_x[3:0])+(Y_ptr[3:0]*Y_ptr[3:0]-2*Y_ptr[3:0]*cnt_y[3:0]+cnt_y[3:0]*cnt_y[3:0]))) <= (r[4:0]*r[4:0])))
  100.                     candidate_tmp <= candidate_tmp+1;
  101.             end
  102.            
  103.             2'b10:begin //set (A | B) - (A & B)
  104.                 if(
  105.                     (((((X_ptr[7:4]*X_ptr[7:4]-2*X_ptr[7:4]*cnt_x[3:0]+cnt_x[3:0]*cnt_x[3:0])+(Y_ptr[7:4]*Y_ptr[7:4]+cnt_y[3:0]*cnt_y[3:0]-2*cnt_y[3:0]*Y_ptr[7:4]))) <= (r[9:5]*r[9:5]))
  106.                 || ((((X_ptr[3:0]*X_ptr[3:0]-2*X_ptr[3:0]*cnt_x[3:0]+cnt_x[3:0]*cnt_x[3:0])+(Y_ptr[3:0]*Y_ptr[3:0]-2*Y_ptr[3:0]*cnt_y[3:0]+cnt_y[3:0]*cnt_y[3:0]))) <= (r[4:0]*r[4:0])))
  107.                 &&
  108.                 !(((((X_ptr[7:4]*X_ptr[7:4]-2*X_ptr[7:4]*cnt_x[3:0]+cnt_x[3:0]*cnt_x[3:0])+(Y_ptr[7:4]*Y_ptr[7:4]+cnt_y[3:0]*cnt_y[3:0]-2*cnt_y[3:0]*Y_ptr[7:4]))) <= (r[9:5]*r[9:5]))
  109.                 && ((((X_ptr[3:0]*X_ptr[3:0]-2*X_ptr[3:0]*cnt_x[3:0]+cnt_x[3:0]*cnt_x[3:0])+(Y_ptr[3:0]*Y_ptr[3:0]-2*Y_ptr[3:0]*cnt_y[3:0]+cnt_y[3:0]*cnt_y[3:0]))) <= (r[4:0]*r[4:0])))
  110.                     )
  111.                     //candidate_tmp1 <= candidate_tmp1 +1;
  112.                    //candidate_tmp2 <= candidate_tmp2+1;
  113.                 candidate_tmp <= candidate_tmp+1;
  114.             end
  115.         //default:mode <= 2'b00;
  116.         endcase
  117.     end
  118. end
  119.  
  120. endmodule
Advertisement
Add Comment
Please, Sign In to add comment