Overriding constraints in extended class

Hello

Can we override constraints of base class in extended class, by defining a new constraint with the same name in extended class.

In the code blow, there are two constraint in “base” class for variable “a” & “b”.

In “extended” class which is extended from base, I defined two more constraint which are in conflict with the constraint in base class. For variable “a” constraint I used the same name as in base class, and for “b” I used a different name.

When I randomized the extended class object, I expected randomization should fail for both the constraint in extended class. But the randomization only fail for the conflicting constraint with different name in base and extended class.

I am using Cadence IUS 8.2u12, is something specific to IUS. I want to keep my code generic.

Thanks in advance

  • Rajkumar
module junk () ;
   class base;
      rand bit [15:0] a,b;
      
      function new();
      endfunction // new
      
      constraint ct_a {a inside {[0:9]};} 
      constraint ct_b {b inside {[10:19]};}
    endclass // base
   
   class extended extends base;
      function new();
      endfunction // new

      constraint ct_a       {a inside {[10:19]};}
      constraint ct_b_new   {b inside {[0:9]};}
   endclass // extended
   
   initial begin
      extended extended_obj;
      extended_obj = new();

      if(!extended_obj.randomize()) $display("Randomization Failed");
   end
endmodule // junk

Hi Raj,
That’s expected as per LRM. With same names, you are “overriding” the base constraint and hence is not conflicting.

Srini
www.cvcblr.com/blog

if u use the same name in extended class that means you are overriding that constraint in the derived class. so the constraint in the base class no more work for the extended class. so for ‘a’ there is no infliction.
where as for ‘b’ both the constraints are working so conflicts occurs in that case.

1 Like

Thanks Guys, Can you please point me to section in LRM explaining constraint overriding, looks like I missed this part.

Thanks again

  • Rajkumar

See 18.5.2 Constraint inheritance in the 1800-2009 LRM

Thanks Dave.