2
\$\begingroup\$

I have just started learning Verilog, so I tried writing a simple 4-bit binary counter. However, when I run the behavioural simulation, the output stays undetermined. I will really appreciate if someone can tell me where I am going wrong.

module counter(input clk, input rst, output reg [3:0] count);
always@ (posedge clk)
begin
 if(rst)
 count <= 4'b0;
 else
 count <= count + 1'b1;
end
endmodule

Here is a screenshot of the behavioural simulation

asked Mar 19, 2020 at 19:43
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Toggle your reset line to initialize the count reg. \$\endgroup\$ Commented Mar 19, 2020 at 19:46
  • 1
    \$\begingroup\$ u could have posted full picture of simulation.anyways as @schadjo mentioned this due to the missing of reset pulse. or else u could do module counter(input clk, input rst, output reg [3:0] count = 0); \$\endgroup\$ Commented Mar 20, 2020 at 8:57

1 Answer 1

3
\$\begingroup\$

Your count starts at X (actually it starts 4'bxxxx.).

Now if you add 1'b1 to 4'bxxxx you still have 4'bxxxx. Sort of like a dead-lock

The only way to break this deadlock in your code is to apply a reset whilst the clock is running. That forces the counter to zero and after removing the reset you can use it to count.

answered Mar 19, 2020 at 19:55
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.