Multidimension array size

bit arry[2:0] ;

How do I constraint the size of multidimensional array ?

I am looking for like how we initial the size of dynamic array . rand bit [2:0] my_array ;
constraint c {my_array.size==6;}

Is it possible to define the size of multidimensional array too ?

Thanks in advance !!

In reply to MP:

bit arry[2:0] ;
How do I constraint the size of multidimensional array ?
I am looking for like how we initial the size of dynamic array . rand bit [2:0] my_array ;
constraint c {my_array.size==6;}
Is it possible to define the size of multidimensional array too ?
Thanks in advance !!

Please refer to the LRM section 18.5.8.1 foreach iterative constraints, this is actually an array of arrays so eachb individual array can have a different size

Also this might help you


class multiD_array;
  rand bit [2:0] arry [] [] ;
  constraint size_c{
    arry.size() == 4; // set the total of arrays in arry
    foreach(arry[i]) {
      arry[i].size() inside {[1:3]}; //set the size of each array in arry
    }
  }
endclass
      
module test();
  
  multiD_array h;
  initial begin 
    repeat(2) begin
      h =  new();
      if(!h.randomize()) $fatal(1, "Randomize Failed");
      $display("h %p", h);
      
    end
  end
endmodule
# KERNEL: h '{arry:'{'{1, 2, 2}, '{2, 7}, '{4, 6, 0}, '{6}}}
# KERNEL: h '{arry:'{'{4, 6, 3}, '{1, 3}, '{2}, '{1, 7}}}

HTH,
-R

Thank you for this !!

I have follow up questions .
I want the size of the array say a*b

I tried and I am always getting size 0 . Can you help me to fix this ?

Thanks,

In reply to MP:

I tried your code and I’m not always getting 0-sized arrays you need to ensure that a != 0 and b != 0


class multiD_array;
  rand bit [1:0] a ; 
  rand bit [2:0] b ; 
  rand bit [2:0] arry [] [] ;
  constraint size_c{
    arry.size() == a*b ; // set the total of arrays in arry
    a != 0;
    b != 0;
    foreach(arry[i]) {
      arry[i].size() inside {[1:3]}; //set the size of each array in arry
    }
  }
endclass
 
module test();
 
  multiD_array h;
  initial begin 
    repeat(2) begin
      h =  new();
      if(!h.randomize()) $fatal(1, "Randomize Failed");
      $display("size =%0d", h.arry.size()); 
      $display("h %p", h);
 
    end
  end
endmodule

# KERNEL: size =2
# KERNEL: h '{a:1, b:2, arry:'{'{2, 2, 2}, '{7}}}
# KERNEL: size =12
# KERNEL: h '{a:3, b:4, arry:'{'{4}, '{5}, '{0, 1}, '{6}, '{6, 2, 2}, '{3, 1}, '{5, 1, 7}, '{2}, '{7, 5}, '{3}, '{1}, '{1, 1, 1}}}


HTH,
-R