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 function developed earlier to generate all prime numbers in a specified range. Since directly generating prime numbers is challenging, we use this function to populate a queue with prime numbers and then use these numbers for our variable.

We populate the queue with prime numbers in the pre_randomize() method to ensure it is done before any randomization starts. To specify the range, we use a parameter, assuming MAX_VALUE is set to 100.

We will utilize the inside constraint to ensure that the variable prime_no takes on values only from the prime_q queue.

constraint prime_c { prime_no inside {prime_q} }

This queue is filled with prime numbers in the range of 0 to 100, populated using the prime_number function. This constraint guarantees that prime_no is assigned only prime numbers as the prime_q contains only the prime_no.

Even though you randomize 100 times, the number of prime numbers in a given range does not change. Therefore, it is unnecessary to generate prime numbers for every randomization. Instead, we can populate the prime_q queue once and then use it for multiple randomizations of the variable. To ensure efficiency, we guard the prime number generation function with an if condition that checks if prime_q.size() is 0. If the queue is empty, we populate it with all the prime numbers in the given range. Once the queue is populated, the if condition fails, preventing the function from executing in subsequent randomizations.

if(prime_q.size()==0) begin
       prime_number function
end        

Check the Below Code:

parameter MAX_VALUE = 100;
class sample;
  rand int prime_no;
       int prime_q [$];
  
  constraint array_c {
    prime_no inside { prime_q};
    }
  
  function void pre_randomize();
    bit fail_f;
    if(prime_q.size()==0) begin
      $display("Prime Number Generation in the range %0d",MAX_VALUE);
    	for(int i=2; i< MAX_VALUE; i++) begin
        	fail_f = 0;
      		for(int j=2; j <= i**0.5; j++) begin
          		if(i%j ==0) begin
            		fail_f =1;
            		j = i;
          		end
      		end
        	if(fail_f == 0) prime_q.push_back(i);
      	end
      	$display("Prime_queue = %p",prime_q);
    end
  endfunction

endclass
                             
module top;
  sample s=new();
  
  initial begin
    $display("######## OUTPUT ########");
    repeat(5) begin
      assert(s.randomize());
      $display("prime_no = %0d",s.prime_no);
    end
    $display("######## END ########");
  end
endmodule        

I appreciate your understanding of the methodology we've utilized to handle constraints and their real-world applications. Your support, through likes and reposts, will indeed encourage further exploration. I'm eagerly looking forward to engaging in meaningful discussions with you. Thank you! ????


Decoding of the Above Code :

Parameter Declaration

  • MAX_VALUE: Declares a parameter named "MAX_VALUE" with a default value of 100.

In Class

  • Class Declaration: Declares a class named "sample" containing a random variable "prime_no" to store a prime number, and a dynamic array "prime_q" to store a list of prime numbers.
  • Random Variable Declaration:Declares a random integer variable named "prime_no" within the "sample" class. The variable is declared as a random variable (rand), which means it will be randomized during object creation.Declares a dynamic array named "prime_q" to store a list of prime numbers.
  • Constraint: Defines a constraint named "array_c" which imposes the following condition on the "prime_no" variable:The value of "prime_no" must be one of the values in the "prime_q" array.

Function pre_randomize():

  • Checks if the "prime_q" array is empty.
  • If the array is empty, it generates a list of prime numbers up to the value of "MAX_VALUE" using the Sieve of Eratosthenes algorithm and stores them in the "prime_q" array.

In Module Top

  • Instantiation of Class: Instantiates an object of the "sample" class named "s".
  • Initial Block: Executes the following actions:Displays a header indicating the beginning of the output.Repeats 5 times:Randomizes the data members of the object "s" according to the constraints specified in the class definition.Displays the value of "prime_no".Displays a footer indicating the end of the output.

This program generates random prime numbers using a dynamic array "prime_q" to store precomputed prime numbers up to a maximum value defined by "MAX_VALUE". The output displays the generated values of "prime_no".


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

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…

    7 条评论
  • 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 #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 条评论
  • Constraint #46

    Constraint #46

    Significance of new() for a RANDC Variable. When using the keyword in SystemVerilog, a variable is declared to have…

社区洞察

其他会员也浏览了