用vga驱动显示器显示一个移动的放块
当方块触碰到边界的时候就会弹开
依然是三个模块:时钟分频,vga驱动,vga显示
除vga显示内容有所区别,其余都和上一节vga彩条显示相同
module vga_blockmove(
input sys_clk, //系统时钟
input sys_rst_n, //复位信号
//VGA接口
output vga_hs, //行同步信号
output vga_vs, //场同步信号
output [15:0] vga_rgb //红绿蓝三原色输出
);
//wire define
wire vga_clk_w; //PLL分频得到25Mhz时钟
wire locked_w; //PLL输出稳定信号
wire rst_n_w; //内部复位信号
wire [15:0] pixel_data_w; //像素点数据
wire [ 9:0] pixel_xpos_w; //像素点横坐标
wire [ 9:0] pixel_ypos_w; //像素点纵坐标
//待PLL输出稳定之后,停止复位
assign rst_n_w = sys_rst_n && locked_w;
vga_pll u_vga_pll( //时钟分频模块
.inclk0 (sys_clk),
.areset (~sys_rst_n),
.c0 (vga_clk_w), //VGA时钟 25M
.locked (locked_w)
);
vga_driver u_vga_driver(
.vga_clk (vga_clk_w),
.sys_rst_n (rst_n_w),
.vga_hs (vga_hs),
.vga_vs (vga_vs),
.vga_rgb (vga_rgb),
.pixel_data (pixel_data_w),
.pixel_xpos (pixel_xpos_w),
.pixel_ypos (pixel_ypos_w)
);
vga_display u_vga_display(
.vga_clk (vga_clk_w),
.sys_rst_n (rst_n_w),
.pixel_xpos (pixel_xpos_w),
.pixel_ypos (pixel_ypos_w),
.pixel_data (pixel_data_w)
);
endmodule
//25MHZ
// synopsys translate_on
module vga_pll (
areset,
inclk0,
c0,
locked);
input areset;
input inclk0;
output c0;
output locked;
module vga_driver(
input vga_clk, //VGA驱动时钟
input sys_rst_n, //复位信号
//VGA接口
output vga_hs, //行同步信号
output vga_vs, //场同步信号
output [15:0] vga_rgb, //红绿蓝三原色输出
input [15:0] pixel_data, //像素点数据
output [ 9:0] pixel_xpos, //像素点横坐标
output [ 9:0] pixel_ypos //像素点纵坐标
);
//parameter define
parameter H_SYNC = 10'd96; //行同步
parameter H_BACK = 10'd48; //行显示后沿
parameter H_DISP = 10'd640; //行有效数据
parameter H_FRONT = 10'd16; //行显示前沿
parameter H_TOTAL = 10'd800; //行扫描周期
parameter V_SYNC = 10'd2; //场同步
parameter V_BACK = 10'd33; //场显示后沿
parameter V_DISP = 10'd480; //场有效数据
parameter V_FRONT = 10'd10; //场显示前沿
parameter V_TOTAL = 10'd525; //场扫描周期
//reg define
reg [9:0] cnt_h;
reg [9:0] cnt_v;
//wire define
wire vga_en;
wire data_req;
//VGA行场同步信号
assign vga_hs = (cnt_h <= H_SYNC - 1'b1) ? 1'b0 : 1'b1;
assign vga_vs = (cnt_v <= V_SYNC - 1'b1) ? 1'b0 : 1'b1;
//使能RGB565数据输出
assign vga_en = (((cnt_h >= H_SYNC+H_BACK) && (cnt_h < H_SYNC+H_BACK+H_DISP))
&&((cnt_v >= V_SYNC+V_BACK) && (cnt_v < V_SYNC+V_BACK+V_DISP)))
? 1'b1 : 1'b0;
//RGB565数据输出
assign vga_rgb = vga_en ? pixel_data : 16'd0;
//请求像素点颜色数据输入
assign data_req = (((cnt_h >= H_SYNC+H_BACK-1'b1) && (cnt_h < H_SYNC+H_BACK+H_DISP-1'b1))
&& ((cnt_v >= V_SYNC+V_BACK) && (cnt_v < V_SYNC+V_BACK+V_DISP)))
? 1'b1 : 1'b0;
//像素点坐标
assign pixel_xpos = data_req ? (cnt_h - (H_SYNC + H_BACK - 1'b1)) : 10'd0;
assign pixel_ypos = data_req ? (cnt_v - (V_SYNC + V_BACK - 1'b1)) : 10'd0;
//行计数器对像素时钟计数
always @(posedge vga_clk or negedge sys_rst_n) begin
if (!sys_rst_n)
cnt_h <= 10'd0;
else begin
if(cnt_h < H_TOTAL - 1'b1)
cnt_h <= cnt_h + 1'b1;
else
cnt_h <= 10'd0;
end
end
//场计数器对行计数
always @(posedge vga_clk or negedge sys_rst_n) begin
if (!sys_rst_n)
cnt_v <= 10'd0;
else if(cnt_h == H_TOTAL - 1'b1) begin
if(cnt_v < V_TOTAL - 1'b1)
cnt_v <= cnt_v + 1'b1;
else
cnt_v <= 10'd0;
end
end
endmodule
module vga_display(
input vga_clk, //VGA驱动时钟
input sys_rst_n, //复位信号
input [ 9:0] pixel_xpos, //像素点横坐标
input [ 9:0] pixel_ypos, //像素点纵坐标
output reg [15:0] pixel_data //像素点数据
);
//parameter define
parameter H_DISP = 10'd640; //分辨率——行
parameter V_DISP = 10'd480; //分辨率——列
localparam SIDE_W = 10'd40; //边框宽度
localparam BLOCK_W = 10'd40; //方块宽度
localparam BLUE = 16'b00000_000000_11111; //边框颜色 蓝色
localparam WHITE = 16'b11111_111111_11111; //背景颜色 白色
localparam BLACK = 16'b00000_000000_00000; //方块颜色 黑色
//reg define
reg [ 9:0] block_x; //方块左上角横坐标
reg [ 9:0] block_y; //方块左上角纵坐标
reg [21:0] div_cnt; //时钟分频计数器
reg h_direct; //方块水平移动方向,1:右移,0:左移
reg v_direct; //方块竖直移动方向,1:向下,0:向上
//wire define
wire move_en; //方块移动使能信号,频率为100hz
assign move_en = (div_cnt == 22'd250000 - 1'b1) ? 1'b1 : 1'b0;
//通过对vga驱动时钟计数,实现时钟分频
always @(posedge vga_clk or negedge sys_rst_n) begin
if (!sys_rst_n)
div_cnt <= 22'd0;
else begin
if(div_cnt < 22'd250000 - 1'b1)
div_cnt <= div_cnt + 1'b1;
else
div_cnt <= 22'd0; //计数达10ms后清零
end
end
//当方块移动到边界时,改变移动方向
always @(posedge vga_clk or negedge sys_rst_n) begin
if (!sys_rst_n) begin
h_direct <= 1'b1; //方块初始水平向右移动
v_direct <= 1'b1; //方块初始竖直向下移动
end
else begin
if(block_x == SIDE_W - 1'b1) //到达左边界时,水平向右
h_direct <= 1'b1;
else //到达右边界时,水平向左
if(block_x == H_DISP - SIDE_W - BLOCK_W)
h_direct <= 1'b0;
else
h_direct <= h_direct;
if(block_y == SIDE_W - 1'b1) //到达上边界时,竖直向下
v_direct <= 1'b1;
else //到达下边界时,竖直向上
if(block_y == V_DISP - SIDE_W - BLOCK_W)
v_direct <= 1'b0;
else
v_direct <= v_direct;
end
end
//根据方块移动方向,改变其纵横坐标
always @(posedge vga_clk or negedge sys_rst_n) begin
if (!sys_rst_n) begin
block_x <= 22'd100; //方块初始位置横坐标
block_y <= 22'd100; //方块初始位置纵坐标
end
else if(move_en) begin
if(h_direct)
block_x <= block_x + 1'b1; //方块向右移动
else
block_x <= block_x - 1'b1; //方块向左移动
if(v_direct)
block_y <= block_y + 1'b1; //方块向下移动
else
block_y <= block_y - 1'b1; //方块向上移动
end
else begin
block_x <= block_x;
block_y <= block_y;
end
end
//给不同的区域绘制不同的颜色
always @(posedge vga_clk or negedge sys_rst_n) begin
if (!sys_rst_n)
pixel_data <= BLACK;
else begin
if((pixel_xpos < SIDE_W) || (pixel_xpos >= H_DISP - SIDE_W)
|| (pixel_ypos < SIDE_W) || (pixel_ypos >= V_DISP - SIDE_W))
pixel_data <= BLUE; //绘制边框为蓝色
else
if((pixel_xpos >= block_x) && (pixel_xpos < block_x + BLOCK_W)
&& (pixel_ypos >= block_y) && (pixel_ypos < block_y + BLOCK_W))
pixel_data <= BLACK; //绘制方块为黑色
else
pixel_data <= WHITE; //绘制背景为白色
end
end
endmodule