# For loop increments at 0 simulation time

**URL:** <https://verificationacademy.com/forums/t/for-loop-increments-at-0-simulation-time/40174>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, for-loop, 0sim-time, forever-loop\
**Created:** [March 19, 2022, 5:34am UTC](https://verificationacademy.com/forums/t/for-loop-increments-at-0-simulation-time/40174 "2022-03-19T05:34:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![natasv](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@natasv](https://verificationacademy.com/forums/u/natasv)\
**Post date:** [March 19, 2022, 5:34am UTC](https://verificationacademy.com/forums/t/for-loop-increments-at-0-simulation-time/40174/1 "2022-03-19T05:34:53Z")

</div>

```verilog
class forever_ex;
	int dt2, dt3;

	task forever_lp_check();
		forever begin
			$display("Inside forever loop: erealtime = %0t, time = %0t", $realtime, $time);
			@(dt2) 
			$display("dt2=%d", dt2);
			dt3 = dt2;
			$display(dt2, dt3);
		end
	endtask

	task incr_dt2();
		for(int ii=0; ii<10; ii++) begin
			//#1ns;
			dt2 +=10;
			$display("Inside dt2 incr : realtime = %0t, time = %0t", $realtime, $time);
			$display(dt2);
		end
	endtask
endclass

module top();

	initial begin
		forever_ex forever_ex_ii;
		forever_ex_ii = new();

		fork
			$display("Before forever loop call: realtime = %0t, time = %0t", $realtime, $time);
			forever_ex_ii.forever_lp_check();
		join_none 

		$display("Before d2 incr call: realtime = %0t, time = %0t", $realtime, $time);
		forever_ex_ii.incr_dt2();
		$display("here1");

	end
endmodule

```

Result:

```auto
Before d2 incr call: realtime = 0, time = 0
Inside dt2 incr : realtime = 0, time = 0
         10
Inside dt2 incr : realtime = 0, time = 0
         20
Inside dt2 incr : realtime = 0, time = 0
         30
Inside dt2 incr : realtime = 0, time = 0
         40
Inside dt2 incr : realtime = 0, time = 0
         50
Inside dt2 incr : realtime = 0, time = 0
         60
Inside dt2 incr : realtime = 0, time = 0
         70
Inside dt2 incr : realtime = 0, time = 0
         80
Inside dt2 incr : realtime = 0, time = 0
         90
Inside dt2 incr : realtime = 0, time = 0
        100
here1
Before forever loop call: realtime = 0, time = 0
Inside forever loop: erealtime = 0, time = 0

```

Question:  
Although I understand the execution of the forever\_lp\_check() task call within fork-join\_none can be random - either before or after incr\_dt2() is executed, what I cannot understand is how can for loop increment at 0 realtime as well as sim time. Could you please help explaining.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 19, 2022, 6:56am UTC](https://verificationacademy.com/forums/t/for-loop-increments-at-0-simulation-time/40174/2 "2022-03-19T06:56:18Z")

</div>

_In reply to [natasv](https://verificationacademy.com/forums/t/for-loop-increments-at-0-simulation-time/40174):_

There are no delays in this code (other than in the comments), so how can time advance beyond 0. Maybe you should clarify what you were expecting to see.

---

<div class="post-metadata">

**Author:** ![natasv](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@natasv](https://verificationacademy.com/forums/u/natasv)\
**Post date:** [March 19, 2022, 11:26am UTC](https://verificationacademy.com/forums/t/for-loop-increments-at-0-simulation-time/40174/3 "2022-03-19T11:26:37Z")

</div>

I was expecting to see the addition operation to consume some real time.  
When I uncommented the #1ns, I saw what I wanted. But then an addition operation counting upto 100 should consume some time in reality, I thought

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 19, 2022, 4:58pm UTC](https://verificationacademy.com/forums/t/for-loop-increments-at-0-simulation-time/40174/4 "2022-03-19T16:58:50Z")

</div>

_In reply to [natasv](https://verificationacademy.com/forums/t/for-loop-increments-at-0-simulation-time/40174/3):_

$realtime returns current time step expressed as a real number in the timescale of the current scope, not the elapsed CPU time it takes to execute the simulation.
