These are callback methods automatically called by the
randomize() function. They let you perform setup
before randomization and cleanup/calculation after.
class Packet;
rand bit [31:0] data[$];
rand int num_words;
bit [31:0] checksum; // Not randomized, calculated
constraint valid_size {
num_words inside {[1:16]};
data.size() == num_words;
}
// Called BEFORE randomization
function void pre_randomize();
$display("Starting randomization...");
// Could set up any pre-conditions here
endfunction
// Called AFTER randomization (if successful)
function void post_randomize();
// Calculate checksum from randomized data
checksum = 0;
foreach(data[i]) begin
checksum ^= data[i];
end
$display("Randomized %0d words, checksum=0x%h",
num_words, checksum);
endfunction
endclass
Packet p = new();
p.randomize(); // Calls pre_randomize, then randomize, then post_randomize
Key Points
pre_randomize() is called before solving constraints.
post_randomize() is called only if randomization succeeds.
Both are called automatically - you just need to define them.