Based on type of object I constraint a Variable
class Base ;
...
endclass
class Ext1 extends Base ;
...
endclass
class Ext2 extends Ext1 ;
...
endclass
class RAND ;
rand bit [1:0] val ;
Base b ; // Will hold Object @ run-time based on which I want to constraint val ;
local Ext1 e1 ;
local Ext2 e2 ;
constraint VALUE { if ( $cast(e1,b) ) // Error !!
{ val == 1 ; }
else if ( $cast(e2,b) ) // Error !!
{ val == 2 ; }
else
{ val == 3 ; }
}
endclass
Rand r ;
Base b;
Ext1 e1 ;
Ext2 e2 ;
initial begin
r = new() ;
b = new();
e1 = new();
e2 = new();
r.b = b ;
void'( r.randomize() ) ; // Yes , I know not recommended
r.b = e1 ;
void'( r.randomize() ) ; // Yes , I know not recommended
r.b = e2 ;
void'( r.randomize() ) ; // Yes , I know not recommended
end
[Q1] Is there a restriction in using $cast() as a Constraint Guard ?
I then use a function call in Constraint Guard
// Inside Class RAND
typedef bit [1:0] BB;
function BB CAST();
if ( $cast(e1,b) )
return 1 ;
else if ( $cast(e2,b) )
return 2 ; }
else
return 3 ;
endfunction
constraint VALUE { if ( CAST() == 1 )
{ val == 1 ; }
else if ( CAST() == 2 )
{ val == 2 ; }
else if ( CAST() == 3 )
{ val == 3 ; }
}
function void post_randomize() ;
if ( $cast(e1,b) )
$display(" val == 1 ") ;
else if ( $cast(e2,b) )
$display("val == 2") ;
else
$display("val == 3") ;
endfunction
Rand r ;
Base b;
Ext1 e1 ;
Ext2 e2 ;
initial begin
r = new() ;
b = new();
e1 = new();
e2 = new();
r.b = b ;
void'( r.randomize() ) ;
r.b = e1 ;
void'( r.randomize() ) ;
r.b = e2 ;
void'( r.randomize() ) ;
end
I get Output as ::
val == 1
val == 1
[Q2] Shouldn’t it display 3 times ( Since I call randomize thrice ) ?
[Q3] I expected it to display all 3 values , where do I go wrong ?
Thanks ,