SystemVerilog classes contain the pre-defined function post_randomize(), which is automatically called at the end of the randomization. One can override this function to do extra processing after randomization.
In SystemVerilog post_randomize() is called top-down and not bottom-up!
The top-down call of post_randomize() is counter-intuitive, especially for those of you in love with e-Language, and it can cause problems that require extra debug effort.
Let’s consider for the sake of the example that we have a simple packet made of a header, 8 bytes of data and a CRC. The header has its own CRC.
Both Header’s and Packet’s CRCs are computed once randomization is finished:
class header;
//random info
rand byte header_info[2];
//crc calculated based on the generated data
byte header_crc;
...
function void post_randomize();
header_crc = compute_header_crc();
$display("header: calling post_randomize() - CRC is: %X", header_crc);
endfunction
endclass
class packet;
rand header header_i;
rand byte data[8];
// crc calculated based on the generated header
byte packet_crc;
...
function void post_randomize();
// At this point the header is randomized, but its CRC ia not computed yet
packet_crc = compute_packet_crc();
$display("packet: calling post_randomize() - header_i.CRC is: %X", header_i.header_crc);
endfunction
endclass
The issue in the code snippet above is: header_crc isn’t available at the time packet_crc is computed since packet.post_randomize() is called before header.post_randomize(). packet_crc will be computed using the default value of header_crc which is 8’h00.
Here is the output:
OUTPUT:
packet: calling post_randomize() - header_i.CRC is: 00
header: calling post_randomize() - CRC is: 8e
Take care e-Language speakers!!!
Hamilton
February 27th, 2015 05:58:07