UVM Constrains inside a sequence are ignored

Hi there,

I am facing some issues trying to create some sequences, the constrains I added are not taken in consideration when the item is randomize. Do you have any idea why?


class normal_mode_seq extends default_seq;

  `uvm_object_utils(normal_mode_seq )

  constraint mode_c{
    item.mode == NORMAL_OP;
  }
  
  function new(string name = "");
    super.new(name); // Item is created in the new function of default_seq
  endfunction : new
  
  virtual task body();
    `uvm_info(get_type_name(),$sformatf("Sequence randomize with %s", item.mode.name()), UVM_NONE)
    start_item(item); 
    if ( !item.randomize())
      `uvm_error(get_type_name(), "Failed to randomize transaction")
    finish_item(item); 
    `uvm_info(get_type_name(),$sformatf("Sequence randomize with %s", item.mode.name()), UVM_NONE)
  endtask : body
  
endclass : normal_mode_seq 

In reply to HelenLG:

Your contraint is in the wrong place. You have to use an inline constraint while randomizing the seq_item, like this

  virtual task body();
    `uvm_info(get_type_name(),$sformatf("Sequence randomize with %s", item.mode.name()), UVM_NONE)
    start_item(item); 
    if ( !item.randomize()with {item.mode == NORMAL_OP;})
      `uvm_error(get_type_name(), "Failed to randomize transaction")
    finish_item(item); 
    `uvm_info(get_type_name(),$sformatf("Sequence randomize with %s", item.mode.name()), UVM_NONE)
  endtask : body

Adding the constraint as a seperate constraint as you did is limited to the usage in the seq_item class.

In reply to HelenLG:

You may need to share code for base class ’ default_seq ’ as I don’t see ’ item ’ being created in your current code .

In reply to HelenLG:

Hi there,
I am facing some issues trying to create some sequences, the constrains I added are not taken in consideration when the item is randomize. Do you have any idea why?


class normal_mode_seq extends default_seq;
`uvm_object_utils(normal_mode_seq )
constraint mode_c{
item.mode == NORMAL_OP;
}
function new(string name = "");
super.new(name); // Item is created in the new function of default_seq
endfunction : new
virtual task body();
`uvm_info(get_type_name(),$sformatf("Sequence randomize with %s", item.mode.name()), UVM_NONE)
start_item(item); 
if ( !item.randomize())
`uvm_error(get_type_name(), "Failed to randomize transaction")
finish_item(item); 
`uvm_info(get_type_name(),$sformatf("Sequence randomize with %s", item.mode.name()), UVM_NONE)
endtask : body
endclass : normal_mode_seq 

This is the code of the base sequence


class default_seq extends uvm_sequence #(default_seq_item);

  `uvm_object_utils(default_seq )
  rand default_seq_item item;

function new(string name = "");
  super.new(name);
  item = default_seq ::type_id::create("item");
endfunction : new


task body();
  `uvm_info(get_type_name(), "Default sequence starting", UVM_HIGH)
  start_item(item); 
  if ( !item.randomize() )
    `uvm_error(get_type_name(), "Failed to randomize transaction")
  finish_item(item); 
  `uvm_info(get_type_name(), "Default sequence completed", UVM_HIGH)
endtask : body

endclass : default_seq 

In reply to HelenLG:

Could you paste some more code, maybe in the EDAPlayground?
It is here

In reply to HelenLG:

Your constraint mode_c is part of class normal_mode_seq whereas you are calling randomize on handle of type default_seq_item

So item.randomize() will ONLY consider constraints within class default_seq_item

Your constraint mode_c will come into picture when you randomize handle of type normal_mode_seq

As chr_sue replied you need to in-line the constraint during call to item.randomize() for the constraint to take effect