1
\$\begingroup\$

Consider the following snippet (please let me know if you need me to include more):

always @(posedge CLK)
begin
 if (RST == 1'b1)
 OUT <= 4'b0000;
 else
 OUT <= OUT + 1
end

The above, let's say, is part of some DUT. Now suppose in my testbench which instantiates this DUT that I update CLK (0 -> 1) and RST (1 -> 0) at the same time; more precisely, suppose that both are updated in different procedural blocks (so they are concurrent) in the same t=100 time slot's active region (both are blocking assignments). Is there any possibility of nondeterministic/simulator-dependent behavior?

I think the crux/answer to my question will depend on how Verilog treats event-controlled evaluation. I see two possibilities:

(1) RST gets updated to 0 before the CLK signal gets updated to 1. Then, when the CLK signal gets updated there is no problem because the always block above, which is sensitive to this edge on the CLK signal, generates an evaluation event in the same t=100 time slot's active region and, eventually, we get OUT <= OUT + 1 as desired.

(2) But what if CLK updated first? In this case, the always block above, which is sensitive to this edge on the CLK signal, generates an evaluation event in the same t=100 time slot's active region. Does this evaluation event occur (a) after or (b) before the RST update? (a) In this case, there is no problem since RST will already be updated and the same decision as in (1) above will be taken. (b) In this case we will take the wrong path.

Does Verilog have a defined behavior in this case, or is it up to the simulator (in which case code should not be written like this and, perhaps, the RST update should be done at some time slot before t=100)?

toolic
10.8k11 gold badges31 silver badges35 bronze badges
asked Mar 19, 2024 at 18:57
\$\endgroup\$
2
  • \$\begingroup\$ You should in general avoid coding like that, because in real hardware, it would represent a setup/hold/recovery time violation. \$\endgroup\$ Commented Mar 19, 2024 at 19:10
  • \$\begingroup\$ That's well taken, and so I should note that this certainly isn't for any real application. This is just part of me trying to understand Verilog :) @DaveTweed \$\endgroup\$ Commented Mar 19, 2024 at 19:16

2 Answers 2

2
\$\begingroup\$

Yes, depending on how you drive the DUT inputs from the testbench, you could have simulation race conditions, leading to different behavior on different simulators.

This is why it is very important to use good coding habits in the design as well as the testbench.

Since your code uses a synchronous reset, you should drive it from the testbench as you drive your logic in the design, specifically:

  • @(posegde CLK)
  • Nonblocking assignments: <=

For example, assuming the testbench has the reg named RST_TB:

initial begin
 RST_TB <= 1;
 repeat (3) @(posedge CLK); // Adjust delay as needed
 RST_TB <= 0;
end

All synchronous inputs to the DUT should be driven in a similar fashion.

answered Mar 19, 2024 at 19:08
\$\endgroup\$
3
  • \$\begingroup\$ Understood (mostly), I think! Just to confirm, and as regards your very first sentence: are you saying that each of the cases 1,2a, and 2b I outlined in the OP are possible (in terms of comporting to the Verilog standard and/or to how most simulators are implemented)? \$\endgroup\$ Commented Mar 19, 2024 at 19:19
  • \$\begingroup\$ @EE18: Unless you are designing your own Verilog simulation software, you should not focus too deeply on event scheduling details. Focus instead on good coding practices to design the digital logic you need to design. \$\endgroup\$ Commented Mar 19, 2024 at 19:40
  • \$\begingroup\$ That is fair, but I would argue that since those good coding practices follow from simulator rules (which in turn follow from trying to model, as best we can, real hardware) that it can't hurt to know the latter :) at any rate, I would greatly appreciate if you can comment on that question. I have accepted either way. \$\endgroup\$ Commented Mar 19, 2024 at 19:50
-1
\$\begingroup\$

In real world, the hold time of a D flip flop is usually negative. Therefore, if you change the input at clock edge, the D flip flop will be written the input before clock edge. I think a simulator will also do this.

answered Mar 19, 2024 at 19:16
\$\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.