# How to randomly pick a valid sequence\_item and form a sequence

**URL:** <https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286>\
**Category:** UVM\
**Tags:** UVM, uvm\_transaction, uvm\_sequence\
**Created:** [July 9, 2017, 11:56am UTC](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286 "2017-07-09T11:56:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raviteja\_b](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@raviteja\_b](https://verificationacademy.com/forums/u/raviteja_b)\
**Post date:** [July 9, 2017, 11:56am UTC](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286/1 "2017-07-09T11:56:50Z")

</div>

Hi,

I have a packet header that have different types of encoding allowed (around 65 in actual).  
Let’s for simplicity take a case of two types of encoding as shown below:-

Ex:-  
bit [64] header; //Actual header width

//valid header type\_1  
header [63:20] field\_1\_type\_1;  
header [19:10] field\_2\_type\_1;  
header [9:0] field\_3\_type\_1;

//valid header type\_2  
header [63:20] field\_1\_type\_2;  
header [19:13] field\_2\_type\_2;  
header [12:9] field\_3\_type\_2;  
header [8:0] field\_4\_type\_2;

My current class organisation is as follows:-  
//Base Class  
class base\_tx extends uvm\_sequence\_item;  
rand bit [64] header;  
…  
//No Constraints are present in this class  
…  
endclass

//Derived class type\_1  
class my\_tx\_1 extends base\_tx;  
rand bit [44] field\_1\_type\_1;  
rand bit [10] field\_2\_type\_1;  
rand bit [10] field\_3\_type\_1;

//Some constraints on each of the above fields  
…

//Pack functions to assign the field values to header  
header[63:20] = field\_1\_type\_1;  
header[19:10] = field\_2\_type\_1;  
…

endclass

//Derived class type\_2  
class my\_tx\_2 extends base\_tx;  
rand bit [44] field\_1\_type\_2;  
rand bit [7] field\_2\_type\_2;  
rand bit [4] field\_3\_type\_2;  
rand bit [9] field\_4\_type\_2;

//Some constraints on each of the above fields  
…

//Pack functions to assign the field values to header  
header[63:20] = field\_1\_type\_2;  
header[19:13] = field\_2\_type\_2;  
…  
endclass

I would like to create random sequences consisting of my\_tx\_1 and my\_tx\_2.  
The problem here is that if I am calling base\_tx.randomize(), it is generating invalid stimulus in some cases.  
I am able to create random sequences with my\_tx\_1 and my\_tx\_2, which are working.

Do I need to change the base class hierarchy to achieve this?  
What would be better hierarchical organisation for my application with UVM?

Thanks in advance.

–Bravo

---

<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:** [July 9, 2017, 5:59pm UTC](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286/2 "2017-07-09T17:59:31Z")

</div>

_In reply to [Bravo](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286):_

I’d recommend to use a union of different structs.

---

<div class="post-metadata">

**Author:** ![raviteja\_b](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@raviteja\_b](https://verificationacademy.com/forums/u/raviteja_b)\
**Post date:** [July 10, 2017, 4:42pm UTC](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286/3 "2017-07-10T16:42:44Z")

</div>

_In reply to [chr\_sue](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286/2):_

Hi,

Can you give a basic example of how this could help me achieve the objective.  
Any references should also be good.

Thanks  
Bravo

---

<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:** [July 10, 2017, 5:44pm UTC](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286/4 "2017-07-10T17:44:55Z")

</div>

_In reply to [Bravo](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286/3):_

You can define the random sequence Arbitration with set\_arbitration(mode) on your sequencer.

---

<div class="post-metadata">

**Author:** ![raviteja\_b](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@raviteja\_b](https://verificationacademy.com/forums/u/raviteja_b)\
**Post date:** [July 21, 2017, 5:14pm UTC](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286/5 "2017-07-21T17:14:01Z")

</div>

Hi all,

Can I define the base class as the one containing bit [64] header.  
Then, extended this to define the different layouts I have for this header?

If this is the case can I access and constraint the different sub-fields in different layouts ?

---

<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:** [July 21, 2017, 5:38pm UTC](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286/6 "2017-07-21T17:38:28Z")

</div>

_In reply to [Bravo](https://verificationacademy.com/forums/t/how-to-randomly-pick-a-valid-sequence-item-and-form-a-sequence/33286/5):_

I believe you should have a look to OOP. You do not have to define in the base class any array if you want to define different Array for different Layouts.  
An extended class has all the data members and methods from the base class available and adds new data members and methods.
