// ============================================================================
// Module Name: pixel_generator_2x2
// Description: Synthesizable 2x2 Pixel GPU Sequential Rasterizer
// Series:      GPU Master - Experiment 02
// Platform:    EcrioniX Open Hardware (https://ecrionix.org/gpu-master/2x2-pixel-gpu/)
//
// Generates a 2x2 pixel frame sequentially across 4 clock cycles:
//   Pixel 0: (X=0, Y=0) Top-Left
//   Pixel 1: (X=1, Y=0) Top-Right
//   Pixel 2: (X=0, Y=1) Bottom-Left
//   Pixel 3: (X=1, Y=1) Bottom-Right
// ============================================================================

`timescale 1ns / 1ps

module pixel_generator_2x2 (
    input  wire        clk,
    input  wire        rst,
    input  wire        start,

    // Pixel 0 (X=0, Y=0): Top-Left RGB
    input  wire [7:0]  p0_r,
    input  wire [7:0]  p0_g,
    input  wire [7:0]  p0_b,

    // Pixel 1 (X=1, Y=0): Top-Right RGB
    input  wire [7:0]  p1_r,
    input  wire [7:0]  p1_g,
    input  wire [7:0]  p1_b,

    // Pixel 2 (X=0, Y=1): Bottom-Left RGB
    input  wire [7:0]  p2_r,
    input  wire [7:0]  p2_g,
    input  wire [7:0]  p2_b,

    // Pixel 3 (X=1, Y=1): Bottom-Right RGB
    input  wire [7:0]  p3_r,
    input  wire [7:0]  p3_g,
    input  wire [7:0]  p3_b,

    // Output Interface
    output reg  [1:0]  pixel_x,
    output reg  [1:0]  pixel_y,
    output reg  [7:0]  red_out,
    output reg  [7:0]  green_out,
    output reg  [7:0]  blue_out,
    output reg         pixel_valid,
    output reg         busy,
    output reg         done
);

    // State Encoding
    localparam STATE_IDLE = 2'b00;
    localparam STATE_GEN  = 2'b01;
    localparam STATE_DONE = 2'b10;

    reg [1:0] state;
    reg [1:0] pixel_cnt;

    // Sequential Rasterization Logic
    always @(posedge clk) begin
        if (rst) begin
            state       <= STATE_IDLE;
            pixel_cnt   <= 2'b00;
            pixel_x     <= 2'b00;
            pixel_y     <= 2'b00;
            red_out     <= 8'h00;
            green_out   <= 8'h00;
            blue_out    <= 8'h00;
            pixel_valid <= 1'b0;
            busy        <= 1'b0;
            done        <= 1'b0;
        end else begin
            case (state)
                STATE_IDLE: begin
                    done <= 1'b0;
                    if (start) begin
                        state       <= STATE_GEN;
                        busy        <= 1'b1;
                        pixel_cnt   <= 2'b00;
                        pixel_valid <= 1'b1;
                        // Output Pixel 0: (0, 0)
                        pixel_x     <= 2'd0;
                        pixel_y     <= 2'd0;
                        red_out     <= p0_r;
                        green_out   <= p0_g;
                        blue_out    <= p0_b;
                    end else begin
                        pixel_valid <= 1'b0;
                        busy        <= 1'b0;
                    end
                end

                STATE_GEN: begin
                    case (pixel_cnt)
                        2'd0: begin
                            // Advance to Pixel 1: (1, 0)
                            pixel_cnt   <= 2'd1;
                            pixel_x     <= 2'd1;
                            pixel_y     <= 2'd0;
                            red_out     <= p1_r;
                            green_out   <= p1_g;
                            blue_out    <= p1_b;
                            pixel_valid <= 1'b1;
                        end

                        2'd1: begin
                            // Advance to Pixel 2: (0, 1)
                            pixel_cnt   <= 2'd2;
                            pixel_x     <= 2'd0;
                            pixel_y     <= 2'd1;
                            red_out     <= p2_r;
                            green_out   <= p2_g;
                            blue_out    <= p2_b;
                            pixel_valid <= 1'b1;
                        end

                        2'd2: begin
                            // Advance to Pixel 3: (1, 1)
                            pixel_cnt   <= 2'd3;
                            pixel_x     <= 2'd1;
                            pixel_y     <= 2'd1;
                            red_out     <= p3_r;
                            green_out   <= p3_g;
                            blue_out    <= p3_b;
                            pixel_valid <= 1'b1;
                        end

                        2'd3: begin
                            // All 4 pixels completed!
                            state       <= STATE_DONE;
                            pixel_valid <= 1'b0;
                            busy        <= 1'b0;
                            done        <= 1'b1;
                        end
                    endcase
                end

                STATE_DONE: begin
                    done  <= 1'b0;
                    state <= STATE_IDLE;
                end

                default: begin
                    state <= STATE_IDLE;
                end
            endcase
        end
    end

endmodule
