Below example helps to understand parent-child relationship better. many things to look at.
module constraint_test();
class parent;
rand int unsigned a;
constraint c1{
(a < 10);
}
endclass
class child extends parent;
rand int unsigned a;
constraint c2{
(a > 10);
}
endclass
parent parent_handle = new();
child child_handle = new();
initial
begin
parent_handle = child_handle;
parent_handle.randomize();
$display("a = %d",parent_handle.a);
$display("a = %d",child_handle.a);
end
endmodule
case 1:
If there is no handle assignment in above example, first display statement will display value less than 10 and second display statement will display "0", since child_handle.randomize() is not given. If there is handle assignment, first will display less than 10 and second will display greater than 10. reason is before assignment, 'a' will have different location, after assignment 'a' will point to child class location. so, when parent_handle.randomize() is called, it randomizes child part variables also even though it doesn't have access to child class properties.
case 2:
Assume, no handle assignment in above example, if constraint names are given same in base class and derived class, if you do child_handle.randomize, since constraints are mutually exclusive, it should give error, but instead it wont throw error, it simply dosen't randomize the value and puts default value in it. randomize() function returns 0. If want to throw the error, capture return value of randomize() function. use assert(child_handle.randomize()), it throws error.
case 3:
we can give initial values to variables declared as rand/randc, if we call randomize() function, it overwrites that value.
module constraint_test();
class parent;
rand int unsigned a;
constraint c1{
(a < 10);
}
endclass
class child extends parent;
rand int unsigned a;
constraint c2{
(a > 10);
}
endclass
parent parent_handle = new();
child child_handle = new();
initial
begin
parent_handle = child_handle;
parent_handle.randomize();
$display("a = %d",parent_handle.a);
$display("a = %d",child_handle.a);
end
endmodule
case 1:
If there is no handle assignment in above example, first display statement will display value less than 10 and second display statement will display "0", since child_handle.randomize() is not given. If there is handle assignment, first will display less than 10 and second will display greater than 10. reason is before assignment, 'a' will have different location, after assignment 'a' will point to child class location. so, when parent_handle.randomize() is called, it randomizes child part variables also even though it doesn't have access to child class properties.
case 2:
Assume, no handle assignment in above example, if constraint names are given same in base class and derived class, if you do child_handle.randomize, since constraints are mutually exclusive, it should give error, but instead it wont throw error, it simply dosen't randomize the value and puts default value in it. randomize() function returns 0. If want to throw the error, capture return value of randomize() function. use assert(child_handle.randomize()), it throws error.
case 3:
we can give initial values to variables declared as rand/randc, if we call randomize() function, it overwrites that value.
No comments:
Post a Comment