Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. //=======================================================
  2. // ECE3400 Fall 2017
  3. // Lab 3: Template top-level module
  4. //
  5. // Top-level skeleton from Terasic
  6. // Modified by Claire Chen for ECE3400 Fall 2017
  7. //=======================================================
  8.  
  9. `define ONE_SEC 25000000
  10.  
  11.  
  12. `define BLACK 8'b000_000_00
  13. `define RED 8'b111_000_00
  14. `define GREEN 8'b000_111_00
  15. `define BLUE 8'b000_000_11
  16. `define ORANGE 8'b110_100_00
  17.  
  18. `define SQUARE_EDGE_0 120
  19. `define SQUARE_EDGE_1 240
  20. `define SQUARE_EDGE_2 360
  21. `define SQUARE_EDGE_3 480
  22. `define SQUARE_EDGE_4 600
  23.  
  24.  
  25. module DE0_NANO(
  26.  
  27. //////////// CLOCK //////////
  28. CLOCK_50,
  29.  
  30. //////////// LED //////////
  31. LED,
  32.  
  33. //////////// KEY //////////
  34. KEY,
  35.  
  36. //////////// SW //////////
  37. SW,
  38.  
  39. //////////// GPIO_0, GPIO_0 connect to GPIO Default //////////
  40. GPIO_0_D,
  41. GPIO_0_IN,
  42.  
  43. //////////// GPIO_0, GPIO_1 connect to GPIO Default //////////
  44. GPIO_1_D,
  45. GPIO_1_IN,
  46. );
  47.  
  48. //=======================================================
  49. // PARAMETER declarations
  50. //=======================================================
  51.  
  52. localparam ONE_SEC = 25000000; // one second in 25MHz clock cycles
  53.  
  54. //=======================================================
  55. // PORT declarations
  56. //=======================================================
  57.  
  58. //////////// CLOCK //////////
  59. input CLOCK_50;
  60.  
  61. //////////// LED //////////
  62. output [7:0] LED;
  63.  
  64. /////////// KEY //////////
  65. input [1:0] KEY;
  66.  
  67. //////////// SW //////////
  68. input [3:0] SW;
  69.  
  70. //////////// GPIO_0, GPIO_0 connect to GPIO Default //////////
  71. inout [33:0] GPIO_0_D;
  72. input [1:0] GPIO_0_IN;
  73.  
  74. //////////// GPIO_0, GPIO_1 connect to GPIO Default //////////
  75. inout [33:0] GPIO_1_D;
  76. input [1:0] GPIO_1_IN;
  77.  
  78. //=======================================================
  79. // REG/WIRE declarations
  80. //=======================================================
  81. reg CLOCK_25;
  82. wire reset; // active high reset signal
  83.  
  84. wire [9:0] PIXEL_COORD_X; // current x-coord from VGA driver
  85. wire [9:0] PIXEL_COORD_Y; // current y-coord from VGA driver
  86. reg [7:0] PIXEL_COLOR; // input 8-bit pixel color for current coords
  87.  
  88. reg [24:0] led_counter; // timer to keep track of when to toggle LED
  89. reg led_state; // 1 is on, 0 is off
  90.  
  91. // Module outputs coordinates of next pixel to be written onto screen
  92. VGA_DRIVER driver(
  93. .RESET(reset),
  94. .CLOCK(CLOCK_25),
  95. .PIXEL_COLOR_IN(PIXEL_COLOR),
  96. .PIXEL_X(PIXEL_COORD_X),
  97. .PIXEL_Y(PIXEL_COORD_Y),
  98. .PIXEL_COLOR_OUT({GPIO_0_D[9],GPIO_0_D[11],GPIO_0_D[13],GPIO_0_D[15],GPIO_0_D[17],GPIO_0_D[19],GPIO_0_D[21],GPIO_0_D[23]}),
  99. .H_SYNC_NEG(GPIO_0_D[7]),
  100. .V_SYNC_NEG(GPIO_0_D[5])
  101. );
  102.  
  103. assign reset = ~KEY[0]; // reset when KEY0 is pressed
  104.  
  105. //assign PIXEL_COLOR = 8'b000_111_00; // Green
  106. //assign LED[0] = led_state;
  107.  
  108. /*
  109. Main_Module dope (
  110. .PIXEL_X(PIXEL_COORD_X),
  111. .PIXEL_Y(PIXEL_COORD_Y),
  112. .PIXEL_COLOR_OUT(PIXEL_COLOR)
  113. );
  114. */
  115.  
  116.  
  117. // LAB 3 CODE
  118.  
  119. //2-by-2 array of bits
  120. reg [7:0] grid_array [1:0][1:0]; //[rows][columns]
  121. reg [4:0] grid_coord_x; //Index x into the array COLUMN
  122. reg [4:0] grid_coord_y; //Index y into the array ROW
  123.  
  124. wire [7:0] arduino_in;
  125. assign arduino_in = {GPIO_1_D[9],GPIO_1_D[11],GPIO_1_D[13],GPIO_1_D[15],GPIO_1_D[17],GPIO_1_D[19],GPIO_1_D[21],GPIO_1_D[23]};
  126. wire [2:0] arduino_data;
  127.  
  128. reg [7:0] vga_ram_write;
  129. reg [4:0] vga_ram_raddr;
  130. wire [4:0] vga_ram_waddr;
  131. wire vga_ram_we;
  132. wire [7:0] vga_ram_rsp;
  133.  
  134. reg [6:0] grid_rel_x;
  135. reg [6:0] grid_rel_y;
  136.  
  137.  
  138. assign vga_ram_waddr = arduino_in[4:0];
  139. assign vga_ram_we = arduino_in[5];
  140. assign arduino_data = arduino_in[7:6];
  141.  
  142. always @(*) begin
  143. case (arduino_data)
  144. 2'd0: vga_ram_write = `GREEN;
  145. 2'd1: vga_ram_write = `RED;
  146. 2'd2: vga_ram_write = `BLUE;
  147. 2'd3: vga_ram_write = `ORANGE;
  148. default: vga_ram_write = `BLACK;
  149. endcase
  150.  
  151. if (PIXEL_COORD_X < `SQUARE_EDGE_0) grid_coord_x = 5'd0;
  152. else if (PIXEL_COORD_X < `SQUARE_EDGE_1) grid_coord_x = 5'd1;
  153. else if (PIXEL_COORD_X < `SQUARE_EDGE_2) grid_coord_x = 5'd2;
  154. else if (PIXEL_COORD_X < `SQUARE_EDGE_3) grid_coord_x = 5'd3;
  155. else if (PIXEL_COORD_X < `SQUARE_EDGE_4) grid_coord_x = 5'd4;
  156. else grid_coord_x = 5'd5;
  157.  
  158. if (PIXEL_COORD_Y < `SQUARE_EDGE_0) grid_coord_y = 5'd0;
  159. else if (PIXEL_COORD_Y < `SQUARE_EDGE_1) grid_coord_y = 5'd1;
  160. else if (PIXEL_COORD_Y < `SQUARE_EDGE_2) grid_coord_y = 5'd2;
  161. else if (PIXEL_COORD_Y < `SQUARE_EDGE_3) grid_coord_y = 5'd3;
  162. else grid_coord_y = 5'd5;
  163.  
  164.  
  165. case (grid_coord_x)
  166. 5'd0: grid_rel_x = PIXEL_COORD_X;
  167. 5'd1: grid_rel_x = PIXEL_COORD_X - `SQUARE_EDGE_0;
  168. 5'd2: grid_rel_x = PIXEL_COORD_X - `SQUARE_EDGE_1;
  169. 5'd3: grid_rel_x = PIXEL_COORD_X - `SQUARE_EDGE_2;
  170. 5'd4: grid_rel_x = PIXEL_COORD_X - `SQUARE_EDGE_3;
  171. default: grid_rel_x = 0;
  172.  
  173. endcase
  174.  
  175.  
  176. case (grid_coord_y)
  177. 5'd0: grid_rel_y = PIXEL_COORD_Y;
  178. 5'd1: grid_rel_y = PIXEL_COORD_Y - `SQUARE_EDGE_0;
  179. 5'd2: grid_rel_y = PIXEL_COORD_Y - `SQUARE_EDGE_1;
  180. 5'd3: grid_rel_y = PIXEL_COORD_Y - `SQUARE_EDGE_2;
  181. default: grid_rel_y = 0;
  182.  
  183. endcase
  184.  
  185.  
  186. if (grid_coord_x == 5'd5) vga_ram_raddr = 5'd31;
  187. else if (grid_coord_y == 5'd0) vga_ram_raddr = grid_coord_x;
  188. else if (grid_coord_y == 5'd1) vga_ram_raddr = grid_coord_x + 5;
  189. else if (grid_coord_y == 5'd2) vga_ram_raddr = grid_coord_x + 10;
  190. else if (grid_coord_y == 5'd3) vga_ram_raddr = grid_coord_x + 15;
  191. else vga_ram_raddr = 5'd31;
  192. end
  193.  
  194. always @(posedge CLOCK_25) begin
  195. if (vga_ram_rsp == `GREEN) PIXEL_COLOR <= kirstin_out;
  196. else if (vga_ram_rsp == `RED) PIXEL_COLOR <= donald_out;
  197. else if (vga_ram_rsp == `BLUE) PIXEL_COLOR <= sonic_out;
  198. else PIXEL_COLOR <= vga_ram_rsp;
  199. end
  200.  
  201. VGA_RAM vga_ram (
  202. .data(vga_ram_write),
  203. .read_addr(vga_ram_raddr),
  204. .write_addr(vga_ram_waddr),
  205. .we(vga_ram_we),
  206. .clk(CLOCK_25),
  207. .q(vga_ram_rsp)
  208. );
  209.  
  210. wire [7:0] donald_out;
  211.  
  212. DONALD_DUCK donald_rom (
  213. .addr({grid_rel_y,grid_rel_x}),
  214. .clk(CLOCK_25),
  215. .q(donald_out)
  216. );
  217.  
  218. wire [7:0] kirstin_out;
  219.  
  220. KIRSTIN kirstin (
  221. .addr({grid_rel_y,grid_rel_x}),
  222. .clk(CLOCK_25),
  223. .q(kirstin_out)
  224. );
  225.  
  226.  
  227. wire [7:0] sonic_out;
  228.  
  229. SONIC sonic (
  230. .addr({grid_rel_y,grid_rel_x}),
  231. .clk(CLOCK_25),
  232. .q(sonic_out)
  233. );
  234.  
  235.  
  236. //=======================================================
  237. // Structural coding
  238. //=======================================================
  239.  
  240. // Generate 25MHz clock for VGA, FPGA has 50 MHz clock
  241. always @ (posedge CLOCK_50) begin
  242. CLOCK_25 <= ~CLOCK_25;
  243. end // always @ (posedge CLOCK_50)
  244.  
  245. /*
  246. // Simple state machine to toggle LED0 every one second
  247. always @ (posedge CLOCK_25) begin
  248. if (reset) begin
  249. led_state <= 1'b0;
  250. led_counter <= 25'b0;
  251. end
  252.  
  253. if (led_counter == ONE_SEC) begin
  254. led_state <= ~led_state;
  255. led_counter <= 25'b0;
  256. end
  257. else begin
  258. led_state <= led_state;
  259. led_counter <= led_counter + 25'b1;
  260. end // always @ (posedge CLOCK_25)
  261. end
  262. */
  263.  
  264. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement