assign VGA_HS = Hsync;
assign VGA_VS = Vsync;
assign data_en = Hact & Vact;
// VGA Driver
always @(posedge pixel_clk, posedge rst)
begin
if( rst )
begin
hcnt <= 0;
Hsync <= 0;
Hact <= 0;
end
else
begin
hcnt <= hcnt + 1;
case( hcnt )
H1: Hsync <= 1'b1;
H2: Hact <= 1'b1;
H3: Hact <= 1'b0;
H4: begin
Hsync <= 1'b0;
hcnt <= 0;
end
default: begin
Hsync <= Hsync;
Hact <= Hact;
end
endcase
end
end
always @(negedge Hsync, posedge rst)
begin
if(rst)
begin
vcnt <= 0;
Vsync <= 0;
Vact <= 0;
end
else
begin
vcnt <= vcnt + 1;
case( vcnt )
V1: Vsync <= 1'b1;
V2: Vact <= 1'b1;
V3: Vact <= 1'b0;
V4: begin
Vsync <= 1'b0;
vcnt <= 0;
end
default: begin
Vsync <= Vsync;
Vact <= Vact;
end
endcase
end
end
endmodule
Of course. There are several significant errors in this VGA controller code, ranging from critical timing issues to potential glitches.
Here is a breakdown of the errors, from most to least critical:
1. Critical Error: Vertical Counter Clocking
The vertical counter (vcnt) is clocked on the negative edge of Hsync (negedge Hsync).
Why this is wrong: VGA monitors and displays expect to see timing signals (HSync, VSync) transition on their specified active edges, which for standard VGA is the positive edge (rising edge). The vertical counter should be incremented once per scanline, which is best detected by the rising edge of Hsync when the horizontal counter wraps around.
The consequence: This will cause a race condition and unpredictable behavior. The vertical timing will not be synchronized correctly with the horizontal timing, leading to a distorted or non-functional display.
2. Functional Error: Missing Reset for Counters
Neither the horizontal counter (hcnt) nor the vertical counter (vcnt) is reset to zero by the rst signal.
Why this is wrong: The rst block only clears the output state registers (Hsync, Hact, Vsync, Vact). The counters themselves will continue counting from whatever value they had when reset was asserted.
The consequence: If the module comes out of reset while hcnt is, for example, in the middle of the active video region, the Hact signal will remain high indefinitely until the counter naturally reaches H4. This will stretch a single line of pixels across many lines on the screen, completely destroying the image geometry.
3. Best Practice / Glitch Error: Latch Inference
Both case statements use a default condition that simply holds the previous value of the output (Hsync <= Hsync;).
Why this is wrong: This coding style infers a transparent latch in hardware. The synthesis tool has to create extra logic to remember the last value of Hsync or Vsync for every possible hcnt or vcnt value that isn't explicitly handled. This can lead to timing violations and glitches.
The fix: A well-designed synchronous block should define a state for every possible input. In this case, you should explicitly set the outputs to their default/inactive state in the default case.
Corrected Code
Here is the corrected version of your module with comments explaining the fixes.
verilog
`timescale 1ns / 1ps
module vga_controller #(
// horizontal timing parameters (default 640x480x60)
parameter HPULSE = 96, // Hsync pulse
parameter HBP = 48, // Back Porch
parameter HACTIVE = 640, // Horizontal pixels
parameter HFP = 16, // Front Porch
parameter H1 = HPULSE,
parameter H2 = HPULSE+HBP,
parameter H3 = HPULSE+HBP+HACTIVE,
parameter H4 = HPULSE+HBP+HACTIVE+HFP,
// vertical timing parameters (default 640x480x60)
parameter VPULSE = 2, // Vsync pulse
parameter VBP = 33, // Back Porch
parameter VACTIVE = 480, // Vertical pixels
parameter VFP = 10, // Front Porch
parameter V1 = VPULSE,
parameter V2 = VPULSE+VBP,
parameter V3 = VPULSE+VBP+VACTIVE,
parameter V4 = VPULSE+VBP+VACTIVE+VFP
)(
input pixel_clk, // 25 MHz default
input rst,
output VGA_HS,
output VGA_VS,
output data_en // 1 = display time, 0 = blanking time
);
reg [$clog2(H4)-1:0] hcnt;
reg [$clog2(V4)-1:0] vcnt;
reg Hsync;
reg Vsync;
reg Hact;
reg Vact;
assign VGA_HS = Hsync;
assign VGA_VS = Vsync;
assign data_en = Hact & Vact; // Active high enable
// VGA Driver - Horizontal
always @(posedge pixel_clk, posedge rst)
begin
if( rst )
begin
hcnt <= 0;
Hsync <= 1'b0; // Explicitly reset to inactive state
Hact <= 1'b0;
end
else
begin
// Only increment if we are not at the end of the line
if (hcnt < H4 - 1) begin
hcnt <= hcnt + 1;
end
case( hcnt )
H1: Hsync <= 1'b1; // Start of HSync pulse (active low for VGA)
H2: Hact <= 1'b1; // Start of active display area
H3: Hact <= 1'b0; // End of active display area
H4: begin
Hsync <= 1'b0; // End of HSync pulse
hcnt <= 0; // Reset horizontal counter
end
default: begin
// Explicitly define default state to prevent latch inference
Hsync <= 1'b0;
Hact <= 1'b0;
end
endcase
end
end
// VGA Driver - Vertical
// FIX #1: Changed clock to posedge pixel_clk
always @(posedge pixel_clk, posedge rst)
begin
if( rst )
begin
vcnt <= 0;
Vsync <= 1'b0; // Explicitly reset to inactive state
Vact <= 1'b0;
end
else
begin
// Only increment if we are at the end of a horizontal line (H4)
if (hcnt == H4 - 1) begin
if (vcnt < V4 - 1) begin
vcnt <= vcnt + 1;
end
end
case( vcnt )
V1: Vsync <= 1'b1; // Start of VSync pulse (active low for VGA)
V2: Vact <= 1'b1; // Start of active display area
V3: Vact <= 1'b0; // End of active display area
V4: begin
Vsync <= 1'b0; // End of VSync pulse
vcnt <= 0; // Reset vertical counter
end
default: begin
// Explicitly define default state to prevent latch inference
Vsync <= 1'b0;
Vact <= 1'b0;
end
endcase
end
end
endmodule
Key Changes in the Corrected Code:
Vertical Counter Clock: The second always block now uses posedge pixel_clk like the first one.
Proper Counter Reset: The rst block now sets hcnt, vcnt, and all state registers (Hsync, Hact, Vsync, Vact) to a known initial state (0).
Counter Logic Update: The counters now only increment when appropriate. vcnt is incremented once per line by checking for hcnt == H4 - 1. This is a more robust way to detect the end of a line than using negedge Hsync.
Latch Prevention: The default cases now explicitly set the outputs to 1'b0, which is the correct inactive state for all signals in this design. This removes any inferred latches and makes the design purely synchronous.
Signal Naming Clarity: I've changed the assignments in the rst block to be explicit (Hsync <= 1'b0;) for clarity. Note that for standard VGA, HSync and VSync are active-low signals, so setting them to 1'b1 means "no sync pulse" and 1'b0 means "sync pulse is active". Your original logic was correct in this regard, but the reset values were not. The corrected code ensures they start in the inactive state.