0
\$\begingroup\$

Code

module block;
  reg a;
  reg b = 1'b0;
  reg c = 1'b1;

  initial begin
    c  = b;
    a <= c;
  end

endmodule

output

I simulated the code fragment shown in figure expecting the value of a to be 1'b1 because the statement a <= c; is nonblocking and the RHS of the statement is evaluated at the beginning of time step(i.e before execution of c = b;).

But this is the result I obtained. Why is the output a 1'b0 and not 1'b1?

PS: I know it's not a good practice to mix blocking and nonblocking statements in the same block, but I wanted to know how the IEEE std explains this special case.

\$\endgroup\$
3
\$\begingroup\$

You have given c a default value of 1, but then at time 0 assign it to be equal to b (blocking). So the simulation copies the value of b to c before sequentially moving on to perform a <= b.

What you have written is essentially:

module block;
  reg a;
  reg b = 1'b0;
  reg c;

  initial begin
    c  = b;
    a <= b;
  end

endmodule
\$\endgroup\$
  • \$\begingroup\$ So is it like the initial statement reg c = 1'b0; doesn't have any effect in the code because the blocking statement also covers it's assignment simultaneously? \$\endgroup\$ – electro_sm11 Dec 3 '14 at 12:59
  • 1
    \$\begingroup\$ @srvn11 yes. initial values assigned like that are often not considered to be synthesizable. Although they probably work for setting FPGA defaults. setting defaults in an initial block for FPGA use is quite common. both types are meaning less for ASIC use, then you should use an active low async reset. \$\endgroup\$ – pre_randomize Dec 3 '14 at 14:36
  • \$\begingroup\$ thank you for the info... I just have one more doubt! The RHS of a nonblocking statement is evaluated at beginning of time step and assigned to LHS at end of time step right? So here value of a should be default value of c i.e 1'bx right? But here it's taking value of b. \$\endgroup\$ – electro_sm11 Dec 4 '14 at 4:23
  • \$\begingroup\$ @srvn11 Your description of non-blocking (<=) is correct. But code is executed sequentially, Blocking(=) is placed first so is executed first. And that is why it is bad to mix styles in the same block because if you flip-the order of the statements you get a different behaviour even though you think you describing parallel hardware! \$\endgroup\$ – pre_randomize Dec 4 '14 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.