SV Constraints #1
Question: Constraint to populate a Queue with the size of 10 to 20 elements, each element should have a value range between 200 to 220, and elements in the queue should be unique values.
class eth_pkt;
rand int payload[$];
rand int len;
constraint random_c {
soft len inside {[10:20]};
payload.size() == len;
foreach (payload[i]) {
payload[i] inside {[200:220]};
}
unique {payload};
}
endclass
module top;
eth_pkt pkt=new();
initial begin
assert(pkt.randomize() with {len == 20;});
$display("r = %p",pkt.payload);
end
endmodule
The constraint defined in the eth_pkt class aims to populate the payload queue with a size ranging from 10 to 20 elements, where each element should have a value between 200 to 220. Additionally, the constraint ensures that all elements in the queue are unique.
Explanation:
soft len inside {[10:20]};: This soft constraint specifies that the variable len should have a random value between 10 and 20, inclusive.
payload.size() == len;: This constraint ensures that the size of the payload queue is equal to the value of len, ensuring that the queue has the desired size.
foreach (payload[i]) { payload[i] inside {[200:220]}; }: This loop constraint iterates over each element in the payload queue and ensures that each element falls within the range of 200 to 220.
unique {payload};: This constraint ensures that all elements in the payload queue are unique, preventing any duplicates.
The assert(pkt.randomize() with {len == 20;}); statement in the top module is used to randomize an instance of eth_pkt (pkt) and specifies that the len variable should be set to 20. Finally, the $display statement prints the randomized payload queue for verification.
Overall, this constraint ensures that the payload queue is populated with unique elements, each within the specified range, and that the queue size falls within the specified range of 10 to 20 elements.