Tutorials

Learn More

Constraint blocks for a parent class can be overridden by its child class. Thus, the inherited class can modify constraints based on the requirement. To do the same, constraint block nomenclature must be the same.

Inheritance in constraint Example

class parent;
  rand bit [5:0] value;
  constraint value_c {value > 0; value < 10;}
endclass

class child extends parent;
  constraint value_c {value inside {[10:30]};}
endclass

module constraint_inh;
  parent p;
  child c;
  
  initial begin
    p = new();
    c = new();
    repeat(3) begin
      p.randomize();
      $display("Parent class: value = %0d", p.value);
    end
    
    repeat(3) begin
      c.randomize();
      $display("Child class: value = %0d", c.value);
    end
  end
endmodule

Output:

Parent class: value = 2
Parent class: value = 3
Parent class: value = 6
Child class: value = 11
Child class: value = 30
Child class: value = 14

System Verilog Tutorials