Odd (to me) Verilog schedualing behavior

Status
Not open for further replies.

beeflobill

Member level 3
Joined
Jun 6, 2012
Messages
61
Helped
8
Reputation
18
Reaction score
7
Trophy points
1,288
Visit site
Activity points
1,872
While working on a project I found an odd Verilog simulation behavior that I wasn't expecting. I thought it was interesting because it was so unexpected to me, and I thought that others might appreciate a ( at least a useful ) reminder of what seems to be subtle Verilog behavior. I do have a couple of questions, what is the technical name for this behavior (if there is one), and is this sort of thing simulator specific or firmly set?

I ran the code below and obtained the output:
a=x b=0
a=1 b=0
a=1 b=1
a=1 b=1​

I was really expecting to see something like:
a=1 b=0
a=1 b=0
a=0 b=1
a=1 b=0​

because I was I thought 'a' would immediately update from the assign statement, but I see that the assign statement isn't updating until the end of the timestep.

Code:
module bench();

reg b;
assign a = ~b;


initial begin
   b = 0;
   $display("a=%b b=%b", a, b);
   #10;
   $display("a=%b b=%b", a, b);
   b = a;
   $display("a=%b b=%b", a, b);
   b = a;
   $display("a=%b b=%b", a, b);
end

endmodule

Thanks
 

Both the actual output and what you expected are correct. You could get either depending on what level of optimization is turned on.

The reason is because the initial block and the continuous assignment statement are two independent processes. There are no delays between the procedural assignment to b and the $display statement. When updating the value of b , that causes the continuous assignment to a to execute. You wind up with a zero-delay race between the execution of the $display statement and the update to a. The simulator is free to choose either statement to execute first.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…