# How do I set constraints for a sequence within a sequence?

**URL:** <https://verificationacademy.com/forums/t/how-do-i-set-constraints-for-a-sequence-within-a-sequence/33086>\
**Category:** UVM\
**Tags:** UVM, layered-sequence, constraints\
**Created:** [April 27, 2017, 10:38pm UTC](https://verificationacademy.com/forums/t/how-do-i-set-constraints-for-a-sequence-within-a-sequence/33086 "2017-04-27T22:38:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jms8](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jms8](https://verificationacademy.com/forums/u/jms8)\
**Post date:** [April 27, 2017, 10:38pm UTC](https://verificationacademy.com/forums/t/how-do-i-set-constraints-for-a-sequence-within-a-sequence/33086/1 "2017-04-27T22:38:00Z")

</div>

I have a set of sequences and sequences of sequences, and I would like to be able to change the constraints for my lowest level sequence from the top level sequence, without modifying the lowest level sequence.

To make this a little more concrete, here is an example. A\_lseq\_signal contains a A\_seq\_stop\_signal, which itself is extended from A\_seq\_base which extends uvm\_sequence #(A\_tb\_txn). wait\_time is a randomizable int within A\_tb\_txn.

My goal is to be able to constrain wait\_time from A\_lseq\_signal. This code is the closest I’ve gotten and it’s giving me a null access error. You can also see my past attempts, which are commented out.

```verilog
class A_seq_base extends uvm_sequence #(A_tb_txn);

   // Constructor
   function new (string name = "A_seq_base");
      super.new(name);
   endfunction : new
   
   `uvm_object_utils(A_seq_base)
   `uvm_declare_p_sequencer(A_tb_sequencer)
    
   // UVM Sequence Body task
   virtual task body();
      
      `uvm_create(req)
      req.randomize();
      `uvm_send(req)

   endtask : body

endclass : A_seq_base

```

```verilog
class A_seq_stop_signal extends A_seq_base;

   // Constructor
   function new (string name = "A_seq_stop_signal");
      super.new(name);
   endfunction : new
   
   `uvm_object_utils(A_seq_stop_signal)
   
   // UVM Sequence Body task
   virtual task body();

      assert(req.randomize() with {req.A_command == A_instructions::STOP_SIGNAL;} );
      `uvm_send(req)

     // `uvm_do_on_with(req,p_sequencer,{req.A_command == A_instructions::STOP_SIGNAL;})

   endtask : body

endclass : A_seq_stop_signal

```

```verilog
class A_lseq_signal extends A_seq_base ;

   `uvm_object_utils(A_lseq_signal)

   A_seq_stop_signal stop_seq;

   // Constructor
   function new (string name = "A_lseq_signal");
      super.new(name);

      // Create the sub sequences
      stop_seq = A_seq_stop_signal::type_id::create("stop_seq");

   endfunction : new

virtual task body();

   assert(stop_seq.randomize() with {stop_seq.req.wait_time == 5;} );
   stop_seq.start(p_sequencer);

// assert(stop_seq.randomize() with {stop_seq.wait_time == 5;} );
// stop_seq.start(p_sequencer);

// `uvm_do_with(stop_seq, {stop_seq.wait_time==5;})

endtask : body            
   
endclass : A_lseq_signal 

```

---

<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:** [April 28, 2017, 4:25pm UTC](https://verificationacademy.com/forums/t/how-do-i-set-constraints-for-a-sequence-within-a-sequence/33086/2 "2017-04-28T16:25:23Z")

</div>

_In reply to [jms8](https://verificationacademy.com/forums/t/how-do-i-set-constraints-for-a-sequence-within-a-sequence/33086):_  
You are getting a null access error because req is not constructed at the time you randomize from the top level sequence. A\_seq\_stop\_signal does not construct req.

You should be using the UVM factory to add the constraint. The factory was specifically designed to allow you modify lower level classes from the top. I don’t have time right now to give you an example. but what you should do is extend a\_tb\_txn with the wait\_time constraint into another class. then set the override in the body of A\_lseq\_signal, and then call super.body(). Hopefully this will give you an idea of what to do.

---

<div class="post-metadata">

**Author:** ![shazra.audience.com](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@shazra.audience.com](https://verificationacademy.com/forums/u/shazra.audience.com)\
**Post date:** [April 28, 2017, 10:00pm UTC](https://verificationacademy.com/forums/t/how-do-i-set-constraints-for-a-sequence-within-a-sequence/33086/3 "2017-04-28T22:00:53Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/how-do-i-set-constraints-for-a-sequence-within-a-sequence/33086/2):_

Hi Dave,

If the body of A\_lseq\_signal class is changed to the code below:-

virtual task body();

super.body();  
`uvm\_do\_with(stop\_seq, {stop\_seq.req.wait\_time==5;})

endtask : body

Should this have worked?
