Hi ,
i want to constraint a variable in such a way that number 5 repeats every third time in a loop.
Can someone help me with an idea?
Thanks
Hi ,
i want to constraint a variable in such a way that number 5 repeats every third time in a loop.
Can someone help me with an idea?
Thanks
In reply to syed taahir ahmed:
Hi ,
These 2 Solutions work
// Sample 1
class A ;
rand bit [3:0] varr ; // Unconstrained by Default
endclass
module Main ;
A a1 ;
initial begin
a1 = new();
for ( int i = 1 ; i < 10 ; i++ ) // Loop
begin
if ( i % 3 != 0 )
begin
if ( a1.randomize() with { a1.varr != 5 ; } ) // In-Line Constraint
$display("Success with a1 == %p",a1);
end
else if ( i % 3 == 0 )
begin
if ( a1.randomize() with { a1.varr == 5 ; } ) // In-Line Constraint
$display("Factor of 3 with a1 == %p",a1);
end
end
end
endmodule
// Sample 2
class A ;
rand bit [3:0] varr ;
bit [3:0] dummy_varr ; // To be Used as a Constraint Guard
constraint C {
if ( ( dummy_varr % 3 ) == 0 ) // Constraint Guard
{
varr == 5 ;
}
else
{
varr != 5 ;
}
}
endclass
module Main ;
A a1 ;
initial begin
a1 = new();
for ( int i = 1 ; i < 10 ; i++ )
begin
a1.dummy_varr = i ;
if ( a1.randomize() )
$display("Success with a1 == %p",a1);
end
end
endmodule
Note the above 2 Codes Make sure that for other times ( Not divisible by 3 ) we Never get 5
Regards ,
AGIS
In reply to Etrx91:
Thanks for the detailed code it helped.
In reply to syed taahir ahmed:
BTW, here’s a way to do it without the code calling randomize() having to know about the constraint:
class A ;
rand bit [3:0] varr ;
int count; // make this static if you repeat constructing the class every time
constraint C {
if ( ( count % 3 ) == 0 ) // Constraint Guard
{ varr == 5 ; }
else
{ varr != 5 ; }
}
function void post_randomize();
count++;
endfunction
endclass
module Main;
A a1 ;
initial begin
a1 = new();
repeat(10)
if ( a1.randomize() )
$display("Success with a1 == %p",a1);
else $error("randomize failed");
end
endmodule : Main
In reply to dave_59:
Thank you very much Dave, yes simple variable.randomize will do the necessary action!! I’ll get this way.