How to randomize 2 dimensional dynamic array in SystemVerilog

Hi,

I want to know how to randomize 2d dynamic array.

//code snippet

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

Best Regards
Abhinav

In reply to abhirula:

Your example does not make any sense, rows_cols[0][0] can only have a single value. And how do you define ordering?

You can always iterate over a dynamic array using a foreach constraint.

constraint c {foreach (rows_cols[ii,jj]) {
          rows_cols[ii,jj] inside {[100:200]}; // applies to every element
          (jj != 0 ) -> rows_cols[ii,jj-1] < rows_cols[ii,jj];
      }
}

In reply to dave_59:

Hi Deve,

Thanks for the reply. I will reprase my question.

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
          


In reply to abhirula:

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

In reply to dave_59:

Hi Deve,

I tried above set of codes, but I am getting compilation error or incorrect output values.

// code snippet //

constraint c5_rows_cols {
foreach(rows_cols[ii,jj])
rows_cols[ii][jj] inside {[1:100]};
(jj != 0 ) -> rows_cols[ii,jj-1] < rows_cols[ii,jj];
}

ncvlog: *E,EXPRBK (…/registers/register_rdwr_base_seq.sv,157|49): expecting a right bracket (‘]’) [4.2.1][4.2][4.2.2(IEEE)].
(`include file: …/registers/register_rdwr_base_seq.sv line 157, file: …/registers/reg_pkg.sv line 11)
(jj != 0 ) → rows_cols[ii,jj-1] < rows_cols[ii,jj];

In rows_cols[ii][jj] (rows_cols[width][height]
(i) how to apply constraint individually on width or height in order to get output = 2 4, 6,

  1. Also I tried to randomize 2d dynamic array using seprate rand variables.
class A,

rand bit [11:0] width;
rand bit [11:0] height;
constraint width_cst {
width inside {[0:100]};
}

constraint height_cst {
height inside {[0:200]};
}

constraint c4_rows_cols {
rows_cols.size() inside {[0: width ]};  
foreach(rows_cols[ii])
rows_cols[ii].size inside {[0: height ]};
}

constraint c5_rows_cols {
foreach(rows_cols[ii,jj])
rows_cols[ii][jj] inside {[1:100]};
}

constraint c6_rows_cols {
foreach(rows_cols[i,j])
(j < height - 1 ) -> rows_cols[i][j] < rows_cols[i][j+1];
}
//post randomize
foreach(rows_cols[i,j])
$display("\t VALUE OF rows_cols [%0d][%0d]=%0d",i,j,rows_cols[i][j]);
$display("\t Value of rows_cols SIZE %d", rows_cols.size());

// simulation result
POST RANDOMIZE: width 75
POST RANDOMIZE: height 190
Value of rows_cols SIZE 16.
Nothing is printed for VALUE OF rows_cols.

Please let me know where is the mistake in my code.

Best Regards
Abhinav

This worked for me:

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

Ref: SystemVerilog foreach Constraint

-Surya