https://verificationguide.com/systemverilog/systemverilog-dynamic-array-randomization/
Dynamic Array Randomize
For a dynamic array, it is possible to randomize both array size and array elements.
randomize dynamic array size
In below example, dynamic array size will get randomized based on size constraint, and array elements will get random values.
- Declare array as rand
- Write constraint for array size, On randomization array size will get the random size
class dynamic_array; rand bit [3:0] array1[ ]; rand bit [7:0] array2[ ]; constraint a1_size_c { array1.size() inside {[4:10]}; } constraint a2_size_c { array2.size() inside {[4:10]}; } function void display(); $display("array1 size is = %0d",array1.size()); $display("array1 = %p",array1); $display("array2 size is = %0d",array2.size()); $display("array2 = %p",array2); endfunction endclass program dynamic_array_randomization; dynamic_array pkt; initial begin pkt = new(); pkt.randomize(); pkt.display(); end endprogram
Simulator Output
array1 size is = 7 array1 = '{'h0, 'hf, 'he, 'h4, 'hf, 'h9, 'h8} array2 size is = 10 array2 = '{'he, 'hab, 'hb0, 'h29, 'h57, 'h52, 'h26, 'h49, 'h34, 'h49}
array size based on another random variable
In the below example, the array size is constrained based on the value of another element.
- Declare array as rand
- On randomization, the array will get size based on the value of burst type
typedef enum {SINGLE,INCR,INCR4,WRAP4,INCR8,WRAP8} burst_t; class dy_array; rand burst_t burst_type; rand bit [7:0] data[ ]; constraint data_s { (burst_type == SINGLE)-> data.size() == 1; (burst_type == INCR) -> data.size() == 1; (burst_type == INCR4) -> data.size() == 4; (burst_type == WRAP4) -> data.size() == 4; (burst_type == INCR8) -> data.size() == 8; (burst_type == WRAP8) -> data.size() == 8; } function void display(); $display("burst_type = %s",burst_type.name()); $display("data array size is = %0d",data.size()); $display("data array = %p",data); endfunction endclass program dynamic_array_randomization; dy_array pkt; initial begin pkt = new(); pkt.randomize(); pkt.display(); end endprogram
Simulator Output
burst_type = WRAP4 data array size is = 4 data array = '{'h54, 'h88, 'h9b, 'h9a}
Generate unique elements in an array
In the below example.
- Constrain array with element value same as an index value
- In post randomization shuffle the array, so that array will not have an incremental values
class dynamic_array; rand bit [7:0] array[ ]; constraint size_c { array.size() inside {[4:10]}; } constraint array_c { foreach(array[i]) array[i] == i;} function void post_randomize(); array.shuffle(); endfunction function void display(); $display("array size is = %0d",array.size()); $display("array = %p",array); endfunction endclass program dynamic_array_randomization; dynamic_array pkt; initial begin pkt = new(); pkt.randomize(); pkt.display(); end endprogram
Simulator Output
array size is = 7 array = '{'h3, 'h6, 'h2, 'h1, 'h4, 'h5, 'h0}
Sum method used in a dynamic array
In the below example, an array is randomized in such a way that the sum of all the elements equals to 45.
- Declare array with rand
- Constraint sum of an array using array method sum()
class dynamic_array; rand bit [7:0] array[ ]; constraint size_c { array.size() inside {[4:10]}; } constraint array_c { array.sum() == 45;} constraint array_ec { foreach(array[i]) array[i] > 1;} function void display(); $display("array size is = %0d",array.size()); $display("array = %p",array); endfunction endclass program dynamic_array_randomization; dynamic_array pkt; initial begin pkt = new(); pkt.randomize(); pkt.display(); end endprogram
Simulator Output
array size is = 7 array = '{'h2, 'h15, 'h2, 'h2, 'h2, 'h2, 'he}标签:pkt,dynamic,Dynamic,display,Randomization,array,data,Array,size From: https://www.cnblogs.com/skyaha/p/17546865.html