VGA Interface Explained
🖥️ VGA Signal Timing Explained
The VGA (Video Graphics Array) interface was a common way for computers to display information on monitors. Despite its 15-pin connector, only five main signals are crucial for displaying an image: Red, Green, Blue, Horizontal Sync (Hsync), and Vertical Sync (Vsync). Other pins are optional and used for monitor-computer communication regarding resolutions.
The operation of VGA is closely tied to the design of old Cathode Ray Tube (CRT) monitors. In CRTs, an electron gun scans across the screen from left to right and top to bottom to "paint" an image. While modern flat-panel monitors don't use this physical process, they still adhere to the timing established by CRT scanning for pixel delivery.
📊 Understanding Scanline Timing
To properly display an image, precise timing is essential, not just for pixel order but also for blanking intervals (margins) that allow the electron beam to stabilize and reposition. These margins exist on the top, bottom, left, and right of the visible display area.
The timing of a single scanline involves several phases:
Visible Part: The duration for displaying actual pixels.
Front Porch: A blank time on the right side of the image, immediately following the visible area.
Sync Pulse (Horizontal Sync): A signal that tells the electron beam to swing back to the left side of the image. This is a dedicated signal line (pin 13 on the VGA connector).
Back Porch: A blank time on the left side of the image, before the next scanline begins.
This timing sequence repeats for each scanline. At the end of an entire frame, a similar sequence occurs for vertical synchronization:
Front Porch (Vertical): Blank scanlines at the bottom of the image.
Vertical Sync Pulse: A signal (pin 14 on the VGA connector) that directs the electron beam back to the top-left of the display for the next frame.
Back Porch (Vertical): Blank scanlines at the top of the next frame.
Critical Point: For any specific display resolution, the horizontal sync and vertical sync signal timings must be precisely correct for the image to display properly. Incorrect timing on older CRT monitors could even cause physical damage.
⏰ VGA Timing Requirements & Pixel Clock
Achieving correct timing is paramount. Different display resolutions have specific timing requirements, including varying pixel clock frequencies. A good video card supports multiple resolutions and might communicate with the monitor to select the optimal mode.
💡 Adapting Pixel Clock Frequency
Building hardware for VGA often involves selecting a suitable pixel clock frequency. These frequencies are typically in the tens of megahertz (MHz), often requiring crystal oscillators. If a specific crystal oscillator frequency isn't available, a slower pixel clock can be used by compensating with fewer pixels. The key is to maintain the overall time duration for each segment of the scanline (visible area, front porch, sync pulse, back porch).
Example:
For a visible area that takes 20 microseconds (µs), using an 800-pixel width with a 40 MHz clock (800 pixels/40×106 pixels/s=20 µs).
If a 10 MHz clock is used instead, the number of pixels must be reduced proportionally to maintain the 20 µs duration: 200 pixels/10×106 pixels/s=20 µs.
This proportional scaling applies to the front porch, sync pulse, and back porch as well, ensuring the total scanline time remains constant. For example, if all pixel counts are divided by 4, the overall scanline time of 26.4 µs can be maintained even with a 10 MHz clock.
🔢 Building a Horizontal Pixel Counter
To implement VGA timing, a pixel counter is needed to track the current pixel position within a scanline. This counter helps determine when to display pixels, when to send sync pulses, and when to blank the screen.
🛠️ Counter Design with 74LS161
A counter for horizontal pixels needs to count up to 264 (the total number of pixel clock cycles per line in the adapted timing). This requires 9 bits (since 28=256 and 29=512). The 74LS161 is a 4-bit counter. To achieve 9 bits, three 74LS161 chips are cascaded.
Connection Details:
Power and Ground: Connected for all chips.
Clear Pin: Tied together across all chips to reset them simultaneously to zero.
Clock Pin: Tied together, eventually connected to the 10 MHz oscillator.
Data Inputs: Left disconnected as no values are loaded; the chips only count.
Enable Pins (G and T): Connected high for the first chip to be always enabled. For cascading, the ripple-carry output of one chip connects to the enable input of the next, allowing the count to roll over.
Load Pin: Tied high as it's an active-low input and not used for loading.
This cascading setup allows counting up to 12 bits, more than sufficient for the required 9 bits. The outputs of the counter can be connected to LEDs for visual testing (at slower clock speeds).
⚙️ Decoding Counter Values with NAND Gates
To precisely control the timing, specific counter values (e.g., 200, 210, 242, 264) need to be detected. This is achieved using NAND gates in combination with inverters.
Detection Logic:
Binary Representation: Convert the target decimal value (e.g., 200) into its 9-bit binary equivalent.
Inverters: For each bit that is '0' in the binary representation, an inverter is placed on the counter's output for that bit. Bits that are '1' are passed directly.
NAND Gate: The outputs (inverted or non-inverted) are fed into an 8-input NAND gate (74LS30). The output of the NAND gate goes low only when all its inputs are high.
Bit Selection: Due to the counter's reset at 264, the counter never reaches values like 456. This often allows ignoring the highest bit (256s place) when detecting values like 200, 210, or 242, as that bit will always be '0'. Similarly, the lowest bit might be ignored for 264. This "cheating" allows using 8-input NAND gates.
Example for 264:
The binary for 264 is 1000010002.
The 9th bit (256s place) must be '1'.
The 8-input NAND gate can check the remaining 8 bits. The lowest bit (1s place) can be ignored because if the other 8 bits are correctly set for 264, the counter will reset before reaching 265.
🔄 Inverter Stage
To provide both inverted and non-inverted copies of each counter bit, 74LS04 hex inverters are used. Since 9 inverters are needed, two 74LS04 chips are required. Each counter output is fed into an inverter, providing both the original signal and its inverted version for the decoding logic.
🚦 Generating Horizontal Sync Signals with SR Latches
Once the specific counter values are detected, SR latches are used to generate the continuous display and sync signals, rather than just momentary pulses. For active-low detection outputs from the NAND gates, inverted SR latches are utilized.
Display Interval Latch:
Set: When the counter reaches 264 (which immediately resets to 0). This marks the start of the visible display area.
Reset: When the counter reaches 200. This marks the end of the visible display area and the start of the blanking interval.
Output (Q): High when pixels should be displayed, low during blanking.
Horizontal Sync (Hsync) Latch:
Set: When the counter reaches 210. This marks the beginning of the Hsync pulse.
Reset: When the counter reaches 242. This marks the end of the Hsync pulse.
Output (NOT Q): The inverted output provides the active-low Hsync signal.
The SR latches are built using two NAND gates from a 74LS00 quad NAND gate chip.
⏱️ Verifying Horizontal Timing with Oscilloscope
With the 10 MHz oscillator connected as the clock source, the LEDs on the counter will appear continuously on due to the high speed. An oscilloscope is crucial for verifying the precise timing.
Measurements:
Display Interval: The positive-going width of the display signal should be approximately 20 µs.
Sync Pulse (Hsync): The negative-going width of the Hsync signal should be approximately 3.2 µs.
Front Porch: Measured between the end of the display interval and the start of the Hsync pulse, this should be about 1 µs.
Back Porch: Measured between the end of the Hsync pulse and the start of the next display interval, this should be about 2.2 µs.
These measurements confirm that the horizontal timing generation is accurate according to the adapted 10 MHz clock and pixel counts.
📏 Building a Vertical Scanline Counter
The vertical sync signal (Vsync) works similarly to Hsync but counts scanlines instead of pixels.
Each time the horizontal counter completes a full scanline (resets at 264 pixels), the vertical counter increments.
The goal is to count up to 628 scanlines (total lines per frame).
Detection points are needed for 600 (end of visible lines), 601 (start of Vsync), 605 (end of Vsync), and 628 (frame reset).
🛠️ Vertical Counter Design
Counter Chips: Counting up to 628 requires 10 bits. Three 74LS161 chips provide 12 bits, which is sufficient.
Inverters: Similar to the horizontal counter, inverters (e.g., five 74LS04 chips) are used to provide inverted and non-inverted signals for decoding.
NAND Gates: Four 8-input NAND gates (74LS30) are used to detect the target values (600, 601, 605, 628). Again, some bits can be ignored due to the reset point at 628, allowing the use of 8-input NAND gates.
SR Latches: Two SR latches are used:
Display Area Latch: Set when the counter resets at 628 (effectively 0), reset at 600.
Vertical Sync Latch: Set at 601, reset at 605.
🔄 Clocking the Vertical Counter
Crucially, the clock input for the vertical counter is the reset signal from the horizontal counter. This ensures the vertical counter increments only after each complete horizontal scanline is drawn.
🔬 Verifying Vertical Timing
Connecting the vertical display and sync signals to the oscilloscope allows for verification:
Visible Area (Vertical): Should be approximately 15.84 milliseconds (ms).
Sync Pulse (Vertical): Should be approximately 0.1056 ms (105.6 µs).
The oscilloscope measurements confirm that the vertical timing is correctly generated, including the number of horizontal sync pulses that occur during the vertical sync interval (e.g., 4 Hsync pulses during Vsync).
🔌 Connecting to a VGA Monitor
The VGA connector has 15 pins. The essential connections for sync signals are:
Ground: All ground pins (including the shell) are connected together.
Horizontal Sync (Pin 13): Connected to the Hsync output from the horizontal timing circuit.
Vertical Sync (Pin 14): Connected to the Vsync output from the vertical timing circuit.
When these sync signals are correctly connected to a VGA monitor, the monitor will typically come out of standby. Although the display will be blank (as no picture data is sent yet), the monitor's menu should indicate it's receiving a signal in the expected mode (e.g., 800x600 at 60 Hz, with a horizontal frequency of 38 kHz and vertical frequency of 60 Hz), confirming the sync signals are properly generated.
The next step in displaying a full image involves sending the actual Red, Green, and Blue pixel data in the correct order, synchronized with these timing signals.

Ask anything
We've read this whole study set, so questions about it get the best answers.