Constraint #41
In today's constraint, we aim to create a 2-dimensional array representing a lower triangle matrix with incremental values. Unlike our previous example where the lower triangle consisted of 5s, this time we're dealing with different numbers in each index of the lower triangle.
When solving such constraints, it's crucial to identify the sequence pattern. In our case, while there isn't a specific sequence, we observe that the numbers in the lower triangle are in incremental order.
Referring to the image above:
We already know how to compute a lower triangle matrix using a 2D array from Constraint #38. Here, we need to generate values from 7 to 700. When j=0, we need to generate a value greater than the previous row's last value. For example, for the second row (i=1 and j=0), the value should be greater than the first row's value at the same index (i=0 and j=0). Similarly, for the third row (i=2 and j=0), the value should be greater than the second row's value at index 1 (i=1 and j=1).
In summary, for the constraint:
if (j <= i) { array[i][j] inside {[7:200]}; if (j == 0 && i != 0) array[i][j] > array[i-1][i-1]; if (j != 0 && i != 0) array[i][j] > array[i][j-1]; } else a[i][j] == 0;
This constraint ensures that each value in the lower triangle of the array is greater than the preceding value, maintaining the incremental pattern.
Don't forget to Check out the Executable Code Below:
领英推荐
class sample;
rand bit[7:0] array[0:3][0:3];
constraint array_c {
foreach(array[i,j]){
if(j<=i) {
array[i][j] inside {[7:700]};
if(j==0 && i!=0) array[i][j] > array[i-1][i-1];
if(j!=0 && i!=0) array[i][j] > array[i][j-1];
}
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
Approach for Displaying in Matrix Format
To format the 2D array into a matrix-like display, we iterated through the array values using a foreach loop. We opted for $write instead of $display to prevent the printing of the next element of the same row on a new line. When the value of 'j' reached 3, indicating the end of the row, we used an empty $display statement to move to the next line. This approach allowed us to achieve the desired matrix format for the 2D array output.
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! ????
Here is the Breakdown of above Code:
In Class
In Module Top
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.
Senior Principal Engineer (Pre-Silicon Verification)
7 个月if(j==0 && i!=0) array[i][j] > array[i-1][i-1]; isn't it should be if(j==0 && i!=0) array[i][j] > array[i-1][j-1];
Design Verification Engineer
7 个月Afzal Mansuri Thanks for resposting