Firstly, my difficulty with this example is that there is nothing that
constrains or directly sets the length of the randomized_cells[]
array to
the length of the longest sequence as you state in the text.
In fact, aren’t you saying that the array should in fact be a static
array declared as long as the longest sequence?
Secondly, isn’t the length variable redundant anyway in this class?
Would something like
`define MAX_LENGTH 100
class atm_cell_seq
rand atm_cell randomized_cells[];
constraint random_length {
randomized_cells.size() > 0;
randomized_cells.size() <= `MAX_LENGTH;
}
endclass: atm_cell_seq
Not do the job just as well?
Setting the length and allocating content of the randomized_cells[] array is something
that should be done in pre_randomize(). A static array should not be used
because you do not know a priori how long a sequence can be. That is
something up to the testcase to decide. Unless you want to hardcode
a maximum length.
Using the length variable instead of the array size itself avoids having
the array repeatedly lenthened and truncated at each
randomization attempt. Better to allocate for the longest one, then simply
use a subrange of the array and avoid memory/garbage collection issues.
It also conveniently avoids the issue of what to put into the array if it
is an array of class references and randomized to be longer. Note that the
LRM does the same by only showing arrays of scalars which do not require
additional memory allocation :-)
This is a gray area in the LRM and I wanted to avoid tool-specific issues and
went for an approach that should be portable. If you are a VCS user, there is
additional functionality/details I can describe.