slice method

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

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

@override
Logic slice(int endIndex, int startIndex) {
  final modifiedStartIndex = IndexUtilities.wrapIndex(startIndex, width);
  final modifiedEndIndex = IndexUtilities.wrapIndex(endIndex, width);

  if (modifiedStartIndex <= modifiedEndIndex) {
    return getRange(modifiedStartIndex, modifiedEndIndex + 1);
  } else {
    return getRange(modifiedEndIndex, modifiedStartIndex + 1).reversed;
  }
}