# Infinite Loop using While

**URL:** <https://verificationacademy.com/forums/t/infinite-loop-using-while/30511>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Infinite-loop\
**Created:** [April 1, 2015, 7:56am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511 "2015-04-01T07:56:27Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![haykp](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@haykp](https://verificationacademy.com/forums/u/haykp)\
**Post date:** [April 1, 2015, 7:56am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/1 "2015-04-01T07:56:27Z")

</div>

Dear Forum,

I am getting infite loop when using while statement. And cannot understand why I am getting infinite loop, please help understand:

```verilog

class Receiver ;
...
    task dutOutMonitor ();
        @ (posedge ifc.wr_doing ) 
        @ (posedge ifc.spi_cs ) 

        while (ifc.spi_cs == 1'b1 ) 
        begin
            @ (posedge ifc.spi_sck ) 
            dataIn = {dataIn, ifc.spi_dout};
        end

         @ (negedge ifc.spi_cs )
          this.ReceiveBox.put (dataIn ); 
          return;
    endtask
endclass

program execTest ();
initial 
begin: WRITE
     genObj.CONFIG();
     genObj.randomize();
     genObj.WRITE();
     receivObj.dutOutMonitor();    
end
endprogram

```

so when executing this code, I am getting infinite loop, even when the value of ifc.spi\_cs = 0.

Please help understand why I am having such behavioral?

---

<div class="post-metadata">

**Author:** ![raku](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@raku](https://verificationacademy.com/forums/u/raku)\
**Post date:** [April 1, 2015, 8:13am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/2 "2015-04-01T08:13:37Z")

</div>

_In reply to [haykp](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511):_

According to me Check following line might be hanging .

@ (negedge ifc.spi\_cs );

Once ifc.spi\_cs is equal to zero it might come out of while loop but to come out @ (negedge ifc.spi\_cs ) it requires ifc.spi\_cs to encounter a negedge not level sensitive zero.

---

<div class="post-metadata">

**Author:** ![haykp](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@haykp](https://verificationacademy.com/forums/u/haykp)\
**Post date:** [April 1, 2015, 8:32am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/3 "2015-04-01T08:32:43Z")

</div>

_In reply to [raku](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/2):_

Raku,

Actually I modified the code and put $display statements in while statement. During the simulation I can see that infitie loop occurs because of the while statement. My code enters in while statement and stays there forever

```verilog

class Receiver ;
...
    task dutOutMonitor ();
        @ (posedge ifc.wr_doing ) 
        @ (posedge ifc.spi_cs ) 
 
        while (ifc.spi_cs == 1'b1 ) 
        begin
            @ (posedge ifc.spi_sck ) 
              $display("Inside while: the value of ifc.spi_cs is %d", ifc.spi_cs);
        end
 
         @ (negedge ifc.spi_cs )
          this.ReceiveBox.put (dataIn ); 
          return;
    endtask
endclass
 
program execTest ();
initial 
begin: WRITE
     genObj.CONFIG();
     genObj.randomize();
     genObj.WRITE();
     receivObj.dutOutMonitor();    
end
endprogram

```

After simulation the simulator always prints this line:

```verilog
Inside while: the value of ifc.spi_cs is 1
Inside while: the value of ifc.spi_cs is 1

```

---

<div class="post-metadata">

**Author:** ![raku](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@raku](https://verificationacademy.com/forums/u/raku)\
**Post date:** [April 1, 2015, 8:37am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/4 "2015-04-01T08:37:35Z")

</div>

_In reply to [haykp](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/3):_

Try putting display statement immediately i.e. before @ (negedge ifc.spi\_cs ) after while loop and check . From this we will know whether it has exited while loop or still in while loop.

---

<div class="post-metadata">

**Author:** ![raku](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@raku](https://verificationacademy.com/forums/u/raku)\
**Post date:** [April 1, 2015, 9:17am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/5 "2015-04-01T09:17:18Z")

</div>

_In reply to [raku](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/4):_

Sorry i also checked that there is @ (posedge ifc.spi\_sck ) inside while loop . So this simulator run waits until it gets posedge then after that it immediately checks while loop condition which if equal to 1 at that particular movement then it may hang. so can you check this condition . i think due to this it getting stuck in while loop

---

<div class="post-metadata">

**Author:** ![Vinay\_Jain](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/vinay_jain/32/3037_2.png) [@Vinay\_Jain](https://verificationacademy.com/forums/u/Vinay_Jain)\
**Post date:** [April 1, 2015, 9:56am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/6 "2015-04-01T09:56:29Z")

</div>

_In reply to [raku](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/5):_

Put the display before clock edge and see if spi\_cs is 0 or not.

```verilog
 while (ifc.spi_cs == 1'b1 ) 
        begin
            $display("Inside while: the value of ifc.spi_cs is %d", ifc.spi_cs);
            @ (posedge ifc.spi_sck )              
        end

```

Also I think CS and synchronized with posedge of CLK thats why #Delay is not captured by simulation.  
Solution1 :

```verilog

while (ifc.spi_cs == 1'b1 ) 
        begin
            $display("Inside while: the value of ifc.spi_cs is %d", ifc.spi_cs);
            @ (negedge ifc.spi_sck )              
        end[

```

Solution2 :

```verilog

while (ifc.spi_cs == 1'b1 ) 
        begin
            $display("Inside while: the value of ifc.spi_cs is %d", ifc.spi_cs);
            @ (posedge ifc.spi_sck )              
            #0 //This will remove delta delay issue if its there.
        end[

```

---

<div class="post-metadata">

**Author:** ![haykp](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@haykp](https://verificationacademy.com/forums/u/haykp)\
**Post date:** [April 1, 2015, 10:10am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/7 "2015-04-01T10:10:48Z")

</div>

_In reply to [Vinay Jain](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/6):_

I have added $display statements in the code like the below:

```verilog

class Receiver ;
...
    task dutOutMonitor ();
        @ (posedge ifc.wr_doing ) 
        @ (posedge ifc.spi_cs ) 
 
        while (ifc.spi_cs == 1'b1 ) 
        begin
            $display ("1. Value of spi_cs is %d", ifc.spi_cs);
            @ (posedge ifc.spi_sck ) 
              $display ("2. Value of spi_cs is %d", ifc.spi_cs);
        end
             $display ("3. Value of spi_cs is %d", ifc.spi_cs);  
         @ (negedge ifc.spi_cs )
             $display ("4. Value of spi_cs is %d", ifc.spi_cs);
    endtask
endclass
 
program execTest ();
initial 
begin: WRITE
     genObj.CONFIG();
     genObj.randomize();
     genObj.WRITE();
     receivObj.dutOutMonitor();    
end
endprogram

```

And this is the output that I am getting

```verilog
===== INFO: Starting WRITE
Task: CONFIG
Task: WRITE
TASK: dutOutMonitor
1. Value of spi_cs is 1
2. Value of spi_cs is 1
1. Value of spi_cs is 1
2. Value of spi_cs is 1
1. Value of spi_cs is 1
2. Value of spi_cs is 1
1. Value of spi_cs is 1
... (all the same sequence)

```

I have a feeling, that when entering into while statement, it doesn’t check the external signals - ifc.spi\_cs. So to get out from the while, I need to modify the ifc.spi\_cs signal inside while cycle.

---

<div class="post-metadata">

**Author:** ![Vinay\_Jain](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/vinay_jain/32/3037_2.png) [@Vinay\_Jain](https://verificationacademy.com/forums/u/Vinay_Jain)\
**Post date:** [April 1, 2015, 11:46am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/8 "2015-04-01T11:46:50Z")

</div>

_In reply to [haykp](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/7):_

Can you add snapshot of waveform where spi\_cs & spi\_sck is present in waveform window.

---

<div class="post-metadata">

**Author:** ![cool\_cake20](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@cool\_cake20](https://verificationacademy.com/forums/u/cool_cake20)\
**Post date:** [April 1, 2015, 3:42pm UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/9 "2015-04-01T15:42:28Z")

</div>

_In reply to [Vinay Jain](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/8):_

Haykp,

```
  Does posedge ifc.spi_cs going low in your simulation? 

```

I tried to understand your requirement , why don’t you write in this way ?

```verilog

  task dutOutMonitor ();
     @ (posedge ifc.wr_doing );
     @ (posedge ifc.spi_cs );
      fork
         begin
	    forever
	      begin
		 @ (posedge ifc.spi_sck );
		 dataIn = {dataIn, ifc.spi_dout};
              end
         end
	 begin
            @ (negedge ifc.spi_cs );
	    this.ReceiveBox.put (dataIn ); 
         end
      join_any
  endtask

```

---

<div class="post-metadata">

**Author:** ![haykp](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@haykp](https://verificationacademy.com/forums/u/haykp)\
**Post date:** [April 2, 2015, 12:36am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/10 "2015-04-02T00:36:39Z")

</div>

_In reply to [cool\_cake20](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/9):_

Dear cool\_cake20 - your solution can also work, but I dont see its clear benefit over the while statement usage.

Vinay Jain, please see the waves screenshot in this link:

[Simulation Waves](https://236f4527-a-62cb3a1a-s-sites.googlegroups.com/site/ysulogicdesign/systemverilog_faq/whilestatementgoestoinfiniteloop/Waves.png?attachauth=ANoY7cpYTK7QY9-PKUthnyHFo9KQb923gp-sBEgImkZKH_KcRQdoluRxCDgoWOoWPkyf7amo4r7oAh72TUopvNnqeRCxeEgqPx4up-no5ullJAvzAeSzNAi_cJNk3-hh_WL9NetYk1rc6aZAQvYM7-mm0RZdJdAgaLrK_vb6rttzTymUepYDnTOeSQ2Ob6z2XimidHaqL3hc3qrhz4UqkjFwFKHSnSBoiDA91WmsGb81OkVp3pnNDyIlAZAnlWtIE9C2sBc0U89knB6Fs6q-n_oBradnDSVpgg%3D%3D&attredirects=0)

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [April 2, 2015, 1:03am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/11 "2015-04-02T01:03:34Z")

</div>

_In reply to [haykp](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/10):_

It’s most likely due to a race condition between spi\_sck and spi\_cs. You are essentially evaluating spi\_cs after the @(posedge spi\_sck). If it is 1, then it waits for the next @(posedge spi\_sck). If spi\_sck doesn’t have a posedge after spi\_cs is 0, then you will be stuck waiting for the nest @(posedge spi\_sck), which when that occurs, spi\_cs is back to the value of 1.

---

<div class="post-metadata">

**Author:** ![haykp](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@haykp](https://verificationacademy.com/forums/u/haykp)\
**Post date:** [April 2, 2015, 3:03am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/12 "2015-04-02T03:03:11Z")

</div>

_In reply to [cgales](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/11):_

cgales,

I agree with your statement! Truly, the code waits for @(posedge spi\_sck) signal for ages.  
Thank you very much for your explanation!

I think I need to try avoid using while statements, because of high chance of having race conditions. Could someone share his experience on using the while statements? and how you usually create iterations in initial blocks

---

<div class="post-metadata">

**Author:** ![Vinay\_Jain](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/vinay_jain/32/3037_2.png) [@Vinay\_Jain](https://verificationacademy.com/forums/u/Vinay_Jain)\
**Post date:** [April 2, 2015, 4:02am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/13 "2015-04-02T04:02:29Z")

</div>

_In reply to [haykp](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/12):_

After looking into Waveform, @cgales is correct when cs is low clk is not there.so it obvious it will stuck into while loop.

workaround - you can have one more clock which is one clock advance to spi\_clk,use this advance clk so that last posedge of adv.clock will detect the cs==0 condition and it will comeout from loop.

However its just a workaround not the good practice ;)

---

<div class="post-metadata">

**Author:** ![haykp](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@haykp](https://verificationacademy.com/forums/u/haykp)\
**Post date:** [April 2, 2015, 5:06am UTC](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/14 "2015-04-02T05:06:36Z")

</div>

_In reply to [Vinay Jain](https://verificationacademy.com/forums/t/infinite-loop-using-while/30511/13):_

Thank you Vinay Jain,

I implemented the solution proposed by cool\_cake20
