# Clock generation through UVC

**URL:** <https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995>\
**Category:** UVM\
**Tags:** UVM, uvc, clock\
**Created:** [June 29, 2016, 7:44am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995 "2016-06-29T07:44:59Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![louis-alexandre.latchimy](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/ba9def/32.png) [@louis-alexandre.latchimy](https://verificationacademy.com/forums/u/louis-alexandre.latchimy)\
**Post date:** [June 29, 2016, 7:44am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/1 "2016-06-29T07:44:59Z")

</div>

Hi everybody,

I would like to know if a clock generation is possible through a dedicated UVC. I have already written some code to describe the agent and the driver. However, it doesn’t work. It seems like the driver does not consume time in its run phase:

```auto
bit state = STARTING_SIGNAL_LEVEL;
vif.clock <= state;

forever begin
    J_ = $urandom_range(2,1) == 2 ? JITTER/100 : -JITTER/100;
    #(((NOMINAL_PERIOD/2)*(DUTY_CYCLE/100)+J_) * 1ps);
    state = ~state;
    vif.clock <= state;
    #(((NOMINAL_PERIOD/2)*((1-DUTY_CYCLE)/100)+J_) * 1ps);
    state = ~state;
    vif.clock <= state;
end

```

If you have any hints, I’ll be glad to hear them.

Thanks.

---

<div class="post-metadata">

**Author:** ![rohitk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@rohitk](https://verificationacademy.com/forums/u/rohitk)\
**Post date:** [June 29, 2016, 8:11am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/2 "2016-06-29T08:11:10Z")

</div>

_In reply to [louis-alexandre.latchimy](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995):_

I always use clock UVC and clock generation part is coded in clock interface itself. Agent sets random frequency to be used and then there are multiple sequences available to start/stop clock. driver gets the command and calls task in interface to start or stop clock accordingly. clock interface looks like this:

```auto
interface clk_if;
output clk;
bit clk_on;
int clk_high;
int clk_low;

always begin
if (clk_on) begin
   clk = 1;
   #clk_high;
   clk = 0;
   #clk_low;
end
end

task start_clock()
 clk_on = 1;
endtask
endinterface

```

---

<div class="post-metadata">

**Author:** ![louis-alexandre.latchimy](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/ba9def/32.png) [@louis-alexandre.latchimy](https://verificationacademy.com/forums/u/louis-alexandre.latchimy)\
**Post date:** [June 29, 2016, 8:21am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/3 "2016-06-29T08:21:35Z")

</div>

_In reply to [rohitk](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/2):_

Thanks. I’ll give it a try.

---

<div class="post-metadata">

**Author:** ![louis-alexandre.latchimy](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/ba9def/32.png) [@louis-alexandre.latchimy](https://verificationacademy.com/forums/u/louis-alexandre.latchimy)\
**Post date:** [June 29, 2016, 9:31am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/4 "2016-06-29T09:31:14Z")

</div>

_In reply to [rohitk](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/3):_

Unfortunately, simulation hang on:

```auto
if (clk_on) begin

```

Have I done something wrong ?

```auto
interface clk_if;
	bit clock;
	bit enable;
	bit STARTING_SIGNAL_LEVEL;
	int CLK_HIGH;
	int CLK_LOW;
	
	always begin
		if(enable) begin
			clock = STARTING_SIGNAL_LEVEL;
			#CLK_HIGH;
			clock = ~STARTING_SIGNAL_LEVEL;
			#CLK_LOW;
		end
	end
	
	task start_clock();
		enable = 1'b1;
	endtask: start_clock
	
	task stop_clock();
		enable = 1'b0;
	endtask: stop_clock
endinterface: clk_if

```

---

<div class="post-metadata">

**Author:** ![rohitk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@rohitk](https://verificationacademy.com/forums/u/rohitk)\
**Post date:** [June 29, 2016, 9:46am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/5 "2016-06-29T09:46:45Z")

</div>

_In reply to [louis-alexandre.latchimy](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/4):_

can you load waveform to check clock is getting generated or not. Adding some prints also will help in debugging. Also I don’t see any code to program CLK\_HIGH and CLK\_LOW duration, this needs to be set before start\_clock is called.

---

<div class="post-metadata">

**Author:** ![louis-alexandre.latchimy](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/ba9def/32.png) [@louis-alexandre.latchimy](https://verificationacademy.com/forums/u/louis-alexandre.latchimy)\
**Post date:** [June 29, 2016, 10:08am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/6 "2016-06-29T10:08:15Z")

</div>

_In reply to [rohitk](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/5):_

I set CLK\_HIGH and CLK\_LOW in the start\_of\_simulation phase of my driver as follow:

```auto
function void start_of_simulation_phase(uvm_phase phase);
    super.start_of_simulation_phase(phase);

    vif.STARTING_SIGNAL_LEVEL = STARTING_SIGNAL_LEVEL;
    vif.CLK_HIGH	= ((NOMINAL_PERIOD/2)*(DUTY_CYCLE/100));
    vif.CLK_LOW = ((NOMINAL_PERIOD/2)*((1-DUTY_CYCLE)/100));
    /*
    STARTING_SIGNAL_LEVEL, NOMINAL_PERIOD and DUTY_CYCLE come from
    a configuration object
    */
endfunction: start_of_simulation_phase

```

Nothing is generated in the waveform.

---

<div class="post-metadata">

**Author:** ![rohitk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@rohitk](https://verificationacademy.com/forums/u/rohitk)\
**Post date:** [July 1, 2016, 4:38am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/7 "2016-07-01T04:38:13Z")

</div>

_In reply to [louis-alexandre.latchimy](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/6):_

I don’t see any issue with the code and similar approach works fine for me. For debugging you can try replacing #CLK\_HIGH and #CLK\_LOW with some constant, add some print in start\_clock to make sure sequence is behaving as intended. If possible try posting a sample code on what you are doing , it will be easier to help then.

Rohit

---

<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:** [July 1, 2016, 5:01am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/8 "2016-07-01T05:01:29Z")

</div>

_In reply to [rohitk](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/7):_  
The problem with the always blocks in your interface is that you have a race condition with the driver setting the values to be used as delays. If the always blocks inside the interface start first and enable (or clk\_on) are 0, you get into a zero-delay loop and you’ll be stuck in the always block. better to code this as

```verilog
always begin
        wait(enable)
        clock = STARTING_SIGNAL_LEVEL;
        #CLK_HIGH
        clock = ~STARTING_SIGNAL_LEVEL;
	#CLK_LOW;
    end

```

---

<div class="post-metadata">

**Author:** ![rohitk](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a698b9/32.png) [@rohitk](https://verificationacademy.com/forums/u/rohitk)\
**Post date:** [July 1, 2016, 5:29am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/9 "2016-07-01T05:29:24Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/8):_

Yup that’s the issue, even in my UVC I have a wait statement, I somehow overlooked it when posted example code for Alex. Thanks Dave for catching this.

```auto
always begin
        wait (clk_en == 1);
	while (clk_en) begin
	    clk = 1;
	    #clk_high_period;
	    clk = 0;
	    #clk_low_period;
	end
    end

```

---

<div class="post-metadata">

**Author:** ![louis-alexandre.latchimy](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/ba9def/32.png) [@louis-alexandre.latchimy](https://verificationacademy.com/forums/u/louis-alexandre.latchimy)\
**Post date:** [July 1, 2016, 7:19am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/10 "2016-07-01T07:19:26Z")

</div>

_In reply to [rohitk](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/9):_

Thanks guys.

---

<div class="post-metadata">

**Author:** ![Anudeep\_J](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c68b51/32.png) [@Anudeep\_J](https://verificationacademy.com/forums/u/Anudeep_J)\
**Post date:** [July 23, 2020, 6:27am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/11 "2020-07-23T06:27:33Z")

</div>

Hi Everyone,

The solution for controlling one clock looks fine. Lets say my clock generator block has multiple clocks(like 10 different clocks) in different interfaces. How can I control those clocks from my test bench to start/stop or any other event has occurred at any point of time?

Thanks,  
Anudeep

---

<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:** [July 23, 2020, 4:16pm UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/12 "2020-07-23T16:16:17Z")

</div>

_In reply to [Anudeep J](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/11):_

What’s not shown in [Rohit’s example](https://verificationacademy.com/forums/uvm/clock-generation-through-uvc#reply-55342) is that there is a clock agent sending sequence items to a driver controlling the clock generation. From your testbench perspective, it’s no different that controlling any other data interface.

If your 10 clock generators are independent, but all have the same functionality. you can replicate the clock agent for each clock.

---

<div class="post-metadata">

**Author:** ![Anudeep\_J](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c68b51/32.png) [@Anudeep\_J](https://verificationacademy.com/forums/u/Anudeep_J)\
**Post date:** [July 24, 2020, 3:03am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/13 "2020-07-24T03:03:19Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/12):_

Thanks Dave.

All my clock generators have same functionality. They can be started/stopped at any point of time and can be frequency changed. Sometimes based on an event I have to start/stop in between the simulation. The above solution works good if it was an agent. But all my clocks are residing inside a module. It was not built in an agent as Iam restricted from doing that for clock generation. In this case, How would I approach?

---

<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:** [July 24, 2020, 4:40pm UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/14 "2020-07-24T16:40:56Z")

</div>

_In reply to [Anudeep J](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/13):_

You haven’t explained how one controls this clock generation module from any kind of testbench, be it a simple Verilog TB. Is it through module ports, calling tasks, or just setting variables through hierarchical reference?

In most cases, you should be able to use the bind construct to insert a module or interface into your clock generation module that reaches upwards to call tasks, or set variables in that module. For example

```verilog
module clockgen (output bit clk);
  real period=50/1ns;
  always #(1ns*period/2) clk=!clk;
endmodule

module clockgen_binder;
  function void set_period(real p);
     clockgen.period = p;
  endfunction
endmodule

module tb_top;
...
  bind clockgen clockgen_binder cgb();
...
endmodule

```

Then read my [DVCon paper](https://verificationacademy.com/forums/downloads/updated-example-code-dvcon-paper-missing-link-testbench-dut-connection) on how to connect UVM drivers to modules using abstract classes.

---

<div class="post-metadata">

**Author:** ![Anudeep\_J](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c68b51/32.png) [@Anudeep\_J](https://verificationacademy.com/forums/u/Anudeep_J)\
**Post date:** [July 25, 2020, 5:46am UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/15 "2020-07-25T05:46:34Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/14):_  
Thanks Dave for the reply.

I will be more clear on my question. I have 10 different interfaces and each interface has a clock signal. All these clocks are driven from a module “clock\_generator”. During my simulation, based on some events I have to start some of the clocks, I have to stop some of them or some of the clocks needs frequency change. We have multiple ways of doing it, but Iam looking for a best approach of implementing it, so that I can control them in a simpler way. Control here refers to stopping/starting the specific clock or set of clocks from any uvm\_components and top module.

---

<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:** [March 9, 2022, 10:39pm UTC](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/16 "2022-03-09T22:39:54Z")

</div>

_In reply to [Niranjan Kumar Naidu](https://verificationacademy.com/forums/t/clock-generation-through-uvc/31995/15):_

See:

[https://verificationacademy.com/forums/uvm/how-generate-clock-driver#reply-38287](https://verificationacademy.com/forums/uvm/how-generate-clock-driver#reply-38287)
