Guest User

Untitled

a guest
Dec 7th, 2019
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //-------------------------------------------------------------------------
  2. // Ball.sv --
  3. // Viral Mehta --
  4. // Spring 2005 --
  5. // --
  6. // Modified by Stephen Kempf 03-01-2006 --
  7. // 03-12-2007 --
  8. // Translated by Joe Meng 07-07-2013 --
  9. // Modified by Po-Han Huang 12-08-2017 --
  10. // Spring 2018 Distribution --
  11. // --
  12. // For use with ECE 385 Lab 8 --
  13. // UIUC ECE Department --
  14. //-------------------------------------------------------------------------
  15.  
  16.  
  17. module ball ( input Clk, // 50 MHz clock
  18. Reset, // Active-high reset signal
  19. frame_clk, // The clock indicating a new frame (~60Hz)
  20. input [9:0] DrawX, DrawY, // Current pixel coordinates
  21. input [7:0] keycode,
  22. output logic is_ball // Whether current pixel belongs to ball or background
  23. //output logic [3:0] LED
  24. );
  25.  
  26. parameter [9:0] Ball_X_Center = 10'd200; // Center position on the X axis
  27. parameter [9:0] Ball_Y_Center = 10'd240; // Center position on the Y axis
  28. parameter [9:0] Ball_X_Min = 10'd0; // Leftmost point on the X axis
  29. parameter [9:0] Ball_X_Max = 10'd639; // Rightmost point on the X axis
  30. parameter [9:0] Ball_Y_Min = 10'd0; // Topmost point on the Y axis
  31. parameter [9:0] Ball_Y_Max = 10'd479; // Bottommost point on the Y axis
  32. parameter [9:0] Ball_X_Step = 10'd1; // Step size on the X axis
  33. parameter [9:0] Ball_Y_Step = 10'd1; // Step size on the Y axis
  34. parameter [9:0] Ball_Size = 10'd4; // Ball size
  35.  
  36. logic [9:0] Ball_X_Pos, Ball_Y_Pos, Ball_Y_Motion;
  37. logic [9:0] Ball_Y_Pos_in, Ball_Y_Motion_in;
  38.  
  39.  
  40. //////// Do not modify the always_ff blocks. ////////
  41. // Detect rising edge of frame_clk
  42. logic frame_clk_delayed, frame_clk_rising_edge;
  43. always_ff @ (posedge Clk) begin
  44. frame_clk_delayed <= frame_clk;
  45. frame_clk_rising_edge <= (frame_clk == 1'b1) && (frame_clk_delayed == 1'b0);
  46. end
  47. // Update registers
  48. always_ff @ (posedge frame_clk_rising_edge)
  49. begin
  50. if(Reset)
  51. begin
  52. Ball_Y_Motion <= Ball_Y_Step;
  53. Ball_Y_Pos <= Ball_Y_Center;
  54. Ball_X_Pos <= Ball_X_Center;
  55. end
  56.  
  57.  
  58. // if(Ball_Y_Motion > 10'd10)
  59. // begin
  60. Ball_Y_Motion <= Ball_Y_Motion_in;
  61. Ball_Y_Pos <= Ball_Y_Pos_in;
  62. Ball_X_Pos <= Ball_X_Center;
  63. // end
  64. // else
  65. // begin
  66. // Ball_Y_Motion <= Ball_Y_Motion_in + 1'b1;
  67. // Ball_Y_Pos <= Ball_Y_Pos_in;
  68. // end
  69. end
  70.  
  71.  
  72. //////// Do not modify the always_ff blocks. ////////
  73.  
  74. // You need to modify always_comb block.
  75. always_comb
  76. begin
  77. // By default, keep motion and position unchanged
  78. Ball_Y_Pos_in = Ball_Y_Pos;
  79. Ball_Y_Motion_in = Ball_Y_Motion;
  80.  
  81. // Update position and motion only at rising edge of frame clock
  82. if (frame_clk_rising_edge)
  83. begin
  84. if(keycode[7:0] == 8'h1A) //up
  85. begin
  86. Ball_Y_Motion_in = (~(10'd10) + 1'b1);
  87. end
  88. else
  89. begin
  90. if(Ball_Y_Motion > 10'd4)
  91. begin
  92. Ball_Y_Motion_in = 10'd4;
  93. end
  94. else
  95. begin
  96. Ball_Y_Motion_in = Ball_Y_Motion + 1'b1;
  97. end
  98. end
  99.  
  100. // Be careful when using comparators with "logic" datatype because compiler treats
  101. // both sides of the operator as UNSIGNED numbers.
  102. // e.g. Ball_Y_Pos - Ball_Size <= Ball_Y_Min
  103. // If Ball_Y_Pos is 0, then Ball_Y_Pos - Ball_Size will not be -4, but rather a large positive number.
  104. // if( Ball_Y_Pos + Ball_Size >= Ball_Y_Max ) // Ball is at the bottom edge, BOUNCE!
  105. // begin
  106. // Ball_Y_Motion_in = 10'd0; // 2's complement.
  107. // Ball_X_Motion_in = 10'd0;
  108. // end
  109. // else if ( Ball_Y_Pos <= Ball_Y_Min + Ball_Size ) // Ball is at the top edge, BOUNCE!
  110. // begin
  111. // Ball_Y_Motion_in = 10'd0;
  112. // Ball_X_Motion_in = 10'd0;
  113. // end
  114. //
  115. // // TODO: Add other boundary detections and handle keypress here.
  116. // else if ( Ball_X_Pos + Ball_Size >= Ball_X_Max )
  117. // begin
  118. // Ball_Y_Motion_in = 10'd0;
  119. // Ball_X_Motion_in = (~(Ball_X_Step) + 1'b1); // 2's complement.
  120. // end
  121. // else if ( Ball_X_Pos <= Ball_X_Min + Ball_Size )
  122. // begin
  123. // Ball_Y_Motion_in = 10'd0;
  124. // Ball_X_Motion_in = Ball_X_Step;
  125. // end
  126.  
  127. // Update the ball's position with its motion
  128. Ball_Y_Pos_in = Ball_Y_Pos + Ball_Y_Motion;
  129. end
  130.  
  131.  
  132. /**************************************************************************************
  133. ATTENTION! Please answer the following quesiton in your lab report! Points will be allocated for the answers!
  134. Hidden Question #2/2:
  135. Notice that Ball_Y_Pos is updated using Ball_Y_Motion.
  136. Will the new value of Ball_Y_Motion be used when Ball_Y_Pos is updated, or the old?
  137. What is the difference between writing
  138. "Ball_Y_Pos_in = Ball_Y_Pos + Ball_Y_Motion;" and
  139. "Ball_Y_Pos_in = Ball_Y_Pos + Ball_Y_Motion_in;"?
  140. How will this impact behavior of the ball during a bounce, and how might that interact with a response to a keypress?
  141. Give an answer in your Post-Lab.
  142. **************************************************************************************/
  143. end
  144.  
  145. // always_ff @ (posedge Clk)
  146. // begin
  147. // if(keycode[7:0] == 8'h1A)
  148. // LED <= 4'b0100;
  149. // else if(keycode[7:0] == 8'h16)
  150. // LED <= 4'b0010;
  151. // else if(keycode[7:0] == 8'h07)
  152. // LED <= 4'b0001;
  153. // else if(keycode[7:0] == 8'h04)
  154. // LED <= 4'b1000;
  155. // end
  156.  
  157. // Compute whether the pixel corresponds to ball or background
  158. /* Since the multiplicants are required to be signed, we have to first cast them
  159. from logic to int (signed by default) before they are multiplied. */
  160. int DistX, DistY, Size;
  161. assign DistX = DrawX - Ball_X_Pos;
  162. assign DistY = DrawY - Ball_Y_Pos;
  163. assign Size = Ball_Size;
  164. always_comb begin
  165. if ( ( DistX*DistX + DistY*DistY) <= (Size*Size) )
  166. is_ball = 1'b1;
  167. else
  168. is_ball = 1'b0;
  169. /* The ball's (pixelated) circle is generated using the standard circle formula. Note that while
  170. the single line is quite powerful descriptively, it causes the synthesis tool to use up three
  171. of the 12 available multipliers on the chip! */
  172. end
  173.  
  174. endmodule
RAW Paste Data