0

This is the testbench in question:

module tb;
    reg a,b;

    initial begin
        $display("a=%0d | b=%0d", a,b);

        a = 1;
        b = 0;
        a <= b;
        b <= a;
    end
endmodule

Ignoring the evaluation of RHS of non-blocking assignments, which will be assigned to the LHS in the NBA region, the active region consists of:

  • $display
  • a=1
  • b=0

Since the active region is non-deterministic in nature, $display will can now either print the default values of a and b, ie. x x, or their values after the assignment, ie. 1 0, or their values after the assignment of any one of the variable, ie. 1 x or x 0, ultimately resulting in a race condition.

This is how I think it should work internally. But I was told by my teacher that this does not undergo any race condition. Can someone explain why?

Killjoy
  • 91
  • 7

1 Answers1

3

The order of execution of statements within a single begin/end process is always deterministic. So the $display statement prints x as the values for a and b. Race conditions are between multiple processes (created by multiple initial or always constructs) executing in the same active region.

dave_59
  • 7,557
  • 1
  • 14
  • 26
  • Can you also explain how things change if I replace `$display` with `#0 $display`? Will this push the `$display` command in the inactive region? – Killjoy Aug 01 '23 at 04:43
  • It won't change a thing for this example--there is still only one process. Also, nothing executes in the inactive region. The `#0` suspends the process to the inactive region waiting for the active region to finish (there is nothing else in active region in this example when encountering the #0.) So the process resumes immediately executing the next statement, which is the $display. – dave_59 Aug 01 '23 at 05:18
  • Okay, got it. Two more cases: 1. If I replace `begin...end` with `fork...join` in the orginal code and 2. if I then replace `$display` with `#0 $display` in case 1. In case 1, it will create a race condition whereas in case 2, the race will be avoided and output will be `a=1 | b=0`. Am I right? – Killjoy Aug 01 '23 at 07:24
  • Correct. BTW, [so] suggests you edit your question or ask new questions rather than discussions in comments – dave_59 Aug 01 '23 at 15:03