# Set constraint in uvm\_seq\_item

**URL:** <https://verificationacademy.com/forums/t/set-constraint-in-uvm-seq-item/34590>\
**Category:** UVM\
**Tags:** UVM, SystemVerilog, Constraint-random-verification\
**Created:** [June 19, 2018, 10:48pm UTC](https://verificationacademy.com/forums/t/set-constraint-in-uvm-seq-item/34590 "2018-06-19T22:48:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![UVM\_Xplorer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/90db22/32.png) [@UVM\_Xplorer](https://verificationacademy.com/forums/u/UVM_Xplorer)\
**Post date:** [June 19, 2018, 10:48pm UTC](https://verificationacademy.com/forums/t/set-constraint-in-uvm-seq-item/34590/1 "2018-06-19T22:48:42Z")

</div>

Hi All,

I am trying to set the constraint and not sure how to achieve the desired result.

//code start

```auto
typedef enum bit [7:0] {instr1, instr2, instr3, instr4, instr5} e_instr; // I have an enum with set of valid instructions in it.
typedef enum (40, 80, 104) e_FreqFlag;

class my_seq_item extends uvm_seq_item;
  rand e_instr inst;
  e_FreqFlag Fflag;
  byte opcode;
  bit [1:0] mode;
  int AddrCycle;
  int DummyCycle;

  constraint c_inst { inst=instr1 -> {opcode=8'h00; mode=2'b00; AddrCycle=0; DataCycle=0; Fflag=0;}
                      inst=instr2 -> {opcode=8'h00; mode=2'b01; AddrCycle=0; DataCycle=0; Fflag=0;} // Note here its instr2 same as the one below
                      inst=instr2 -> {opcode=8'h00; mode=2'b10; AddrCycle=1; DataCycle=0; Fflag=0;} // note the field mode & AddrCycle has chaged. 
                      #inst=instr2 -> {opcode=8'h00; mode inside {2'b01,2'10}; if(mode=01)AddrCycle=0 else AddrCycle=1; DataCycle=0; Fflag=0;} 
                      // Pseudo code for what i wish to achieve is in above line commented.. is there something nested constaint i can use to achive this.
                    }
endclass

```

//code end

Thanks for your help and guidance.

---

<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:** [June 19, 2018, 11:16pm UTC](https://verificationacademy.com/forums/t/set-constraint-in-uvm-seq-item/34590/2 "2018-06-19T23:16:31Z")

</div>

_In reply to [PIYUSH PANWAR](https://verificationacademy.com/forums/t/set-constraint-in-uvm-seq-item/34590):_

You can certainly nest constraints in an implication or if-else constraint. There is an example of nesting in section 18.5.7.

But remember all constraint get assembled into a set of concurrent equations you can also write something like

```verilog
constraint c_inst { inst==instr1 -> {opcode==8'h00; mode==2'b00; AddrCycle==0; DataCycle==0; Fflag==0;}
                    inst==instr2 -> {opcode==8'h00; mode inside {2'b01,2'10}; DataCycle=0; Fflag=0;}
                    mode==2'b01 -> AddrCycle=0;
                    mode==2'b10 -> AddrCycle=1;
  }

```

---

<div class="post-metadata">

**Author:** ![UVM\_Xplorer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/90db22/32.png) [@UVM\_Xplorer](https://verificationacademy.com/forums/u/UVM_Xplorer)\
**Post date:** [June 21, 2018, 9:14pm UTC](https://verificationacademy.com/forums/t/set-constraint-in-uvm-seq-item/34590/3 "2018-06-21T21:14:17Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/set-constraint-in-uvm-seq-item/34590/2):_  
Thanks a lot for your time and help Dave.  
My requirement was more on if-else for the constraint.  
I have implemented that way. If face any issue, will update and seek more help :)

---

<div class="post-metadata">

**Author:** ![UVM\_Xplorer](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/90db22/32.png) [@UVM\_Xplorer](https://verificationacademy.com/forums/u/UVM_Xplorer)\
**Post date:** [July 27, 2018, 6:13pm UTC](https://verificationacademy.com/forums/t/set-constraint-in-uvm-seq-item/34590/4 "2018-07-27T18:13:18Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/set-constraint-in-uvm-seq-item/34590/3):_

Hi Dave,

it worked perfectly and desired result was achieved.  
Thanks for your help and guidance.  
Appreciate it.

Regards,  
Piyush
