Constraint Override in System Verilog:

Here in this overriding of constraint if we will have the same constraint name in the parent class as well as child class then we can say that our constraint is overridden in the child class.

Let us understand through an example:

class parent;
  rand int data;
  constraint ct_c
  {
    data inside {[100:200]};
  }
endclass : parent
 
class child extends parent;
  constraint ct_c
  {
    data inside {[250:500]};
  }
endclass : child
 
program main;
  child c;
  initial begin
    c = new();
    if (!c.randomize()) begin
      $display("Randomization failed");
    end
    else begin
      $display("data = %0d", c.data);
    end
  end
endprogram : main

In the above example, you can observe that the EDA tool will try to solve only child (extended/derived) class’s constraint because Child class Override parent class’s Constraint as both constraints is having the same name.

Let us see another example with a different constraint name:

class parent;
  rand int data;
  constraint ct_c
  {
    data inside {[100:200]};
  }
endclass : parent
 
class child extends parent;
  constraint ct_child
  {
    data inside {[250:500]};
  }
endclass : child
 
program main;
  child c;
  initial begin
    c = new();
    if (!c.randomize()) begin
      $display("Randomization failed");
    end
    else begin
      $display("data = %0d", c.data);
    end
  end
endprogram : main

In the above example, EDA Tool will consider two different constraints so it will try to resolve both constraints because the Same variable is constrained using two different constraints (Names are different so the Child class can’t override the Parent class’s constraint) having different names and both constraints are contradict to each other.

About the author

Avatar photo

The Art of Verification

Hi, I’m Hardik, and welcome to The Art of Verification.

I’m a Verification Engineer who loves to crack complex designs and here to help others commit to mastering Verification Skills through self-learning, System Verilog, UVM, and most important to develop that thought process that every verification engineer should have.

I’ve made it my mission to give back and serve others beyond myself.

I will NEVER settle for less than I can be, do, give, or create.

View all posts