# How do I constrain data within hierarchy at test level?

**URL:** <https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937>\
**Category:** UVM\
**Tags:** UVM, constraints\
**Created:** [April 4, 2021, 3:29am UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937 "2021-04-04T03:29:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dragewood](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@Dragewood](https://verificationacademy.com/forums/u/Dragewood)\
**Post date:** [April 4, 2021, 3:29am UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/1 "2021-04-04T03:29:22Z")

</div>

Hi, I’m new to UVM and have a constraint/general test bench architecture question. I’m wondering how can I override my sequence\_item/transaction constraints (that are buried within my hierarchy)? I’m architecting this test bench such that the default constraints buried in the hierarchy have the “good” configuration (such that the DUT receives “good/expected” data). I want to have the atypical/variations on the “good/default” case occur in each test via overriding various constraints. I’m having trouble figuring out how to access these constraints at the test level. Below is the pseudocode of my hierarchy. In this example, how could I constrain “value” to something other than [0:200] at the test level?

class my\_data extends uvm\_object;  
rand bit [15:0] value;  
`uvm_object_utils_begin(my_data) `uvm\_field\_int(value, UVM\_ALL\_ON)   
`uvm\_object\_utils\_end   
constraint value\_c {value inside {[0:200]}; }  
endclass

class my\_transaction extends uvm\_sequence\_item;  
rand my\_data data\_obj;  
`uvm_object_utils_begin(my_transaction) `uvm\_field\_object(data\_obj, UVM\_ALL\_ON)   
`uvm\_object\_utils\_end   
endclass

class my\_sequence extends uvm\_sequence#(my\_transaction);  
task body();  
my\_transaction my\_tr;  
repeat(10) begin  
my\_tr = my\_transaction::type\_id::create(“my\_tr”);  
start\_item(my\_tr);  
assert(my\_tr.randomize());  
finish\_item(my\_tr);  
end  
endtask: body  
endclass

class my\_base\_test extends uvm\_test;  
task run\_phase(uvm\_phase phase);  
my\_sequence my\_seq;

```
    phase.raise_objection(.obj(this));
        my_seq = my_sequence::type_id::create("my_seq", this);
        assert(my_seq.randomize());
        my_seq.start(my_env.my_agnt.my_seqr);
    phase.drop_objection(.obj(this));
endtask: run_phase

```

endclass: my\_base\_test

---

<div class="post-metadata">

**Author:** ![tfitz](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/tfitz/32/3025_2.png) [@tfitz](https://verificationacademy.com/forums/u/tfitz)\
**Post date:** [April 5, 2021, 1:46pm UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/2 "2021-04-05T13:46:55Z")

</div>

_In reply to [Dragewood](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937):_

Instead of trying to manage the constraints explicitly, it would be easier to override data from the factory.

Incidentally, I think you have one too many layers of hierarchy. I see no benefit in declaring data as a separate object rather than just putting value directly into the sequence item:

```verilog

class my_transaction extends uvm_sequence_item;
rand bit [15:0] value;
`uvm_object_utils_begin(my_transaction)
`uvm_field_object(data_obj, UVM_ALL_ON)
`uvm_object_utils_end
endclass

```

And I’ll once again admonish you not to use the field macros.

---

<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:** [April 5, 2021, 2:06pm UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/3 "2021-04-05T14:06:40Z")

</div>

_In reply to [tfitz](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/2):_

It is a bad approach to implement constraints in the seq\_item definition. You should contrain data memebres of the seq\_item in the sequence.  
If you are using the constraints in the sequence item this constraint might conflict with constraints coming from the sequence.

---

<div class="post-metadata">

**Author:** ![tfitz](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/tfitz/32/3025_2.png) [@tfitz](https://verificationacademy.com/forums/u/tfitz)\
**Post date:** [April 5, 2021, 2:22pm UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/4 "2021-04-05T14:22:22Z")

</div>

_In reply to [chr\_sue](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/3):_

Suppose you want to run one test with value \< 100 and another with value \> 100. You could have two sequence\_items and use the factory to override the sequence\_item type and then have one sequence to generate the items. This would let you have a base test that starts the sequence and an extended test that just overrides the sequence\_item type and uses super.run to execute the same sequence.

Or you could have two sequences, each of which randomizes the sequence\_item using a ‘with’ clause. Then you could have two tests, each of which runs one sequence. If you make one sequence an extension of the other (ie overriding the body() method), then you could still have a base test and an extended test to override the sequence type.

Personally, I’m not a big fan of randomize-with when I can use the factory override. The override is particularly useful when you have multiple fields and multiple constraints that you may want to override.

---

<div class="post-metadata">

**Author:** ![harsh\_pandya](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/harsh_pandya/32/2973_2.png) [@harsh\_pandya](https://verificationacademy.com/forums/u/harsh_pandya)\
**Post date:** [April 5, 2021, 5:03pm UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/5 "2021-04-05T17:03:12Z")

</div>

_In reply to [Dragewood](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/4):_

As, I understand from your query you wanted to good/default constraints as it is and try to use some variation in constraints when it needed.

You can also achieve your objective by below way.

```verilog

rand bit [15:0] value;

constraint value_c {
  // Declare string type variable inside top_cfg
  // Overwrite by command line using $value$plusargs.
  if(top_cfg.constraints == "DEFAULT") {
     value inside {[0:200]};
  } else {
     // What ever you want.
  }
}

```

Thanks,  
Harsh

---

<div class="post-metadata">

**Author:** ![Dragewood](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@Dragewood](https://verificationacademy.com/forums/u/Dragewood)\
**Post date:** [April 6, 2021, 3:42am UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/6 "2021-04-06T03:42:37Z")

</div>

_In reply to [tfitz](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/5):_

Thanks for the suggestions. I went with the “my\_tr.randomize() with { [list all the constraints] }” approach, such that I can extend a sequence/test pair each time I add a test. This worked. One drawback of this approach is, in the future if I add several tests, and then need to add/modify a constraint in the base sequence, I’ll need to make that same change in every extended sequence.

---

<div class="post-metadata">

**Author:** ![tfitz](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/tfitz/32/3025_2.png) [@tfitz](https://verificationacademy.com/forums/u/tfitz)\
**Post date:** [April 6, 2021, 12:41pm UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/7 "2021-04-06T12:41:24Z")

</div>

_In reply to [Dragewood](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/6):_

> _In reply to [tfitz](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/6):_  
> One drawback of this approach is, in the future if I add several tests, and then need to add/modify a constraint in the base sequence, I’ll need to make that same change in every extended sequence.

That’s a pretty big drawback and is the main reason I recommended against using randomize-with. Since you have to manage test/sequence pairs anyway, you could just as easily manage test/item pairs, where the test uses the factory to choose the properly-constrained sequence item type. Then, if you have to modify a constraint in the base item type, it will automatically get picked up by all the others and you don’t have to make the same edit in a whole bunch of sequences. That’s kind of the whole point of object-oriented programming, by the way.

Good luck,  
-Tom

---

<div class="post-metadata">

**Author:** ![sjohnson-aril](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/7c8e57/32.png) [@sjohnson-aril](https://verificationacademy.com/forums/u/sjohnson-aril)\
**Post date:** [April 13, 2021, 7:46pm UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/8 "2021-04-13T19:46:33Z")

</div>

_In reply to [harsh pandya](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/7):_

> _In reply to [Dragewood](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/7):_  
> As, I understand from your query you wanted to good/default constraints as it is and try to use some variation in constraints when it needed.  
> You can also achieve your objective by below way.

```verilog

rand bit [15:0] value;
constraint value_c {
// Declare string type variable inside top_cfg
// Overwrite by command line using $value$plusargs.
if(top_cfg.constraints == "DEFAULT") {
value inside {[0:200]};
} else {
// What ever you want.
}
}

```

Thanks,  
Harsh

Where/how does top\_cfg get set? I assume this constraint is inside the sequence\_item?

---

<div class="post-metadata">

**Author:** ![harsh\_pandya](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/harsh_pandya/32/2973_2.png) [@harsh\_pandya](https://verificationacademy.com/forums/u/harsh_pandya)\
**Post date:** [April 14, 2021, 4:47am UTC](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/9 "2021-04-14T04:47:26Z")

</div>

_In reply to [sjohnson-aril](https://verificationacademy.com/forums/t/how-do-i-constrain-data-within-hierarchy-at-test-level/38937/8):_

No, constraints written inside sequence.  
Because, generally dut configuration register/field is not part of vip.  
So,related field and constraints written inside sequence.

And here top\_cfg set from top\_base\_test and get inside sequence.

```verilog

//build phase of top_base_test
uvm_config_db#(top_configuration)::set(null, "*", "top_cfg", top_cfg);

//sequence extends from uvm_sequence
function new(string name= "test_seq");
  uvm_config_db#(top_configuration)::get(null, "", "top_cfg", top_cfg);
endfunction : new

```

Thanks,  
Harsh
