// EP11 test picture: 8 colour bars + moving marker box + frame border
module video_pattern (
    input  wire        pclk,
    input  wire        rst,
    input  wire [11:0] x,
    input  wire [11:0] y,
    input  wire        de,
    output reg  [23:0] rgb
);
    // 8 bars: white, yellow, cyan, green, magenta, red, blue, black
    function [23:0] bar_col;
        input [2:0] idx;
        case (idx)
            3'd0: bar_col = 24'hFF_FF_FF;
            3'd1: bar_col = 24'hFF_FF_00;
            3'd2: bar_col = 24'h00_FF_FF;
            3'd3: bar_col = 24'h00_FF_00;
            3'd4: bar_col = 24'hFF_00_FF;
            3'd5: bar_col = 24'hFF_00_00;
            3'd6: bar_col = 24'h00_00_FF;
            default: bar_col = 24'h00_00_00;
        endcase
    endfunction

    reg [11:0] box_x;
    reg [11:0] box_y;
    reg [1:0]  dir_x;
    reg [1:0]  dir_y;

    always @(posedge pclk) begin
        if (rst) begin
            box_x <= 12'd200;
            box_y <= 12'd160;
            dir_x <= 2'd0;
            dir_y <= 2'd0;
        end else if (de && x == 12'd0 && y == 12'd0) begin
            // advance the marker once per frame
            if (dir_x == 2'd0) begin
                if (box_x >= 12'd1200) dir_x <= 2'd1; else box_x <= box_x + 12'd8;
            end else begin
                if (box_x <= 12'd40)   dir_x <= 2'd0; else box_x <= box_x - 12'd8;
            end
            if (dir_y == 2'd0) begin
                if (box_y >= 12'd640)  dir_y <= 2'd1; else box_y <= box_y + 12'd4;
            end else begin
                if (box_y <= 12'd24)   dir_y <= 2'd0; else box_y <= box_y - 12'd4;
            end
        end
    end

    wire in_box = de && (x >= box_x) && (x < box_x + 12'd80) &&
                       (y >= box_y) && (y < box_y + 12'd80);
    wire on_border = de && ((x < 12'd4) || (x >= 12'd1276) ||
                            (y < 12'd4) || (y >= 12'd716));

    always @(posedge pclk) begin
        if (rst)
            rgb <= 24'h00_00_00;
        else if (!de)
            rgb <= 24'h00_00_00;
        else if (in_box)
            rgb <= 24'h20_20_20;                    // dark grey marker
        else if (on_border)
            rgb <= 24'hC0_C0_C0;
        else
            rgb <= bar_col(x[9:7]);                 // 128 px per bar
    end
endmodule
