Unique Constraint - using Dynamic Arrays

Hi all,

Just a quick question on using Dynamic Arrays on Unique Constraints.

// `timescale 1ms / 1ns

module example_unique (

);
/*-----CLASSES-----*/
class the_inputs; 
    rand bit [2:0]  inputA;
    rand bit [2:0]  inputB;

    rand int the_array[8];    
    rand int that_array[8];

    constraint limit {
                        unique {inputB, inputA};
                        inputB inside {[0:7]};
                        inputA inside {[0:7]};
                     }

    constraint limit_array {    unique {the_array};

                                foreach(the_array[i]) 
                                    {the_array[i] inside {[0:7]};}   
                            }

    constraint lim_arrver2 {    unique{that_array};

                                foreach(that_array[i]) 
                                    {that_array[i] inside {[0:7]};}                            
                            }

endclass :  the_inputs


initial begin
    the_inputs test1; 
    test1 = new();

    for (int i = 0; i < 8; i++)
    begin
        test1.randomize(); 
        $display("inputA: %0d   inputB: %0d   arrayA: %0d   arrayB: %0d ", test1.inputA, test1.inputB, test1.the_array[i], test1.that_array[i]);

    end

    $finish; 
    end

endmodule

I was just wondering for my declaration/instantiation of the two arrays within the class the_inputs, how would the following code be different if I were to use dynamic arrays of undefined size? For example, what if it were to be:

class the_inputs; 
    rand bit [2:0]  inputA;
    rand bit [2:0]  inputB;

    rand int the_array[];    
    rand int that_array[];

    constraint limit {
                        unique {inputB, inputA};
                        inputB inside {[0:7]};
                        inputA inside {[0:7]};
                     }

    constraint limit_array {    unique {the_array};

                                foreach(the_array[i]) 
                                    {the_array[i] inside {[0:7]};}   
                            }

    constraint lim_arrver2 {    unique{that_array};

                                foreach(that_array[i]) 
                                    {that_array[i] inside {[0:7]};}                            
                            }

endclass :  the_inputs

Would anything be different, or is it something I have to make changes in the rest of my code?

Thank you!

Sangwoo

You should new the dynamic array before you use foreach.

The SystemVerilog solver will not change the size of a dynamic array without a constraint on the size of the array. Before randomization starts, you either need to construct with new[], initialize it from another array, or have a constraint on its size.

1 Like