HDMI实现方块移动

发布时间 2023-12-04 19:47:34作者: carpe--diem
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2023/12/04 18:50:05
// Design Name: 
// Module Name: block_move
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module block_move (
    input                   pixel_clk,
    input                   sys_rst_n,
    input           [10:0]  pixel_xpos,
    input           [10:0]  pixel_ypos,

    output  reg     [23:0]  pixel_data

);


//parameter define
parameter H_DISP = 11'd1280;
parameter V_DISP = 11'd720;

localparam SIDE_W = 11'd40; //屏幕边框宽度
localparam BLOCK_W = 11'd40;//方块宽度
localparam BLUE = 24'b00000000_00000000_11111111;
localparam WHITE = 24'b11111111_11111111_11111111;
localparam BLACK = 24'b00000000_00000000_00000000;

//reg define
reg [10:0] block_x;//方块左上角横坐标
reg [10:0] block_y;//方块左上角纵坐标
reg [21:0] div_cnt;
reg h_direct; //方块水平移动方向, 1:右移, 0:左移
reg v_direct; //方块竖直移动方向, 1:向下, 0:向上
wire move_en;

// 10ms计数器
always @(posedge pixel_clk or negedge sys_rst_n) begin
    if(!sys_rst_n)
        div_cnt <= 22'd0;
    else if(div_cnt == 22'd734214)
        div_cnt <= 22'd0;
    else    
        div_cnt <= div_cnt + 1'd1;
end

//当方块移动到边界时,改变移动方向
always @(posedge pixel_clk or negedge sys_rst_n) begin
    if(!sys_rst_n) begin
        h_direct <= 1'b1;
    end
    else if(block_x == SIDE_W)
        h_direct <= 1'b1;
    else if(block_x ==H_DISP - SIDE_W - BLOCK_W)
        h_direct <= 1'b0;
    else
        h_direct <= h_direct;
end

always @(posedge pixel_clk or negedge sys_rst_n) begin
    if(!sys_rst_n) begin
        v_direct <= 1'b1;
    end
    else if(block_x == SIDE_W)
        v_direct <= 1'b1;
    else if(block_x ==H_DISP - SIDE_W - BLOCK_W)
        v_direct <= 1'b0;
    else
        v_direct <= v_direct;
end

//根据方块移动方向,改变其纵横坐标

 always @(posedge pixel_clk or negedge sys_rst_n) begin
    if (!sys_rst_n) begin
    block_x <= SIDE_W; //方块初始位置横坐标
    block_y <= SIDE_W; //方块初始位置纵坐标
    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 pixel_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