Search code examples
verilogfpgawaveform

Verilog can't figure out why a reg is always X


I am trying to do a VGA output using verilog but I can't seem to figure out why r_hcount stays X. The simulation waveforms show that r_vcount is being reset to 0 properly but for some reason r_hcount never gets reset to 0. I can't figure out why...

Verilog code:

module m_VGA640x480(
  input  wire        iw_clock,
  input  wire        iw_pix_stb,
  input  wire        iw_rst,
  output wire        ow_hs,
  output wire        ow_vs,
  output wire        ow_blanking,
  output wire        ow_active,
  output wire        ow_screenend,
  output wire        ow_animate,
  output wire  [9:0] ow_x,
  output wire  [9:0] ow_y
  );
  localparam HS_STA = 16;
  localparam HS_END = 16 + 96;
  localparam HA_STA = 16 + 96 + 48;
  localparam VS_STA = 480 + 11;
  localparam VS_END = 400 + 11 + 2;
  localparam VA_END = 480;
  localparam LINE   = 800;
  localparam SCREEN = 524;

  reg [9:0] r_hcount;
  reg [9:0] r_vcount;

  assign ow_hs = ~((r_hcount >= HS_STA) & (r_hcount < HS_END));
  assign ow_vs = ~((r_vcount >= VS_STA) & (r_vcount < VS_END));

  assign ow_x = (r_hcount <  HA_STA) ? 0 : (r_hcount - HA_STA);
  assign ow_y = (r_vcount >= VA_END) ? (VA_END - 1) : (r_vcount);

  assign ow_blanking = ((r_hcount < HA_STA) | (r_vcount > VA_END - 1));

  assign ow_active = ~((r_hcount < HA_STA) | (r_vcount > VA_END - 1));

  assign ow_screenend = ((r_vcount == SCREEN - 1) & (r_hcount == LINE));

  assign ow_animate = ((r_vcount ==VA_END - 1) & (r_hcount == LINE));

  always @(posedge iw_clock)
  begin
    if (iw_rst)
    begin
      r_hcount <= 0;
      r_vcount <= 0;
    end
    if (iw_pix_stb)
    begin
      if (r_hcount == LINE)
      begin
        r_hcount <= 0;
        r_vcount <= r_vcount + 1;
      end
      else
        r_hcount <= r_hcount + 1;

      if (r_vcount == SCREEN)
        r_vcount <= 0;
    end
  end
endmodule

Here is the result of the simulation. r_hcount is bugged... The code is supposed to set both counters to 0 when reset is 1 but for some reason it's not getting reset to 0. Please help.

Wavefrorm


Solution

  • From your work, I notice one point may cause the issue

    always @(posedge iw_clock)
    begin
        if (iw_rst)
        //you define r_hcount <= 0 here
        .....
        if (iw_pix_stb) //<== another condition
        // r_hcount <= 0 is also defined here
    

    So if posedge clock happened, r_hcount may be bugged here. I suggest it should be done like this

     else if (iw_pix_stb) <=== else if here
    

    Good luck.