Constraint #26
Constraint #26

Constraint #26

write a constraint to generate a unique no. ,and the size of the array is 10

"Formulate a constraint to generate a unique set of numbers within an array of size 10. To address this constraint, let's break it down into two key elements:

  1. The array should be of fixed size, specifically 10 elements.This can be achieved by either using a fixed-size array directly with a size of 10, or by creating a dynamic array and constraining its size to be 10.
  2. All elements within the array must be unique.This uniqueness condition can be satisfied by using the 'unique' keyword or implementing two foreach loops to ensure the generation of distinct values for the array.

class sample;
  rand bit [3:0] array1[];

  constraint array_c {
    array1.size() == 10;
    unique { array1 };
  }
endclass

module top;
  sample s = new();

  initial begin
    repeat (5) begin
      assert(s.randomize());
      $display("s=%p", s);
    end
  end
endmodule        

This code ensures that the array 'array1' has a fixed size of 10 and that all its elements are unique. The 'unique' keyword enforces the uniqueness constraint, providing a systematic way to generate distinct values within the array."

EDA Playground link for Above Example

I trust you've comprehended the methodology we employed to address the constraints and their real-world applications. Your support, in the form of likes and reposts, will fuel additional exploration. Eagerly anticipating engaging in meaningful discussions. Thank you! ??

Program Decoding

This SystemVerilog program defines a class sample and a top-level module top. The class sample has a random array named array1 of 4-bit elements. The constraints within the class ensure that the size of array1 is fixed at 10, and all elements in the array are unique.

The top module creates an instance s of the sample class and attempts to randomize it in a loop executed five times. After each randomization attempt, the program displays the values of the s object using the $display statement.

Let's break down the key elements:

  1. Class Definition (sample):rand bit [3:0] array1[];: Declares a random array named array1 consisting of 4-bit elements.constraint array_c { array1.size() == 10; unique { array1 }; }: Defines a constraint named array_c within the class. This constraint ensures that the size of array1 is fixed at 10 (array1.size() == 10) and all elements are unique (unique { array1 }).
  2. Module Definition (top):sample s = new();: Creates an instance s of the sample class.repeat (5) begin ... end: Executes the block of code inside the begin and end five times.assert(s.randomize());: Attempts to randomize the s object using the constraints defined in the sample class. The assert statement checks whether the randomization is successful.$display("s=%p", s);: Displays the values of the s object after each randomization attempt.

In summary, this program aims to generate instances of the sample class (s) with a unique array of size 10. The loop attempts randomization five times, and the results are displayed. The constraints within the class ensure that the generated arrays meet the specified criteria.


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

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

社区洞察

其他会员也浏览了