Advertisement
kevinking0814

Untitled

Dec 7th, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module ball_tb;
  2.  
  3.     reg clk;
  4.     reg rst;
  5.     reg [10:0] curr_x;
  6.     reg [9:0] curr_y;
  7.     reg [10:0] x_paddle1, x_paddle2, vcount, curr_y;
  8.     reg [9:0] y_paddle1, y_paddle2, hcount, curr_x;
  9.  
  10.     // Outputs
  11.     wire [1:0] x_velocity, y_velocity;
  12.     wire [3:0] p1_score, p2_score;
  13.     wire [10:0] x_ball;
  14.     wire [9:0] y_ball;
  15.     wire touch1, touch2;
  16.    
  17.     localparam L_position = 11'd350;
  18.     localparam R_position = 11'd850;
  19.     localparam paddlewidth = 11'd50;
  20.     localparam paddleheight1 = 10'd590;
  21.     localparam paddleheight2 = 10'd590;
  22.    
  23.     localparam V_active = 10'd799;
  24.     localparam y_initial = V_active/2;
  25.     localparam x_initial1 = L_position;
  26.     localparam x_initial2 = R_position;
  27.     localparam H_active = 11'd1279;
  28.  
  29.  
  30.     // Instantiate the ball module
  31.     ball uut (
  32.         .clk(clk),
  33.         .rst(rst),
  34.         .curr_x(curr_x),
  35.         .curr_y(curr_y),
  36.         .x_paddle1(x_inital1),
  37.         .x_paddle2(x_initial2),
  38.         .y_paddle1(y_initial),
  39.         .y_paddle2(y_initial),
  40.         .paddlewidth(paddlewidth),
  41.         .paddleheight1(paddleheight1),
  42.         .paddleheight2(paddleheight2),
  43.         .p1_score(p1_score),
  44.         .p2_score(p2_score),
  45.         .x_ball(x_ball),
  46.         .y_ball(y_ball),
  47.         .x_velocity(x_velocity),
  48.         .y_velocity(y_velocity),
  49.         .p1_touch(touch1),
  50.         .p2_touch(touch2)
  51.     );
  52.        
  53.     always @(posedge clk)
  54.     begin
  55.         if(hcount < 1679)
  56.             hcount <= hcount + 1'b1;
  57.          else
  58.             begin
  59.             if(vcount< 827)
  60.                 begin
  61.                 hcount <= 11'd0;
  62.                 vcount <= vcount + 1'b1;
  63.                 end
  64.              else
  65.                 begin
  66.                 hcount <= 11'd0;
  67.                 vcount <= 10'd0;
  68.                 end
  69.             end  
  70.     end
  71.    
  72.     assign display = (335 <= hcount && hcount <= 1614) && (27 <= vcount && vcount <= 826);
  73.  
  74.  
  75.     always @(posedge clk)
  76.     begin
  77.     if(display)
  78.     begin
  79.         if(curr_x < 1279)
  80.             curr_x <= curr_x + 1'b1;
  81.         else
  82.             begin
  83.             if(curr_y < 799)
  84.                 begin
  85.                     curr_y <= curr_y + 1'b1;
  86.                     curr_x <= 11'd0;
  87.                 end
  88.              else
  89.                 begin
  90.                 curr_x <= 11'd0;
  91.                 curr_y <= 10'd0;
  92.                 end
  93.             end
  94.     end  
  95.     end
  96.  
  97.     // Initial values
  98.     initial begin
  99.         clk = 1'b0;
  100.         rst = 1'b1;
  101.         curr_x = 640; // Initial x-coordinate for ball
  102.         curr_y = 400; // Initial y-coordinate for ball
  103.         x_paddle1 = x_initial1; // Initial x-coordinate for paddle 1
  104.         x_paddle2 = x_initial2; // Initial x-coordinate for paddle 2
  105.         y_paddle1 = y_initial; // Initial y-coordinate for paddle 1
  106.         y_paddle2 = y_initial; // Initial y-coordinate for paddle 2
  107.  
  108.         #10 rst = 1'b0;
  109.  
  110.         $finish;
  111.         end
  112.  
  113.     always #5 clk = ~clk;
  114.  
  115. endmodule
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement