In reply to CRVAddict:
module automatic test;
typedef struct packed {
bit [7:0] stream_num;
bit [7:0] speaker_num;
bit [7:0] length;
bit [7:0] record_type;
} speaker_record_hdr;
class my_class;
rand speaker_record_hdr my_hdr[$];
constraint my_hdr_c {
//1. Make sure that length field of the header is always is always 5 bit long
my_hdr.size() == 10;
//my_hdr.sum() with (int'(item.length)) == 5;
//2. Make sure that length field of the header is always 5.
foreach(my_hdr[i]) {
my_hdr[i].length == 5;
}
}
endclass : my_class
initial begin
my_class c;
c = new();
c.randomize();
$display("The value of hdr is %p", c.my_hdr);
end
endmodule : test
The output when the length is 5 bits long:
The value of hdr is ‘{’{stream_num:'h12, speaker_num:'h2c, length:'h1, record_type:'ha1}, '{stream_num:'he2, speaker_num:'h10, length:'h3, record_type:'h2e}, '{stream_num:'hcd, speaker_num:'h9, length:'h1, record_type:'hdf}}
The Output when the length itself is 5.
The value of hdr is ‘{’{stream_num:'h59, speaker_num:'h42, length:'h5, record_type:'hd8}, '{stream_num:'h6a, speaker_num:'hd5, length:'h5, record_type:'h85}, '{stream_num:'hcb, speaker_num:'hd3, length:'h5, record_type:'h82}, '{stream_num:'h45, speaker_num:'h48, length:'h5, record_type:'h95}, '{stream_num:'hfe, speaker_num:'h41, length:'h5, record_type:'haf}, '{stream_num:'hca, speaker_num:'hba, length:'h5, record_type:'h89}, '{stream_num:'h89, speaker_num:'heb, length:'h5, record_type:'h84}, '{stream_num:'h92, speaker_num:'h28, length:'h5, record_type:'h83}, '{stream_num:'h1d, speaker_num:'hd3, length:'h5, record_type:'hc2}, '{stream_num:'h7f, speaker_num:'h7d, length:'h5, record_type:'hee}}