User guide

24-101
SystemVerilog Testbench Constructs
constraint con_size {
s > 0;
s < 50;
}
endclass
class Pkt;
integer header[7];
rand bit [7:0] payload[];
size sz;
function void pre_randomize();
/* Randomize the sz.s variable and instantiate
the dynamic array. */
integer res;
sz = new();
res = sz.randomize(); /* calling randomize()
on object sz of class size. Randomizes rand
variable s (constrained to >0 and <50 */
if(res==0)
$display("Randomization Failed");
else
begin
if((sz.s>0) && (sz.s<50))/* check for
size between 0 and 50*/
payload = new[sz.s];/* the new[] operator
allocates storage and initializes the
rand variable s in sz*/
else
$display("Failed to generate proper
size");
end //end of outer else block
endfunction
function void post_randomize();
/* display size of payload dynamic array using
the size() built-in method*/
$display("payload.size = %d", payload.size);
endfunction
endclass
Pkt pkt;