# Repeat or for loop in assertion

**URL:** <https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317>\
**Category:** SystemVerilog\
**Tags:** assertion\
**Created:** [August 26, 2024, 7:20am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317 "2024-08-26T07:20:45Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brurya](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@Brurya](https://verificationacademy.com/forums/u/Brurya)\
**Post date:** [August 26, 2024, 7:20am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/1 "2024-08-26T07:20:45Z")

</div>

Hi all,

I want to write an assertion with this flow:  
A is going down for 100ns.  
B toggle 20 times, only when A in low.  
I need to check that the last edge of B happen at least 20ns before A go to high.

How can I catch the last edge?  
Can I write a loop of 20 iterations?

Tried some options of loops, but nothing worked.

Thanks

---

<div class="post-metadata">

**Author:** ![hdlcohen](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hdlcohen](https://verificationacademy.com/forums/u/hdlcohen)\
**Post date:** [August 26, 2024, 4:40pm UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/2 "2024-08-26T16:40:25Z")

</div>

If you don’t have a clock for signals A and B, you can use tasks.  
If you have clocks, then you can use SVA  
I am showing 2 untested solutions, but they look OK

```auto
// A is going down for 100ns.
// B toggle 20 times, only when A in low.
// I need to check that the last edge of B happen at least 20ns before A go to high.
bit a, b;
task automatic toggle;
  $realtime b_now;
  b_now=$realtime;
  int count;
  fork
    begin // check b
      forever begin // while (a==0)
        @(b) count++ ;
        b_now=$realtime;
      end
    end

    begin
      @(posedge a) 
      am_tdiff: assert($realtime - b_now == 20ns);
      am_numb: assert(count==20);
     end
   join_any
 endtask

   always @(negedge a) toggle(); 
//-----------
   bit a, b, clk; 
   property p; 
    realtime v; 
    int count;
    @(posedge clk) ($fell(a), v=$realtime, count=0) |->
           (@(clk) (1, v=$realtime)[*1:$] intersect a[->1]) ##0
             $realtime - b_now == 20ns ##0 count==20;
   endproperty

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Brurya](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@Brurya](https://verificationacademy.com/forums/u/Brurya)\
**Post date:** [September 4, 2024, 8:17am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/3 "2024-09-04T08:17:53Z")

</div>

Thanks,  
I tried to do it, but understand that the require is different a bit.  
A is going down - don’t know for how long.  
B should toggle 22 times  
I need to check that from last edge of B there are at least 8ns before A will be high.

Tried to rewrite the code you gave, but still not work to me.

could you help me please?  
Thanks.

---

<div class="post-metadata">

**Author:** ![hdlcohen](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hdlcohen](https://verificationacademy.com/forums/u/hdlcohen)\
**Post date:** [September 4, 2024, 8:33pm UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/4 "2024-09-04T20:33:21Z")

</div>

Show your code and testbench  
Explain the requirements in more details.

---

<div class="post-metadata">

**Author:** ![Brurya](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@Brurya](https://verificationacademy.com/forums/u/Brurya)\
**Post date:** [September 5, 2024, 10:48am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/5 "2024-09-05T10:48:07Z")

</div>

This is the code, I’m sure something wrong, but don’t know what.

the requires:  
I’m implementing SPI protocol, once CS fall, the SCLK toggle 22 times and after CS rise again.  
I want to check from last edge of the clock, CS is stable at least for 8ns.

Thanks

```auto
module spi_assertios ();
 logic assert_clk = 0;
    always #1ns assert_clk = ~assert_clk;

 property wait_clock(); 
        realtime v,b; 
        int count;
        (1, v=$realtime, count=0) |->
               (@(posedge spi_qspi_vif.SCLK) (1, b=$realtime)[*1:$] intersect spi_qspi_vif.CS[->1]) ##0
                 $realtime - b == 8ns ##0 count==22;
                 @(negedge spi_qspi_vif.SCLK);
   endproperty

assert_Thss : assert property (@(posedge assert_clk) @(negedge spi_qspi_vif.CS) -> (wait_clock()) |-> )$display("Assertion assert_Thss Passed");
                 else $display("Assertion assert_Thss Failed"); 

endmodule

```

---

<div class="post-metadata">

**Author:** ![Brurya](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/f07891/32.png) [@Brurya](https://verificationacademy.com/forums/u/Brurya)\
**Post date:** [September 12, 2024, 5:54am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/6 "2024-09-12T05:54:02Z")

</div>

who can please help me?

---

<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:** [September 16, 2024, 10:17pm UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/7 "2024-09-16T22:17:03Z")

</div>

Show us a simple module with no interfaces. Generate 2 sets of stimulus: one that passes and one that should fail.

---

<div class="post-metadata">

**Author:** ![nitin62goel](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@nitin62goel](https://verificationacademy.com/forums/u/nitin62goel)\
**Post date:** [August 13, 2025, 7:31am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/8 "2025-08-13T07:31:35Z")

</div>

> [@hdlcohen](#):
>
> ```auto
> bit a, b, clk; 
> property p; 
> realtime v; 
> int count;
> @(posedge clk) ($fell(a), v=$realtime, count=0) |->
> (@(clk) (1, v=$realtime)[*1:$] intersect a[->1]) ##0
> $realtime - b_now == 20ns ##0 count==20;
> endproperty
> 
> ```

Hi Ben,  
where is count changes in the property you shared  
bit a, b, clk;  
property p;  
realtime v;  
int count;  
@(posedge clk) ($fell(a), v=$realtime, count=0) |-\>  
(@(clk) (1, v=realtime)[\*1:] intersect a[-\>1]) ##0  
$realtime - b\_now == 20ns ##0 count==20;  
endproperty

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [August 13, 2025, 3:26pm UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/9 "2025-08-13T15:26:57Z")

</div>

Sorry, it should have been

```auto
bit a, b, clk; 
   property p; 
    realtime v; 
    int count;
    @(posedge clk) ($fell(a), v=$realtime, count=0) |->
           (@(clk) (1, v=$realtime, cout++)[*1:$] intersect a[->1]) ##0
             $realtime - b_now == 20ns ##0 count==20;
   endproperty

```

---

<div class="post-metadata">

**Author:** ![LFT](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/34f0e0/32.png) [@LFT](https://verificationacademy.com/forums/u/LFT)\
**Post date:** [August 13, 2025, 5:59pm UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/10 "2025-08-13T17:59:03Z")

</div>

> [@user49](#):
>
> Sorry, it should have been
> 
> ```auto
> bit a, b, clk; 
> property p; 
> realtime v; 
> int count;
> @(posedge clk) ($fell(a), v=$realtime, count=0) |->
> (@(clk) (1, v=$realtime, count++)[*1:$] intersect a[->1]) ##0
> $realtime - b_now == 20ns ##0 count==20;
> endproperty
> 
> ```

Ben, two quick questions  
(1) As the requirement in “B toggle 20 times, only when A in low”, **shouldn’t ‘b’ be part of lhs of intersect operator ?**

(2) Can lhs & rhs of intersect operator have **different clocking event**?

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [August 13, 2025, 8:26pm UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/11 "2025-08-13T20:26:37Z")

</div>

1. rephrase the requirements

(2) Can lhs & rhs of intersect operator have **different clocking event**?  
No. But, OK FOR THIS

```auto
@(posedge clk0) x ##0 @(posedge clk1)
(seq1 intersect seq2) ##0 @(posedge clk0) seq3

```

---

<div class="post-metadata">

**Author:** ![LFT](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/34f0e0/32.png) [@LFT](https://verificationacademy.com/forums/u/LFT)\
**Post date:** [August 15, 2025, 4:29pm UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/12 "2025-08-15T16:29:07Z")

</div>

> [@user49](#):
>
> 1. rephrase the requirements

Hi Ben,  
(A) The requirement at the top of this thread mentions

> A is going down for 100ns.  
> B toggle 20 times, only when A in low.  
> I need to check that the last edge of B happens at least 20ns before A go to high.

As we want to check that ‘b’ should toggle 20 times when ‘a’ is low,  
**shouldn’t we check for ‘b’ in lhs of intersect operator ?**

```auto
property p; 
    realtime v; 
    int count;
    @(posedge clk) ($fell(a), v=$realtime, count=0) |->
           (@(clk) <seq_expr_involving_b>[*1:$] intersect a[->1]) ##0 $realtime - b_now == 20ns ##0 count==20;
   endproperty

```

The requirement that ‘b’ should toggle 20 times when ‘a’ is low has 2 possible interpretations  
(1) Change in ‘b’ is is checked on each posedge of clk  
(2) Change in ‘b’ is is detected asynchronously

Limitation with (1) is that **if ‘b’ were to toggle twice b/w 2 posedges of clk Tx & Ty,**  
sampled value of ‘b’ at Ty would be same as the previous clock Tx (as ‘b’ changed twice)

**I feel a more robust solution would be (2) as it detects transition in ‘b’ between 2 clock edge as well**

(B)

> [@user49](#):
>
> (2) Can lhs & rhs of intersect operator have **different clocking event**?  
> [Ben] No.

I found an [older post](https://verificationacademy.com/forums/t/using-multi-clocked-intersect-operator/42219/12) where the logical reasons behind legality are mentioned.  
**For multiclocked ‘intersect’, it’s acceptable if the lhs & rhs sequence start at different clocks (similar to multiclocked ‘and’)**  
**However, the issue with multiclocked ‘intersect’ is that the ending of the 2 sequences can’t be guaranteed with non-identical clocks (for multiclocked ‘and’ the 2 sequences could end at different clocks)**

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [August 15, 2025, 9:19pm UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/13 "2025-08-15T21:19:20Z")

</div>

See [http://systemverilog.us/vf/a20bnota.pdf](http://systemverilog.us/vf/a20bnota.pdf)

```auto
task automatic t(); 

    int count=0, pass=0;

    realtime vnow=$realtime, vend;

    fork 

      begin @(posedge a); end

      begin forever begin

          @(b) count++; 

          vend=$realtime;

         end 

      end      

    join_any;

    am_ab: assert($realtime-vend <- 20ns && count ==20);

    am_a2a: assert($realtime-vnow == 100ns);

  endtask

  always @(posedge a) t(); 

```

I found an [older post]

> [@Using Multi-clocked intersect operator](https://verificationacademy.com/forums/t/using-multi-clocked-intersect-operator/42219/12):
>
> In reply to [ben@SystemVerilog.us](https://verificationacademy.com/forums/t/using-multi-clocked-intersect-operator/42219/11): Would like to add a point related to throughout operator being illegal as a multi-clocked property. Syntax for throughout operator as per Syntax 16-11 : expression\_or\_dist throughout sequence\_expr i.e LHS of throughout operator can’t be a sequence or sub-sequence . Only a signal or expression is valid as LHS. As a result ‘throughout’ operator as a multi-clocked property is illegal. To summarize multi-clocked properties : 1) Using and/or operator is Legal…

where the logical reasons behind legality are mentioned.  
Correct, multiclocking is is illegal in intersect  
ap: assert property(@ (posedge clk) a[\*5] and @(b) b[\*4] );  
Directive ‘ap’ has multiple leading clocks for its maximal property.

ap: assert property(@ (posedge clk) a[\*5] and @(posedge clk) 1 ##0 @(b) b[\*4] );  
OK

[SystemVerilog.us](http://SystemVerilog.us)

---

<div class="post-metadata">

**Author:** ![Have\_A\_Doubt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@Have\_A\_Doubt](https://verificationacademy.com/forums/u/Have_A_Doubt)\
**Post date:** [August 16, 2025, 8:52am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/14 "2025-08-16T08:52:04Z")

</div>

> [@user49](#):
>
> See [http://systemverilog.us/vf/a20bnota.pdf](http://systemverilog.us/vf/a20bnota.pdf)

Ben,  
On testing the code from above pdf

```auto
bit clk , a = 1 , b ;
  
always #5 clk = !clk;

property p; 
 realtime v; 
 int count;
 ($fell(a), v=$realtime, count=0) |-> @(clk) ( ($changed(b), count++)[*1:$] intersect a[->1] )
 ##0 @(posedge clk) $realtime - v == 20ns ##0 count==20;
 endproperty

ap:assert property( @(posedge clk) p);

initial begin
 #14; a = 0; // Antecedent matches at T:15 & consequent starts evaluation
 #06; b = !b; // T: 20 'b' toggles for the 1st time
 // Assume that 'b' toggles 19 more times before 'a' is asserted
 #2 $finish();
end

```

Assertion fails at T:15 as lhs of intersect is false whereas rhs matches due to !a[\*1]  
( RHS `a[->1]` is equivalent to `!a[$0:$] ##1 a` )  
For intersect to pass **both lhs & rhs should match at T:15 i.e the starting clocking event**  
I believe property ‘p’ is incorrect since it expects **$changed(b) to match on every clocking event i.e @(clk)**

Since this thread already has a lot of replies I am thinking to create a new one to discuss possible solution using concurrent assertion.

Will share the link here once I post it

EDIT :: [using-go-to-v-s-non-consecutive-repetition-within-intersect-operator](https://verificationacademy.com/forums/t/using-go-to-v-s-non-consecutive-repetition-within-intersect-operator/51309)

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [August 17, 2025, 2:28am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/15 "2025-08-17T02:28:01Z")

</div>

```auto
 ($fell(a), v=$realtime, count=0) |-> @(clk) ( ($changed(b), count++)[*1:$] intersect a[->1] )
 ##0 @(posedge clk) $realtime - v == 20ns ##0 count==20;

```

Asserting on an illegal multiply clocked temporal expression. The clock flow cannot change in the RHS of ‘intersect’ operator.

---

<div class="post-metadata">

**Author:** ![Have\_A\_Doubt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@Have\_A\_Doubt](https://verificationacademy.com/forums/u/Have_A_Doubt)\
**Post date:** [August 17, 2025, 5:26am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/16 "2025-08-17T05:26:29Z")

</div>

Ben,

> [@user49](#):
>
> ```auto
> ($fell(a), v=$realtime, count=0) |-> @(clk) ( ($changed(b), count++)[*1:$] intersect a[->1] )
> ##0 @(posedge clk) $realtime - v == 20ns ##0 count==20;
> 
> ```
> 
> Asserting on an illegal multiply clocked temporal expression. The clock flow cannot change in the RHS of ‘intersect’ operator.

On [edalink](https://www.edaplayground.com/x/8ULs) I don’t see the compilation/elaboration error that you have mentioned.  
I observe the fail action block executes at T:15 as per expectations for the two stimulus  
Am I missing something ?

---

<div class="post-metadata">

**Author:** ![LFT](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/34f0e0/32.png) [@LFT](https://verificationacademy.com/forums/u/LFT)\
**Post date:** [August 17, 2025, 9:58am UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/17 "2025-08-17T09:58:14Z")

</div>

> [@user49](#):
>
> See [http://systemverilog.us/vf/a20bnota.pdf](http://systemverilog.us/vf/a20bnota.pdf)
> 
> ```auto
> task automatic t(); 
> int count=0, pass=0;
> realtime vnow=$realtime, vend;
> 
> fork 
> 
> begin @(posedge a); end
> 
> begin forever begin
> @(b) count++; 
> vend=$realtime;
> end 
> end      
> 
> join_any;
> 
> am_ab: assert($realtime-vend <- 20ns && count ==20);
> 
> am_a2a: assert($realtime-vnow == 100ns);
> 
> endtask
> 
> always @(posedge a) t(); 
> 
> ```

Hi Ben,  
Wanted to check with you on 2 quick changes

(1) As the task t() should be called **only when ‘a’ is deasserted, shouldn’t we write $fell(a) ?**

```auto
always@(posedge clk) begin
   if( $fell(a) ) // Infers clocking event from procedural block
         t(); // Call task 't' only when $fell(a) is true
endtask

```

(2) Within the task, **shouldn’t we write $rose(a,@(posedge clk)) instead of @(posedge clk) ?**

@(posedge clk) would unexpectedly unblock for 0 → X or 0 → Z transition whereas  
$rose(a,@(posedge clk) ) would be true only when there 0/X/Z to 1 transition

```auto
task automatic t(); 
    int count,pass; // Default value of 0
    realtime vnow = $realtime; // Used in 'alow_2_ahigh'
    realtime vend = 0; // Will be at default incase 'b' doesn't transition at all

    fork 
      begin 
         wait( $rose(a,@(posedge clk) ); // Replaced @(posedge a) !!
      end

      begin 
         forever begin
          @(b) count++; 
          vend = $realtime();
         end 
      end 
    join_any;

    disable fork; // Added
   
    if( count > 0 ) // True if 'b' toggled at least once
        // Check that 'b' toggled 20 times and last toggle of 'b' was at least 20ns prior to $rose(a) 
       am_ab: assert( ($realtime-vend >= 20ns) && count ==20);
    else
       $error("Signal b did not toggle between $fell(a) & $rose(a)");

    // Check that 'a' was de-asserted for 100ns
    alow_2_ahigh: assert($realtime-vnow == 100ns);

endtask

```

```auto

```

---

<div class="post-metadata">

**Author:** ![user49](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user49/32/4533_2.png) [@user49](https://verificationacademy.com/forums/u/user49)\
**Post date:** [August 17, 2025, 8:43pm UTC](https://verificationacademy.com/forums/t/repeat-or-for-loop-in-assertion/48317/18 "2025-08-17T20:43:01Z")

</div>

[You] (1) As the task t() should be called **only when ‘a’ is deasserted, shouldn’t we write $fell(a) ?**  
[Ben] Yes, minor mistake, buyt the approach is good

[You]

```auto
 ($fell(a), v=$realtime, count=0) |-> @(clk) ( ($changed(b), count++)[*1:$] intersect a[->1] )
 ##0 @(posedge clk) $realtime - v == 20ns ##0 count==20;

```

Asserting on an illegal multiply clocked temporal expression. The clock flow cannot change in the RHS of ‘intersect’ operator.

On edalink I don’t see the compilation/elaboration error that you have mentioned.  
[Ben] I have a tool that doesn’t like it.  
We don’t lalk about tools.  
In any case, these tyes of problems are not frequent and can be handled with automatic tasks/
