// FPGA VGA Graphics Part 1: Top Module (static squares)
// (C)2017-2018 Will Green - Licensed under the MIT License
// Learn more at https://timetoexplore.net/blog/arty-fpga-vga-verilog-01

module vga01(
	// inputs
	input wire clk,             	// board clock: 50 MHz on Cyclone II board
   input wire rst_btn,         	// reset button
	// outputs
   output wire vga_hsync,      	// horizontal sync output
   output wire vga_vsync,      	// vertical sync output
   output wire [1:0] vga_r,    	// 2-bit VGA R output
   output wire [1:0] vga_g,    	// 2-bit VGA G output
   output wire [1:0] vga_b    	// 2-bit VGA B output
   );

	// invert reset button logic as it's active low
   wire rst = ~rst_btn;

   // generate a 25 MHz pixel strobe
   reg [15:0] cnt;
   reg pix_en;
   always @(posedge clk)
		{pix_en, cnt} <= cnt + 16'h8000;  // divide 50 MHz by 2 for 25 MHz: (2^16)/2 = 0x8000

   wire [9:0] x;  // current pixel x position: 10-bit value: 0-1023
   wire [9:0] y;  // current pixel y position: 10-bit value: 0-1023
	wire blanking;	// high when in vertical blanking

	// create a 640x480 synch and pixel 
   sync_generator #(640,480) display(
      .clk(clk),
      .pix_enable(pix_en),
      .reset(rst),
      .hsync(vga_hsync), 
      .vsync(vga_vsync), 
		.x(x), 
		.y(y),
		.blanking(blanking)
   );

   // Display four overlapping squares for something to look at, maybe?
	// Alternatively, could always create vertical colour bars based on x-position
   wire sq_a, sq_b, sq_c, sq_d;
   assign sq_a = ((x > 120) & (y >  40) & (x < 280) & (y < 200)) ? 1'b1 : 1'b0;
   assign sq_b = ((x > 200) & (y > 120) & (x < 360) & (y < 280)) ? 1'b1 : 1'b0;
   assign sq_c = ((x > 280) & (y > 200) & (x < 440) & (y < 360)) ? 1'b1 : 1'b0;
   assign sq_d = ((x > 360) & (y > 280) & (x < 520) & (y < 440)) ? 1'b1 : 1'b0;

	// Assign zero values to the R, G and B LSBs to prevent 'not driven' warnings
	assign vga_r[0] = 1'b0;
   assign vga_g[0] = 1'b0;
   assign vga_b[0] = 1'b0;
	
	// Assign color values to R, G and B according to pixel position and square positions
   assign vga_r[1] = sq_b;         // square b is red
   assign vga_g[1] = sq_a | sq_d;  // squares a and d are green
   assign vga_b[1] = sq_c;         // square c is blue
	
endmodule
