Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.79 KB | None | 0 0
  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 PinkGhost ( //input logic [9:0] Ghost_X_start_TEST, Ghost_Y_start_TEST,
  18.  
  19. input [2:0] mem_out [0:135575],
  20. input logic [7:0] SW,
  21. 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.  
  26. // output logic is_pacMan , // Whether current pixel belongs to pacman or background
  27. output [18:0] address,
  28.  
  29. output [9:0] PinkGhost_X_out,
  30. output [9:0] PinkGhost_Y_out
  31.  
  32. );
  33.  
  34. assign PinkGhost_X_out = Ghost_X_start;
  35. assign PinkGhost_Y_out = Ghost_Y_start;
  36.  
  37.  
  38. //Desired start location of drawn sprite
  39.  
  40. logic [9:0] Ghost_X_OGstart = 10'd283; // Center position on the X axis
  41. logic [9:0] Ghost_Y_OGstart = 10'd111;
  42.  
  43. logic [9:0] Ghost_X_start = 10'd283; // Center position on the X axis
  44. logic [9:0] Ghost_Y_start = 10'd111; // Center position on the Y axis
  45.  
  46. //Pac to the right
  47.  
  48. parameter [9:0] Ghost_X_eyesRight = 10'd3;
  49. parameter [9:0] Ghost_Y_eyesRight = 10'd143;
  50.  
  51. parameter [9:0] Ghost_X_eyesMoreRight = 10'd20; // offset horizontal
  52. parameter [9:0] Ghost_Y_eyesMoreRight = 10'd143; // offset vertical
  53.  
  54. //Pac to the left
  55.  
  56. parameter [9:0] Ghost_X_eyesLeft = 10'd37;
  57. parameter [9:0] Ghost_Y_eyesLeft = 10'd143;
  58.  
  59. parameter [9:0] Ghost_X_eyesMoreLeft = 10'd54; // offset horizontal
  60. parameter [9:0] Ghost_Y_eyesMoreLeft = 10'd143;
  61.  
  62. //Pac to UP
  63.  
  64. parameter [9:0] Ghost_X_eyesUp = 10'd71;
  65. parameter [9:0] Ghost_Y_eyesUp = 10'd143;
  66.  
  67. parameter [9:0] Ghost_X_eyesMoreUp = 10'd88; // offset horizontal
  68. parameter [9:0] Ghost_Y_eyesMoreUp = 10'd143; // offset vertical
  69.  
  70. //Pac to Down
  71.  
  72. parameter [9:0] Ghost_X_eyesDown = 10'd105;
  73. parameter [9:0] Ghost_Y_eyesDown = 10'd143;
  74.  
  75. parameter [9:0] Ghost_X_eyesMoreDown = 10'd122; // offset horizontal
  76. parameter [9:0] Ghost_Y_eyesMoreDown = 10'd143;
  77.  
  78.  
  79. //Closed Used by Everyone
  80. parameter [9:0] Ghost_X_closed = 10'd2; // offset horizontal
  81. parameter [9:0] Ghost_Y_closed = 10'd85; // offset vertical
  82.  
  83.  
  84.  
  85. //For Right
  86. logic [9:0] new_x_eyesRight; // offset horizontal
  87. logic [9:0] new_y_eyesRight; // offset vertical
  88.  
  89. logic [9:0] new_x_eyesMoreRight; // offset horizontal
  90. logic [9:0] new_y_eyesMoreRight; // offset vertical
  91.  
  92. //For Left
  93. logic [9:0] new_x_eyesLeft; // offset horizontal
  94. logic [9:0] new_y_eyesLeft; // offset vertical
  95.  
  96. logic [9:0] new_x_eyesMoreLeft; // offset horizontal
  97. logic [9:0] new_y_eyesMoreLeft; // offset vertical
  98.  
  99. //For Up
  100. logic [9:0] new_x_eyesUp; // offset horizontal
  101. logic [9:0] new_y_eyesUp; // offset vertical
  102.  
  103. logic [9:0] new_x_eyesMoreUp; // offset horizontal
  104. logic [9:0] new_y_eyesMoreUp; // offset vertical
  105.  
  106. //For Down
  107. logic [9:0] new_x_eyesDown; // offset horizontal
  108. logic [9:0] new_y_eyesDown; // offset vertical
  109.  
  110. logic [9:0] new_x_eyesMoreDown; // offset horizontal
  111. logic [9:0] new_y_eyesMoreDown; // offset vertical
  112.  
  113.  
  114.  
  115.  
  116. parameter [9:0] Ghost_X_Min = 10'd0; // Leftmost point on the X axis
  117. parameter [9:0] Ghost_X_Max = 10'd639; // Rightmost point on the X axis
  118. parameter [9:0] Ghost_Y_Min = 10'd0; // Topmost point on the Y axis
  119. parameter [9:0] Ghost_Y_Max = 10'd479; // Bottommost point on the Y axis
  120.  
  121. parameter [9:0] Ghost_X_Step = 10'd1; // Step size on the X axis
  122. parameter [9:0] Ghost_Y_Step = 10'd1; // Step size on the Y axis
  123.  
  124.  
  125.  
  126.  
  127.  
  128. logic [6:0] flash_counter = 7'b0000000;
  129.  
  130. logic [6:0] counter_TOTAL = 7'b1111111;
  131.  
  132.  
  133.  
  134. ///Update going right
  135. logic [18:0] eyesRight;
  136. logic [18:0] eyesMoreRight;
  137.  
  138.  
  139. assign new_x_eyesRight = DrawX - Ghost_X_start + Ghost_X_eyesRight;
  140. assign new_y_eyesRight = DrawY - Ghost_Y_start + Ghost_Y_eyesRight;
  141.  
  142. assign new_x_eyesMoreRight = DrawX - Ghost_X_start + Ghost_X_eyesMoreRight;
  143. assign new_y_eyesMoreRight = DrawY - Ghost_Y_start + Ghost_Y_eyesMoreRight;
  144.  
  145. ///Update going left
  146. logic [18:0] eyesLeft;
  147. logic [18:0] eyesMoreLeft;
  148.  
  149.  
  150. assign new_x_eyesLeft = DrawX - Ghost_X_start + Ghost_X_eyesLeft;
  151. assign new_y_eyesLeft = DrawY - Ghost_Y_start + Ghost_Y_eyesLeft;
  152.  
  153. assign new_x_eyesMoreLeft = DrawX - Ghost_X_start + Ghost_X_eyesMoreLeft;
  154. assign new_y_eyesMoreLeft = DrawY - Ghost_Y_start + Ghost_Y_eyesMoreLeft;
  155.  
  156.  
  157. ///Update going Up
  158. logic [18:0] eyesUp;
  159. logic [18:0] eyesMoreUp;
  160.  
  161.  
  162. assign new_x_eyesUp = DrawX - Ghost_X_start + Ghost_X_eyesUp;
  163. assign new_y_eyesUp = DrawY - Ghost_Y_start + Ghost_Y_eyesUp;
  164.  
  165. assign new_x_eyesMoreUp = DrawX - Ghost_X_start + Ghost_X_eyesMoreUp;
  166. assign new_y_eyesMoreUp = DrawY - Ghost_Y_start + Ghost_Y_eyesMoreUp;
  167.  
  168. ///Update going Down
  169. logic [18:0] eyesDown;
  170. logic [18:0] eyesMoreDown;
  171.  
  172.  
  173. assign new_x_eyesDown = DrawX - Ghost_X_start + Ghost_X_eyesDown;
  174. assign new_y_eyesDown = DrawY - Ghost_Y_start + Ghost_Y_eyesDown;
  175.  
  176. assign new_x_eyesMoreDown = DrawX - Ghost_X_start + Ghost_X_eyesMoreDown;
  177. assign new_y_eyesMoreDown = DrawY - Ghost_Y_start + Ghost_Y_eyesMoreDown;
  178.  
  179.  
  180.  
  181. //Right Addresses
  182. assign eyesRight = (new_y_eyesRight * 538) + new_x_eyesRight;
  183. assign eyesMoreRight = (new_y_eyesMoreRight * 538) + new_x_eyesMoreRight;
  184.  
  185. //Left Addresses
  186. assign eyesLeft = (new_y_eyesLeft * 538) + new_x_eyesLeft;
  187. assign eyesMoreLeft = (new_y_eyesMoreLeft * 538) + new_x_eyesMoreLeft;
  188.  
  189. //Up Addresses
  190. assign eyesUp = (new_y_eyesUp * 538) + new_x_eyesUp;
  191. assign eyesMoreUp = (new_y_eyesMoreUp * 538) + new_x_eyesMoreUp;
  192.  
  193. //Down Addresses
  194. assign eyesDown = (new_y_eyesDown * 538) + new_x_eyesDown;
  195. assign eyesMoreDown = (new_y_eyesMoreDown * 538) + new_x_eyesMoreDown;
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. always_ff @ (posedge frame_clk)
  205. begin
  206.  
  207. //Ghost_Y_start <= Ghost_Y_start +1;
  208. flash_counter <= flash_counter + 1'b1;
  209.  
  210.  
  211.  
  212. if(flash_counter == counter_TOTAL)
  213.  
  214. begin
  215.  
  216. flash_counter <= 0;
  217.  
  218. end
  219.  
  220. end
  221.  
  222.  
  223.  
  224. always_comb
  225. begin
  226.  
  227.  
  228. if(currDirec_in == 4'b0000)
  229. begin
  230. if((DrawX >= Ghost_X_start) && (DrawX <=Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) )
  231. address = eyesMoreRight;
  232. else
  233. address = 19'd0;
  234.  
  235. end
  236.  
  237. else if(currDirec == 4'b0010)
  238. begin
  239. if( (DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start + 13) && (flash_counter < 7'b0101001 ) )
  240. address = eyesMoreRight;
  241.  
  242. else if((DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start + 13) && (flash_counter < 7'b1010010 ))
  243. address = eyesRight;
  244.  
  245. else if((DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter > 7'b1010010 ))
  246. address = eyesRight;
  247. else
  248. begin
  249. address = 19'd0;
  250.  
  251. end
  252.  
  253. end
  254.  
  255. else if(currDirec == 4'b1000 )
  256. begin
  257. if( (DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter < 7'b0101001 ) )
  258. address = eyesMoreLeft;
  259.  
  260. else if((DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter < 7'b1010010 ))
  261. address = eyesLeft;
  262.  
  263. else if((DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter > 7'b1010010 ))
  264. address = eyesLeft;
  265. else
  266. begin
  267. address = 19'd0;
  268.  
  269. end
  270. end
  271. else if(currDirec == 4'b0001 )
  272. begin
  273. if( (DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter < 7'b0101001 ) )
  274. address = eyesMoreDown;
  275.  
  276. else if((DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter < 7'b1010010 ))
  277. address = eyesDown;
  278.  
  279. else if((DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter > 7'b1010010 ))
  280. address = eyesDown;
  281. else
  282. begin
  283. address = 19'd0;
  284.  
  285. end
  286.  
  287. end
  288. //
  289. else if(currDirec == 4'b0100 )
  290. begin
  291. if( (DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter < 7'b0101001 ) )
  292. address = eyesMoreUp;
  293.  
  294. else if((DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter < 7'b1010010 ))
  295. address = eyesUp;
  296.  
  297. else if((DrawX >= Ghost_X_start) && (DrawX <= Ghost_X_start + 13) && (DrawY >= Ghost_Y_start) && (DrawY <= Ghost_Y_start+13) && (flash_counter > 7'b1010010 ))
  298. address = eyesUp;
  299. else
  300. begin
  301. address = 19'd0;
  302.  
  303. end
  304. end
  305.  
  306. else
  307. begin
  308. address = 19'd0;
  309.  
  310. end
  311.  
  312.  
  313. end
  314.  
  315. //
  316. // //////// Do not modify the always_ff blocks. ////////
  317. // Detect rising edge of frame_clk
  318. logic frame_clk_delayed, frame_clk_rising_edge;
  319. always_ff @ (posedge Clk) begin
  320. frame_clk_delayed <= frame_clk;
  321. frame_clk_rising_edge <= (frame_clk == 1'b1) && (frame_clk_delayed == 1'b0);
  322. end
  323.  
  324.  
  325.  
  326.  
  327. logic [9:0] Ghost_Y_start_in;
  328. logic [9:0] Ghost_X_start_in;
  329.  
  330. logic [3:0] currDirec;
  331. logic [3:0] currDirec_in;
  332.  
  333. // Update registers
  334. always_ff @ (posedge Clk)
  335. begin
  336. if (Reset)
  337. begin
  338. Ghost_X_start <= Ghost_X_OGstart;
  339. Ghost_Y_start <= Ghost_Y_OGstart;
  340.  
  341. currDirec <= 4'b0000;
  342. end
  343. else
  344. begin
  345.  
  346. Ghost_Y_start <= Ghost_Y_start_in;
  347. Ghost_X_start <= Ghost_X_start_in;
  348. currDirec <= currDirec_in;
  349.  
  350. end
  351. end
  352. //////// Do not modify the always_ff blocks. ////////
  353.  
  354. // You need to modify always_comb block.
  355. always_comb
  356. begin
  357. // By default, keep motion and position unchanged
  358.  
  359. Ghost_Y_start_in = Ghost_Y_start;
  360. Ghost_X_start_in = Ghost_X_start;
  361. currDirec_in = currDirec;
  362.  
  363. // Update position and motion only at rising edge of frame clock
  364. if (frame_clk_rising_edge)
  365. begin
  366.  
  367.  
  368.  
  369.  
  370. unique case(SW)
  371.  
  372. 8'h52: // w (up)
  373. begin
  374. if( (mem_out[((Ghost_Y_start - 1) * 538) + (Ghost_X_start)] != 5'd5) ||
  375. (Ghost_Y_start - 1 >= 102 && Ghost_X_start >= 284 && Ghost_X_start <= 302 ) )
  376. currDirec_in = 4'b0100;
  377. end
  378. 8'h4F://right
  379. begin
  380. if((mem_out[((Ghost_Y_start ) * 538) + (Ghost_X_start + 14)] != 5'd5))
  381. currDirec_in = 4'b0010;
  382.  
  383. end
  384. 8'h51://down
  385. begin
  386. if((mem_out[((Ghost_Y_start + 14 ) * 538) + (Ghost_X_start )] != 5'd5))
  387. currDirec_in = 4'b0001;
  388. end
  389. 8'h50://left
  390. begin
  391.  
  392. if((mem_out[((Ghost_Y_start) * 538) + (Ghost_X_start - 1)] != 5'd5))
  393. currDirec_in = 4'b1000;
  394.  
  395.  
  396. end
  397.  
  398. default:
  399. begin
  400.  
  401. Ghost_Y_start_in = Ghost_Y_start;
  402. currDirec_in = currDirec;
  403. end
  404. endcase
  405.  
  406.  
  407.  
  408.  
  409. if(currDirec_in == 4'b0100)
  410. begin
  411. if((mem_out[((Ghost_Y_start - 1) * 538) + (Ghost_X_start)] != 5'd5) ||
  412. (Ghost_Y_start - 1 >= 102 && Ghost_X_start >= 284 && Ghost_X_start <= 302 ))
  413. Ghost_Y_start_in = Ghost_Y_start - 1;
  414. else
  415. currDirec_in = 4'b0000;
  416. end
  417.  
  418. else if(currDirec_in == 4'b0010)
  419. begin
  420. if( (mem_out[((Ghost_Y_start ) * 538) + (Ghost_X_start + 14)] != 5'd5))
  421. Ghost_X_start_in = Ghost_X_start + 1;
  422. else
  423. currDirec_in = 4'b0000;
  424. end
  425. else if(currDirec_in == 4'b0001)
  426. begin
  427. if( (mem_out[((Ghost_Y_start + 14 ) * 538) + (Ghost_X_start )] != 5'd5))
  428. Ghost_Y_start_in = Ghost_Y_start + 1;
  429. else
  430. currDirec_in = 4'b0000;
  431. end
  432. else if(currDirec_in == 4'b1000)
  433. begin
  434. if((mem_out[((Ghost_Y_start) * 538) + (Ghost_X_start - 1)] != 5'd5))
  435. Ghost_X_start_in = Ghost_X_start - 1;
  436. else
  437. currDirec_in = 4'b0000;
  438. end
  439.  
  440. ///////
  441.  
  442.  
  443.  
  444.  
  445. ////////
  446. end
  447.  
  448. end
  449.  
  450.  
  451.  
  452. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement