`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2015/12/22 21:55:24
// Design Name:
// Module Name: vga
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module vga(
input wire SYS_XRST ,
input wire SYS_CLK ,
output wire [3:0] VGA_R ,
output wire [3:0] VGA_G ,
output wire [3:0] VGA_B ,
output wire HS ,
output wire VS
);
parameter P_H_WHOLE = 12'd1055 ;
parameter P_V_WHOLE = 12'd627 ;
reg [3:0] r_VGA_R ;
reg [3:0] r_VGA_G ;
reg [3:0] r_VGA_B ;
reg r_HS ;
reg r_VS ;
reg [11:0] r_HS_COUNT ;
reg [11:0] r_VS_COUNT ;
reg r_HS_OFF ;
reg r_VS_OFF ;
reg [3:0] r_R_OFF ;
reg [3:0] r_G_OFF ;
reg [3:0] r_B_OFF ;
reg [11:0] r_VGA_COLOR ;
reg r_HS_EN ;
reg r_VS_EN ;
wire SYS_CLK40M ;
//=========================================
// VGA Display
//=========================================
// Horizontal count
always @(posedge SYS_CLK40M or negedge SYS_XRST) begin
if(SYS_XRST == 1'b0) begin
r_HS_COUNT <= {12{1'b0}};
end else begin
if(r_HS_COUNT == P_H_WHOLE) begin
r_HS_COUNT <= {12{1'b0}};
end else begin
r_HS_COUNT <= r_HS_COUNT+1'b1;
end
end
end
//Horizontal sync
always @(posedge SYS_CLK40M or negedge SYS_XRST) begin
if(SYS_XRST == 1'b0) begin
r_HS <= 1'b1;
end else begin
if(r_HS_COUNT == 12'd39) begin
r_HS <= 1'b0;
end else begin
if(r_HS_COUNT == 12'd167)begin
r_HS <= 1'b1;
end
end
end
end
always @(posedge SYS_CLK40M or negedge SYS_XRST) begin
if(SYS_XRST == 1'b0) begin
r_HS_EN <= 1'b0;
end else begin
if(r_HS_COUNT == 12'd255) begin
r_HS_EN <= 1'b1;
end else begin
if(r_HS_COUNT == 12'd1055) begin
r_HS_EN <= 1'b0;
end
end
end
end
// Vertical count
always @(posedge SYS_CLK40M or negedge SYS_XRST) begin
if(SYS_XRST == 1'b0) begin
r_VS_COUNT <= {12{1'b0}};
end else begin
if(r_HS_COUNT == P_H_WHOLE) begin
if(r_VS_COUNT == P_V_WHOLE) begin
r_VS_COUNT <= {12{1'b0}};
end else begin
r_VS_COUNT <= r_VS_COUNT+1'b1;
end
end
end
end
//Vertical sync
always @(posedge SYS_CLK40M or negedge SYS_XRST) begin
if(SYS_XRST == 1'b0) begin
r_VS <= 1'b1;
end else begin
if(r_HS_COUNT == P_H_WHOLE) begin
if(r_VS_COUNT == 12'd0) begin
r_VS <= 1'b0;
end else
if(r_VS_COUNT == 12'd4) begin
r_VS <= 1'b1;
end
end
end
end
always @(posedge SYS_CLK40M or negedge SYS_XRST) begin
if(SYS_XRST == 1'b0) begin
r_VS_EN <= 1'b0;
end else begin
if(r_HS_COUNT == P_H_WHOLE) begin
if(r_VS_COUNT == 12'd27) begin
r_VS_EN <= 1'b1;
end else
if(r_VS_COUNT == 12'd627) begin
r_VS_EN <= 1'b0;
end
end
end
end
/*========================================================================+/
|| ||
|| // Color Signals ||
|| ||
/+========================================================================*/
always @(posedge SYS_CLK40M or negedge SYS_XRST) begin
if(SYS_XRST == 1'b0) begin
{r_VGA_R,r_VGA_G,r_VGA_B} <= 12'd0;
end else begin
if(r_VS_EN == 1'b1 && r_HS_EN == 1'b1) begin
if(r_HS_COUNT == 12'd256) begin
r_VGA_R <= {4{1'b1}};
r_VGA_G <= {4{1'b0}};
r_VGA_B <= {4{1'b0}};
end else if(r_HS_COUNT == 12'd456) begin
r_VGA_R <= {4{1'b0}};
r_VGA_G <= {4{1'b1}};
r_VGA_B <= {4{1'b0}};
end else if(r_HS_COUNT == 12'd657) begin
r_VGA_R <= {4{1'b0}};
r_VGA_G <= {4{1'b0}};
r_VGA_B <= {4{1'b1}};
end else if(r_HS_COUNT == 12'd858) begin
r_VGA_R <= {4{1'b1}};
r_VGA_G <= {4{1'b1}};
r_VGA_B <= {4{1'b1}};
end
end
end
end
//add register to increase stability
always @(posedge SYS_CLK40M or negedge SYS_XRST) begin
if(SYS_XRST == 1'b0) begin
r_HS_OFF <= 1'd1;
r_VS_OFF <= 1'd1;
r_R_OFF <= 4'd0;
r_G_OFF <= 4'd0;
r_B_OFF <= 4'd0;
end else begin
r_HS_OFF <= r_HS;
r_VS_OFF <= r_VS;
r_R_OFF <= r_VGA_R;
r_G_OFF <= r_VGA_G;
r_B_OFF <= r_VGA_B;
end
end
clk_wiz_0 PLL
(
// Clock in ports
.clk_in1(SYS_CLK), // input clk_in1
// Clock out ports
.clk_out1( SYS_CLK40M )); // output clk_out1
// Output Ports
assign HS = r_HS_OFF ;
assign VS = r_VS_OFF ;
assign VGA_R = r_R_OFF ;
assign VGA_G = r_G_OFF ;
assign VGA_B = r_B_OFF ;
endmodule