Constraint #40

Constraint #40

We have solved a similar constraint in our #34 article. The approach we used in that article may not be supported in some simulators or versions of the simulator. In such cases, we can use the approach discussed below.

In our case, we need to generate a real number from 3.5 to 5.5. we can generate a whole number from 35 to 55. Then, divide the number by 10 in the post_randomize() function, which will give us the Required real number.

To achieve this, we need to declare two variables: one integer type to generate the whole number (which should be rand) and the actual variable of the real type (which should not be a rand variable as mentioned before):

rand int a_temp; real a;

Constraint to generate the Whole number From 35 to 55

constraint a_c { a_temp inside {[35:55]}; }

then Divide the Whole number by 10 and assign it to the real Variable

function void post_randomize(); a = a_temp/10.0; // always provide .0 decimal value for real division. endfunction

By using the same method, you can generate any number. In the constraint, generate the whole number, which is the value multiplied by 10^n, where n is the number of decimals. In our case, the decimal is 1, so 3.5 10 = 35 to 5.5 10 = 55. Then, divide the generated number by 10^n in the post_randomize method.

Here is an example to generate a real number from 65.58 to 75.29:

In this case, the required decimal point is 2, so n = 2. In the constraint, generate the range 65.58 100 = 6558 to 75.29 100 = 7529, and then divide the generated number by 100 in the post_randomize method.

b_temp inside {[6558:7529]};

function void post_randomize(); b = b_temp/100.0; // always provide . decimal value for real division. endfunction

This method allows us to generate real numbers within a specified range even when the simulator does not directly support generating real numbers in that range.

Check out the Executable Code:

class sample;
  rand int 	 a_temp;
  		real a;
  
  constraint a_c {
    a_temp inside {[35:55]};
  }
  function void post_randomize();
    a = a_temp/10.0;
  endfunction
endclass

module top;
  sample s=new();
  
  initial begin 
    $display("########OUTPUT########");
    repeat(5) begin
      s.randomize();
      $display("The value of a = %0.1f",s.a);
    end
    $display("########END########");
  end
endmodule        
https://www.edaplayground.com/x/bEmJ

The EDA Link for the Above Code - Click Here

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:

In Class

  • Class Declaration: Declares a class named "sample" containing a random integer variable "a_temp" and a real variable "a".
  • Random Variable Declaration:Declares a random integer variable named "a_temp" within the "sample" class. The variable is declared as a random variable (rand), which means it will be randomized during object creation.Declares a real variable named "a" within the "sample" class.
  • Constraint: Defines a constraint named "a_c" which imposes the following condition on the "a_temp" variable:The value of "a_temp" must be in the range [35, 55].
  • Post-Randomize Function: Defines a post-randomize function named "post_randomize()" which calculates the value of "a" based on the value of "a_temp" after randomization. It divides "a_temp" by 10.0 and assigns the result to "a".

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 the randomized variable "a" with one decimal place.Displays a footer indicating the end of the output.

This program generates random values for the variable "a_temp" and calculates the corresponding value of "a" based on the constraint and the post-randomize function. The output displays the calculated value of "a" with one decimal place.

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

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

社区洞察

其他会员也浏览了