# To check clock toggling

**URL:** <https://verificationacademy.com/forums/t/to-check-clock-toggling/41486>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, assertion, clockgeneraation\
**Created:** [April 7, 2023, 11:25am UTC](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486 "2023-04-07T11:25:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kishankk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@Kishankk](https://verificationacademy.com/forums/u/Kishankk)\
**Post date:** [April 7, 2023, 11:25am UTC](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486/1 "2023-04-07T11:25:36Z")

</div>

Hi,  
What are the different ways to check clock is toggling or not when reset is enabled?  
(with and without assertions)

---

<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:** [April 7, 2023, 2:34pm UTC](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486/2 "2023-04-07T14:34:01Z")

</div>

_In reply to [Kishankk](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486):_

This link should give you 2 approaches you can take.  
[https://verificationacademy.com/forums/systemverilog/assertion-implementation#reply-111758](https://verificationacademy.com/forums/systemverilog/assertion-implementation#reply-111758)

Ben Cohen  
[Ben@systemverilog.us](mailto:Ben@systemverilog.us)  
Link to the list of papers and books that I wrote, many are now donated.

> **[Cohen\_Links\_to\_papers\_books.pdf](https://systemverilog.us/vf/Cohen_Links_to_papers_books.pdf)**
>
> 93.89 KB

or [Links\_to\_papers\_books - Google Docs](https://rb.gy/ibks5p)

---

<div class="post-metadata">

**Author:** ![Kishankk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@Kishankk](https://verificationacademy.com/forums/u/Kishankk)\
**Post date:** [April 8, 2023, 4:30am UTC](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486/3 "2023-04-08T04:30:29Z")

</div>

Thank you so much Ben for the assertion approach and your papers and books link.  
Much Useful!!!

Can you provide any other ways to check the clock toggling without assertions?

---

<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:** [April 8, 2023, 8:40pm UTC](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486/4 "2023-04-08T20:40:21Z")

</div>

_In reply to [Kishankk](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486/3):_  
That link

> **[Assertion implementation](https://verificationacademy.com/forums/systemverilog/assertion-implementation#reply-111758)**
>
> How to implement the following check through assertions: When the en signal is pulled low, the clk can still jump normally for a maximum of 3 times, and then it must be turned off, that is, it should remain unchanged.

provides a solution without SVA.

> Hi,  
> What are the different ways to check clock is toggling or not when reset is enabled?  
> (with and without assertions)

Using the same concepts as explained in my previous link

```verilog

// Modified to your requirements that for now are very vague. 
// If reset==0 then check clock toggling forever. Period of the clk==1/2 T
// If reset==1 then no toggle  
// 1) Using a task that can be called without a timeout 
   bit err0, err1; // for debug
   let T= 3; // T is a 1/2 period
   let T1= T+1; //T1, a bit more than T to capture a clk edge 
    task automatic t_clk_reset1();
       int count; 
       while(reset==1) begin 
         fork
          #T1; // a bit more than a half period 
          // If reset==1 then no toggle until reset==1 
          @(posedge clk) if(reset==1) count=count+1; 
           // should not count when reset==1
         join_any
         am_3clk_after_en: assert_final(reset==1 && count==0) else err1=err1+1;
         count=0; 
      end
    endtask

   task automatic t_clk_reset0();
     int count; 
     while(reset==0) begin
       fork
          #T1; // a bit more than a half period 
          // If reset==0 then check clock toggling .
           @(posedge clk) if(reset==0) count=count+1; // should count
       join_any
       am_3clk_after_en: assert_final(reset==0 && count>0) else err0=err0+1;
       count=0; 
     end
    endtask

    always @(reset) begin
      t_clk_reset1();
      t_clk_reset0();
    end

```

Getting started with verification with SystemVerilog

> **[Ben Cohen on LinkedIn: Road map to start with verification: SV, SVA , UVM and TB](https://www.linkedin.com/posts/ben-cohen-1172a0_road-map-to-start-with-verification-sv-activity-7050267628208062464-wnzN?utm_source=share&utm_medium=member_desktop)**
>
> Got this question: 
> Q: I need your advice in getting started with verification with systemverilog. I 've built various projects using Verilog and they was…

Ben Cohen  
[Ben@systemverilog.us](mailto:Ben@systemverilog.us)  
Link to the list of papers and books that I wrote, many are now donated.

> **[Cohen\_Links\_to\_papers\_books.pdf](https://systemverilog.us/vf/Cohen_Links_to_papers_books.pdf)**
>
> 141.32 KB

or [Links\_to\_papers\_books - Google Docs](https://rb.gy/ibks5p)

---

<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:** [April 8, 2023, 9:09pm UTC](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486/5 "2023-04-08T21:09:23Z")

</div>

_In reply to [ben@SystemVerilog.us](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486/4):_

DId an update above.  
This is untested, but it looks OK now.  
Tasks are fired upon a change in reset.  
Each task forks 2 processes, one is a fixed delay during which a clk event may occur and may update a count.  
Any of the processes, timeout or clocking event, conclude the fork and an immediate assertion check the count.  
The while repeats the test until a change in the reset, upon which new tasks are fired.

---

<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:** [April 10, 2023, 7:32pm UTC](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486/6 "2023-04-10T19:32:04Z")

</div>

_In reply to [ben@SystemVerilog.us](https://verificationacademy.com/forums/t/to-check-clock-toggling/41486/5):_  
TB for the solution if clock toggling or not toggling with reset

> **[EDA Playground](https://www.edaplayground.com/x/gK3M)**
>
> Edit, save, simulate, synthesize SystemVerilog, Verilog, VHDL and other HDLs from your web browser.

Note the firing of the tasks with a fork join\_none

```verilog

module m;
  bit reset, clk, tbclk, a=1, ferror; 
  int err0, err1; // for debug
  always #4 tbclk = !tbclk; // testbench clock 
  always #3 clk = !clk && ferror==0; 
  function void f(bit i);
      if(i==1) err1=err1+1;
      else err0=err0+1;
  endfunction
                    
  task automatic t_clk_reset1();
     int count; 
     while(reset==1) begin 
        fork
          #8; // time to detect a clk posedge 
              // If reset==1 then no toggle until reset==1 
          @(posedge clk) if(reset==1) count=count+1; 
              // should not count when reset==1
        join_any
        am_3clk_after_en: assert final(reset==0 || count==0) else f(1);
        count=0; 
      end
  endtask
              
  task automatic t_clk_reset0();
     int count; 
    while(reset==0) begin
      fork
        #8; // a bit more than a half period 
            // If reset==0 then check clock toggling .
        @(posedge clk) if(reset==0) count=count+1; // should count
      join_any
      am_3clk_after_en: assert final(reset==1 || count>0) else f(0);
      count=0; 
    end
  endtask
              
  always @(reset) begin // tas firing 
    fork 
      t_clk_reset1();
      t_clk_reset0();
    join_none
  end

  initial begin 
    $dumpfile("dump.vcd"); $dumpvars;
    // normal operation
    #1 reset=1; #1;
    reset=0; ferror=0; 
    repeat(5) @(posedge clk);
    reset=1; 
    #7 fork repeat(5) begin ferror=1; @(posedge clk); end join_none 
    repeat(5) @(posedge tbclk); 
    reset=0; 
    #7 fork repeat(5) begin ferror=1; @(posedge clk); end join_none 
    repeat(9) @(posedge tbclk); 
    ferror=0; 
    reset=0; #30 $finish;
  end
endmodule 

```
