How to generate clock from the driver

i have to generate my clock from the driver instead of top module.can we use interface signal as it is whil;e generating the clock.

task gen;
bit testclk = 0;
 
if (testmode_en ==1)
 Fork
    While (1)
     Begin
       #20 test_clk = ~test_clk;
       stc_IF.Testmode_clk  <= test_clk;
     end
join_any
end
endtask:gen

this code i have written in the driver…but clock is not generating .plz help me.

You have not shown nearly enough code for anyone to help you. Do you know if the while loop is even being executed?

This style of clock generation is very poor for performance. The clock signal is usually the most active signal in a simulation and you want the least amount of indirection. A much better suggestion is to put this looping task inside the interface and have the driver call the task.

In reply to dave_59:

Hi Dave,

Could you please explain a bit more on how is it different to have the clocks generated inside the interface tasks than the driver tasks?
Thanks in advance!

In reply to Heisenberg:

interface itf;
  bit inside_clk;
  int inside_clk_period=5ns;
  bit outside_clk;

  // clock generated inside interface
  always #(inside_clk_period/2) inside_clk = ! inside_clk;

endinterface

class driver;
...
virtual itf vif;
int period1,period2; // assume these are set from some configuration
function void driver::start_of_simulation(...);
   vif.inside_clk_period = period1;
endfunction
task driver::run_phase(...);
  fork // clock generated in driver
    forever #(period2/2) vif.outside_clk = ! vif.outside_clk;
  join_none
  ...
endtask

endclass

In reply to dave_59:

Dave,
Thanks a lot for your response. So, from the simulation performance point of view, the way inside_clk getting generated will have better performance compared to outside_clk and is recommended? Is my understanding correct?

In reply to Heisenberg:

Yes, direct references are usually more efficient than indirect references.

In reply to dave_59:

Thanks a lot Dave!