Build a 4×4 Pixel GPU — Coordinate Bit Slicing & Truecolor Multiplexing
Step up from a 4-pixel toy to a genuine 16-pixel 2D matrix. Explore the architectural magic of
power-of-2 coordinate bit slicing where X = index[1:0] and Y = index[3:2]
require zero arithmetic logic gates! Simulate 16 clock cycles of rasterization, inspect digital timing waveforms,
and compile real synthesizable Verilog RTL directly in your browser.
📐 4×4 GPU Hardware Architecture Block Diagram
Inspect how sixteen 24-bit pixel registers feed into a 16-to-1 Truecolor MUX controlled by a 4-bit raster counter. Notice how coordinates (X, Y) are extracted via bit slicing without dividers or multipliers! Click any block to inspect its RTL behavior.
| Signal Port | Direction | Width | Current Hardware Value | Description / Role |
|---|---|---|---|---|
state [1:0] |
Internal Reg | 2 bits | 2'b00 (IDLE) | FSM State: 00=IDLE, 01=GENERATE (16 cycles), 10=DONE |
pixel_cnt [3:0] |
Internal Reg | 4 bits | 0 (4'b0000) | Raster index driving 16:1 MUX and coordinate slicing |
pixel_valid |
Output | 1 bit | 0 (LOW) | Asserted high for all 16 consecutive pixel data cycles |
busy |
Output | 1 bit | 0 (LOW) | High while the rasterizer is actively processing a 16-pixel frame |
done |
Output | 1 bit | 0 (IDLE) | Pulsed high for 1 clock cycle upon emitting the 16th pixel (P15) |
pixel_x, pixel_y |
Output | 2x 2 bits | X=0, Y=0 | Extracted coordinates: X = cnt[1:0], Y = cnt[3:2] |
red_out, green_out, blue_out |
Output | 3x 8 bits | (0, 0, 0) | Registered 24-bit Truecolor RGB pixel output bus |
* Notice how pixel_valid stays asserted for exactly 16 consecutive clock cycles, emitting pixels P0 through P15 in scanline order.
🖼️ 4×4 Frame Scaling & PNG Export
In digital graphics, all 16 pixels combined form one complete raster image. Download your rendered frame as a clean 256×256 PNG file, enlarged with nearest-neighbor interpolation to ensure crystal-clear pixel borders without algorithmic blur.
📚 Engineering Deep Dive: The Power-of-2 Hardware Breakthrough
Moving from a 2×2 grid to a 4×4 grid illustrates one of the most celebrated design principles in digital VLSI design: the elimination of arithmetic dividers and multipliers through binary bit slicing.
1. General Grid Addressing vs Power-of-2 Grids
For any arbitrary screen width \(W\), translating a linear frame counter into 2D Cartesian screen coordinates requires division and modulo operations:
\(\text{Linear Index} = Y \times W + X\)
\(X = \text{Index} \pmod W\)
\(Y = \lfloor \text{Index} / W \rfloor\)
In digital hardware, integer dividers are expensive: they consume hundreds of logic gates, introduce long propagation delay paths, and often require multiple clock cycles (multi-cycle divider IP).
2. Power-of-2 Bit Slicing: Zero-Gate Arithmetic
When the width is a power of two (\(W = 2^k\)), division by \(W\) is equivalent to a right-shift by \(k\) bits, and modulo \(W\) is equivalent to masking the lowest \(k\) bits!
For our 4×4 GPU (\(W = 4 = 2^2\), \(k = 2\)):
- X Coordinate (Column): \(\text{Index} \pmod 4 \equiv \text{Index}[1:0]\) (Lower 2 bits)
- Y Coordinate (Row): \(\lfloor \text{Index} / 4 \rfloor \equiv \text{Index}[3:2]\) (Upper 2 bits)
In Verilog RTL, this requires zero logic gates: it is purely wiring!
assign pixel_x = pixel_cnt[1:0]; // 2 wires, 0 gates, 0 delay!
assign pixel_y = pixel_cnt[3:2]; // 2 wires, 0 gates, 0 delay!
3. Frame Rate & Clock Frequency Calculations
If the pixel generator runs at clock frequency \(F_{\text{clk}}\), emitting 1 pixel per clock cycle, the theoretical frame rate is:
At a modest VLSI clock frequency of 10 MHz (10,000,000 Hz) and 16 cycles per frame: \[\text{Frame Rate} = \frac{10,000,000}{16} = 625,000 \text{ frames per second}\]
🎯 Student Hardware Challenges
Solve these interactive engineering challenges by configuring the 4×4 canvas and testing your hardware comprehension:
// ============================================================================
// Module Name: pixel_generator_4x4
// Description: Synthesizable 4x4 Pixel GPU Sequential Rasterizer (16 Pixels)
// Series: GPU Master - Experiment 03
// Platform: EcrioniX Open Hardware (https://ecrionix.org/gpu-master/4x4-pixel-gpu/)
//
// Generates a 4x4 pixel frame sequentially across 16 clock cycles:
// Row 0: P0(0,0), P1(1,0), P2(2,0), P3(3,0)
// Row 1: P4(0,1), P5(1,1), P6(2,1), P7(3,1)
// Row 2: P8(0,2), P9(1,2), P10(2,2), P11(3,2)
// Row 3: P12(0,3), P13(1,3), P14(2,3), P15(3,3)
//
// 2D Coordinate Mapping:
// pixel_x = pixel_cnt[1:0] (Index % 4)
// pixel_y = pixel_cnt[3:2] (Index / 4)
// ============================================================================
`timescale 1ns / 1ps
module pixel_generator_4x4 (
input wire clk,
input wire rst,
input wire start,
// 16 Truecolor RGB Pixel Inputs (Row 0: P0..P3)
input wire [7:0] p0_r, p0_g, p0_b,
input wire [7:0] p1_r, p1_g, p1_b,
input wire [7:0] p2_r, p2_g, p2_b,
input wire [7:0] p3_r, p3_g, p3_b,
// Row 1: P4..P7
input wire [7:0] p4_r, p4_g, p4_b,
input wire [7:0] p5_r, p5_g, p5_b,
input wire [7:0] p6_r, p6_g, p6_b,
input wire [7:0] p7_r, p7_g, p7_b,
// Row 2: P8..P11
input wire [7:0] p8_r, p8_g, p8_b,
input wire [7:0] p9_r, p9_g, p9_b,
input wire [7:0] p10_r, p10_g, p10_b,
input wire [7:0] p11_r, p11_g, p11_b,
// Row 3: P12..P15
input wire [7:0] p12_r, p12_g, p12_b,
input wire [7:0] p13_r, p13_g, p13_b,
input wire [7:0] p14_r, p14_g, p14_b,
input wire [7:0] p15_r, p15_g, p15_b,
// Output Raster Stream Interface
output reg [1:0] pixel_x,
output reg [1:0] pixel_y,
output reg [3:0] pixel_index,
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 [3:0] pixel_cnt;
// Sequential Rasterization State Machine
always @(posedge clk) begin
if (rst) begin
state <= STATE_IDLE;
pixel_cnt <= 4'd0;
pixel_x <= 2'd0;
pixel_y <= 2'd0;
pixel_index <= 4'd0;
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 <= 4'd0;
pixel_valid <= 1'b1;
// Emit Pixel 0: (0, 0)
pixel_x <= 2'd0;
pixel_y <= 2'd0;
pixel_index <= 4'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
if (pixel_cnt < 4'd15) begin
// Advance to next pixel in scan order
pixel_cnt <= pixel_cnt + 4'd1;
pixel_index <= pixel_cnt + 4'd1;
// Spatial coordinate decomposition: X = bit[1:0], Y = bit[3:2]
pixel_x <= (pixel_cnt + 4'd1) & 2'b11;
pixel_y <= ((pixel_cnt + 4'd1) >> 2) & 2'b11;
// Latch multiplexed RGB for the next pixel index
case (pixel_cnt + 4'd1)
4'd1: begin red_out <= p1_r; green_out <= p1_g; blue_out <= p1_b; end
4'd2: begin red_out <= p2_r; green_out <= p2_g; blue_out <= p2_b; end
4'd3: begin red_out <= p3_r; green_out <= p3_g; blue_out <= p3_b; end
4'd4: begin red_out <= p4_r; green_out <= p4_g; blue_out <= p4_b; end
4'd5: begin red_out <= p5_r; green_out <= p5_g; blue_out <= p5_b; end
4'd6: begin red_out <= p6_r; green_out <= p6_g; blue_out <= p6_b; end
4'd7: begin red_out <= p7_r; green_out <= p7_g; blue_out <= p7_b; end
4'd8: begin red_out <= p8_r; green_out <= p8_g; blue_out <= p8_b; end
4'd9: begin red_out <= p9_r; green_out <= p9_g; blue_out <= p9_b; end
4'd10: begin red_out <= p10_r; green_out <= p10_g; blue_out <= p10_b; end
4'd11: begin red_out <= p11_r; green_out <= p11_g; blue_out <= p11_b; end
4'd12: begin red_out <= p12_r; green_out <= p12_g; blue_out <= p12_b; end
4'd13: begin red_out <= p13_r; green_out <= p13_g; blue_out <= p13_b; end
4'd14: begin red_out <= p14_r; green_out <= p14_g; blue_out <= p14_b; end
4'd15: begin red_out <= p15_r; green_out <= p15_g; blue_out <= p15_b; end
endcase
pixel_valid <= 1'b1;
end else begin
// Frame scan complete: all 16 pixels emitted!
state <= STATE_DONE;
pixel_valid <= 1'b0;
busy <= 1'b0;
done <= 1'b1;
end
end
STATE_DONE: begin
done <= 1'b0;
state <= STATE_IDLE;
end
default: begin
state <= STATE_IDLE;
end
endcase
end
end
endmodule
// ============================================================================
// Testbench: pixel_generator_4x4_tb
// Description: Comprehensive Self-Checking Verification for 4x4 Pixel GPU (16 Pixels)
// Series: GPU Master - Experiment 03
// Platform: EcrioniX Open Hardware (https://ecrionix.org/gpu-master/4x4-pixel-gpu/)
//
// Verifies sequential rasterization of a 4x4 pixel grid (P0 through P15)
// with cycle-accurate timing, spatial coordinates (X, Y), and handshaking.
// ============================================================================
`timescale 1ns / 1ps
module pixel_generator_4x4_tb;
reg clk;
reg rst;
reg start;
// 16 Truecolor RGB Inputs
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;
reg [7:0] p4_r, p4_g, p4_b;
reg [7:0] p5_r, p5_g, p5_b;
reg [7:0] p6_r, p6_g, p6_b;
reg [7:0] p7_r, p7_g, p7_b;
reg [7:0] p8_r, p8_g, p8_b;
reg [7:0] p9_r, p9_g, p9_b;
reg [7:0] p10_r, p10_g, p10_b;
reg [7:0] p11_r, p11_g, p11_b;
reg [7:0] p12_r, p12_g, p12_b;
reg [7:0] p13_r, p13_g, p13_b;
reg [7:0] p14_r, p14_g, p14_b;
reg [7:0] p15_r, p15_g, p15_b;
// Outputs
wire [1:0] pixel_x;
wire [1:0] pixel_y;
wire [3:0] pixel_index;
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;
integer idx;
// Instantiate Unit Under Test (UUT)
pixel_generator_4x4 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),
.p4_r(p4_r), .p4_g(p4_g), .p4_b(p4_b),
.p5_r(p5_r), .p5_g(p5_g), .p5_b(p5_b),
.p6_r(p6_r), .p6_g(p6_g), .p6_b(p6_b),
.p7_r(p7_r), .p7_g(p7_g), .p7_b(p7_b),
.p8_r(p8_r), .p8_g(p8_g), .p8_b(p8_b),
.p9_r(p9_r), .p9_g(p9_g), .p9_b(p9_b),
.p10_r(p10_r), .p10_g(p10_g), .p10_b(p10_b),
.p11_r(p11_r), .p11_g(p11_g), .p11_b(p11_b),
.p12_r(p12_r), .p12_g(p12_g), .p12_b(p12_b),
.p13_r(p13_r), .p13_g(p13_g), .p13_b(p13_b),
.p14_r(p14_r), .p14_g(p14_g), .p14_b(p14_b),
.p15_r(p15_r), .p15_g(p15_g), .p15_b(p15_b),
.pixel_x (pixel_x),
.pixel_y (pixel_y),
.pixel_index(pixel_index),
.red_out (red_out),
.green_out (green_out),
.blue_out (blue_out),
.pixel_valid(pixel_valid),
.busy (busy),
.done (done)
);
// 100MHz clock (10ns period: 5ns HIGH, 5ns LOW)
always #5 clk = ~clk;
// Arrays for test validation
reg [7:0] exp_r [0:15];
reg [7:0] exp_g [0:15];
reg [7:0] exp_b [0:15];
// Verification Task
task verify_4x4_frame;
input [160:0] test_name;
integer k;
integer exp_x, exp_y;
begin
@(negedge clk);
start = 1;
@(negedge clk);
start = 0;
for (k = 0; k < 16; k = k + 1) begin
exp_x = k % 4;
exp_y = k / 4;
if (pixel_valid === 1'b1 && busy === 1'b1 &&
pixel_x === exp_x[1:0] && pixel_y === exp_y[1:0] && pixel_index === k[3:0] &&
red_out === exp_r[k] && green_out === exp_g[k] && blue_out === exp_b[k]) begin
$display("[PASS] %s - Pixel %0d (X=%0d, Y=%0d) RGB=(%0d,%0d,%0d)", test_name, k, exp_x, exp_y, red_out, green_out, blue_out);
tests_passed = tests_passed + 1;
end else begin
$display("[FAIL] %s - Pixel %0d Mismatch! Expected (X=%0d,Y=%0d) RGB=(%0d,%0d,%0d), Got (X=%0d,Y=%0d) RGB=(%0d,%0d,%0d) valid=%b busy=%b",
test_name, k, exp_x, exp_y, exp_r[k], exp_g[k], exp_b[k], pixel_x, pixel_y, red_out, green_out, blue_out, pixel_valid, busy);
errors = errors + 1;
end
if (k < 15) @(negedge clk);
end
// Check Cycle 17: Frame Done
@(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, valid=%b", test_name, done, busy, pixel_valid);
errors = errors + 1;
end
// Check Cycle 18: Clean Return to IDLE
@(negedge clk);
if (done === 1'b0 && busy === 1'b0 && pixel_valid === 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! Got done=%b, busy=%b", test_name, done, busy);
errors = errors + 1;
end
end
endtask
initial begin
$dumpfile("pixel_4x4.vcd");
$dumpvars(0, pixel_generator_4x4_tb);
clk = 0;
rst = 1;
start = 0;
$display("==========================================================");
$display(" STARTING TESTBENCH: 4x4 PIXEL GPU SEQUENTIAL RASTERIZER");
$display("==========================================================");
// TEST 1: Reset Check
#20;
@(negedge clk);
if (pixel_valid === 1'b0 && busy === 1'b0 && done === 1'b0 && pixel_x === 2'd0 && pixel_y === 2'd0 && pixel_index === 4'd0) 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: Unique 16-Pixel Palette Test (Scan Order Verification)
p0_r = 8'd10; p0_g = 8'd0; p0_b = 8'd0; exp_r[0] = p0_r; exp_g[0] = p0_g; exp_b[0] = p0_b;
p1_r = 8'd20; p1_g = 8'd1; p1_b = 8'd0; exp_r[1] = p1_r; exp_g[1] = p1_g; exp_b[1] = p1_b;
p2_r = 8'd30; p2_g = 8'd2; p2_b = 8'd0; exp_r[2] = p2_r; exp_g[2] = p2_g; exp_b[2] = p2_b;
p3_r = 8'd40; p3_g = 8'd3; p3_b = 8'd0; exp_r[3] = p3_r; exp_g[3] = p3_g; exp_b[3] = p3_b;
p4_r = 8'd50; p4_g = 8'd4; p4_b = 8'd0; exp_r[4] = p4_r; exp_g[4] = p4_g; exp_b[4] = p4_b;
p5_r = 8'd60; p5_g = 8'd5; p5_b = 8'd0; exp_r[5] = p5_r; exp_g[5] = p5_g; exp_b[5] = p5_b;
p6_r = 8'd70; p6_g = 8'd6; p6_b = 8'd0; exp_r[6] = p6_r; exp_g[6] = p6_g; exp_b[6] = p6_b;
p7_r = 8'd80; p7_g = 8'd7; p7_b = 8'd0; exp_r[7] = p7_r; exp_g[7] = p7_g; exp_b[7] = p7_b;
p8_r = 8'd90; p8_g = 8'd8; p8_b = 8'd0; exp_r[8] = p8_r; exp_g[8] = p8_g; exp_b[8] = p8_b;
p9_r = 8'd100; p9_g = 8'd9; p9_b = 8'd0; exp_r[9] = p9_r; exp_g[9] = p9_g; exp_b[9] = p9_b;
p10_r = 8'd110; p10_g = 8'd10; p10_b = 8'd0; exp_r[10] = p10_r; exp_g[10] = p10_g; exp_b[10] = p10_b;
p11_r = 8'd120; p11_g = 8'd11; p11_b = 8'd0; exp_r[11] = p11_r; exp_g[11] = p11_g; exp_b[11] = p11_b;
p12_r = 8'd130; p12_g = 8'd12; p12_b = 8'd0; exp_r[12] = p12_r; exp_g[12] = p12_g; exp_b[12] = p12_b;
p13_r = 8'd140; p13_g = 8'd13; p13_b = 8'd0; exp_r[13] = p13_r; exp_g[13] = p13_g; exp_b[13] = p13_b;
p14_r = 8'd150; p14_g = 8'd14; p14_b = 8'd0; exp_r[14] = p14_r; exp_g[14] = p14_g; exp_b[14] = p14_b;
p15_r = 8'd160; p15_g = 8'd15; p15_b = 8'd0; exp_r[15] = p15_r; exp_g[15] = p15_g; exp_b[15] = p15_b;
verify_4x4_frame("TEST 2: Unique 16-Pixel Palette");
#20;
// TEST 3: Checkerboard Pattern
for (idx = 0; idx < 16; idx = idx + 1) begin
if (((idx % 4) + (idx / 4)) % 2 == 0) begin
exp_r[idx] = 8'd255; exp_g[idx] = 8'd255; exp_b[idx] = 8'd255;
end else begin
exp_r[idx] = 8'd0; exp_g[idx] = 8'd0; exp_b[idx] = 8'd0;
end
end
p0_r = exp_r[0]; p0_g = exp_g[0]; p0_b = exp_b[0];
p1_r = exp_r[1]; p1_g = exp_g[1]; p1_b = exp_b[1];
p2_r = exp_r[2]; p2_g = exp_g[2]; p2_b = exp_b[2];
p3_r = exp_r[3]; p3_g = exp_g[3]; p3_b = exp_b[3];
p4_r = exp_r[4]; p4_g = exp_g[4]; p4_b = exp_b[4];
p5_r = exp_r[5]; p5_g = exp_g[5]; p5_b = exp_b[5];
p6_r = exp_r[6]; p6_g = exp_g[6]; p6_b = exp_b[6];
p7_r = exp_r[7]; p7_g = exp_g[7]; p7_b = exp_b[7];
p8_r = exp_r[8]; p8_g = exp_g[8]; p8_b = exp_b[8];
p9_r = exp_r[9]; p9_g = exp_g[9]; p9_b = exp_b[9];
p10_r = exp_r[10]; p10_g = exp_g[10]; p10_b = exp_b[10];
p11_r = exp_r[11]; p11_g = exp_g[11]; p11_b = exp_b[11];
p12_r = exp_r[12]; p12_g = exp_g[12]; p12_b = exp_b[12];
p13_r = exp_r[13]; p13_g = exp_g[13]; p13_b = exp_b[13];
p14_r = exp_r[14]; p14_g = exp_g[14]; p14_b = exp_b[14];
p15_r = exp_r[15]; p15_g = exp_g[15]; p15_b = exp_b[15];
verify_4x4_frame("TEST 3: Checkerboard Pattern");
#20;
// TEST 4: Reset Override During Active Rasterization
@(negedge clk);
start = 1;
@(negedge clk);
start = 0;
repeat (5) @(negedge clk);
rst = 1;
@(negedge clk);
rst = 0;
@(negedge clk);
if (uut.state === 2'b00 && busy === 1'b0 && pixel_valid === 1'b0 && done === 1'b0) begin
$display("[PASS] TEST 4: Reset override during active rasterization cleanly aborts to IDLE.");
tests_passed = tests_passed + 1;
end else begin
$display("[FAIL] TEST 4: Reset mid-stream abort failed! State=%b, busy=%b, valid=%b", uut.state, busy, pixel_valid);
errors = errors + 1;
end
// TEST 5: Restart Frame Generation After Abort
verify_4x4_frame("TEST 5: Restart After Abort");
$display("==========================================================");
$display(" TEST SUMMARY: %0d PASSED, %0d FAILED", tests_passed, errors);
$display("==========================================================");
#20;
$finish;
end
endmodule
Click "⚡ Run in Cloud Icarus Verilog" above to compile and execute this testbench on a real HDL simulator.