FPGA Project: VGA Color Bars at 640 x 480
A VGA monitor draws the picture line by line, pixel by pixel, and needs only two counters to drive it: a horizontal counter that counts the 800 pixel times in a line (640 visible plus blanking) and a vertical counter that counts the 525 lines in a frame (480 visible). Sync pulses are low during fixed counter ranges, and the color outputs are on only in the visible area. At a 25 MHz pixel rate that gives 59.5 frames a second, which monitors accept as the 640 x 480 at 60 Hz mode. The Basys 3 and DE10-Lite both have VGA connectors with 4 bits per color; the Arty A7 does not, so it is left out.
How this was checked
The design was simulated in Icarus Verilog 12 and linted with Verilator 5.036 (-Wall, no warnings) for every board's top level. The Basys 3 and Arty A7 versions were synthesized, placed and routed in Vivado 2025.2, met timing, and produced a bitstream. The DE10-Lite version was simulated and linted but not compiled, because Intel Quartus was not used here; its pin assignments come from Terasic's DE10-Lite System Builder file. None of the bitstreams has been loaded onto a board yet. If you build it and something doesn't behave as described, please tell us through the contact page.
The timing
| Visible | Front porch | Sync | Back porch | Total | |
|---|---|---|---|---|---|
| Horizontal (pixels) | 640 | 16 | 96 | 48 | 800 |
| Vertical (lines) | 480 | 10 | 2 | 33 | 525 |
The Verilog
`timescale 1ns/1ps
// 640 x 480 at 60 Hz VGA timing and eight 80-pixel color bars
// (black, blue, green, cyan, red, magenta, yellow, white).
// pix_en pulses once per pixel (25 MHz): every 4th clock at 100 MHz,
// every 2nd at 50 MHz. Sync pulses are active low.
module vga_pattern #(parameter CLK_DIV = 4) (
input wire clk,
output reg [3:0] r, g, b,
output reg hsync_n, vsync_n
);
localparam H_VIS = 640, H_FP = 16, H_SYNC = 96, H_BP = 48, H_TOT = H_VIS + H_FP + H_SYNC + H_BP; // 800
localparam V_VIS = 480, V_FP = 10, V_SYNC = 2, V_BP = 33, V_TOT = V_VIS + V_FP + V_SYNC + V_BP; // 525
localparam integer DW = $clog2(CLK_DIV);
localparam integer DIV_LAST_I = CLK_DIV - 1;
localparam [DW-1:0] DIV_LAST = DIV_LAST_I[DW-1:0];
reg [DW-1:0] div = 0;
wire pix_en = (div == DIV_LAST);
reg [9:0] hc = 0, vc = 0;
reg [6:0] col = 0; // pixel within the current 80-pixel bar
reg [2:0] bar = 0; // which of the 8 bars, 0 to 7
always @(posedge clk) begin
div <= pix_en ? {DW{1'b0}} : div + 1'b1;
if (pix_en) begin
if (hc == H_TOT - 1) begin
hc <= 0;
vc <= (vc == V_TOT - 1) ? 10'd0 : vc + 10'd1;
end else hc <= hc + 10'd1;
if (hc == H_TOT - 1) begin col <= 0; bar <= 0; end
else if (col == 7'd79) begin col <= 0; bar <= bar + 3'd1; end
else col <= col + 7'd1;
end
end
// Registered outputs, so sync and color change together on a pixel edge.
wire visible = (hc < H_VIS) && (vc < V_VIS);
always @(posedge clk) if (pix_en) begin
hsync_n <= ~((hc >= H_VIS + H_FP) && (hc < H_VIS + H_FP + H_SYNC));
vsync_n <= ~((vc >= V_VIS + V_FP) && (vc < V_VIS + V_FP + V_SYNC));
r <= visible && bar[2] ? 4'hF : 4'h0;
g <= visible && bar[1] ? 4'hF : 4'h0;
b <= visible && bar[0] ? 4'hF : 4'h0;
end
endmodule
The pixel enable is every 4th clock on the Basys 3 (100 MHz) and every 2nd on the DE10-Lite (50 MHz). The bar index comes from a second counter that steps every 80 pixels, which avoids dividing by 80. The outputs are registered so sync and color change together.
The numbers
| Quantity | Value | How |
|---|---|---|
| Pixel times per frame | 420,000 | 800 x 525 |
| Frame rate at 25 MHz | 59.52 Hz | 25,000,000 / 420,000 |
| Frame rate at the standard 25.175 MHz | 59.94 Hz | 25,175,000 / 420,000 |
| Line time at 25 MHz | 32 µs | 800 / 25 MHz |
| hsync pulse | 3.84 µs | 96 / 25 MHz |
Simulation and build results
The testbench measured the generator directly: 1,600 clocks per line at a pixel enable of every 2nd clock (800 pixels), an hsync pulse 96 pixels wide, and 525 lines per frame. In Vivado the Basys 3 design used 36 LUTs and 46 flip-flops and met timing with 6.232 ns of slack at 100 MHz.
Common mistakes
- Sync pulses of the wrong polarity: 640 x 480 uses negative (active low) sync.
- Color outputs left on during blanking, which can confuse the monitor's black level.
- Counting from 1, which shifts every boundary by a pixel.
- Expecting an exact 25.175 MHz from a 100 MHz clock: 25 MHz is close enough for monitors, or use a clocking wizard.
Check your understanding
- Q1. How many pixel times are in one frame? Answer: 800 x 525 = 420,000.
- Q2. What frame rate does a 25 MHz pixel clock give? Answer: 59.52 Hz.
- Q3. How long is each color bar? Answer: 80 pixels, since 640 / 8 = 80.
Beginner FPGA projects
Last verified
Simulated, linted and built on 17 November 2026; the timing numbers are checked in lib/content2Numbers.test.js. The files are in the site's repository under scripts/fpga.