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.

To view or add a comment, sign in

More articles by mohamed irsath I

  • 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 Comments
  • 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 Comments
  • 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 Comments
  • System Verilog Assertions

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

    1 Comment
  • 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 Comments
  • 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 Comments
  • 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…

    8 Comments
  • Constraint #49

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

    10 Comments
  • 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 to generate any one Prime No. in a given range #1 To generate a prime number, we first need to understand…

    3 Comments

Insights from the community

Others also viewed

Explore topics