LaneCompactor<LogicType extends Logic> constructor

LaneCompactor<LogicType extends Logic>({
  1. required MaskedLanes<LogicType> maskedLanes,
  2. required int countWidth,
  3. String name = 'lane_compactor',
})

Creates a protocol-neutral stable lane compactor.

Implementation

LaneCompactor({
  required MaskedLanes<LogicType> maskedLanes,
  required this.countWidth,
  String name = 'lane_compactor',
})  : laneCount = maskedLanes.laneCount,
      super(
        definitionName: 'LaneCompactor_L${maskedLanes.laneCount}_'
            'W${maskedLanes.lanes.elementWidth}_C${countWidth}_'
            '${payloadShapeIdentity(maskedLanes.lanes)}',
        name: name,
      ) {
  if (countWidth < widthFor(laneCount + 1)) {
    throw ArgumentError.value(
      countWidth,
      'countWidth',
      'Must represent every valid lane count through $laneCount.',
    );
  }
  if (!rohdPayloadsCompatible(maskedLanes.lanes.arrayElements,
      ignoreConstantNames: true)) {
    throw ArgumentError('Input lanes must have compatible payloads.');
  }

  this.maskedLanes = addTypedInput('maskedLanes', maskedLanes);
  compactedLanes = addTypedPayloadOutput(
    this,
    'compactedLanes',
    CompactedLanes<LogicType>(
      this.maskedLanes.lanes.cloneTyped(name: '${name}_compacted_lanes'),
      countWidth: countWidth,
      name: '${name}_compacted',
    ),
  );

  final prefixCounts = <Logic>[];
  Logic runningCount = Const(0, width: countWidth);
  for (var inputLane = 0; inputLane < laneCount; inputLane++) {
    prefixCounts.add(runningCount);
    runningCount = (runningCount +
            this.maskedLanes.validMask[inputLane].zeroExtend(countWidth))
        .named('${name}_prefix_count_$inputLane');
  }

  for (var outputLane = 0; outputLane < laneCount; outputLane++) {
    Logic selected = Const(0, width: compactedLanes.lanes.elementWidth);
    for (var inputLane = laneCount - 1; inputLane >= 0; inputLane--) {
      final selectedInput = (this.maskedLanes.validMask[inputLane] &
              prefixCounts[inputLane].eq(outputLane))
          .named('${name}_select_${inputLane}_for_$outputLane');
      selected = mux(
        selectedInput,
        this.maskedLanes.lanes.arrayElements[inputLane],
        selected,
      ).named('${name}_compacted_${outputLane}_from_$inputLane');
    }
    compactedLanes.lanes.arrayElements[outputLane] <= selected;
  }
  compactedLanes.validCount <= runningCount;
  compactedLanes.consumedMask <= this.maskedLanes.validMask;
}