Advertisement
kevinking0814

Untitled

Dec 7th, 2023
114
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.     // Constants for paddle and display dimensions
  18.     localparam L_position = 11'd350;
  19.     localparam R_position = 11'd850;
  20.     localparam paddlewidth = 11'd50;
  21.     localparam paddleheight1 = 10'd590;
  22.     localparam paddleheight2 = 10'd590;
  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.     // Instantiate the ball module
  30.     ball uut (
  31.         .clk(clk),
  32.         .rst(rst),
  33.         .curr_x(curr_x),
  34.         .curr_y(curr_y),
  35.         .x_paddle1(x_paddle1),
  36.         .x_paddle2(x_paddle2),
  37.         .y_paddle1(y_paddle1),
  38.         .y_paddle2(y_paddle2),
  39.         .paddlewidth(paddlewidth),
  40.         .paddleheight1(paddleheight1),
  41.         .paddleheight2(paddleheight2),
  42.         .p1_score(p1_score),
  43.         .p2_score(p2_score),
  44.         .x_ball(x_ball),
  45.         .y_ball(y_ball),
  46.         .x_velocity(x_velocity),
  47.         .y_velocity(y_velocity),
  48.         .p1_touch(touch1),
  49.         .p2_touch(touch2)
  50.     );
  51.  
  52.     // Counter for horizontal and vertical display positions
  53.     always @(posedge clk) begin
  54.         if (hcount < 1679)
  55.             hcount <= hcount + 1'b1;
  56.         else begin
  57.             if (vcount < 827) begin
  58.                 hcount <= 11'd0;
  59.                 vcount <= vcount + 1'b1;
  60.             end else begin
  61.                 hcount <= 11'd0;
  62.                 vcount <= 10'd0;
  63.             end
  64.         end
  65.     end
  66.  
  67.     // Display condition
  68.     assign display = (335 <= hcount && hcount <= 1614) && (27 <= vcount && vcount <= 826);
  69.  
  70.     // Logic to simulate the movement of the ball
  71.     always @(posedge clk) begin
  72.         if (display) begin
  73.             if (curr_x < 1279)
  74.                 curr_x <= curr_x + 1'b1;
  75.             else begin
  76.                 if (curr_y < 799) begin
  77.                     curr_y <= curr_y + 1'b1;
  78.                     curr_x <= 11'd0;
  79.                 end else begin
  80.                     curr_x <= 11'd0;
  81.                     curr_y <= 10'd0;
  82.                 end
  83.             end
  84.         end
  85.     end
  86.  
  87.     // Initial values
  88.     initial begin
  89.         clk = 1'b0;
  90.         rst = 1'b1;
  91.         curr_x = 640; // Initial x-coordinate for ball
  92.         curr_y = 400; // Initial y-coordinate for ball
  93.         x_paddle1 = x_initial1; // Initial x-coordinate for paddle 1
  94.         x_paddle2 = x_initial2; // Initial x-coordinate for paddle 2
  95.         y_paddle1 = y_initial; // Initial y-coordinate for paddle 1
  96.         y_paddle2 = y_initial; // Initial y-coordinate for paddle 2
  97.  
  98.         #10 rst = 1'b0;
  99.  
  100.         $finish;
  101.     end
  102.  
  103.     // Clock generation
  104.     always #5 clk = ~clk;
  105.  
  106. endmodule
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement