# Leaf level sequencer's stop\_sequences() is terminating virtual sequence which is running on virtual sequencer

**URL:** <https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612>\
**Category:** UVM\
**Tags:** UVM, Cookbook-SequencesStopping, Use-of-virtual-sequence-and-virtual-sequencer\
**Created:** [December 3, 2016, 11:41am UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612 "2016-12-03T11:41:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kerulmodi](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/kerulmodi/32/1996_2.png) [@kerulmodi](https://verificationacademy.com/forums/u/kerulmodi)\
**Post date:** [December 3, 2016, 11:41am UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/1 "2016-12-03T11:41:18Z")

</div>

Hi,  
I came across problem where stop\_sequences function of leaf level sequencer is terminating virtual sequence which is running on virtual sequencer.  
Let me more elaborate on this.

Here ,

- my\_seqr - leaf level sequencer
- my\_vir\_seqr - virtual sequencer
- my\_vir\_seq - virtual sequence
- my\_tr - leaf level transaction

Where my\_tr transaction is sent from virtual sequence (i.e. my\_vir\_seq) by using `uvm\_do\_on() macro to leaf level sequencer and it has been driven successfully and driver has done item\_done, then after certain delay , reset is being applied and leaf level sequencer (my\_seqr) is calling stop\_sequences function to kill sequence if any sequence is running on it. But at time of reset , there is no sequence running on leaf level sequencer. Only running sequence is virtual sequence which is running of virtual sequencer (my\_vir\_seqr) and it is not generating any kind of traffic on my\_seqr (leaf level sequencer), though stop\_sequences() is killing my\_vir\_seq.

**Snippet:**  
**Transaction Class:**

```verilog
class my_tr extends uvm_transaction;
  function new (string name = "my_tr");
    super.new(name);
  endfunction : new

  `uvm_object_utils(my_tr)
endclass : my_tr

```

**Leaf level Sequencer:**

```verilog
class my_seqr extends uvm_sequencer#(my_tr);

  virtual my_if my_vif;

  function new (string name = "my_seqr" , uvm_component parent = null);
    super.new(name,parent);
  endfunction : new 

  function void build_phase(uvm_phase phase);
   // get virtual interface by using uvm_config_db
  endfunction : build_phase

  task run_phase (uvm_phase phase);
    forever
    begin
      wait (my_vif.reset == 0);// wait for reset to be asserted.
      stop_sequences();
      wait (my_vif.reset == 1);// wait for reset to be de-asserted
    end
  endtask : run_phase

  `uvm_component_utils(my_seqr)

endclass : my_seqr

```

**Leaf level driver:**

```verilog
class my_drv extends uvm_driver#(my_tr);

  // ------- --- build_phase
  
  // ----- ---- connect phase

  task run_phase(uvm_phase phase);
    forever
    begin
      seq_item_port.get_next_item(req);
      #10; // some delay
      seq_item_port.item_done();
    end
  endtask : run_phase

endclass : my_drv

```

**Virtual Sequencer (top level sequencer):**

```verilog
class my_vir_seqr extends uvm_sequencer;

  my_seqr my_seqr_h; // handle of leaf level sequencer

  function new (string name = "my_vir_seqr",uvm_component parent = null);
    super.new(name,parent);
  endfunction : new

  `uvm_component_utils(my_vir_seqr)

endclass : my_vir_seqr

```

**Virtual sequence:**

```verilog
class my_vir_seq extends uvm_sequence;

  `uvm_object_utils(my_vir_seq)
  `uvm_declare_p_sequencer(my_vir_seq)
   
   process p_seq;

  function new (string name = "my_vir_seq" );
    super.new(name);
  endfunction : new 

  task body();
    my_tr tr;
    p_seq = process :: self();
    `uvm_info(get_name(),"sequence is running",UVM_LOW)
    `uvm_do_on( tr, p_sequencer.my_seqr_h); // send transaction on leaf level sequencer
      #1000; // Added delay to execute this sequence longer time
    `uvm_info(get_name(),"sequence is completed",UVM_LOW)
  endtask : body

endclass : my_vir_seq

```

**UVM Test:**

```verilog
class my_test extends uvm_test;

  task run_phase (uvm_phase phase);
    my_vir_seq vir_seq = my_vir_seq :: type_id :: create("vir_seq");
    // raise objection Here
    //-------
    // start sequence on virtual sequencer
    vir_seq.start(env.my_vir_sqr_h); 
    `uvm_info("PROCESS_STATE",$sformatf("%0s",vir_seq.p_seq.state()),UVM_LOW)

    // -------
    // drop objection here
  endtask : run_phase
endclass : my_test

```

**TOP file** :

```verilog
module top;

bit reset;

// interface declaration and connection

initial
begin
  reset = 1;
  #80; // adding delay
  reset = 0 ; // reset is applied
  #10; 
  reset = 1;
end

initial
  run_test();

endmodule : top

```

**OUTPUT:**

> UVM\_INFO@80[PROCESS\_STATE]: KILLED

```
I checked UVM code of uvm_sequencer and found that whenever **start_item** is being called (through `uvm_do_on()) that time , sequencer is storing parent sequence pointer inside reg_sequences[] associative array, Thus here my_vir_seq's pointer would be stored inside this array. But at time of **finish_item** , UVM is not un-registering that sequence pointer inside this array.

```

Thus I needed to unregister my virtual sequence pointer explicitly by calling below function after `uvm\_do\_on()

```verilog
// m_unregister_sequence
 // ---------------------
  
   function void uvm_sequencer_base::m_unregister_sequence(int sequence_id);
     if (!reg_sequences.exists(sequence_id))
       return;
     reg_sequences.delete(sequence_id);
   endfunction

```

So i dont understand why finish\_item is not un-registering sequence pointer of parent sequence. Is there a **BUG** in UVM?  
Should i consider that we should not generate/send leaf level transaction by using virtual sequence?

---

<div class="post-metadata">

**Author:** ![DigvijaysinhSuryavanshi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@DigvijaysinhSuryavanshi](https://verificationacademy.com/forums/u/DigvijaysinhSuryavanshi)\
**Post date:** [December 5, 2016, 9:52am UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/2 "2016-12-05T09:52:06Z")

</div>

_In reply to [kerulmodi](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612):_

Hi Kerul,

Following code is implemented inside leaf level sequencer’s run phase.

```auto
task run_phase (uvm_phase phase);
    forever
    begin
      wait (my_vif.reset == 0);// wait for reset to be asserted.
      stop_sequences();
      wait (my_vif.reset == 1);// wait for reset to be de-asserted
    end
  endtask : run_phase

```

Above logic can be easily taken care from test or virtual sequence.

Now coming to your question.

As per UVM reference manual,  
**stop\_sequences** method is exactly doing the thing for what it is meant for -

**stop\_sequences ::**  
Tells the sequencer to kill all sequences and child sequences currently operating on the  
sequencer, and remove all requests, locks and responses that are currently queued.

---

<div class="post-metadata">

**Author:** ![chr\_sue](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/chr_sue/32/1199_2.png) [@chr\_sue](https://verificationacademy.com/forums/u/chr_sue)\
**Post date:** [December 5, 2016, 10:38am UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/3 "2016-12-05T10:38:11Z")

</div>

_In reply to [kerulmodi](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/2):_

If you are following the UVM User Manual you do not have to take care for register/unregister any object.  
The key weakness I see in you code is you are using signals in a transaction level object (your sequencer). Furthermore you do not need any p\_sequencer declaration.  
Additionally it is unclear for me what are yxou doing with your process object.

---

<div class="post-metadata">

**Author:** ![kerulmodi](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/kerulmodi/32/1996_2.png) [@kerulmodi](https://verificationacademy.com/forums/u/kerulmodi)\
**Post date:** [December 5, 2016, 11:22am UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/4 "2016-12-05T11:22:16Z")

</div>

_In reply to [chr\_sue](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/3):_

> _In reply to [kerulmodi](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/3):_  
> If you are following the UVM User Manual you do not have to take care for register/unregister any object. Furthermore you do not need any p\_sequencer declaration.  
> Additionally it is unclear for me what are yxou doing with your process object.

I completely understand that we should not take care about register/unregister sequence inside sequencer. I have used this as patch to overcome sequence termination issue.

Process (p\_seq) is just used to indicate that virtual sequence body method has been killed. you can see output in quote.

To access leaf level sequencer (p\_sequencer.my\_seqr\_h), i had to declare virtual sequencer as p\_sequencer.

---

<div class="post-metadata">

**Author:** ![kerulmodi](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/kerulmodi/32/1996_2.png) [@kerulmodi](https://verificationacademy.com/forums/u/kerulmodi)\
**Post date:** [December 5, 2016, 11:25am UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/5 "2016-12-05T11:25:31Z")

</div>

_In reply to [DigvijayS](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/4):_

> stop\_sequences ::  
> Tells the sequencer to kill all sequences and child sequences currently operating on the  
> sequencer, and remove all requests, locks and responses that are currently queued.

As you have used word “ **currently** ” , let me clear you that at time of reset(when stop\_sequences()has been called) , no sequence is running on leaf level sequencer (my\_seqr).  
Thus it should not terminate any sequence.

---

<div class="post-metadata">

**Author:** ![DigvijaysinhSuryavanshi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@DigvijaysinhSuryavanshi](https://verificationacademy.com/forums/u/DigvijaysinhSuryavanshi)\
**Post date:** [December 5, 2016, 12:06pm UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/6 "2016-12-05T12:06:43Z")

</div>

_In reply to [kerulmodi](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/5):_

1).Actual sequence gets executed on leaf sequencer using below statement  
`uvm\_do\_on( tr, p\_sequencer.my\_seqr\_h);

2).Inside leaf sequencer below statement gets executed at @80 time-stamp as inside  
top module (reset=0) has been set after delay of #80 time-unit.  
…  
stop\_sequences();  
…

```auto
initial
begin
  reset = 1;
  #80; // adding delay
  reset = 0 ; // reset is applied
  #10; 
  reset = 1;
end

```

So,your sequence has been already started and executes for 80 time-units before stop\_sequences() function is being called.

Once this function is called **“currently”** running sequence on leaf sequencer gets killed.

---

<div class="post-metadata">

**Author:** ![kerulmodi](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/kerulmodi/32/1996_2.png) [@kerulmodi](https://verificationacademy.com/forums/u/kerulmodi)\
**Post date:** [December 5, 2016, 2:02pm UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/7 "2016-12-05T14:02:22Z")

</div>

_In reply to [DigvijayS](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/6):_

at time of reset , `uvm\_do\_on is already executed successfully so there is no transaction or sequence is going on into leaf level sequencer.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Alvarez](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/67e7ee/32.png) [@Jonathan\_Alvarez](https://verificationacademy.com/forums/u/Jonathan_Alvarez)\
**Post date:** [December 2, 2017, 10:51pm UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/8 "2017-12-02T22:51:38Z")

</div>

_In reply to [kerulmodi](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/7):_

Hi kerulmodi,  
I am having exactly the same problem. I do a reset and my virtual sequence is killed. I have seen that commenting stop\_sequences solves the problem. Did you find any other solution?  
Kind regards,  
Jonathan

---

<div class="post-metadata">

**Author:** ![Jonathan\_Alvarez](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/67e7ee/32.png) [@Jonathan\_Alvarez](https://verificationacademy.com/forums/u/Jonathan_Alvarez)\
**Post date:** [December 3, 2017, 8:29pm UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/9 "2017-12-03T20:29:35Z")

</div>

_In reply to [kerulmodi](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/8):_

Hi Kerulmodi,  
I think i have found the problem and a workaround to avoid commenting the **stop\_sequences** function.

As already described above, **stop\_sequences** will kill the sending sequence. However, as you noticed, it is not true that it will kill the sequence that is **currently** active in the sequencer. The **stop\_sequence** function has a sequence registration mechanism. It will register the sequence used for a sequencer once the “uvm\_send” has been sent from the sequence. No matter if the “uvm\_send” and the transaction has already been processed and the sequencer is not doing anything, if the sequence did “uvm\_send” over a sequencer, this sequence **will be registered**.

I understand, as you did, that this is not 100% OK but UVM do so. In my opinion, UVM should un-register that sequence once the driver of the sequencer has all objections dropped or is waiting for an item of the sequence. In other words, it should not kill an idle sequence.

OK. Now to the solutions.  
I guess that you want to use **stop\_sequence** to kill all unfinished sequence items from the sequence when a reset is monitored. This is the best reset handling approach i have seen in UVM, see this [https://www.embedded.com/print/4396934](https://www.embedded.com/print/4396934) for more information of this approach. Therefore, i assume that you want to keep your **stop\_sequence** () function in your code.

The workaround is simple, you just avoid using any UVM\_DO or any “uvm\_send” inside your virtual sequence. I explain. If you want to send your stimuli, then you need to create a sub-sequence (inside your virtual sequence) that wrap all the stimuli (“uvm\_send” to any sequencer that uses stop\_sequence). Doing that the registered and killed sequence (by stop\_sequences) will be the sub-sequence and not your virtual sequence. (For completation, if you use the UVM register model functions (read/write/mirror/update) each time you write to a register of the UVM, the register model creates a subsequence for you so you don’t have to worry about wrapping those read/write/mirror/update register logic functions, they do already right)

For your code, the following need to be done (approximately):

Go from your current Virtual sequence:

```verilog

class my_vir_seq extends uvm_sequence;
 
  `uvm_object_utils(my_vir_seq)
  `uvm_declare_p_sequencer(my_vir_seq)
 
   process p_seq;
 
  function new (string name = "my_vir_seq" );
    super.new(name);
  endfunction : new 
 
  task body();
    my_tr tr;
    p_seq = process :: self();
    `uvm_info(get_name(),"sequence is running",UVM_LOW)
    `uvm_do_on( tr, p_sequencer.my_seqr_h); // send transaction on leaf level sequencer
      #1000; // Added delay to execute this sequence longer time
    `uvm_info(get_name(),"sequence is completed",UVM_LOW)
  endtask : body
 
endclass : my_vir_seq

```

To the new Virtual sequence:

```verilog

typedef class my_sub_vir_seq;//forward declaration to be able to use this type inside my_vir_seq 
class my_vir_seq extends uvm_sequence;
 
  `uvm_object_utils(my_vir_seq)
  `uvm_declare_p_sequencer(my_vir_seq)
 
   process p_seq;
 
  function new (string name = "my_vir_seq" );
    super.new(name);
  endfunction : new 
 
  task body();    
    p_seq = process :: self(); 
    my_sub_vir_seq this_seq_has_all_stimuli_ready2bekilled;
    this_seq_has_all_stimuli_ready2bekilled = my_sub_vir_seq::type_id::create("this_seq_has_all_stimuli_ready2bekilled");
    this_seq_has_all_stimuli_ready2bekilled.start(p_sequencer.my_seqr_h);//the subsequence is started 
  endtask : body 
endclass : my_vir_seq

//sub sequence with stimuli definition that is used inside the virtual sequence
class my_sub_vir_seq extends uvm_sequence; 
  `uvm_object_utils(my_sub_vir_seq )
  `uvm_declare_p_sequencer(my_vir_seq)
 
   process p_seq;
 
  function new (string name = "my_sub_vir_seq " );
    super.new(name);
  endfunction : new 
 
  task body();
    my_tr tr;    
    `uvm_info(get_name(),"sequence is running",UVM_LOW)
    `uvm_do_on( tr, p_sequencer.my_seqr_h); // send transaction on leaf level sequencer
      #1000; // Added delay to execute this sequence longer time
    `uvm_info(get_name(),"sequence is completed",UVM_LOW)
  endtask : body
 
endclass : my_sub_vir_seq 

```

Of course, you can have in your virtual sequence the reset handling. In other words, you can for example detect a reset in your virtual sequence and restart your sub-sequence (the one with the stimuli) after reset or other stimuli.

I hope that this helps.  
Best regards,  
Jonathan

---

<div class="post-metadata">

**Author:** ![kerulmodi](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/kerulmodi/32/1996_2.png) [@kerulmodi](https://verificationacademy.com/forums/u/kerulmodi)\
**Post date:** [December 5, 2017, 1:43pm UTC](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/10 "2017-12-05T13:43:50Z")

</div>

_In reply to [Jonathan\_Alvarez](https://verificationacademy.com/forums/t/leaf-level-sequencers-stop-sequences-is-terminating-virtual-sequence-which-is-running-on-virtual-sequencer/32612/9):_

I appreciate that you went so deep for this problem and found an approach for solution.  
Whatever you understood is correct and that is aligned with my understanding.

Finally , i had to create another wrapper sequence which can send transactions to leaf level sequencer (legacy flow) to avoid this problem as you suggested.

Thanks again for sharing reset handling mechanism information.

Regards,  
Kerul Modi
