module VGA_top//--------------------------------顶层
(
input refclk,
input rst_n,
output vs,
output hs,
output [4:0]r,
output [5:0]g,
output [4:0]b
);
pll
pll_inst
(
.areset ( ~rst_n ),
.inclk0 ( refclk ),
.c0 ( clk ),
.locked ( rst )
);
wire [9:0]cont_v;
vga
vga_inst
(
.clk(clk),
.rst(rst),
.en(en),
.vga_vs(vs),
.vga_hs(hs),
.cont_v(cont_v)
);
rgb
rgb_inst
(
.clk(clk),
.rst(rst),
.en(en),
.cont_v(cont_v),
//.uart_s(out_uart),
.r(r),
.g(g),
.b(b)
);
endmodule
//------------------------------------时序
module vga
(
input clk,
input rst,
output en,
output vga_vs,
output vga_hs,
output [9:0]cont_v
);
reg [10:0]cont_hs;
always@(posedge clk or negedge rst)
begin
if(~rst)
cont_hs<=11'd0;
else if(cont_hs==11'd1055)
cont_hs<=11'd0;
else
cont_hs<=cont_hs+1'b1;
end
reg [9:0]cont_vs;
always@(posedge clk or negedge rst)
begin
if(~rst)
cont_vs<=10'd0;
else if(cont_hs==11'd1055)
begin
if(cont_vs==10'd627)
cont_vs<=10'd0;
else
cont_vs<=cont_vs+1'b1;
end
end
assign cont_v=cont_vs;
reg vga_vs1;//行同步
always@(posedge clk or negedge rst)
begin
if(~rst)
vga_vs1<=1'b0;
else if(cont_vs==10'd0)
vga_vs1<=1'b1;
else if(cont_vs==10'd3)
vga_vs1<=1'b0;
end
assign vga_vs=vga_vs1;
reg vga_hs1;//列同步
always@(posedge clk or negedge rst)
begin
if(~rst)
vga_hs1<=1'b0;
else if(cont_hs==11'd0)
vga_hs1=1'b1;
else if(cont_hs==11'd128)
vga_hs1<=1'b0;
end
assign vga_hs=vga_hs1;
//--------------------------------
reg en_vs;
always@(posedge clk or negedge rst)
begin
if(~rst)
en_vs<=1'b0;
else if(cont_vs==10'd26)
en_vs<=1'b1;
else if(cont_vs==10'd626)
en_vs<=1'b0;
end
reg en_hs;
always@(posedge clk or negedge rst)
begin
if(~rst)
en_hs<=1'b0;
else if(cont_hs==11'd215)
en_hs<=1'b1;
else if(cont_hs==11'd1015)
en_hs<=1'b0;
end
reg en1;//有效信号
always@(posedge clk or negedge rst)
begin
if(~rst)
en1<=1'b0;
else if((en_vs==1'b1)&&(en_hs==1'b1))
en1<=1'b1;
else
en1<=1'b0;
end
assign en=en1;
endmodule
//-----------------------------------显示
module VGA_top
(
input refclk,
input rst_n,
output vs,
output hs,
output [4:0]r,
output [5:0]g,
output [4:0]b
);
pll
pll_inst
(
.areset ( ~rst_n ),
.inclk0 ( refclk ),
.c0 ( clk ),
.locked ( rst )
);
wire [9:0]cont_v;
vga
vga_inst
(
.clk(clk),
.rst(rst),
.en(en),
.vga_vs(vs),
.vga_hs(hs),
.cont_v(cont_v)
);
rgb
rgb_inst
(
.clk(clk),
.rst(rst),
.en(en),
.cont_v(cont_v),
.r(r),
.g(g),
.b(b)
);
endmodule
//-----------------------------测试
`timescale 1ns/1ns
module VGA_top_tb();
reg clk;
reg rst_n;
initial
begin
rst_n=0;
#800 rst_n=1;
#100000000 $stop;
end
initial
begin
clk=0;
end
always #20 clk<=~clk;
wire [4:0]r,b;
wire [5:0]g;
VGA_top
VGA_top_inst
(
.refclk(clk),
.rst_n(rst_n),
.vs(vs),
.hs(hs),
.r(r),
.g(g),
.b(b)
);
endmodule