Hello everyone ,
I am trying to Constraint sum using the following Code :
Class ASUM ;
rand bit [1:0] Arr[] ;
constraint S1D { Arr.size() == 3 ; }
constraint SUMM { SUM_CALC ( Arr ) == 7 ; }
function int SUM_CALC ( input bit [1:0] Arr [] ) ;
$display(" Inside Function SUM_CALC with size == %0d ",Arr.size() ); // To See if Function Gets Called b4 OR After Size Constraint !!
foreach(Arr[i])
begin
SUM_CALC = SUM_CALC + Arr[i] ;
end
endfunction
endclass
.....
if ( a1.randomize() ) // a1 is Class Object
begin
$display("Success with %p ",a1);
end
else
$display("Fails with %p",a1);
end
I get the following Output ::
Inside Function SUM_CALC with size == 3
Fails with '{Arr:'{}}
(a) I don’t get why the Constraint Fails .
(b) According to my understanding , Function gets Called before any Constraint gets Solved
( apart from randc ) , hence I used a display inside the function to see whether the size() Constraint is Applied before the
Function Call . As observed from output I see size() Constraint gets Solved before Function Call .
How is that possible . AM i missing something ?
(c) I can achieve the Sum Constraint using :: A.sum() with ( int’item ) == 7 ;
Is there any Other way ?
Another Issue is when the Arr is declared 2D ( rand bit [1:0] Arr )
rand bit [1:0] Arr[][];
constraint S1D { Arr.size() == 9 ; }
constraint S2D { foreach(Arr[i])
{
Arr[i].size() == 2 ;
}
}
constraint sum_2d {
if ( Arr.size() > 0 ) // Refer (d) below
Arr.sum(item) with ( item.sum() with ( int'(item) ) ) == 27 ;
}
(d) In the Constraint Guard above How Can I Check that Both Dimensions 1 as well as 2 of Arr are greater than 0 ?
According to LRM :: $size(Arr,1) && $size(Arr,2) won’t work since the Dimensions are Dynamic .
Arr.sum(item) with ( item.sum() with ( int'(item) ) ) Unfortunately doesn't work in declarative Context . I observe that it works fine in procedural Context which is why I tried the Following ::
constraint sum_2d {
if ( Arr.size() > 0 ) // Would be evaluated Only Once
FUNCTN_SUM(Arr) == 27 ;
}
typedef int unsigned intu ;
function intu FUNCTN_SUM (input bit [1:0] Arr[][] ) ;
$display(" Inside FUNCTN_SUM ");
FUNCTN_SUM = Arr.sum(item) with ( item.sum() with ( int'(item) ) ) ;
endfunction
I get the following Output ::
Inside FUNCTN_SUM
Fails with '{A:'{}} // Similar Issue as (a) Above
Thanks in advance ,
FS