How does #delay work for verilog non blocking statements?

拜拜、爱过 提交于 2019-12-12 04:43:23

问题


What will be printed for A and B in the second $display statement?

module blocking;
reg[0:7] A, B;
initial begin
   A = 3;
   #1 A = A + 1; 
   B = A + 1;
  $display("Blocking: A= %d B= %d", A, B ); // A = 4, B = 5
   A = 3;
   #1 A <= A + 1;
   B <= A + 1;
   #1 $display("Non-blocking: A= %d B= %d", A, B ); // A = ?, B = ?
end
endmodule

Any pointers on how event scheduling in verilog works with respect to delays and non blocking statements will be really helpful. Thanks.


回答1:


because you have #1 before the second $display statement, it will be executed in the next cycle after A and B are settled.

Say we are at cycle #1.

A = 3; // at #1
#1 // (does not matter) --> #2
A <= A + 1; // #2 will do A + 1 and wait till the end of the cycle 
B <= A + 1; // #2 same as above
// at the end of the cycle #2 (nba scheduling bucket) before switching to #3 
//    A and B will be assigned '4'
#1 // --> #3
// new values of A and B are available here (4)
$display("Non-blocking: A= %d B= %d", A, B );



回答2:


In the 2nd $display, since you have put the display in another timeslot (with #1), the updated value of A & B will be printed.

module blocking;
   reg[0:7] A, B;

   initial begin
     A = 3;
#1   A = A + 1; 
     B = A + 1;
     $display("Blocking: A = %0d B = %0d", A, B ); // A = 4, B = 5
     A = 3;
#1   A <= A + 1;
     B <= A + 1;
#1   $display("Non-blocking: A = %0d B = %0d", A, B ); // A = ?, B = ?
   end
endmodule

// Output - 
Blocking: A = 4 B = 5
Non-blocking: A = 4 B = 4

But if you put the $display in the same timeslot (without #1), then the unupdated values of A & B will be printed.

module blocking;
   reg[0:7] A, B;

   initial begin
     A = 3;
#1   A = A + 1; 
     B = A + 1;
     $display("Blocking: A = %0d B = %0d", A, B ); // A = 4, B = 5
     A = 3;
#1   A <= A + 1;
     B <= A + 1;
     $display("Non-blocking: A = %0d B = %0d", A, B ); // A = ?, B = ?
   end
endmodule

// Output - 
Blocking: A = 4 B = 5
Non-blocking: A = 3 B = 5

The reason for that is scheduling of events in Verilog.

$display is scheduled in the active region, which is before the NBA (Non Blocking Assignment) region and hence it will have the original values of the nonblocking assigned signals in the same timeslot.



来源:https://stackoverflow.com/questions/46327851/how-does-delay-work-for-verilog-non-blocking-statements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!