SV Constraints #1

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.

Gadam Rahul

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

回复

要查看或添加评论,请登录

mohamed irsath I的更多文章

  • Types of Assertion

    Types of Assertion

    In this article, we are going to discuss the Types of Assertion. There are two types of assertion, They are 1)…

    4 条评论
  • What is an Assertion and its needs DV

    What is an Assertion and its needs DV

    Assertions are checks which used to verify that your design meets the given requirements. Assertions in design…

    5 条评论
  • Constraint for a Palindrome No.

    Constraint for a Palindrome No.

    In order to solve the above constraints, we first need to understand what a palindrome number is. A palindrome number…

    2 条评论
  • System Verilog Assertions

    System Verilog Assertions

    Dear Followers, We are excited to announce that our upcoming newsletter will be focusing on SystemVerilog Assertions…

    1 条评论
  • Constraint for AXI Strobe Signal

    Constraint for AXI Strobe Signal

    In the AXI protocol, the wstrb signal (write strobe) is used to indicate which bytes of the wdata signal (write data)…

    6 条评论
  • Corner case in constraint #49 Learnings & Solution

    Corner case in constraint #49 Learnings & Solution

    Constraint for a variable i) on every 2nd randomization the value should be the reverse of the previous value ii)…

    4 条评论
  • Constraint for AXI 4kb Boundary

    Constraint for AXI 4kb Boundary

    In order to achieve the above constraint, first, we need to understand what the AXI-4Kb Boundary is. AXI is a parallel…

    6 条评论
  • Constraint #49

    Constraint #49

    Constraint for a variable i) on every 2nd randomization the value should be the reverse of the previous value ii)…

    10 条评论
  • Constraint #48

    Constraint #48

    Constraint to generate any Prime No. in a given range #2 To achieve the given constraint, we utilize the prime_number…

  • Constraint #47

    Constraint #47

    Constraint to generate any one Prime No. in a given range #1 To generate a prime number, we first need to understand…

    3 条评论

社区洞察

其他会员也浏览了