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.
📐 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.
| 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 |
* 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:
\(\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\)!
pixel_cnt = 2'b00→ \(X = 0, Y = 0\) → Pixel 0 (Top-Left)pixel_cnt = 2'b01→ \(X = 1, Y = 0\) → Pixel 1 (Top-Right)pixel_cnt = 2'b10→ \(X = 0, Y = 1\) → Pixel 2 (Bottom-Left)pixel_cnt = 2'b11→ \(X = 1, Y = 1\) → Pixel 3 (Bottom-Right)
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:
// ============================================================================
// 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
// ============================================================================
// 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
Click "⚡ Run in Cloud Icarus Verilog" above to compile and execute this testbench on a real HDL simulator.