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

VisibleFront porchSyncBack porchTotal
Horizontal (pixels)640169648800
Vertical (lines)48010233525
One VGA lineEach line is 640 visible pixels, a 16-pixel front porch, a 96-pixel sync pulse with hsync low, and a 48-pixel back porch. Not to scale.regionvisibleFPsyncBPhsyncvideo on
One line of 800 pixel times: hsync is low only during the 96-pixel sync. Not to scale.
The VGA generatorA pixel enable divides the board clock to 25 MHz; it advances the horizontal counter, which advances the vertical counter at the end of each line; comparisons on the counters produce the sync pulses and the color bars.Pixel tick25 MHzh counter0 to 799v counter0 to 524Outputssync, color
Two counters and a few comparisons are the whole display controller.

The Verilog

vga_pattern.v
`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

QuantityValueHow
Pixel times per frame420,000800 x 525
Frame rate at 25 MHz59.52 Hz25,000,000 / 420,000
Frame rate at the standard 25.175 MHz59.94 Hz25,175,000 / 420,000
Line time at 25 MHz32 µs800 / 25 MHz
hsync pulse3.84 µs96 / 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

Related

Sources

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.