EXPERIMENT 02 • 4-PIXEL HARDWARE RASTERIZER

Build a 2×2 Pixel GPU — Sequential Rasterization

In Experiment 01, we explored how digital hardware drives a single pixel. Now we scale from 1 to 4 pixels. Discover how a graphics processor addresses a 2D spatial grid, serializes color data on clock edges, generates coordinates with a 2-bit counter, and forms a complete digital image frame.

← Previous: Experiment 01 (1-Pixel GPU) GPU MASTER SERIES • EXPERIMENT 2 OF 12 Next: Experiment 03 (4×4 Pixel GPU) →
🖥️ Interactive 2×2 Pixel Canvas
Click any pixel to select and modify its RGB values
4 PIXELS TOTAL
P0 (0,0)
#FF0000
P1 (1,0)
#00FF00
P2 (0,1)
#0000FF
P3 (1,1)
#FFFF00
🎨 Built-in Demonstrations
🔍 Selected Pixel Inspector
Real-time hardware register values & 24-bit binary vector
P0 (Pixel 0) (X=0, Y=0)
Red [7:0]
255
Green [7:0]
0
Blue [7:0]
0
Hex Color Code
#FF0000
24-Bit Hardware Bus Vector {Red, Green, Blue}
11111111 _ 00000000 _ 00000000
🔴 Red Channel 255
🟢 Green Channel 0
🔵 Blue Channel 0
Quick Presets for Selected Pixel:

📐 2×2 GPU Hardware Architecture Block Diagram

Inspect how four internal pixel registers feed into a multiplexer controlled by a 2-bit counter, delivering a serialized digital pixel stream. Click any block to inspect its RTL function.

RGB INPUT REGISTERS P0: {R0, G0, B0} P1: {R1, G1, B1} P2: {R2, G2, B2} P3: {R3, G3, B3} 4x 24b 4:1 MUX RGB [23:0] 2-BIT PIXEL COUNTER pixel_cnt[1:0] (0..3) OUTPUT REGISTERS pixel_x [1:0] (X) pixel_y [1:0] (Y) RGB [23:0] (Color) pixel_valid (Flag) busy / done (Handshake) Streaming Bus 2×2 FRAME (0,0) (1,0) (0,1) (1,1)
💡 Tip: Click any block in the architecture diagram above to inspect how pixel data flows through the 2×2 GPU pipeline!
⚡ Live RTL-Equivalent Simulation Engine
Observe sequential pixel emission, coordinate generation, and hardware flags clock-by-clock
Clock Speed:
FSM State
IDLE
Clock Cycle
0
Active Pixel
—
Coordinates (X,Y)
—
Output RGB
—
pixel_valid
0 (LOW)
busy
0 (LOW)
done
0 (LOW)
Raster Order:
1. P0 (0,0) Top-Left
→
2. P1 (1,0) Top-Right
→
3. P2 (0,1) Bottom-Left
→
4. P3 (1,1) Bottom-Right
→
Frame Done ✅
Signal Port Direction Width Current Hardware Value Description / Role
state [1:0] Internal Reg 2 bits 2'b00 (IDLE) Sequential FSM: IDLE(0), GENERATE(1), DONE(2)
pixel_cnt [1:0] Internal Reg 2 bits 2'd0 Pixel counter driving column (bit 0) and row (bit 1)
pixel_valid Output 1 bit 1'b0 Asserted high for each valid pixel payload on the bus
busy Output 1 bit 1'b0 Indicates the rasterizer is actively processing a 4-pixel frame
done Output 1 bit 1'b0 Pulsed high for 1 clock cycle upon emitting the final pixel
pixel_x, pixel_y Output 2x 2 bits X=0, Y=0 Raster coordinates of current pixel
red_out, green_out, blue_out Output 3x 8 bits (0, 0, 0) Registered 24-bit RGB pixel color components
📈 Multi-Channel Digital Waveforms
Real-time signal transitions: clock edges, valid strobe, coordinates, and pixel RGB vectors
Zoom: 100%
Hover waveform to inspect signal states & active pixel emission
Move cursor over waveform to inspect cycle-accurate signal states

* Notice how pixel_valid stays asserted for exactly 4 consecutive clock cycles, emitting (0,0), (1,0), (0,1), and (1,1).

🖼️ The Concept of a Frame

In computer graphics, a frame is one complete rasterized image displayed on screen. In our 2×2 GPU, all 4 pixels together form the final 2×2 digital image. Download your frame as a real PNG image, enlarged cleanly using nearest-neighbour interpolation to preserve crisp pixel boundaries.

📚 Engineering Deep Dive: How GPUs Rasterize 2D Pixel Grids

In modern 3D graphics (Vulkan, DirectX, Metal) and digital displays (HDMI, DisplayPort), screens are modeled as a two-dimensional Cartesian plane where the origin (0,0) represents the top-left corner.

1. Why (0,0) is at the Top-Left

In standard high-school mathematics, the origin (0,0) is at the bottom-left. Why do computer displays place it at the top-left? This convention originates from Cathode Ray Tube (CRT) monitors: electron guns scanned from left to right, line by line, from top to bottom. Digital video controllers maintain this exact scan order for complete interoperability.

2. The Linear Index to 2D Coordinate Transformation Formula

A frame buffer in GPU VRAM is stored in linear memory addresses (1D array), but is displayed on a 2D screen:

Mathematical Transformation for any Width \(W\):
\(\text{Linear Index} = Y \times W + X\)
\(X = \text{Index} \pmod W\)
\(Y = \lfloor \text{Index} / W \rfloor\)

For our 2×2 GPU where \(W=2\), the division and modulo reduce to trivial bit slicing! Because 2 is a power of 2 (\(2^1\)), Bit 0 of our 2-bit counter is directly \(X\), and Bit 1 is directly \(Y\)!

3. Sequential Pipeline vs Parallel Storage

While all 4 pixels exist in storage simultaneously, display interfaces like HDMI or MIPI DSI serialize data into a clock-synchronized stream. Our pixel_valid strobe tells the downstream display timing generator that the current RGB values on the bus are authentic and ready to be sampled.

🎯 Student Hardware Challenges

Solve these interactive engineering challenges by configuring the pixel canvas and testing your hardware comprehension:

Challenge 01 In Progress
Four-Colour Image
Configure the 2×2 canvas so that every single pixel has a completely distinct, unique color.
Make sure all 4 pixels have distinct colors.
Challenge 02 In Progress
Checkerboard Pattern
Create a true checkerboard pattern where P0 matches P3, P1 matches P2, and the two pairs contrast.
Set P0=P3 and P1=P2 with contrasting colors.
Challenge 03 In Progress
All-White Frame
Saturate all color channels to maximum (RGB = 255, 255, 255) across all four pixels.
Set all 4 pixels to #FFFFFF.
Challenge 04 In Progress
Diagonal Red Pattern
Light up the primary diagonal: make P0 and P3 pure Red (#FF0000) while P1 and P2 are other colors.
P0 and P3 must be #FF0000.
Challenge 05 Quiz
Bottom-Right Coordinate Check
What are the (X,Y) coordinates of the bottom-right pixel in our 0-indexed 2×2 grid?
Click an answer above to verify.
Challenge 06 Quiz
Why 4 Valid Clock Events?
Why does generating 4 pixels require 4 separate valid clock events instead of 1?
Select your hardware hypothesis above.
rtl/pixel_generator_2x2.v (Synthesizable Verilog)
Download .v
// ============================================================================
// 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/)
// ============================================================================

`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
tb/pixel_generator_2x2_tb.v (Self-Checking Testbench)
Download .v
// ============================================================================
// 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/)
// ============================================================================

`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;

    // 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

        rst = 0;
        #10;

        // TEST 2: Four-Colour Demo
        verify_frame(8'd255, 8'd0, 8'd0, 8'd0, 8'd255, 8'd0, 8'd0, 8'd0, 8'd255, 8'd255, 8'd255, 8'd0, "TEST 2: Four-Colour Demo");
        #20;

        // TEST 3: Checkerboard Pattern
        verify_frame(8'd0, 8'd0, 8'd0, 8'd255, 8'd255, 8'd255, 8'd255, 8'd255, 8'd255, 8'd0, 8'd0, 8'd0, "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: Reset Override
        @(negedge clk);
        start = 1;
        @(negedge clk);
        start = 0;
        @(negedge clk);
        @(negedge clk);
        rst = 1;
        @(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("==========================================================");

        $finish;
    end

endmodule
💻 Cloud Icarus Verilog Simulation Console:
Click "⚡ Run in Cloud Icarus Verilog" above to compile and execute this testbench on a real HDL simulator.
←
Previous Experiment
Experiment 01: 1-Pixel GPU
GPU Master Curriculum
From 1 Pixel to Full 2D Graphics Processor
Next Experiment
Experiment 03: 4×4 Pixel GPU →
→