getRange method
Returns a subset Logic. It is inclusive of startIndex
, exclusive of
endIndex
.
The startIndex
must come before the endIndex
. If startIndex
and
endIndex
are equal, then a zero-width signal is returned.
Negative/Positive index values are allowed. (The negative indexing starts from where the array ends)
If endIndex
is not provided, width of the Logic will
be used as the default values which assign it to the last index.
Logic nextVal = addOutput('nextVal', width: width);
// Example: val = 0xce, val.width = 8, bin(0xce) = "0b11001110"
// Negative getRange
nextVal <= val.getRange(-3, val.width); // = val.getRange(5,8) & output: 0b110, where the output.width=3
// Positive getRange
nextVal <= val.getRange(0, 6); // = val.slice(0, -2) & output: 0b001110, where the output.width=6
// Get range from startIndex
nextVal <= val.getRange(-3); // the endIndex will be auto assign to val.width
Implementation
Logic getRange(int startIndex, [int? endIndex]) {
endIndex ??= width;
if (endIndex == startIndex) {
return Const(0, width: 0);
}
// Given start and end index, if either of them are seen to be -ve index
// value(s) then conver them to a +ve index value(s)
final modifiedStartIndex =
IndexUtilities.wrapIndex(startIndex, width, allowWidth: true);
final modifiedEndIndex =
IndexUtilities.wrapIndex(endIndex, width, allowWidth: true);
IndexUtilities.validateRange(modifiedStartIndex, modifiedEndIndex);
return slice(modifiedEndIndex - 1, modifiedStartIndex);
}