// ============================================================================
// Testbench: pixel_generator_2x2_tb
// Description: Self-Checking Hardware Verification for 2x2 Pixel GPU
// Series:      GPU Master - Experiment 02
// Platform:    EcrioniX Open Hardware (https://ecrionix.org/gpu-master/2x2-pixel-gpu/)
//
// Verifies sequential rasterization of a 2x2 pixel grid (P0, P1, P2, P3)
// with cycle-accurate timing and handshaking.
// ============================================================================

`timescale 1ns / 1ps

module pixel_generator_2x2_tb;

    reg        clk;
    reg        rst;
    reg        start;

    reg [7:0]  p0_r, p0_g, p0_b;
    reg [7:0]  p1_r, p1_g, p1_b;
    reg [7:0]  p2_r, p2_g, p2_b;
    reg [7:0]  p3_r, p3_g, p3_b;

    wire [1:0] pixel_x;
    wire [1:0] pixel_y;
    wire [7:0] red_out;
    wire [7:0] green_out;
    wire [7:0] blue_out;
    wire       pixel_valid;
    wire       busy;
    wire       done;

    integer errors = 0;
    integer tests_passed = 0;

    // Instantiate Unit Under Test (UUT)
    pixel_generator_2x2 uut (
        .clk        (clk),
        .rst        (rst),
        .start      (start),
        .p0_r       (p0_r), .p0_g(p0_g), .p0_b(p0_b),
        .p1_r       (p1_r), .p1_g(p1_g), .p1_b(p1_b),
        .p2_r       (p2_r), .p2_g(p2_g), .p2_b(p2_b),
        .p3_r       (p3_r), .p3_g(p3_g), .p3_b(p3_b),
        .pixel_x    (pixel_x),
        .pixel_y    (pixel_y),
        .red_out    (red_out),
        .green_out  (green_out),
        .blue_out   (blue_out),
        .pixel_valid(pixel_valid),
        .busy       (busy),
        .done       (done)
    );

    // 100MHz clock (10ns period)
    always #5 clk = ~clk;

    // Automated Verification Task
    task verify_frame;
        input [7:0] in_p0_r, in_p0_g, in_p0_b;
        input [7:0] in_p1_r, in_p1_g, in_p1_b;
        input [7:0] in_p2_r, in_p2_g, in_p2_b;
        input [7:0] in_p3_r, in_p3_g, in_p3_b;
        input [160:0] test_name;
        begin
            p0_r = in_p0_r; p0_g = in_p0_g; p0_b = in_p0_b;
            p1_r = in_p1_r; p1_g = in_p1_g; p1_b = in_p1_b;
            p2_r = in_p2_r; p2_g = in_p2_g; p2_b = in_p2_b;
            p3_r = in_p3_r; p3_g = in_p3_g; p3_b = in_p3_b;

            // Trigger frame generation on inactive clock edge
            @(negedge clk);
            start = 1;
            @(negedge clk);
            start = 0;

            // Sample Pixel 0: (0, 0)
            if (pixel_valid === 1'b1 && busy === 1'b1 && pixel_x === 2'd0 && pixel_y === 2'd0 &&
                red_out === in_p0_r && green_out === in_p0_g && blue_out === in_p0_b) begin
                $display("[PASS] %s - Pixel 0 (X=0, Y=0) [R=%0d G=%0d B=%0d]", test_name, in_p0_r, in_p0_g, in_p0_b);
                tests_passed = tests_passed + 1;
            end else begin
                $display("[FAIL] %s - Pixel 0 (0,0) Mismatch! Got X=%0d Y=%0d RGB=(%0d,%0d,%0d)", test_name, pixel_x, pixel_y, red_out, green_out, blue_out);
                errors = errors + 1;
            end

            // Sample Pixel 1: (1, 0)
            @(negedge clk);
            if (pixel_valid === 1'b1 && busy === 1'b1 && pixel_x === 2'd1 && pixel_y === 2'd0 &&
                red_out === in_p1_r && green_out === in_p1_g && blue_out === in_p1_b) begin
                $display("[PASS] %s - Pixel 1 (X=1, Y=0) [R=%0d G=%0d B=%0d]", test_name, in_p1_r, in_p1_g, in_p1_b);
                tests_passed = tests_passed + 1;
            end else begin
                $display("[FAIL] %s - Pixel 1 (1,0) Mismatch! Got X=%0d Y=%0d RGB=(%0d,%0d,%0d)", test_name, pixel_x, pixel_y, red_out, green_out, blue_out);
                errors = errors + 1;
            end

            // Sample Pixel 2: (0, 1)
            @(negedge clk);
            if (pixel_valid === 1'b1 && busy === 1'b1 && pixel_x === 2'd0 && pixel_y === 2'd1 &&
                red_out === in_p2_r && green_out === in_p2_g && blue_out === in_p2_b) begin
                $display("[PASS] %s - Pixel 2 (X=0, Y=1) [R=%0d G=%0d B=%0d]", test_name, in_p2_r, in_p2_g, in_p2_b);
                tests_passed = tests_passed + 1;
            end else begin
                $display("[FAIL] %s - Pixel 2 (0,1) Mismatch! Got X=%0d Y=%0d RGB=(%0d,%0d,%0d)", test_name, pixel_x, pixel_y, red_out, green_out, blue_out);
                errors = errors + 1;
            end

            // Sample Pixel 3: (1, 1)
            @(negedge clk);
            if (pixel_valid === 1'b1 && busy === 1'b1 && pixel_x === 2'd1 && pixel_y === 2'd1 &&
                red_out === in_p3_r && green_out === in_p3_g && blue_out === in_p3_b) begin
                $display("[PASS] %s - Pixel 3 (X=1, Y=1) [R=%0d G=%0d B=%0d]", test_name, in_p3_r, in_p3_g, in_p3_b);
                tests_passed = tests_passed + 1;
            end else begin
                $display("[FAIL] %s - Pixel 3 (1,1) Mismatch! Got X=%0d Y=%0d RGB=(%0d,%0d,%0d)", test_name, pixel_x, pixel_y, red_out, green_out, blue_out);
                errors = errors + 1;
            end

            // Sample Frame Complete
            @(negedge clk);
            if (done === 1'b1 && busy === 1'b0 && pixel_valid === 1'b0) begin
                $display("[PASS] %s - Frame Done asserted (done=1, busy=0, valid=0)", test_name);
                tests_passed = tests_passed + 1;
            end else begin
                $display("[FAIL] %s - Frame done flag failed. Got done=%b busy=%b", test_name, done, busy);
                errors = errors + 1;
            end

            // Sample Return to IDLE
            @(negedge clk);
            if (done === 1'b0 && busy === 1'b0) begin
                $display("[PASS] %s - Return to IDLE cleanly", test_name);
                tests_passed = tests_passed + 1;
            end else begin
                $display("[FAIL] %s - Failed return to IDLE", test_name);
                errors = errors + 1;
            end
        end
    endtask

    initial begin
        $dumpfile("pixel_2x2.vcd");
        $dumpvars(0, pixel_generator_2x2_tb);

        clk   = 0;
        rst   = 1;
        start = 0;
        p0_r = 0; p0_g = 0; p0_b = 0;
        p1_r = 0; p1_g = 0; p1_b = 0;
        p2_r = 0; p2_g = 0; p2_b = 0;
        p3_r = 0; p3_g = 0; p3_b = 0;

        $display("==========================================================");
        $display("   STARTING TESTBENCH: 2x2 PIXEL GPU SEQUENTIAL RASTERIZER");
        $display("==========================================================");

        // ----------------------------------------------------
        // TEST 1: Reset Check
        // ----------------------------------------------------
        #20;
        @(negedge clk);
        if (pixel_valid === 1'b0 && busy === 1'b0 && done === 1'b0) begin
            $display("[PASS] TEST 1: Synchronous reset initializes all control lines to 0.");
            tests_passed = tests_passed + 1;
        end else begin
            $display("[FAIL] TEST 1: Expected idle state on reset.");
            errors = errors + 1;
        end

        // Release reset
        rst = 0;
        #10;

        // ----------------------------------------------------
        // TEST 2: Four-Colour Demo (Red, Green, Blue, Yellow)
        // ----------------------------------------------------
        verify_frame(
            8'd255, 8'd0,   8'd0,     // P0: Red
            8'd0,   8'd255, 8'd0,     // P1: Green
            8'd0,   8'd0,   8'd255,   // P2: Blue
            8'd255, 8'd255, 8'd0,     // P3: Yellow
            "TEST 2: Four-Colour Demo"
        );
        #20;

        // ----------------------------------------------------
        // TEST 3: Checkerboard Pattern (Black, White, White, Black)
        // ----------------------------------------------------
        verify_frame(
            8'd0,   8'd0,   8'd0,     // P0: Black
            8'd255, 8'd255, 8'd255,   // P1: White
            8'd255, 8'd255, 8'd255,   // P2: White
            8'd0,   8'd0,   8'd0,     // P3: Black
            "TEST 3: Checkerboard Pattern"
        );
        #20;

        // ----------------------------------------------------
        // TEST 4: All-White Frame
        // ----------------------------------------------------
        verify_frame(
            8'd255, 8'd255, 8'd255,
            8'd255, 8'd255, 8'd255,
            8'd255, 8'd255, 8'd255,
            8'd255, 8'd255, 8'd255,
            "TEST 4: All-White Frame"
        );
        #20;

        // ----------------------------------------------------
        // TEST 5: Synchronous Reset Override During Generation
        // ----------------------------------------------------
        @(negedge clk);
        start = 1;
        @(negedge clk);
        start = 0;
        @(negedge clk); // Pixel 1
        @(negedge clk); // Pixel 2
        rst = 1;        // Assert reset mid-frame
        @(negedge clk);
        if (uut.state == 2'b00 && busy === 1'b0 && pixel_valid === 1'b0) begin
            $display("[PASS] TEST 5: Reset override during frame generation cleanly aborts to IDLE.");
            tests_passed = tests_passed + 1;
        end else begin
            $display("[FAIL] TEST 5: Reset override failed to return to IDLE.");
            errors = errors + 1;
        end

        rst = 0;
        #20;

        $display("==========================================================");
        $display("   TEST SUMMARY: %0d PASSED, %0d FAILED", tests_passed, errors);
        $display("==========================================================");

        if (errors == 0) $display("ALL TESTS PASSED SUCCESSFULLY! 2x2 GPU RTL IS 100%% VERIFIED.");
        else $display("FAILURES DETECTED! CHECK RTL LOGIC.");

        $finish;
    end

endmodule
