I am not really sure what is wrong with my code bellow for a vga. All I want the program to do is display a solid color on the monitor. I want to use the switches on my card to change the color displayed. I ran the simulation and all the hsync and vsync signals seem to be as expected. When I flick the switches I get a short pulse of color on the display and then it goes black. Anyone know what is wrong with this code?

module vga(output reg [3:0] red, green, blue,
           input [15:0] sw,
           output hsync, vsync, 
           input clk);
    wire vga_clk; //25.203 Mhz
    vga_clk_gen gen(.clk_in1(clk), .clk_out1(vga_clk));

    reg [9:0] hpx = 0, vpx = 0;
    reg [9:0] next_hpx, next_vpx;
    reg [3:0] next_red, next_green, next_blue;

    assign hsync = !(hpx >= 660 && hpx <= 756);
    assign vsync = !(vpx >= 494 && vpx <= 495);

    always @(posedge vga_clk) begin
        hpx <= next_hpx;
        vpx <= next_vpx;
        red <= next_red;
        green <= next_green;
        blue <= next_blue;

    always @* begin
        next_red = sw[3:0];
        next_blue = sw[7:4];
        next_green = sw[11:8];
        next_vpx = vpx;

        if(hpx == 800) begin
            next_hpx = 0;
            if(vpx == 525)
                next_vpx = 0;
                next_vpx = vpx + 1;
        end else begin
            next_hpx = hpx + 1;
  • \$\begingroup\$ Are you sure it is not an electrical problem? \$\endgroup\$
    – Eugene Sh.
    Jan 4, 2016 at 16:34
  • \$\begingroup\$ No I am not. One thing I forgot to do is turn of color during the sync. Maybe that is an issue. \$\endgroup\$
    – chasep255
    Jan 4, 2016 at 16:35
  • \$\begingroup\$ And you should check whether or not you synthesis tool is supporting synthesis of register initialization (for hpx = 0, vpx = 0;). A better way would be to add a reset to it.. \$\endgroup\$
    – Eugene Sh.
    Jan 4, 2016 at 16:37
  • \$\begingroup\$ I know the initial value works since I have used it before. Also how do I add a reset? There is no option for it in the template XDC file I got from diligent. Also I managed to fix it. I should not have been outputting colour during the sync. \$\endgroup\$
    – chasep255
    Jan 4, 2016 at 16:39
  • 3
    \$\begingroup\$ I can't explain why flipping the switches gives you flashes of color, but I want to point out a very minor error in your logic: Your hpx counts from 0 to 800, which takes 801 cycles, and your vpx counts from 0 to 525, which is a total of 526 lines. This means that your sync frequencies are very slightly off, but not by enough to explain your problem. Maybe your monitor is not happy with the lack of video blanking around the sync pulses. It may also be looking for a composite sync signal on the green video line (look up "sync-on-green" for details). \$\endgroup\$
    – Dave Tweed
    Jan 4, 2016 at 17:19

1 Answer 1


The VGA signal must be blanked (outputting black) while hsync or vsync are active. Many monitors will use the signal levels during sync periods to calibrate a "black level", which would lead to the behavior you're describing.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.