slice method

Logic slice(
  1. int endIndex,
  2. int startIndex
)

Accesses a subset of this signal from startIndex to endIndex, both inclusive.

If endIndex comes before the startIndex on position, the returned value will be reversed relative to the original signal. Negative/Positive index values are allowed. (The negative indexing starts from where the array ends)

Logic nextVal = addOutput('nextVal', width: width);
// Example: val = 0xce, val.width = 8, bin(0xce) = "0b11001110"
// Negative Slicing
nextVal <= val.slice(val.width - 1, -3); // = val.slice(7,5) & output: 0b110, where the output.width=3

// Positive Slicing
nextVal <= val.slice(5, 0); // = val.slice(-3, -8) & output: 0b001110, where the output.width=6

Implementation

Logic slice(int endIndex, int startIndex) {
  // Given start and end index, if either of them are seen to be -ve index
  // value(s) then convert them to a +ve index value(s)
  final modifiedStartIndex = IndexUtilities.wrapIndex(startIndex, width);
  final modifiedEndIndex = IndexUtilities.wrapIndex(endIndex, width);

  if (modifiedStartIndex == 0 && modifiedEndIndex == width - 1) {
    // ignore: avoid_returning_this
    return this;
  }

  // Create a new bus subset
  return BusSubset(this, modifiedStartIndex, modifiedEndIndex).subset;
}