Constraint #38

Constraint #38

Constraints can be applied to a 2-dimensional array to form a lower triangular matrix, as shown below:

5? 0? 0? 0

5? 5? 0? 0

5? 5? 5? 0

5? 5? 5? 5

In this matrix, each row has one more non-zero element than the previous row, and all elements above the main diagonal are zero. To implement this constraint, we can use a 2-dimensional array with a size of 4x4.

rand bit[8:0] array[3:0][3:0];

Use this to understand the Below Explanation

We iterate through the array using two indices,

i for the row and j for the column. The value in each cell depends on the relationship between i and j.

For the first row, i=0, and only the element at (0,0) should be 5, with all other elements being 0.

For the second row, i=1, elements at (1,0) and (1,1) should be 5, with all other elements being 0.

For the third row, i=2, elements at (2,0), (2,1), and (2,2) should be 5, with all other elements being 0.

Based on the observation, when I <= j, the value is 5. Else 0

To write the constraint for the lower triangular matrix pattern, we need to use two foreach loops. Like Mentioned below

foreach(array[i]) { ????????? foreach(array[i]) {? ???????? // condition } }

we can use a single foreach loop with two variables. The condition for the value assignment is such that when j ≤ i, the value should be 5, otherwise 0.

??? foreach (array[i,j]) {

??????? if (i >= j) ???array[i][j] == 5;

??????? else ??????????array[i][j] == 0;

??????? }

This constraint ensures the array forms a lower triangular matrix with the specified pattern.

class sample;
  rand bit[7:0] array[0:3][0:3];
  
  constraint array_c {
    foreach(array[i,j]){
      if(j<=i) {
        array[i][j] == 5;
      }
      else array[i][j] == 0;
    }
  }
endclass
                             
module top;
  sample s=new();
  
  initial begin
    $display("######## OUTPUT ########");
      assert(s.randomize());
      foreach(s.array[i,j]) begin
          $write("%0d \t",s.array[i][j]);
        if(j==3)
          $display("");
      end
    $display("######## END ########");
  end
endmodule        
https://www.edaplayground.com/x/Xgp2

EDA link for the Above EX. Click Me

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! ????


Let's Break Down the Above Constraint:

In Class

  • Class Declaration: Declares a class named "sample" containing a random 2D array "array" of 8-bit elements.
  • Random 2D Array Declaration: Declares a random 2D array named "array" with dimensions [0:3][0:3] within the "sample" class. The array is declared as a random variable (rand), which means it will be randomized during object creation.
  • Constraint: Defines a constraint named "array_c" which imposes the following conditions on the "array" variable:For each element in the 2D array "array[i][j]":If the column index "j" is less than or equal to the row index "i", the element must be 5.Otherwise, the element must be 0.

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.Randomizes the data members of the object "s" according to the constraints specified in the class definition.Displays the values of the randomized 2D array "array" in a tabular format using a nested loop.Displays a footer indicating the end of the output.

This program generates random 2D arrays with specific patterns of elements based on the constraints specified. The output displays the generated 2D array in a tabular format for better understanding.


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

社区洞察

其他会员也浏览了