I need to randomize a variable X . it needs to take from 1 to 16
Another variable Y , it needs to take from 1 to 16
I should get a combination of X and Y , such that
X =1 , Y = {1 to 16}
X =2 , Y = {1 to 16}
X =3, Y = {1 to 16}
…
.
X=16, Y = {1 to 16}
Here there 16X16 = 256 unique combinations.
How can i write a constraint like that .
class A;
rand bit [4:0] X , Y ;
rand bit [9:0] XY ;
bit [9:0] XYQ[$];
constraint VAL {
X inside {[1:16]};
Y inside {[1:16]} ;
{X,Y} == XY;
}
constraint XY_UNIQ { unique { XYQ, XY } ; }
function void post_randomize() ;
XYQ.push_back( {X,Y} );
if ( XYQ.size() == 256 )
XYQ = {};
endfunction
endclass
module top;
A a= new;
initial for(int i=1;i<270;i++) begin
assert(a.randomize());
$display("%3d X %d Y %d",i, a.X, a.Y);
end
endmodule
There are two varaible,X,Y
Actaully there can be multipe varaible from 2 to 10 depends on one passing variable variable
I just rearranged the code with queue
In this below code, i passed num_var=2, it worked as it
Say if num_var=3, how it will work?
I am not getting how we can concatinate with XY which is 10 bits.
class A;
//rand bit [4:0] X , Y ;
rand bit [4:0] Var_q[$];
int num_var;
rand bit [9:0] XY ;
bit [9:0] XYQ[$];
constraint VAL {
Var_q.size() == num_var;
//X inside {[1:16]};
//Y inside {[1:16]} ;
{Var_q[0],Var_q[1]} == XY;
}
constraint XY_UNIQ { unique { XYQ, XY } ; }
function void post_randomize() ;
bit [9:0] TMP;
foreach(Var_q[i]) begin
TMP = {TMP,Var_q[i]};
end
//XYQ.push_back( {X,Y} );
if ( XYQ.size() == 256 )
XYQ = {};
endfunction
endclass
module top;
A a= new;
initial begin
a.num_var = 2;
for(int i=1;i<270;i++) begin
assert(a.randomize());
//$display("%3d X %d Y %d",i, a.X, a.Y);
$display("%3d X %d Y %d",i, a.Var_q[0], a.Var_q[1]);
end
end
endmodule