Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.47 KB | None | 0 0
  1. `define NUMOFENEMIES 2
  2.  
  3. module Brick_Escape
  4. (
  5. CLOCK_50, // On Board 50 MHz
  6. // Your inputs and outputs here
  7. KEY,
  8. SW,
  9. // The ports below are for the VGA output. Do not change.
  10. VGA_CLK, // VGA Clock
  11. VGA_HS, // VGA H_SYNC
  12. VGA_VS, // VGA V_SYNC
  13. VGA_BLANK_N, // VGA BLANK
  14. VGA_SYNC_N, // VGA SYNC
  15. VGA_R, // VGA Red[9:0]
  16. VGA_G, // VGA Green[9:0]
  17. VGA_B // VGA Blue[9:0]
  18. );
  19.  
  20. input CLOCK_50; // 50 MHz
  21. input [9:0] SW;
  22. input [3:0] KEY;
  23.  
  24. // Declare your inputs and outputs here
  25. // Do not change the following outputs
  26. output VGA_CLK; // VGA Clock
  27. output VGA_HS; // VGA H_SYNC
  28. output VGA_VS; // VGA V_SYNC
  29. output VGA_BLANK_N; // VGA BLANK
  30. output VGA_SYNC_N; // VGA SYNC
  31. output [9:0] VGA_R; // VGA Red[9:0]
  32. output [9:0] VGA_G; // VGA Green[9:0]
  33. output [9:0] VGA_B; // VGA Blue[9:0]
  34.  
  35. wire resetn;
  36. assign resetn = ~SW[0];
  37.  
  38. // Create the colour, x, y and writeEn wires that are inputs to the controller.
  39. wire [2:0] colour;
  40. wire [6:0] x;
  41. wire [6:0] y;
  42.  
  43. // writeEn will always be high
  44. wire writeEn = 1'b1;
  45.  
  46. // Create an Instance of a VGA controller - there can be only one!
  47. // Define the number of colours as well as the initial background
  48. // image file (.MIF) for the controller.
  49. vga_adapter VGA(
  50. .resetn(resetn),
  51. .clock(CLOCK_50),
  52. .colour(colour),
  53. .x(x),
  54. .y(y),
  55. .plot(writeEn),
  56. /* Signals for the DAC to drive the monitor. */
  57. .VGA_R(VGA_R),
  58. .VGA_G(VGA_G),
  59. .VGA_B(VGA_B),
  60. .VGA_HS(VGA_HS),
  61. .VGA_VS(VGA_VS),
  62. .VGA_BLANK(VGA_BLANK_N),
  63. .VGA_SYNC(VGA_SYNC_N),
  64. .VGA_CLK(VGA_CLK));
  65. defparam VGA.RESOLUTION = "160x120";
  66. defparam VGA.MONOCHROME = "FALSE";
  67. defparam VGA.BITS_PER_COLOUR_CHANNEL = 1;
  68. defparam VGA.BACKGROUND_IMAGE = "black.mif";
  69.  
  70. // RATE DIVIDER TO SLOW DOWN THE UPDATE PROCESS FOR PLAYER AND ENEMIES
  71. wire update; // Will be used to update position of player and enemies
  72. rateDivider ratedivider(CLOCK_50, update);
  73.  
  74.  
  75. // PLAYER PROPERTIES /////////////////////////////////////////////////////////////////
  76. wire [6:0] playerx; // x position of the player
  77. wire [6:0] playery; // y position of the player
  78.  
  79. Player player(update, resetn, playerx, playery, KEY);
  80.  
  81.  
  82.  
  83. // ENEMIES PROPERTIES ////////////////////////////////////////////////////////////////
  84. wire [6:0] enemyx;
  85. wire [6:0] enemyy;
  86. wire [3:0] enemyIterator;
  87. localparam [3:0] NUMOFENEMIES = 2;
  88.  
  89. EnemyController enemies(update, resetn, enemyx, enemyy, enemyIterator);
  90.  
  91.  
  92.  
  93. // CONTROL AND DATAPATH PROPERTIES ///////////////////////////////////
  94. wire go_next, clear_screen, update_player, update_enemy, wait_screen; // ENABLE SIGNALS
  95.  
  96. Controller controller(CLOCK_50, resetn, go_next, clear_screen, update_player, update_enemy, wait_screen);
  97.  
  98. Datapath datapath(CLOCK_50, resetn, go_next, clear_screen, update_player, update_enemy, wait_screen, playerx, playery, enemyx, enemyy, enemyIterator, x, y, colour);
  99.  
  100. endmodule
  101.  
  102. module Datapath(clk, resetn, go_next, clear_screen, update_player, update_enemy, wait_screen, playerx, playery, enemyx, enemyy, enemyIterator, x, y, colour);
  103. input clk;
  104. input resetn;
  105. input clear_screen;
  106. input update_player;
  107. input update_enemy;
  108. input wait_screen;
  109. input [6:0] playerx;
  110. input [6:0] playery;
  111. input [6:0] enemyx;
  112. input [6:0] enemyy;
  113. parameter NUMOFENEMIES = 2;
  114. reg [30:0] wait_screen_counter;
  115. reg [6:0] clear_screen_x;
  116. reg [6:0] clear_screen_y;
  117. output reg [3:0] enemyIterator;
  118. output reg go_next;
  119. output reg [6:0] x;
  120. output reg [6:0] y;
  121. output reg [2:0] colour;
  122.  
  123. always@(posedge clk)
  124. begin
  125. if(~resetn) // Basic reset functionality
  126. begin
  127. clear_screen_x = 1'd0;
  128. clear_screen_y = 1'd0;
  129. go_next = 1'd0;
  130. x = 1'd0;
  131. y = 1'd0;
  132. colour = 3'b000;
  133. end
  134. else
  135. begin
  136. /* This will fully clear the screen, in other words this part of the datapath will go
  137. through each pixel on the vga screen and place a black pixel on the current
  138. pixel and will continue to do so until iterates through the whole screen.
  139. */
  140. if(clear_screen)
  141. begin
  142. go_next = 1'd0;
  143. x = clear_screen_x;
  144. y = clear_screen_y;
  145. colour = 3'b000;
  146. clear_screen_x = clear_screen_x + 1'd1; // Continue onto the next pixel to the right
  147. if(clear_screen_x > 7'd126) // Iteration reaches the far right of the screen
  148. begin
  149. clear_screen_x = 1'd0; // Restart the iteration back to the beginning (far left of the screen)
  150. clear_screen_y = clear_screen_y + 1'd1; // Go one pixel down
  151. end
  152. else if(clear_screen_x == 7'd126 && clear_screen_y == 7'd126) // Iteration reaches the end of the screen (bottom-right corner)
  153. begin
  154. go_next = 1'd1; // Go to the next state
  155. clear_screen_x = 1'd0; // Reset the iteration for future iterations
  156. clear_screen_y = 1'd0;
  157. end
  158. end
  159. /* This will re-display the updated x and y position of the player
  160. */
  161. if(update_player)
  162. begin
  163. go_next = 1'd0;
  164. x = playerx;
  165. y = playery;
  166. colour = 3'b011;
  167. go_next = 1'd1;
  168. end
  169. /* This will re-display the updated x and y position of each enemy
  170. */
  171. if(update_enemy)
  172. begin
  173. go_next = 1'd0;
  174. x = enemyx;
  175. y = enemyy;
  176. colour = 3'b100;
  177. enemyIterator = enemyIterator + 1'd1;
  178. if(enemyIterator >= NUMOFENEMIES)
  179. begin
  180. go_next = 1'd1;
  181. enemyIterator = 1'd0;
  182. end
  183. end
  184. /* Acts as the end of the refresh
  185. */
  186. if(wait_screen)
  187. begin
  188. go_next = 1'd1;
  189. end
  190. end
  191. end
  192. endmodule
  193.  
  194. module Controller(clk, resetn, go_next, clear_screen, update_player, update_enemy, wait_screen);
  195. input clk;
  196. input resetn;
  197. input go_next;
  198. output reg clear_screen;
  199. output reg update_player;
  200. output reg update_enemy;
  201. output reg wait_screen;
  202.  
  203. reg [1:0] current_state, next_state;
  204.  
  205. // Represents the different states of the finite state machine
  206. localparam S_CLEAR_SCREEN = 2'd0,
  207. S_UPDATE_PLAYER = 2'd1,
  208. S_UPDATE_ENEMY = 2'd2,
  209. S_WAIT_SCREEN = 2'd3;
  210.  
  211. always @(*)
  212. begin: state_table
  213. case (current_state)
  214. S_CLEAR_SCREEN: next_state = go_next ? S_UPDATE_PLAYER : S_CLEAR_SCREEN;
  215. S_UPDATE_PLAYER: next_state = go_next ? S_UPDATE_ENEMY : S_UPDATE_PLAYER;
  216. S_UPDATE_ENEMY: next_state = go_next ? S_WAIT_SCREEN : S_UPDATE_ENEMY;
  217. S_WAIT_SCREEN: next_state = go_next ? S_CLEAR_SCREEN : S_WAIT_SCREEN;
  218. default: next_state = S_UPDATE_PLAYER;
  219. endcase
  220. end
  221.  
  222. always @(*)
  223. begin: enable_signals
  224. clear_screen = 1'd0;
  225. update_player = 1'd0;
  226. update_enemy = 1'd0;
  227. wait_screen = 1'd0;
  228. case (current_state)
  229. S_CLEAR_SCREEN:
  230. begin
  231. clear_screen = 1'd1;
  232. end
  233. S_UPDATE_PLAYER:
  234. begin
  235. update_player = 1'd1;
  236. end
  237. S_UPDATE_ENEMY:
  238. begin
  239. update_enemy = 1'd1;
  240. end
  241. S_WAIT_SCREEN:
  242. begin
  243. wait_screen = 1'd1;
  244. end
  245. endcase
  246. end
  247.  
  248. always @(posedge clk)
  249. begin: curr_state_FSM
  250. if(~resetn)
  251. current_state <= S_CLEAR_SCREEN;
  252. else
  253. current_state <= next_state;
  254. end
  255.  
  256. endmodule
  257.  
  258. module EnemyController(update, resetn, enemyx, enemyy, enemyIterator);
  259. input update;
  260. input resetn;
  261. input [3:0] enemyIterator;
  262. parameter NUMOFENEMIES = 2;
  263. output reg [6:0] enemyx; // x position of the current enemy
  264. output reg [6:0] enemyy; // y position of the current enemy
  265. wire [6:0] enemyx_array [0:NUMOFENEMIES-1]; // x position of all enemies
  266. wire [6:0] enemyy_array [0:NUMOFENEMIES-1]; // y position of all enemies
  267. reg [6:0] enemyx_init[0:1]; // initial x position of the enemies
  268. reg [6:0] enemyy_init[0:1]; // initial y position of the enemies
  269. reg left_and_right[0:1]; // Determines whether the enemy will move up and down, or left and right
  270.  
  271. // Initializes the enemy's starting position and other properties
  272. initial
  273. begin
  274. enemyx_init[0] = 7'd50;
  275. enemyx_init[1] = 7'd100;
  276.  
  277. enemyy_init[0] = 7'd50;
  278. enemyy_init[1] = 7'd100;
  279.  
  280. left_and_right[0] = 1'd0;
  281. left_and_right[1] = 1'd1;
  282. end
  283.  
  284. // Instantiates multiple enemies
  285. genvar index;
  286. generate
  287. for (index = 0; index < NUMOFENEMIES; index = index + 1)
  288. begin: create_enemies
  289. Enemy enemy(update, resetn, enemyx_array[index], enemyy_array[index], left_and_right[index], 7'd50, 7'd50);
  290. end
  291. endgenerate
  292.  
  293. always @(*)
  294. begin
  295. enemyx = enemyx_array[0];
  296. enemyy = enemyy_array[0];
  297. end
  298.  
  299. endmodule
  300.  
  301. module Enemy(update, resetn, enemyx, enemyy, left_and_right, enemyx_init, enemyy_init);
  302.  
  303. input update;
  304. input resetn;
  305. input left_and_right; // if 0 the enemy will move up and down, if 1 the enemy will move left and right
  306. input [6:0] enemyx_init;
  307. input [6:0] enemyy_init;
  308. reg [3:0] movement_position;
  309. reg left_right; // if 0 move left, if 1 move right
  310. reg up_down; // if 0 move up, if 1 move down
  311. output reg [6:0] enemyx;
  312. output reg [6:0] enemyy;
  313. reg initialize;
  314.  
  315. always @(posedge update)
  316. begin
  317. if(!initialize)
  318. begin
  319. enemyx = enemyx_init;
  320. enemyy = enemyy_init;
  321. initialize = 1'd1;
  322. end
  323. else if(~resetn)
  324. begin
  325. enemyx = 1'd0;
  326. enemyy = 1'd0;
  327. end
  328. else
  329. begin
  330. if(left_and_right) // Enemy will move left and right
  331. begin
  332. if(movement_position == 4'd10) // Change direction every time it moves 10 pixels left or right
  333. begin
  334. left_right = ~left_right;
  335. movement_position = 1'd0;
  336. end
  337. if(left_right == 1'b0)
  338. begin
  339. enemyx = enemyx - 1'd1; // Move enemy to the left by one pixel
  340. end
  341. else
  342. begin
  343. enemyx = enemyx + 1'd1; // Move enemy to the right by one pixel
  344. end
  345. end
  346. else // Enemy will move up and down
  347. begin
  348. if(movement_position == 4'd10) // Change direction everytime it moves 10 pixels up or down
  349. begin
  350. up_down = ~up_down;
  351. movement_position = 1'd0;
  352. end
  353. if(up_down == 1'b0)
  354. begin
  355. enemyy = enemyy + 1'd1; // Move enemy down by one pixel
  356. end
  357. else
  358. begin
  359. enemyy = enemyy - 1'd1; // Move enemy up by one pixel
  360. end
  361. end
  362. movement_position <= movement_position + 1'b1; // Iterate how much pixels the enemy has moved by 1
  363. end
  364. end
  365. endmodule
  366.  
  367. module Player(update, resetn, playerx, playery, KEY);
  368.  
  369. input update;
  370. input resetn;
  371. output reg [6:0] playerx;
  372. output reg [6:0] playery;
  373. input [3:0] KEY;
  374.  
  375. // Initialize the starting position of the player
  376. initial
  377. begin
  378. playerx = 7'd25;
  379. playery = 7'd25;
  380. end
  381.  
  382. /* This will constantly check for player input and move the player
  383. accordingly.
  384. */
  385. always @(posedge update)
  386. begin
  387. if(~resetn)
  388. begin
  389. playerx = 1'b0;
  390. playery = 1'b0;
  391. end
  392. else
  393. begin
  394. if(~KEY[3]) // MOVE PLAYER LEFT
  395. begin
  396. playerx = playerx - 1'b1;
  397. end
  398. else if(~KEY[2]) // MOVE PLAYER UP
  399. begin
  400. playery = playery - 1'b1;
  401. end
  402. else if(~KEY[1]) // MOVE PLAYER DOWN
  403. begin
  404. playery = playery + 1'b1;
  405. end
  406. else if(~KEY[0]) // MOVE PLAYER RIGHT
  407. begin
  408. playerx = playerx + 1'b1;
  409. end
  410. end
  411. end
  412. endmodule
  413.  
  414. module rateDivider(clk, update);
  415. input clk;
  416. output reg update;
  417. reg [25:0] counter;
  418.  
  419. always @(posedge clk)
  420. begin
  421. counter = counter + 1'd1;
  422. if(counter > 26'd3124999)
  423. begin
  424. counter = 1'd0;
  425. end
  426. update = (counter == 1'd0) ? 1 : 0;
  427. end
  428.  
  429. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement