Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.75 KB | None | 0 0
  1. // Part 2 skeleton
  2.  
  3. module chicken
  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. HEX0,
  19. HEX1
  20. );
  21. input CLOCK_50; // 50 MHz
  22. input [9:0] SW;
  23. input [3:0] KEY;
  24. output [6:0] HEX0;
  25. output [6:0] HEX1;
  26. // Declare your inputs and outputs here
  27. // Do not change the following outputs
  28. output VGA_CLK; // VGA Clock
  29. output VGA_HS; // VGA H_SYNC
  30. output VGA_VS; // VGA V_SYNC
  31. output VGA_BLANK_N; // VGA BLANK
  32. output VGA_SYNC_N; // VGA SYNC
  33. output [9:0] VGA_R; // VGA Red[9:0]
  34. output [9:0] VGA_G; // VGA Green[9:0]
  35. output [9:0] VGA_B; // VGA Blue[9:0]
  36.  
  37. wire resetn;
  38. assign resetn = alwaysOne ;
  39.  
  40. // Create the colour, x, y and writeEn wires that are inputs to the controller.
  41. reg [2:0] colour;// notice they were originally wire , I made them reg edit:Mar20, 2:30am
  42. reg [6:0] x;
  43. reg [6:0] y;
  44. reg writeEn;
  45.  
  46.  
  47. // Create an Instance of a VGA controller - there can be only one!
  48. // Define the number of colours as well as the initial background
  49. // image file (.MIF) for the controller.
  50. vga_adapter VGA(
  51. .resetn(resetn),
  52. .clock(CLOCK_50),
  53. .colour(colour),
  54. .x(x),
  55. .y(y),
  56. .plot(writeEn),
  57. /* Signals for the DAC to drive the monitor. */
  58. .VGA_R(VGA_R),
  59. .VGA_G(VGA_G),
  60. .VGA_B(VGA_B),
  61. .VGA_HS(VGA_HS),
  62. .VGA_VS(VGA_VS),
  63. .VGA_BLANK(VGA_BLANK_N),
  64. .VGA_SYNC(VGA_SYNC_N),
  65. .VGA_CLK(VGA_CLK));
  66. defparam VGA.RESOLUTION = "160x120";
  67. defparam VGA.MONOCHROME = "FALSE";
  68. defparam VGA.BITS_PER_COLOUR_CHANNEL = 1;
  69. defparam VGA.BACKGROUND_IMAGE = "black.mif";
  70.  
  71. // Put your code here. Your code should produce signals x,y,colour and writeEn/plot
  72. // for the VGA controller, in addition to any other functionality your design may require.
  73. reg alwaysOne = 1'b1;
  74. reg alwaysZero = 1'b0;
  75. reg speed1 = 2'b00;
  76. reg speed2 = 2'b01;
  77. reg speed3 = 2'b10;
  78.  
  79. wire ld_x, ld_y;
  80. wire [3:0] stateNum;
  81. reg [6:0] init_player_coord = 7'b0101111; // this is x coord
  82. wire [2:0] colour_player;
  83. wire [6:0] x_player;
  84. wire [6:0] y_player;
  85. wire writeEn_player;
  86. reg [25:0] counter_for_player = 26'b00000000000000000000000000;
  87. reg [6:0] init_y_p = 7'b1110000;
  88. reg [2:0] acolour_p = 3'b110;
  89. // Instansiate datapath
  90. datapath d0(.clk(CLOCK_50), .ld_x(ld_x), .ld_y(ld_y), .in( init_player_coord), .reset_n(resetn), .x(x_player), .y(y_player), .colour(colour_player), .write(writeEn_player), .stateNum(stateNum), .init_y(init_y_p), .acolour(acolour_p));
  91.  
  92. // Instansiate FSM control
  93. control c0(.clk(CLOCK_50), .move_r(~KEY[0]), .move_l(~KEY[3]), .move_d(~KEY[1]), .move_u(~KEY[2]), .reset_n(resetn), .ld_x(ld_x), .ld_y(ld_y), .stateNum(stateNum), .reset_game(reset_game), .dingding(counter_for_player), .how_fast(speed1));
  94.  
  95. wire writeEn_car0[1:0];
  96. wire [2:0] colour_car0[1:0];
  97. wire ld_x_car0[1:0], ld_y_car0[1:0];
  98. wire [3:0] stateNum_car0[1:0];
  99.  
  100. reg [6:0] car_coord[1:0];
  101.  
  102. wire [6:0] x_car0[1:0];
  103. wire [6:0] y_car0[1:0];
  104.  
  105. reg [25:0] counter_for_car[1:0];
  106.  
  107. reg [6:0] init_y_car[1:0];
  108.  
  109. reg [2:0] acolour_car[1:0];
  110.  
  111. assign car_coord[0] = 7'b0000100;
  112.  
  113. initial
  114. begin
  115. //Initial car position
  116. car_coord[0] = 7'b0000100;
  117. car_coord[1] = 7'b0000101;
  118. //Counter for cars
  119. counter_for_car[0] = 26'b00000000000000000000000001;
  120. counter_for_car[1] = 26'b00000000000000000000000010;
  121. //Initial y position
  122. init_y_car[0] = 7'b0000100;
  123. init_y_car[1] = 7'b0000100;
  124. //Colour for car
  125. acolour_car[0] = 3'b010;
  126. acolour_car[1] = 3'b011;
  127. end
  128.  
  129. datapath car_datapath(.clk(CLOCK_50), .ld_x(ld_x_car0[0]), .ld_y(ld_y_car0[0]), .in( car_coord[0]), .reset_n(resetn), .x(x_car0[0]), .y(y_car0[0]), .colour(colour_car0[0]), .write(writeEn_car0[0]), .stateNum(stateNum_car0[0]), .init_y(init_y_car[0]), .acolour(acolour_car[0]));
  130.  
  131. // Instansiate FSM control
  132. control car_FSM(.clk(CLOCK_50), .move_r(alwaysZero), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car0[0]), .ld_y(ld_y_car0[0]), .stateNum(stateNum_car0[0]), .reset_game(reset_game), .dingding(counter_for_car[0]), .how_fast(speed1));
  133.  
  134.  
  135.  
  136.  
  137. // genvar i;
  138. // generate
  139. // for (i = 0; i <= 0; i = i + 1)
  140. // begin : test
  141. // // Instansiate datapath
  142. // datapath car_datapath(.clk(CLOCK_50), .ld_x(ld_x_car0[i]), .ld_y(ld_y_car0[i]), .in( car_coord[i]), .reset_n(resetn), .x(x_car0[i]), .y(y_car0[i]), .colour(colour_car0[i]), .write(writeEn_car0[i]), .stateNum(stateNum_car0[i]), .init_y(init_y_car[i]), .acolour(acolour_car[i]));
  143. //
  144. // // Instansiate FSM control
  145. // control car_FSM(.clk(CLOCK_50), .move_r(alwaysZero), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car0[i]), .ld_y(ld_y_car0[i]), .stateNum(stateNum_car0[i]), .reset_game(reset_game), .dingding(counter_for_car[i]), .how_fast(speed1));
  146. // end
  147. // endgenerate
  148.  
  149.  
  150.  
  151. //The outputs are: x_car0, y_car0, colour_car0, writeEn_car0
  152. //car0 movement ends here----------------------------------------------------------------------------------------------------
  153.  
  154.  
  155. // wire ld_x_car1, ld_y_car1;
  156. // wire [3:0] stateNum_car1;
  157. // reg [6:0] car1_coord = 7'b0101100; // this is init x coord!!!
  158. // wire [2:0] colour_car1;
  159. // wire [6:0] x_car1;
  160. // wire [6:0] y_car1;
  161. // wire writeEn_car1;
  162. // reg [25:0] counter_for_car1 = 26'b00000000000000000000000010;
  163. // reg [6:0] init_y_c1 = 7'b0100111;
  164. // reg [2:0] acolour_c1 = 3'b101;
  165. // // Instansiate datapath
  166. // datapath car_1_d(.clk(CLOCK_50), .ld_x(ld_x_car1), .ld_y(ld_y_car1), .in( car1_coord), .reset_n(resetn), .x(x_car1), .y(y_car1), .colour(colour_car1), .write(writeEn_car1), .stateNum(stateNum_car1), .init_y(init_y_c1), .acolour(acolour_c1));
  167. //
  168. // // Instansiate FSM control
  169. // control car_1_c(.clk(CLOCK_50), .move_r(alwaysZero), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car1), .ld_y(ld_y_car1), .stateNum(stateNum_car1), .reset_game(alwaysZero), .dingding(counter_for_car1), .how_fast(speed3));
  170. // //car1 movement ends here----------------------------------------------------------------------------------------------------
  171. //
  172. //
  173. // wire ld_x_car2, ld_y_car2;
  174. // wire [3:0] stateNum_car2;
  175. // reg [6:0] car2_coord = 7'b0000100; // this is init x coord!!!
  176. // wire [2:0] colour_car2;
  177. // wire [6:0] x_car2;
  178. // wire [6:0] y_car2;
  179. // wire writeEn_car2;
  180. // reg [25:0] counter_for_car2 = 26'b00000000000000000000000011;
  181. // reg [6:0] init_y_c2 = 7'b0000100;
  182. // reg [2:0] acolour_c2 = 3'b110;
  183. // // Instansiate datapath
  184. // datapath car_2_d(.clk(CLOCK_50), .ld_x(ld_x_car2), .ld_y(ld_y_car2), .in(car2_coord), .reset_n(resetn), .x(x_car2), .y(y_car2), .colour(colour_car2), .write(writeEn_car2), .stateNum(stateNum_car2), .init_y(init_y_c2), .acolour(acolour_c2));
  185. //
  186. // // Instansiate FSM control
  187. // control car_2_c(.clk(CLOCK_50), .move_r(alwaysZero), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car2), .ld_y(ld_y_car2), .stateNum(stateNum_car2), .reset_game(alwaysZero), .dingding(counter_for_car2), .how_fast(speed1));
  188. //
  189. //
  190. // //car2 movement ends here----------------------------------------------------------------------------------------------------
  191. //
  192. //
  193. // wire ld_x_car3, ld_y_car3;
  194. // wire [3:0] stateNum_car3;
  195. // reg [6:0] car3_coord = 7'b0101111; // this is init x coord!!!
  196. // wire [2:0] colour_car3;
  197. // wire [6:0] x_car3;
  198. // wire [6:0] y_car3;
  199. // wire writeEn_car3;
  200. // reg [25:0] counter_for_car3 = 26'b00000000000000000000000100;
  201. // reg [6:0] init_y_c3 = 7'b0000100;
  202. // reg [2:0] acolour_c3 = 3'b111;
  203. // // Instansiate datapath
  204. // datapath car_3_d(.clk(CLOCK_50), .ld_x(ld_x_car3), .ld_y(ld_y_car3), .in(car3_coord), .reset_n(resetn), .x(x_car3), .y(y_car3), .colour(colour_car3), .write(writeEn_car3), .stateNum(stateNum_car3), .init_y(init_y_c3), .acolour(acolour_c3));
  205. //
  206. // // Instansiate FSM control
  207. // control car_3_c(.clk(CLOCK_50), .move_r(alwaysZero), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car3), .ld_y(ld_y_car3), .stateNum(stateNum_car3), .reset_game(alwaysZero), .dingding(counter_for_car3), .how_fast(speed1));
  208. // //car3 movement ends here----------------------------------------------------------------------------------------------------
  209. //
  210. //
  211. // wire ld_x_car4, ld_y_car4;
  212. // wire [3:0] stateNum_car4;
  213. // reg [6:0] car4_coord = 7'b0000110; // this is init x coord!!!
  214. // wire [2:0] colour_car4;
  215. // wire [6:0] x_car4;
  216. // wire [6:0] y_car4;
  217. // wire writeEn_car4;
  218. // reg [25:0] counter_for_car4 = 26'b00000000000000000000000101;
  219. // reg [6:0] init_y_c4 = 7'b0000100;
  220. // reg [2:0] acolour_c4 = 3'b001;
  221. // // Instansiate datapath
  222. // datapath car_4_d(.clk(CLOCK_50), .ld_x(ld_x_car4), .ld_y(ld_y_car4), .in(car4_coord), .reset_n(resetn), .x(x_car4), .y(y_car4), .colour(colour_car4), .write(writeEn_car4), .stateNum(stateNum_car4), .init_y(init_y_c4), .acolour(acolour_c4));
  223. //
  224. // // Instansiate FSM control
  225. // control car_4_c(.clk(CLOCK_50), .move_r(alwaysZero), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car4), .ld_y(ld_y_car4), .stateNum(stateNum_car4), .reset_game(alwaysZero), .dingding(counter_for_car4), .how_fast(speed1));
  226. // //car4 movement ends here----------------------------------------------------------------------------------------------------
  227. //
  228. //
  229. // wire ld_x_car5, ld_y_car5;
  230. // wire [3:0] stateNum_car5;
  231. // reg [6:0] car5_coord = 7'b1010001; // this is init x coord!!!
  232. // wire [2:0] colour_car5;
  233. // wire [6:0] x_car5;
  234. // wire [6:0] y_car5;
  235. // wire writeEn_car5;
  236. // reg [25:0] counter_for_car5 = 26'b00000000000000000000000110;
  237. // reg [6:0] init_y_c5 = 7'b0011100;
  238. // reg [2:0] acolour_c5 = 3'b110;
  239. // // Instansiate datapath
  240. // datapath car_5_d(.clk(CLOCK_50), .ld_x(ld_x_car5), .ld_y(ld_y_car5), .in(car5_coord), .reset_n(resetn), .x(x_car5), .y(y_car5), .colour(colour_car5), .write(writeEn_car5), .stateNum(stateNum_car5), .init_y(init_y_c5), .acolour(acolour_c5));
  241. //
  242. // // Instansiate FSM control
  243. // control car_5_c(.clk(CLOCK_50), .move_r(alwaysZero), .move_l(alwaysOne), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car5), .ld_y(ld_y_car5), .stateNum(stateNum_car5), .reset_game(alwaysZero), .dingding(counter_for_car5), .how_fast(speed1));
  244. // //car5 movement ends here----------------------------------------------------------------------------------------------------
  245. //
  246. //
  247. // wire ld_x_car6, ld_y_car6;
  248. // wire [3:0] stateNum_car6;
  249. // reg [6:0] car6_coord = 7'b0111000; // this is init x coord!!!
  250. // wire [2:0] colour_car6;
  251. // wire [6:0] x_car6;
  252. // wire [6:0] y_car6;
  253. // wire writeEn_car6;
  254. // reg [25:0] counter_for_car6 = 26'b00000000000000000000000111;
  255. // reg [6:0] init_y_c6 = 7'b0011010;
  256. // reg [2:0] acolour_c6 = 3'b001;
  257. // // Instansiate datapath
  258. // datapath car_6_d(.clk(CLOCK_50), .ld_x(ld_x_car6), .ld_y(ld_y_car6), .in(car6_coord), .reset_n(resetn), .x(x_car6), .y(y_car6), .colour(colour_car6), .write(writeEn_car6), .stateNum(stateNum_car6), .init_y(init_y_c6), .acolour(acolour_c6));
  259. //
  260. // // Instansiate FSM control
  261. // control car_6_c(.clk(CLOCK_50), .move_r(alwaysOne), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car6), .ld_y(ld_y_car6), .stateNum(stateNum_car6), .reset_game(alwaysZero), .dingding(counter_for_car6), .how_fast(speed2));
  262. // //car6 movement ends here----------------------------------------------------------------------------------------------------
  263. //
  264. //
  265. // wire ld_x_car7, ld_y_car7;
  266. // wire [3:0] stateNum_car7;
  267. // reg [6:0] car7_coord = 7'b0100000; // this is init x coord!!!
  268. // wire [2:0] colour_car7;
  269. // wire [6:0] x_car7;
  270. // wire [6:0] y_car7;
  271. // wire writeEn_car7;
  272. // reg [25:0] counter_for_car7 = 26'b00000000000000000000001000;
  273. // reg [6:0] init_y_c7 = 7'b1011001;
  274. // reg [2:0] acolour_c7 = 3'b101;
  275. // // Instansiate datapath
  276. // datapath car_7_d(.clk(CLOCK_50), .ld_x(ld_x_car7), .ld_y(ld_y_car7), .in(car7_coord), .reset_n(resetn), .x(x_car7), .y(y_car7), .colour(colour_car7), .write(writeEn_car7), .stateNum(stateNum_car7), .init_y(init_y_c7), .acolour(acolour_c7));
  277. //
  278. // // Instansiate FSM control
  279. // control car_7_c(.clk(CLOCK_50), .move_r(alwaysOne), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car7), .ld_y(ld_y_car7), .stateNum(stateNum_car7), .reset_game(alwaysZero), .dingding(counter_for_car7), .how_fast(speed3));
  280. // //car7 movement ends here----------------------------------------------------------------------------------------------------
  281. //
  282. //
  283. // wire ld_x_car8, ld_y_car8;
  284. // wire [3:0] stateNum_car8;
  285. // reg [6:0] car8_coord = 7'b0110001; // this is init x coord!!!
  286. // wire [2:0] colour_car8;
  287. // wire [6:0] x_car8;
  288. // wire [6:0] y_car8;
  289. // wire writeEn_car8;
  290. // reg [25:0] counter_for_car8 = 26'b00000000000000000000001001;
  291. // reg [6:0] init_y_c8 = 7'b0010000;
  292. // reg [2:0] acolour_c8 = 3'b110;
  293. // // Instansiate datapath
  294. // datapath car_8_d(.clk(CLOCK_50), .ld_x(ld_x_car8), .ld_y(ld_y_car8), .in(car8_coord), .reset_n(resetn), .x(x_car8), .y(y_car8), .colour(colour_car8), .write(writeEn_car8), .stateNum(stateNum_car8), .init_y(init_y_c8), .acolour(acolour_c8));
  295. //
  296. // // Instansiate FSM control
  297. // control car_8_c(.clk(CLOCK_50), .move_r(alwaysOne), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car8), .ld_y(ld_y_car8), .stateNum(stateNum_car8), .reset_game(alwaysZero), .dingding(counter_for_car8), .how_fast(speed1));
  298. // //car8 movement ends here----------------------------------------------------------------------------------------------------
  299. //
  300. //
  301. // wire ld_x_car9, ld_y_car9;
  302. // wire [3:0] stateNum_car9;
  303. // reg [6:0] car9_coord = 7'b1000000; // this is init x coord!!!
  304. // wire [2:0] colour_car9;
  305. // wire [6:0] x_car9;
  306. // wire [6:0] y_car9;
  307. // wire writeEn_car9;
  308. // reg [25:0] counter_for_car9 = 26'b00000000000000000000001010;
  309. // reg [6:0] init_y_c9 = 7'b0010111;
  310. // reg [2:0] acolour_c9 = 3'b010;
  311. // // Instansiate datapath
  312. // datapath car_9_d(.clk(CLOCK_50), .ld_x(ld_x_car9), .ld_y(ld_y_car9), .in(car9_coord), .reset_n(resetn), .x(x_car9), .y(y_car9), .colour(colour_car9), .write(writeEn_car9), .stateNum(stateNum_car9), .init_y(init_y_c9), .acolour(acolour_c9));
  313. //
  314. // // Instansiate FSM control
  315. // control car_9_c(.clk(CLOCK_50), .move_r(alwaysOne), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car9), .ld_y(ld_y_car9), .stateNum(stateNum_car9), .reset_game(alwaysZero), .dingding(counter_for_car9), .how_fast(speed2));
  316. // //car9 movement ends here----------------------------------------------------------------------------------------------------
  317. //
  318. //
  319. // wire ld_x_car10, ld_y_car10;
  320. // wire [3:0] stateNum_car10;
  321. // reg [6:0] car10_coord = 7'b0000100; // this is init x coord!!!
  322. // wire [2:0] colour_car10;
  323. // wire [6:0] x_car10;
  324. // wire [6:0] y_car10;
  325. // wire writeEn_car10;
  326. // reg [25:0] counter_for_car10 = 26'b00000000000000000000001011;
  327. // reg [6:0] init_y_c10 = 7'b1010110;
  328. // reg [2:0] acolour_c10 = 3'b011;
  329. // // Instansiate datapath
  330. // datapath car_10_d(.clk(CLOCK_50), .ld_x(ld_x_car10), .ld_y(ld_y_car10), .in(car10_coord), .reset_n(resetn), .x(x_car10), .y(y_car10), .colour(colour_car10), .write(writeEn_car10), .stateNum(stateNum_car10), .init_y(init_y_c10), .acolour(acolour_c10));
  331. //
  332. // // Instansiate FSM control
  333. // control car_10_c(.clk(CLOCK_50), .move_r(alwaysZero), .move_l(alwaysOne), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car10), .ld_y(ld_y_car10), .stateNum(stateNum_car10), .reset_game(alwaysZero), .dingding(counter_for_car10), .how_fast(speed3));
  334. // //car10 movement ends here----------------------------------------------------------------------------------------------------
  335. //
  336. //
  337. // wire ld_x_car11, ld_y_car11;
  338. // wire [3:0] stateNum_car11;
  339. // reg [6:0] car11_coord = 7'b0110111; // this is init x coord!!!
  340. // wire [2:0] colour_car11;
  341. // wire [6:0] x_car11;
  342. // wire [6:0] y_car11;
  343. // wire writeEn_car11;
  344. // reg [25:0] counter_for_car11 = 26'b00000000000000000000001100;
  345. // reg [6:0] init_y_c11 = 7'b0010101;
  346. // reg [2:0] acolour_c11 = 3'b101;
  347. // // Instansiate datapath
  348. // datapath car_11_d(.clk(CLOCK_50), .ld_x(ld_x_car11), .ld_y(ld_y_car11), .in(car11_coord), .reset_n(resetn), .x(x_car11), .y(y_car11), .colour(colour_car11), .write(writeEn_car11), .stateNum(stateNum_car11), .init_y(init_y_c11), .acolour(acolour_c11));
  349. //
  350. // // Instansiate FSM control
  351. // control car_11_c(.clk(CLOCK_50), .move_r(alwaysOne), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car11), .ld_y(ld_y_car11), .stateNum(stateNum_car11), .reset_game(alwaysZero), .dingding(counter_for_car11), .how_fast(speed1));
  352. // //car11 movement ends here----------------------------------------------------------------------------------------------------
  353. //
  354. //
  355. // wire ld_x_car12, ld_y_car12;
  356. // wire [3:0] stateNum_car12;
  357. // reg [6:0] car12_coord = 7'b0110100; // this is init x coord!!!
  358. // wire [2:0] colour_car12;
  359. // wire [6:0] x_car12;
  360. // wire [6:0] y_car12;
  361. // wire writeEn_car12;
  362. // reg [25:0] counter_for_car12 = 26'b00000000000000000000001101;
  363. // reg [6:0] init_y_c12 = 7'b1010100;
  364. // reg [2:0] acolour_c12 = 3'b010;
  365. // // Instansiate datapath
  366. // datapath car_12_d(.clk(CLOCK_50), .ld_x(ld_x_car12), .ld_y(ld_y_car12), .in(car12_coord), .reset_n(resetn), .x(x_car12), .y(y_car12), .colour(colour_car12), .write(writeEn_car12), .stateNum(stateNum_car12), .init_y(init_y_c12), .acolour(acolour_c12));
  367. //
  368. // // Instansiate FSM control
  369. // control car_12_c(.clk(CLOCK_50), .move_r(alwaysOne), .move_l(alwaysZero), .move_d(alwaysZero), .move_u(alwaysZero), .reset_n(resetn), .ld_x(ld_x_car12), .ld_y(ld_y_car12), .stateNum(stateNum_car12), .reset_game(alwaysZero), .dingding(counter_for_car12), .how_fast(speed2));
  370. // //car12 movement ends here----------------------------------------------------------------------------------------------------
  371. //
  372. //
  373. // --------------------------------------car movement ends here, for all cars----------------------------------------------------------
  374.  
  375.  
  376. wire [9:0] score;
  377. wire reset_game;
  378. //scoreCounter player_and_car0(x_player, y_player, x_car0, y_car0, x_car1, y_car1, x_car2, y_car2, x_car3, y_car3, x_car4, y_car4, x_car5, y_car5, x_car6, y_car6, x_car7, y_car7, x_car8, y_car8, x_car9, y_car9, x_car10, y_car10, x_car11, y_car11, x_car12, y_car12, score, reset_game);
  379.  
  380. hex_display first_digit(score, HEX1[6:0], HEX0[6:0]); //notice score is displayed in hexadecimal
  381.  
  382. //The following for processing player movement and car movement (make sure that only one of play or the car can move on the same clock cycle)
  383. // If player is moving ,then link the vga to the player, if the car is moving, than link the vga to the car
  384. // If both of them are moving in the same clock cycle (very unlikely), then move the player
  385. // edited: mar 20, 5pmreset_n
  386. // Notice: writeEn_player is write Enable for player; writeEn_car0 is write enable for car_0
  387. // The following is for choosing to print the player movement or to print the car movement
  388.  
  389. always @(posedge CLOCK_50)
  390. begin
  391. if(writeEn_player)
  392. begin
  393. writeEn <= writeEn_player; // Do I use <= or = ????? writeEn, x, y and colour are originally type wire, but I need to make them type reg??? ????????????????????????????????
  394. x <= x_player;
  395. y <= y_player;
  396. colour = colour_player; // Notice: I made the following variable type reg: writeEn, x, y, colour
  397. end
  398. else if (writeEn_car0[0]) // if player isnt moving, then let the car move
  399. begin
  400. writeEn <= writeEn_car0[0];
  401. x <= x_car0[0];
  402. y <= y_car0[0];
  403. colour <= colour_car0[0];
  404. end
  405. // Added on Mar27 -------------
  406. else if (writeEn_car0[1])
  407. begin
  408. writeEn <= writeEn_car0[1];
  409. x <= x_car0[1];
  410. y <= y_car0[1];
  411. colour <= colour_car0[1];
  412. end
  413. // else if (writeEn_car2)
  414. // begin
  415. // writeEn <= writeEn_car2;
  416. // x <= x_car2;
  417. // y <= y_car2;
  418. // colour <= colour_car2;
  419. // end
  420. // else if (writeEn_car3)
  421. // begin
  422. // writeEn <= writeEn_car3;
  423. // x <= x_car3;
  424. // y <= y_car3;
  425. // colour <= colour_car3;
  426. // end
  427. // else if (writeEn_car4)
  428. // begin
  429. // writeEn <= writeEn_car4;
  430. // x <= x_car4;
  431. // y <= y_car4;
  432. // colour <= colour_car4;
  433. // end
  434. // else if (writeEn_car5)
  435. // begin
  436. // writeEn <= writeEn_car5;
  437. // x <= x_car5;
  438. // y <= y_car5;
  439. // colour <= colour_car5;
  440. // end
  441. // else if (writeEn_car6)
  442. // begin
  443. // writeEn <= writeEn_car6;
  444. // x <= x_car6;
  445. // y <= y_car6;
  446. // colour <= colour_car6;
  447. // end
  448. // else if (writeEn_car7)
  449. // begin
  450. // writeEn <= writeEn_car7;
  451. // x <= x_car7;
  452. // y <= y_car7;
  453. // colour <= colour_car7;
  454. // end
  455. // else if (writeEn_car8)
  456. // begin
  457. // writeEn <= writeEn_car8;
  458. // x <= x_car8;
  459. // y <= y_car8;
  460. // colour <= colour_car8;
  461. // end
  462. // else if (writeEn_car9)
  463. // begin
  464. // writeEn <= writeEn_car9;
  465. // x <= x_car9;
  466. // y <= y_car9;
  467. // colour <= colour_car9;
  468. // end
  469. // else if (writeEn_car10)
  470. // begin
  471. // writeEn <= writeEn_car10;
  472. // x <= x_car10;
  473. // y <= y_car10;
  474. // colour <= colour_car10;
  475. // end
  476. // else if (writeEn_car11)
  477. // begin
  478. // writeEn <= writeEn_car11;
  479. // x <= x_car11;
  480. // y <= y_car11;
  481. // colour <= colour_car11;
  482. // end
  483. // else if (writeEn_car12)
  484. // begin
  485. // writeEn <= writeEn_car12;
  486. // x <= x_car12;
  487. // y <= y_car12;
  488. // colour <= colour_car12;
  489. // end
  490. //Mar27 adding end---------------
  491. end
  492.  
  493. endmodule
  494.  
  495.  
  496. //You need to extend the parameter of this module such that all cood. of all cars are inputed into this module
  497. //outputs are score and reset,
  498. //if reset is 1, this means the play crashed into a car and game should be reset
  499. module scoreCounter(x_player, y_player,
  500. x_car0, y_car0, x_car1, y_car1, x_car2, y_car2, x_car3, y_car3, x_car4, y_car4, x_car5, y_car5, x_car6, y_car6,
  501. x_car7, y_car7, x_car8, y_car8, x_car9, y_car9, x_car10, y_car10, x_car11, y_car11, x_car12, y_car12,
  502. score, reset_game);
  503. input [6:0] x_player;
  504. input [6:0] y_player;
  505. input [6:0] x_car0;
  506. input [6:0] y_car0;
  507. input [6:0] x_car1;
  508. input [6:0] y_car1;
  509. input [6:0] x_car2;
  510. input [6:0] y_car2;
  511. input [6:0] x_car3;
  512. input [6:0] y_car3;
  513. input [6:0] x_car4;
  514. input [6:0] y_car4;
  515. input [6:0] x_car5;
  516. input [6:0] y_car5;
  517. input [6:0] x_car6;
  518. input [6:0] y_car6;
  519. input [6:0] x_car7;
  520. input [6:0] y_car7;
  521. input [6:0] x_car8;
  522. input [6:0] y_car8;
  523. input [6:0] x_car9;
  524. input [6:0] y_car9;
  525. input [6:0] x_car10;
  526. input [6:0] y_car10;
  527. input [6:0] x_car11;
  528. input [6:0] y_car11;
  529. input [6:0] x_car12;
  530. input [6:0] y_car12;
  531. output reg [9:0] score;
  532. output reg reset_game;
  533.  
  534.  
  535. always @(*) //Notice it is not positive edge
  536. begin
  537. if (y_player == 7'b0000011) // When player reaches the top of the screen
  538. begin // it is going into this state
  539. score = score + 1'b1;
  540. reset_game = 1'b1;
  541. end
  542. else if (y_player == 7'b1110001)
  543. begin
  544. reset_game = 1'b1;
  545. score = score;
  546. end
  547. else if ((x_player == x_car0 && y_player == y_car0) || (x_player == x_car1 && y_player == y_car1) ||
  548. (x_player == x_car2 && y_player == y_car2) || (x_player == x_car3 && y_player == y_car3) ||
  549. (x_player == x_car4 && y_player == y_car4) || (x_player == x_car5 && y_player == y_car5) ||
  550. (x_player == x_car6 && y_player == y_car6) || (x_player == x_car7 && y_player == y_car7) ||
  551. (x_player == x_car8 && y_player == y_car8) || (x_player == x_car9 && y_player == y_car9) ||
  552. (x_player == x_car10 && y_player == y_car10) || (x_player == x_car11 && y_player == y_car11) ||
  553. (x_player == x_car12 && y_player == y_car12) ) // player collide with car (any of the 13 cars)
  554. begin
  555. reset_game = 1'b1;
  556. score = 1'b0;
  557. end
  558.  
  559. else
  560. reset_game = 1'b0;
  561. end
  562.  
  563. endmodule
  564.  
  565.  
  566. module control(clk, move_r, move_l, move_d, move_u, reset_n, ld_x, ld_y, stateNum, reset_game, dingding, how_fast);
  567. input [25:0] dingding; // dingding is the counter! It counts like this: Ding!!! Ding!!! Ding!!! Ding!!! Ding!!!
  568. input reset_game;
  569. input clk, move_r, move_l, move_d, move_u, reset_n;
  570. input [1:0] how_fast;
  571. output reg ld_y, ld_x;
  572. reg [3:0] curr, next;
  573. output reg [3:0] stateNum;
  574. localparam S_CLEAR = 4'b0000;
  575. localparam S_LOAD_X = 4'b0001;
  576. localparam S_WAIT_Y = 4'b0010;
  577. localparam S_LOAD_Y = 4'b0011;
  578.  
  579. localparam wait_input = 4'b0100;
  580. localparam clear_all = 4'b0101;
  581. localparam print_right = 4'b0110;
  582. localparam print_left = 4'b0111;
  583. localparam print_down = 4'b1000;
  584. localparam print_up = 4'b1001;
  585. localparam print = 4'b1111;
  586. localparam temp_selecting_state = 4'b1010;
  587. localparam after_drawing = 4'b1011;
  588. localparam cleanUp = 4'b1100;
  589. wire [26:0] press_now;
  590. wire [26:0] press_now_for_car;
  591. wire result_press_now;
  592. reg [25:0] speed;
  593. //wire result_for_car;
  594.  
  595. always @(*)
  596. begin
  597. if (how_fast == 2'b00)
  598. speed <= 26'b0101111101011110000100;
  599.  
  600. else if (how_fast == 2'b01)
  601. speed <= 26'b010111110101111000010;
  602. else
  603. speed <= 26'b01011111010111100001;
  604. end
  605. RateDividerForCar player_counter1(clk, press_now, reset_n, speed);
  606.  
  607. assign result_press_now = (press_now == dingding) ? 1 : 0;
  608.  
  609. always @(*)
  610. begin: state_table
  611. case (curr)
  612. S_CLEAR: next = S_LOAD_X ;
  613. S_LOAD_X: next = S_WAIT_Y;
  614. S_WAIT_Y: next = S_LOAD_Y;
  615.  
  616. S_LOAD_Y: next = temp_selecting_state; // the next line is edited on Mar 27
  617. temp_selecting_state: next = reset_game ? cleanUp : ( ((move_r || move_l || move_d || move_u || ~move_u && ~move_d && ~move_l && ~move_r) && result_press_now) ? clear_all : S_LOAD_Y );
  618.  
  619. clear_all:
  620. begin
  621. if(move_r) // is this how to connect two wires ?????????????????????????????????????????????????????????
  622. next <= print_right;
  623. else if (move_l) // if player isnt moving, then let the car move
  624. next <= print_left;
  625. else if (move_d) // if player isnt moving, then let the car move
  626. next <= print_down;
  627. else if (move_u) // if player isnt moving, then let the car move
  628. next <= print_up;
  629. else if (~move_u && ~move_d && ~move_l && ~move_r)//default
  630. next <= print;
  631. end
  632. cleanUp: next = S_CLEAR;
  633. //
  634. print_right: next = reset_game ? S_LOAD_Y : after_drawing;
  635. print_left: next = reset_game ? S_LOAD_Y : after_drawing;
  636. print_down: next = reset_game ? S_LOAD_Y : after_drawing;
  637. print_up: next = reset_game ? S_LOAD_Y : after_drawing;
  638. print: next = reset_game ? S_LOAD_Y: after_drawing;
  639. after_drawing: next= temp_selecting_state;
  640.  
  641. default: next = S_CLEAR;
  642. endcase
  643. end
  644.  
  645. always@(*)
  646. begin: enable_signals
  647. ld_x = 1'b0;
  648. ld_y = 1'b0;
  649. //write = 1'b0;
  650. stateNum = 4'b0000;
  651. case (curr)
  652. S_LOAD_X: begin
  653. ld_x = 1'b1;
  654. end
  655. S_LOAD_Y: begin
  656. ld_y = 1'b1;
  657. end
  658. cleanUp: begin // this IS suppose to be the same as clear all (edited on mar27)
  659. stateNum = 4'b0001;
  660. ld_y = 1'b0;
  661. //write = 1'b1;
  662. end
  663. clear_all: begin
  664. stateNum = 4'b0001;
  665. ld_y = 1'b0;
  666. //write = 1'b1;
  667. end
  668.  
  669. print_right: begin
  670. stateNum = 4'b0100;
  671. ld_y = 1'b0;
  672. //write = 1'b1;
  673. end
  674.  
  675. print_down: begin
  676. stateNum = 4'b0011;
  677. ld_y = 1'b0;
  678. //write = 1'b1;
  679. end
  680.  
  681. print_left: begin
  682. stateNum = 4'b0010;
  683. ld_y = 1'b0;
  684.  
  685. //write = 1'b1;
  686. end
  687.  
  688. print_up: begin
  689. stateNum = 4'b1001;
  690. ld_y = 1'b0;
  691.  
  692. //write = 1'b1;
  693. end
  694.  
  695. print: begin
  696. stateNum = 4'b1111;
  697. ld_y = 1'b0;
  698. //write = 1'b1;
  699. end
  700.  
  701. after_drawing: begin
  702. stateNum = 4'b1000;
  703. end
  704.  
  705.  
  706. endcase
  707. end
  708.  
  709. always @(posedge clk)
  710. begin: states
  711. if(!reset_n)
  712. curr <= S_LOAD_X;
  713. else
  714. curr <= next;
  715. end
  716.  
  717. endmodule
  718.  
  719. module datapath(clk, ld_x, ld_y, in, reset_n, x, y, colour, stateNum, write, init_y, acolour);
  720. input clk;
  721. input [6:0] in;
  722. input [6:0] init_y;
  723. input [2:0] acolour;
  724. input ld_x, ld_y;
  725. input reset_n;
  726. output reg [2:0] colour;
  727. output reg write;
  728. output reg [6:0] y;
  729. output reg [6:0] x;
  730. input [3:0] stateNum;
  731.  
  732. always @(posedge clk)
  733. begin
  734. if(!reset_n)
  735. begin
  736. x <= 6'b000000;
  737. y <= 6'b000000;
  738. colour <= 3'b000;
  739. end
  740. else
  741. begin
  742. if(ld_x)
  743. begin
  744. x[6:0] <= in;
  745. y <= init_y;
  746. write <= 1'b1;
  747. end
  748. else if(ld_y)
  749. begin
  750. write <= 1'b0;
  751. end
  752.  
  753. // The following is for clearing
  754. else if(stateNum == 4'b0001)
  755. begin
  756. colour <= 3'b000;
  757. write <= 1'b1;
  758. end
  759.  
  760. // The following is for moving right
  761. else if(stateNum == 4'b0100)
  762. begin
  763.  
  764. x[6:0] <= x + 6'b000001;
  765. colour <= acolour;
  766. write <= 1'b1;
  767. end
  768.  
  769. // The following is for moving left
  770. else if(stateNum == 4'b0010)
  771. begin
  772.  
  773. x[6:0] <= x - 6'b000001;
  774. colour <= acolour;
  775. write <= 1'b1;
  776. end
  777.  
  778. // The following is for moving down
  779. else if(stateNum == 4'b0011)
  780. begin
  781. begin
  782. if (x != 7'b1110000)
  783. begin
  784.  
  785. y[6:0] <= y + 6'b000001;
  786. colour <= acolour;
  787. write <= 1'b1;
  788. end
  789. else
  790. write <= 1'b0;
  791.  
  792. end
  793. end
  794.  
  795. else if(stateNum == 4'b1001)//for moving up
  796. begin
  797.  
  798. y[6:0] <= y - 6'b000001;
  799. colour <= acolour;
  800. write <= 1'b1;
  801. end
  802. else if(stateNum == 4'b1111)//for stationary
  803. begin
  804. colour <= acolour;
  805. write <= 1'b1;
  806. end
  807.  
  808. else if(stateNum == 4'b1000)//after drawing
  809. begin
  810. write <= 1'b0;
  811. end
  812.  
  813. end
  814. end
  815.  
  816. endmodule
  817.  
  818.  
  819. module RateDividerForCar (clock, q, Clear_b, how_speedy); // Note that car is 4 times faster than the player
  820. input [0:0] clock;
  821. input [0:0] Clear_b;
  822. input [25:0] how_speedy;
  823. output reg [26:0] q; // declare q
  824. //wire [27:0] d; // declare d, not needed
  825. always@(posedge clock) // triggered every time clock rises
  826. begin
  827. // else if (ParLoad == 1'b1) // Check if parallel load, not needed!!!!
  828. // q <= d; // load d
  829. if (q == how_speedy) // when q is the maximum value for the counter, this number is 50 million - 1
  830. q <= 0; // q reset to 0
  831. else if (clock == 1'b1) // increment q only when Enable is 1
  832. q <= q + 1'b1; // increment q
  833. // q <= q - 1'b1; // decrement q
  834. end
  835. endmodule
  836.  
  837.  
  838.  
  839.  
  840. // The hex display for showing the level of the player
  841. module hex_display(IN, OUT1, OUT2);
  842. input [4:0] IN;
  843. output reg [7:0] OUT1, OUT2;
  844.  
  845. always @(*)
  846. begin
  847. case(IN[4:0])
  848. 5'b00000:
  849. begin
  850. OUT1 = 7'b1000000;
  851. OUT2 = 7'b1000000;
  852. end
  853. 5'b00001:
  854. begin
  855. OUT1 = 7'b1000000;
  856. OUT2 = 7'b1111001;
  857. end
  858. 5'b00010:
  859. begin
  860. OUT1 = 7'b1000000;
  861. OUT2 = 7'b0100100;
  862. end
  863. 5'b00011:
  864. begin
  865. OUT1 = 7'b1000000;
  866. OUT2 = 7'b0110000;
  867. end
  868. 5'b00100:
  869. begin
  870. OUT1 = 7'b1000000;
  871. OUT2 = 7'b0011001;
  872. end
  873. 5'b00101:
  874. begin
  875. OUT1 = 7'b1000000;
  876. OUT2 = 7'b0010010;
  877. end
  878. 5'b00110:
  879. begin
  880. OUT1 = 7'b1000000;
  881. OUT2 = 7'b0000010;
  882. end
  883. 5'b00111:
  884. begin
  885. OUT1 = 7'b1000000;
  886. OUT2 = 7'b1111000;
  887. end
  888. 5'b01000:
  889. begin
  890. OUT1 = 7'b1000000;
  891. OUT2 = 7'b0000000;
  892. end
  893. 5'b01001:
  894. begin
  895. OUT1 = 7'b1000000;
  896. OUT2 = 7'b0011000;
  897. end
  898. 5'b01010:
  899. begin
  900. OUT1 = 7'b1111001;
  901. OUT2 = 7'b1000000;
  902. end
  903. 5'b01011:
  904. begin
  905. OUT1 = 7'b1111001;
  906. OUT2 = 7'b1111001;
  907. end
  908. 5'b01100:
  909. begin
  910. OUT1 = 7'b1111001;
  911. OUT2 = 7'b0100100;
  912. end
  913. 5'b01101:
  914. begin
  915. OUT1 = 7'b1111001;
  916. OUT2 = 7'b0110000;
  917. end
  918. 5'b01110:
  919. begin
  920. OUT1 = 7'b1111001;
  921. OUT2 = 7'b0011001;
  922. end
  923. 5'b01111:
  924. begin
  925. OUT1 = 7'b1111001;
  926. OUT2 = 7'b0010010;
  927. end
  928. 5'b10000:
  929. begin
  930. OUT1 = 7'b1111001;
  931. OUT2 = 7'b0000010;
  932. end
  933. 5'b10001:
  934. begin
  935. OUT1 = 7'b1111001;
  936. OUT2 = 7'b1111000;
  937. end
  938. 5'b10010:
  939. begin
  940. OUT1 = 7'b1111001;
  941. OUT2 = 7'b0000000;
  942. end
  943. 5'b10011:
  944. begin
  945. OUT1 = 7'b1111001;
  946. OUT2 = 7'b0011000;
  947. end
  948. 5'b10100:
  949. begin
  950. OUT1 = 7'b0100100;
  951. OUT2 = 7'b1000000;
  952. end
  953. 5'b10101:
  954. begin
  955. OUT1 = 7'b0100100;
  956. OUT2 = 7'b1111001;
  957. end
  958. 5'b10110:
  959. begin
  960. OUT1 = 7'b0100100;
  961. OUT2 = 7'b0100100;
  962. end
  963. 5'b10111:
  964. begin
  965. OUT1 = 7'b0100100;
  966. OUT2 = 7'b0110000;
  967. end
  968. 5'b11000:
  969. begin
  970. OUT1 = 7'b0100100;
  971. OUT2 = 7'b0011001;
  972. end
  973. 5'b11001:
  974. begin
  975. OUT1 = 7'b0100100;
  976. OUT2 = 7'b0010010;
  977. end
  978. 5'b11010:
  979. begin
  980. OUT1 = 7'b0100100;
  981. OUT2 = 7'b0000010;
  982. end
  983. 5'b11011:
  984. begin
  985. OUT1 = 7'b0100100;
  986. OUT2 = 7'b1111000;
  987. end
  988. 5'b11100:
  989. begin
  990. OUT1 = 7'b0100100;
  991. OUT2 = 7'b0000000;
  992. end
  993. 5'b11101:
  994. begin
  995. OUT1 = 7'b0100100;
  996. OUT2 = 7'b0011000;
  997. end
  998. 5'b11110:
  999. begin
  1000. OUT1 = 7'b0110000;
  1001. OUT2 = 7'b1000000;
  1002. end
  1003. 5'b11111:
  1004. begin
  1005. OUT1 = 7'b0110000;
  1006. OUT2 = 7'b0100100;
  1007. end
  1008.  
  1009. default:
  1010. begin
  1011. OUT1 = 7'b0110000;
  1012. OUT2 = 7'b1111111;
  1013. end
  1014. endcase
  1015.  
  1016. end
  1017. endmodule
  1018.  
  1019.  
  1020.  
  1021. /* The following is the python code for testing generating random speeds (edit mar 27)
  1022.  
  1023.  
  1024. def testing(depth, A):
  1025. if depth == 0:
  1026. return A;
  1027. else:
  1028. A[1] = (A[0] + A[1])%3+1;
  1029. A[2] = (A[1] + A[2])%3+1;
  1030. A[3] = (A[2] + A[3])%3+1;
  1031. A[4] = (A[3] + A[4])%3+1;
  1032. A[5] = (A[4] + A[5])%3+1;
  1033. A[6] = (A[5] + A[6])%3+1;
  1034. A[7] = (A[6] + A[7])%3+1;
  1035. A[8] = (A[7] + A[8])%3+1;
  1036. A[9] = (A[8] + A[9])%3+1;
  1037. A[10] = (A[9] + A[10])%3+1;
  1038. A[11] = (A[10] + A[11])%3+1;
  1039. A[12] = (A[11] + A[12])%3+1;
  1040. return testing(depth - 1, A);
  1041.  
  1042.  
  1043. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement