Advertisement
Guest User

ball.sv final project version

a guest
Apr 12th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // will be transformed into player logic
  2.  
  3. // final project version
  4.  
  5. //-------------------------------------------------------------------------
  6. //    Ball.sv                                                            --
  7. //    Viral Mehta                                                        --
  8. //    Spring 2005                                                        --
  9. //                                                                       --
  10. //    Modified by Stephen Kempf 03-01-2006                               --
  11. //                              03-12-2007                               --
  12. //    Translated by Joe Meng    07-07-2013                               --
  13. //    Modified by Po-Han Huang  12-08-2017                               --
  14. //    Spring 2018 Distribution                                           --
  15. //                                                                       --
  16. //    For use with ECE 385 Lab 8                                         --
  17. //    UIUC ECE Department                                                --
  18. //-------------------------------------------------------------------------
  19.  
  20.  
  21. module  ball ( input         Clk,                // 50 MHz clock
  22.                              Reset,              // Active-high reset signal
  23.                              frame_clk,          // The clock indicating a new frame (~60Hz)
  24.                input [9:0]   DrawX, DrawY,       // Current pixel coordinates
  25.                     input [7:0]   keycode,               // Keyboard input
  26.                output logic  is_ball             // Whether current pixel belongs to ball or background
  27.               );
  28.    
  29.     parameter [9:0] Ball_X_Center = 10'd320;  // Center position on the X axis
  30.     parameter [9:0] Ball_Y_Center = 10'd240;  // Center position on the Y axis
  31.     parameter [9:0] Ball_X_Min = 10'd0;       // Leftmost point on the X axis
  32.     parameter [9:0] Ball_X_Max = 10'd639;     // Rightmost point on the X axis
  33.     parameter [9:0] Ball_Y_Min = 10'd0;       // Topmost point on the Y axis
  34.     parameter [9:0] Ball_Y_Max = 10'd479;     // Bottommost point on the Y axis
  35.     parameter [9:0] Ball_X_Step = 10'd2;      // Step size on the X axis
  36.     parameter [9:0] Ball_Y_Step = 10'd2;      // Step size on the Y axis
  37.     parameter [9:0] Ball_Size = 10'd5;        // Ball size
  38.    
  39.     logic [9:0] Ball_X_Pos, Ball_X_Motion, Ball_Y_Pos, Ball_Y_Motion;
  40.     logic [9:0] Ball_X_Pos_in, Ball_X_Motion_in, Ball_Y_Pos_in, Ball_Y_Motion_in;
  41.    
  42.     //////// Do not modify the always_ff blocks. ////////
  43.     // Detect rising edge of frame_clk
  44.     logic frame_clk_delayed, frame_clk_rising_edge;
  45.     always_ff @ (posedge Clk) begin
  46.         frame_clk_delayed <= frame_clk;
  47.         frame_clk_rising_edge <= (frame_clk == 1'b1) && (frame_clk_delayed == 1'b0);
  48.     end
  49.      
  50.     // Update registers
  51.     always_ff @ (posedge Clk)
  52.     begin
  53.         if (Reset)
  54.         begin
  55.             Ball_X_Pos <= Ball_X_Center;
  56.             Ball_Y_Pos <= Ball_Y_Center;
  57. //            Ball_X_Motion <= 10'd0;
  58. //            Ball_Y_Motion <= Ball_Y_Step;
  59.         end
  60.         else
  61.         begin
  62.             Ball_X_Pos <= Ball_X_Pos_in;
  63.             Ball_Y_Pos <= Ball_Y_Pos_in;
  64. //            Ball_X_Motion <= Ball_X_Motion_in;
  65. //            Ball_Y_Motion <= Ball_Y_Motion_in;
  66.         end
  67.     end
  68.     //////// Do not modify the always_ff blocks. ////////
  69.    
  70.     // You need to modify always_comb block.
  71.     always_comb
  72.     begin
  73.         // By default, position unchanged and motion at 0
  74.         Ball_X_Pos_in = Ball_X_Pos;
  75.         Ball_Y_Pos_in = Ball_Y_Pos;
  76. //        Ball_X_Motion_in = 10'd0;
  77. //        Ball_Y_Motion_in = 10'd0;
  78.        
  79.         // Update position and motion only at rising edge of frame clock
  80.         if (frame_clk_rising_edge)
  81.         begin
  82.                 case (keycode) //w and s
  83.                     8'h1a: //W
  84.                     begin
  85.                         Ball_Y_Pos_in = Ball_Y_Pos - Ball_Y_Step;
  86.                     end
  87.                     8'h16: //S
  88.                     begin
  89.                         Ball_Y_Pos_in = Ball_Y_Pos + Ball_X_Step;
  90.                     end
  91.                     8'h04: //A
  92.                     begin
  93.                         Ball_X_Pos_in = Ball_X_Pos - Ball_X_Step;
  94.                     end
  95.                     8'h07: //D
  96.                     begin
  97.                         Ball_X_Pos_in = Ball_X_Pos + Ball_X_Step;
  98.                     end
  99.                     default:
  100.                     begin
  101.                         Ball_X_Pos_in = Ball_X_Pos;
  102.                         Ball_Y_Pos_in = Ball_Y_Pos;
  103.                     end
  104.                 endcase
  105.         end
  106.        
  107.         /**************************************************************************************
  108.             ATTENTION! Please answer the following quesiton in your lab report! Points will be allocated for the answers!
  109.             Hidden Question #2/2:
  110.                Notice that Ball_Y_Pos is updated using Ball_Y_Motion.
  111.               Will the new value of Ball_Y_Motion be used when Ball_Y_Pos is updated, or the old?
  112.               What is the difference between writing
  113.                 "Ball_Y_Pos_in = Ball_Y_Pos + Ball_Y_Motion;" and
  114.                 "Ball_Y_Pos_in = Ball_Y_Pos + Ball_Y_Motion_in;"?
  115.               How will this impact behavior of the ball during a bounce, and how might that interact with a response to a keypress?
  116.               Give an answer in your Post-Lab.
  117.         **************************************************************************************/
  118.     end
  119.    
  120.     // Compute whether the pixel corresponds to ball or background
  121.     /* Since the multiplicants are required to be signed, we have to first cast them
  122.        from logic to int (signed by default) before they are multiplied. */
  123.     int DistX, DistY, Size;
  124.     assign DistX = DrawX - Ball_X_Pos;
  125.     assign DistY = DrawY - Ball_Y_Pos;
  126.     assign Size = Ball_Size;
  127.     always_comb begin
  128.         if ( ( DistX*DistX + DistY*DistY) <= (Size*Size) )
  129.             is_ball = 1'b1;
  130.         else
  131.             is_ball = 1'b0;
  132.         /* The ball's (pixelated) circle is generated using the standard circle formula.  Note that while
  133.            the single line is quite powerful descriptively, it causes the synthesis tool to use up three
  134.            of the 12 available multipliers on the chip! */
  135.     end
  136.    
  137. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement