Hello,
I am trying to understand what’s going wrong with the following code.
class A extends ovm_object
rand int num1;
rand int num2;
constraint c_num1;
constraint c_num2;
endclass
//CONSTRAINTS ARE KEPT IN A SEPARATE FILE FOR EASE OF USE
constraint A::c_num1
{ num1 < 10; }
constraint A::c_num2
{ num2 > 100;}
class B extends A
constraint c_num1
{num > 25}
endclass
By default instance of class A is used, but in one case test overrides the instance A with B using factory overrides. After randomization, the soft constraints of class A, doesn’t seem to take effect. In this case, how can I create an extended class of A and change just the necessary constraints in B and still keep the others unaffected ?
In reply to jjose:
Your question does not make a lot of sense, or there are a lot of typos in your example. There are no soft constraints, and the is no variable called num.
Also, if you intend to declare constraints outside the class body, you should use
As Dave said, there will not be any functional difference between constraints defined inside or outside the class definition.
When you are randomizing an object of class B, class A’s constraint will not be considered because of the same constraint identifier name (c_num1) in class A & B in your example.
You can make all the constraints to be considered by specifying different constraint names in the extended classes.
As per what is mentioned in the LRM if the constraints of the derived class have the same name as of the Base class, then the method gets overriden as the randomize function is virtual.
In ext Class B for Class A’s soft constraints to be applied all you need is to declare the constraint with a different name in Class B.
Then this constraint of Class B will be an additional constraint that will get used when instance of Class B gets randomized. Also if this contains any conflicting constraint with Class A then the randomize will fail.
18.5.2 Constraint inheritance (LRM)
Constraints follow the same general rules for inheritance as other class members. The randomize()
method is virtual and therefore honors constraints of the object on which it was called, regardless of the data
type of the object handle through which the method was called.
A derived class shall inherit all constraints from its superclass. Any constraint in a derived class having the
same name as a constraint in its superclass shall replace the inherited constraint of that name. Any constraint
in a derived class that does not have the same name as a constraint in the superclass shall be an additional
constraint.
If a derived class has a constraint prototype with the same name as a constraint in its superclass, that
constraint prototype shall replace the inherited constraint. Completion of the derived class’s constraint
prototype shall then follow the rules described in 18.5.1.