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.
Rtl Design Engineer @ PRSsemicon Technologies
10 个月Sir tried this pb here you are using soft constraint so that you can change as your wish during randomization but I wish to be any size between 10 to 20.So I am not using soft constraint sir and written code for it sir but I am getting an following error:- ?class gen; ?int my_queue[$]; ??int dsize; ?? ?constraint d1{ ??dsize inside {[10:20]}; ??my_queue.size()==dsize; ??foreach(my_queue[i]) my_queue[i] inside {[200:220]}; ??unique {my_queue}; ?} ?? ??function void display(); ???$display("%p",my_queue);?? ??endfunction ?? ?endclass? ////////////// module tb; ?gen g1; ?initial begin ??g1=new(); ??assert(g1.randomize()) else $display("randomization failed"); ??g1.display(); ?end endmodule # RCKERNEL: Warning: RC_0024 testbench.sv(24): Randomization failed. The condition of randomize call cannot be satisfied. # RCKERNEL: Info: RC_0103 testbench.sv(24): ... the following condition cannot be met: (10<=(g1.dsize=0)) # RCKERNEL: Info: RC_1007 testbench.sv(3): ... see class 'gen' declaration. # KERNEL: randomization failed # KERNEL: '{} Can you please explain why randomization is failed sir