I am using ISE to write my first Verilog code. I wrote a counter:

`timescale 1ns / 1ps

module my_counter( input clk , output reg [3:0] out);
 always @(posedge clk) begin
     out <= out+1 ;


I then used ISE to make a testbench (added clk myself):

`timescale 1ns / 1ps

module mycounter_test;

// Inputs
reg clk;

// Outputs
wire [3:0] out;

// Instantiate the Unit Under Test (UUT)
my_counter uut (

initial begin
    // Initialize Inputs
    clk = 0;

    // Wait 100 ns for global reset to finish

always begin
    #5 clk = 1 ; #5 clk =0;

When I use ISE simulation mode, the waveform is like:

enter image description here

To get the proper waveform, I first set the time.

Then click the "restart" button and then "run for the time specified in the toolbar ".

Why is the output still X after several cycles of the clock?

Does it have anything to do with the comment "Wait 100 ns for global reset to finish"?

I also tried running the counter for 120 ns, but I got the same result.


2 Answers 2


The initial value (at time 0) of the reg type in Verilog is X. At the first posedge of clk, 1 is added to X, which results in X. So, the out signal remains at X throughout the simulation. You have two choices:

  1. Initialize out in an initial block (to 0, for example), or

  2. Use a reset signal to initialize out

  • \$\begingroup\$ I also initialized out to 0 in the initial block . then i had to define out as a reg in the testbench . It got value 0 even when i ran the simulation till 120ns . \$\endgroup\$
    – KFkf
    Commented Dec 9, 2015 at 14:29
  • \$\begingroup\$ I also tried adding a reset signal and it worked . I first initialized reset to 1 then changed it to 0 . \$\endgroup\$
    – KFkf
    Commented Dec 9, 2015 at 15:17

Here is something basic that i would like to share is the initial value of the Counter register is not zero by default it is xx since counter itself is made up of flipflops which have three states 0, 1 and x. For your code to work you need to add a reset signal which would reset the counter register and set it to initial value as zero

modify the design according to this code snippet:-

    ////////////////////Code snippet start
    always @ (posedge clk or posedge rst) 
        counter <= 4'd0;
        counter <= 4'd1;
    /////////////////Code snippet end

modify the test bench according to this code snippet

    //////////////////Code snippet start
    initial begin
    clk = 0;
    rst = 0;
    rst = 1;
    rst = 0;
    //Code snippet end

also it is a good practice to generate the clock as shown below in a separate initial begin block

    //////////Code Snippet start
    initial begin
    always #5 clk = ~ clk;
    ///////////Code Snippet end

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.