Blocking/Nonblocking with Delay


I am now confused by one piece of Verilog Codee, its kind of testing the blocking or non-blocking assignment features that combination with Delay model.

 module cl_tb;

  reg x,y,z;

initial begin
    x = 2;
    y <= #9 x;
    x = 1;
    #50 $stop;

initial begin

always @(x,y)
   z = # 2 x+y;


By hand-calculation, I could get the result
[ Time: X value, Y value, Z value] below:
[0: 2,x,x]
[2: 2,0,2]
[4: 1,0,2]
[6: 1,0,1]
[13: 1,2,1]
[15: 1,2,3]

However, the simulation result is

Which you can see from the link

I am wondering why the always@(x,y) was not triggered at timeslot 2 and 4.


Best Answer

Register variables in Verilog represent flip-flops in hardware.

The declaration:

reg x;

creates a single-bit register that can only hold the values:
x (unknown), 0 or 1.

The cl_tb.v code is attempting to assign values to register x that are too large to fit in a single bit value. Verilog converts the integers to binary representation and fills in the register starting at the LSB until it runs out of bits in the register and it stops. It issues no warnings for this (perhaps because it's expected that sometimes you might want to use the LSBs of a value).

Change your declaration for the x, y and z registers to make them wide enough to handle your largest anticipated value.

reg [2:0] x, y, z;

Then the output is:


The 'hand calculation' you show is incorrect. For example, the always block triggers when x is updated at time zero, but y is not assigned until #9 later. Hence register z is also still unknown and the monitor is not triggered.