Hardware interview practice
Constrain a bounded packet split
A transfer is divided into a fixed number of random packet lengths while leaving enough bytes for every remaining packet. Write a SystemVerilog constraint class for the packet lengths.
Starting point
Question code
class packet_split;
rand int unsigned len[8];
int unsigned total, count, min_len, max_len;
constraint legal_c;
endclassReviewed example
Work through one case
Input
total=12, count=3, min_len=4, and max_len=4.Expected output
The only legal randomized array is {4,4,4,0,0,0,0,0}.Three active entries must each equal four and sum to twelve, while every inactive entry is constrained to zero.
What to cover
Requirements
- randomize() is legal only when 1<=count<=8, max_len<=65535, min_len<=max_len, total<=65535, and count*min_len<=total<=count*max_len.
- For every active i<count, require min_len<=len[i]<=max_len and the sum of active lengths to equal total.
- Set every inactive len[i] for i>=count to zero.
- Add ordering or solve-before constraints only if needed for solver clarity; do not use post_randomize to repair an illegal result.
