Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Multidimensional Dynamic Arrays

SystemVerilog Constraints

A foreach constraint that fixes the outer size first, then randomizes each inner sub-array's size and element values.

Open in IDE
testbench.sv
//--------------------------------------------------------------
// Author  : Admin, ChipVerify
// Website : www.chipverify.com
//--------------------------------------------------------------

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



/*
Simulation Log:
---------------

ncsim> run
md_array = '{'{'h9, 'h6, 'h7, 'h9, 'h1}, '{'h5, 'h9, 'h4, 'h2}}
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit
*/                           
design.sv
// Code your design here
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.