class A;
rand bit [11:0] rows_cols [][];
// how to constraint (i,j) rows_cols[i][j] using some other random variable.
// Also how to assign random values in rows_cols[i][j] in increasing order.
for example :-
rows_cols[0][0] = 4,5;
rows_cols[0][1] = 4,6;
rows_cols[1][0] = 5,3;
rows_cols[1][1] = 5,4;
endclass
I need to know how to constraint the size of the array by using some other random vaiables which are randomized before these array.
For Example : -
class A
rand bit [11:0] width [];
rand bit [11:0] height [];
rand bit [11:0] rows_cols [][];
constraint width_cst {
width.size() inside{[100:500];
height.size() inside {[200:700]};
constraint c {foreach (rows_cols[ii,jj]) {
rows_cols[ii,jj].size inside {[width:height]}; // applies to every element
Ok, your question makes more sense now. SystemVerilog multi-dimensional arrays are more like arrays of arrays. That means you have to deal with each dimension separately, and each elements that is an array needs to be sized.
class A;
rand bit [11:0] width [];
rand bit [11:0] height [];
rand bit [11:0] rows_cols [][];
constraint width_cst {
width.size() inside{[100:5]};
height.size() inside {[200:700]};}
constraint c {rows_cols.size() == width.size();
foreach(rows_cols[ii])
rows_cols[ii].size == height.size();
}
constraint each_element {foreach(rows_cols[ii,jj])
rows_cols[ii][jj] inside {[1:255]};}endclass
class ABC;
rand bit[3:0] md_array [][]; // Multidimansional Arrays with unknown size
constraint c_md_array {
// First assign the size of the first dimension of md_array
md_array.size() == 2;
// Then for each sub-array in the first dimension do the following:
foreach (md_array[i]) {
// Randomize size of the sub-array to a value within the range
md_array[i].size() inside {[1:5]};
// Iterate over the second dimension
foreach (md_array[i][j]) {
// Assign constraints for values to the second dimension
md_array[i][j] inside {[1:10]};
}
}
}
endclass
module tb;
initial begin
ABC abc = new;
abc.randomize();
$display ("md_array = %p", abc.md_array);
end
endmodule