# Controlling clock generation

**URL:** <https://verificationacademy.com/forums/t/controlling-clock-generation/36399>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, clockgeneraation\
**Created:** [August 9, 2019, 9:31am UTC](https://verificationacademy.com/forums/t/controlling-clock-generation/36399 "2019-08-09T09:31:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhiseksarkar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/bbe5ce/32.png) [@Abhiseksarkar](https://verificationacademy.com/forums/u/Abhiseksarkar)\
**Post date:** [August 9, 2019, 9:31am UTC](https://verificationacademy.com/forums/t/controlling-clock-generation/36399/1 "2019-08-09T09:31:48Z")

</div>

i need to generate a clock that can be controlled by a signal driven by driver.  
my scenerio is like \>\> whenever CS will go low the clock start toggling.

i have implented a clock generation inside interface like…  
interface my\_interface;  
bit clk;  
bit start\_clk;

always  
begin  
if(start\_clk)  
#5 clk =~clk;  
end  
endinterface

inside driver run phase i have assinged the value of start\_clk like vif.start\_clk=1’b1;  
but its not working.

---

<div class="post-metadata">

**Author:** ![het](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@het](https://verificationacademy.com/forums/u/het)\
**Post date:** [August 9, 2019, 11:20am UTC](https://verificationacademy.com/forums/t/controlling-clock-generation/36399/2 "2019-08-09T11:20:21Z")

</div>

_In reply to [Abhisek Sarkar](https://verificationacademy.com/forums/t/controlling-clock-generation/36399):_

Hi Abhisek,

You should try something like as follows:

interface my\_interface;  
bit clk;  
bit start\_clk;

always@(start\_clk)  
begin  
while(start\_clk)  
begin  
if(!start\_clk)  
break;  
#5 clk =~clk;  
end  
end  
endinterface

It will work!

---

<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:** [August 9, 2019, 12:43pm UTC](https://verificationacademy.com/forums/t/controlling-clock-generation/36399/3 "2019-08-09T12:43:27Z")

</div>

_In reply to [Abhisek Sarkar](https://verificationacademy.com/forums/t/controlling-clock-generation/36399/2):_

```verilog

interface my_interface;
  bit clk;
  bit clk_enable;
  wire clk_out;
  
  initial begin
    clk = 1'b0;
    clk_enable = 1'b1;
    forever #5 clk = ~clk;
  end

  assign clk_out = clk & clk_enable;

  function clk_control(input bit ctrl);
    clk_enable = ctrl;
  endfunction

endinterface

```
