Advertisement
Guest User

Untitled

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