I want to compare variable-width slices of fixed arrays. Since the part selects (-:,+:) require a constant width, I can’t use those.
My first attempt was to try something like:
bit [31:0] array1[100];
bit [31:0] array2[100];
int length_to_compare = 70; // This needs to be able to change
if (array1[0:length_to_compare] == array2[0:length_to_compare]) // This doesn't work
// Do Something
But this generates a compilation error.
I next tried to use the streaming operators:
// Compiles but has runtime errors
if( {>>{array1 with [0:length_to_compare]}} == {>>{array1 with [0:length_to_compare]}} )
// Do Something
array1.and() with (item.index >= length_to_compare || item == array2[item.index])
Absolute pure genius!
Your solution made me go look up in the LRM that the .and() method can do alternate "AND"ing operation when you use the with clause to do something other than just AND the elements of the same array together.
I am mildly confused about this part- item.index>=length_to_compare
Does this limit comparison to length_to_compare (starting from 0) or starts comparison from length_to_compare to the end of the array?
The clause under the with condition, as long as it evaluates to TRUE, the elements are ANDED. Is this statement true. If so, I believe, this would convey the intent more clearly -
array1.and() with (item.index<=length_to_compare ? item == array2[item.index] : 1)
module m;
bit [31:0] array1[100];
bit [31:0] array2[100];
int length_to_compare = 70; // This needs to be able to change
initial
begin
foreach(array1[i]) begin
if (i>length_to_compare) begin
array1[i] = i+4;
array2[i] = i+3;
end
else begin
array1[i] = i;
array2[i] = i;
end
end
end
initial begin
#2;
$display("%d", array1.and() with (item.index<=length_to_compare ? item == array2[item.index] : 1));
end
endmodule
It ensures that anything above length_to_compare is always true and only compares with the other array elements whose indicies are < length_to_compare.
Never mind. I got what you said. I just thought the different coding style just made the intent clearer for people like me, who are not very advanced users of the language.
Is there something similar also for packed array with more than one dimension specified on the left side? For example:
bit [2:0][31:0][100:0] array1;
bit [2:0][31:0][100:0] array2;
How can I check if array1[2:0][31:0][length_to_compare:0] == array2[2:0][31:0][length_to_compare:0]?