ScheduledFramedLaneAligner<LogicType extends Logic> constructor

ScheduledFramedLaneAligner<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required Frame<CompactedLanes<LogicType>> inputFrame,
  4. required Logic start,
  5. required Logic write,
  6. required Logic advance,
  7. required Logic inputOffset,
  8. required Logic outputOffset,
  9. required Logic advanceCount,
  10. int bufferBeats = 2,
  11. String name = 'scheduled_framed_lane_aligner',
})

Creates an explicitly scheduled framed lane aligner.

Implementation

ScheduledFramedLaneAligner({
  required Logic clk,
  required Logic reset,
  required Frame<CompactedLanes<LogicType>> inputFrame,
  required Logic start,
  required Logic write,
  required Logic advance,
  required Logic inputOffset,
  required Logic outputOffset,
  required Logic advanceCount,
  this.bufferBeats = 2,
  String name = 'scheduled_framed_lane_aligner',
})  : laneCount = inputFrame.data.laneCount,
      super(
        definitionName: 'ScheduledFramedLaneAligner_L'
            '${inputFrame.data.laneCount}_B$bufferBeats'
            '_W${inputFrame.data.lanes.elementWidth}_'
            'I${inputOffset.width}_O${outputOffset.width}_'
            'A${advanceCount.width}_${payloadShapeIdentity(inputFrame)}',
        name: name,
      ) {
  if (bufferBeats < 1) {
    throw ArgumentError.value(
      bufferBeats,
      'bufferBeats',
      'Must retain at least one beat.',
    );
  }
  if (!rohdPayloadsCompatible(inputFrame.data.lanes.arrayElements,
      ignoreConstantNames: true)) {
    throw ArgumentError('Input lanes must have compatible payloads.');
  }

  clk = addInput('clk', clk);
  reset = addInput('reset', reset);
  this.inputFrame = addTypedInput('inputFrame', inputFrame);
  this.start = addInput('start', start);
  this.write = addInput('write', write);
  this.advance = addInput('advance', advance);
  this.inputOffset = addInput(
    'inputOffset',
    inputOffset,
    width: inputOffset.width,
  );
  this.outputOffset = addInput(
    'outputOffset',
    outputOffset,
    width: outputOffset.width,
  );
  this.advanceCount = addInput(
    'advanceCount',
    advanceCount,
    width: advanceCount.width,
  );
  alignedFrame = addTypedPayloadOutput(
    this,
    'alignedFrame',
    Frame<MaskedLanes<LogicType>>(
      MaskedLanes<LogicType>(
        this.inputFrame.data.lanes.cloneTyped(name: '${name}_aligned_lanes'),
        Logic(name: '${name}_aligned_mask', width: laneCount),
        name: '${name}_aligned_payload',
      ),
      name: '${name}_aligned_frame',
    ),
  );
  addOutput('presenting');
  addOutput('canWrite');
  addOutput(
    'bufferedCount',
    width: widthFor((laneCount * bufferBeats) + 1),
  );
  addOutput('invalidInputCount');

  final capacity = laneCount * bufferBeats;
  final queueCountWidth = widthFor(capacity + 1);
  final arithmeticWidth = max(
        queueCountWidth,
        max(
          this.advanceCount.width,
          max(this.outputOffset.width, this.inputOffset.width),
        ),
      ) +
      1;
  final normalizedInputOffset =
      _normalizedOffset(this.inputOffset, arithmeticWidth);
  final rotatedLanes =
      TypedLogicArray<_ScheduledFramedLane<LogicType>, LogicValue>(
    [laneCount],
    ({name}) => _ScheduledFramedLane<LogicType>(
      this.inputFrame.data.lanes.arrayElements.first.cloneTyped(name: 'data'),
      name: name,
    ),
    name: '${name}_rotated_lanes',
  );
  final rotatedInput = CompactedLanes<_ScheduledFramedLane<LogicType>>(
    rotatedLanes,
    countWidth: this.inputFrame.data.countWidth,
    name: '${name}_rotated_input',
  );
  rotatedInput.validCount <= this.inputFrame.data.validCount;
  rotatedInput.consumedMask <= this.inputFrame.data.consumedMask;
  for (var streamLane = 0; streamLane < laneCount; streamLane++) {
    final sourceIndex =
        normalizedInputOffset + Const(streamLane, width: arithmeticWidth);
    final wrappedSourceIndex = mux(
      sourceIndex.gte(Const(laneCount, width: arithmeticWidth)),
      sourceIndex - Const(laneCount, width: arithmeticWidth),
      sourceIndex,
    );
    Logic selected = Const(
      0,
      width: this.inputFrame.data.lanes.elementWidth,
    );
    for (var physicalLane = laneCount - 1;
        physicalLane >= 0;
        physicalLane--) {
      selected = mux(
        wrappedSourceIndex.eq(physicalLane),
        this.inputFrame.data.lanes.arrayElements[physicalLane],
        selected,
      );
    }
    rotatedLanes.arrayElements[streamLane].data <= selected;
    rotatedLanes.arrayElements[streamLane].last <=
        this.inputFrame.last &
            this.inputFrame.data.validCount.eq(streamLane + 1);
  }

  final outputOccupied = Logic(name: '${name}_output_occupied');
  final outputConsumed =
      Logic(name: '${name}_output_consumed', width: queueCountWidth);
  final effectiveAdvance =
      (this.advance & outputOccupied & ~this.start).named(
    '${name}_effective_advance',
  );
  final queue = ScheduledCountedLaneWindow<_ScheduledFramedLane<LogicType>>(
    clk: clk,
    reset: reset,
    inputLanes: rotatedInput,
    load: this.start & this.write,
    write: ~this.start & this.write,
    advance: effectiveAdvance,
    advanceCount: outputConsumed,
    clear: this.start & ~this.write,
    bufferBeats: bufferBeats,
    name: '${name}_window',
  );

  final firstOutputPending = Logic(name: '${name}_first_output_pending');
  final frameOutputOffset = Logic(
    name: '${name}_frame_output_offset',
    width: this.outputOffset.width,
  );
  final current = queue.bufferedCount.zeroExtend(arithmeticWidth);
  final consumedCount = mux(
    effectiveAdvance,
    outputConsumed.zeroExtend(arithmeticWidth),
    Const(0, width: arithmeticWidth),
  );
  final remainingCount = current - consumedCount;
  final captureQueueOffset = consumedCount;
  final captureQueueCount = remainingCount;
  final captureFirstOutputPending = mux(
    effectiveAdvance,
    Const(0),
    firstOutputPending,
  );
  final normalizedOutputOffset =
      _normalizedOffset(frameOutputOffset, arithmeticWidth);
  final firstOutputOffset = mux(
    captureFirstOutputPending,
    normalizedOutputOffset,
    Const(0, width: arithmeticWidth),
  );
  final outputSpace =
      Const(laneCount, width: arithmeticWidth) - firstOutputOffset;
  final requestedAdvance = this.advanceCount.zeroExtend(arithmeticWidth);
  final clampedAdvance = mux(
    requestedAdvance.eq(0),
    Const(1, width: arithmeticWidth),
    requestedAdvance,
  );
  final limitedAdvance = mux(
    clampedAdvance.lte(captureQueueCount),
    clampedAdvance,
    captureQueueCount,
  );
  final initialOffer = mux(
    limitedAdvance.lte(outputSpace),
    limitedAdvance,
    outputSpace,
  );
  var offeredCount = initialOffer;
  Logic offeredLast = Const(0);
  for (var lane = laneCount - 1; lane >= 0; lane--) {
    Logic queuedLast = Const(0);
    for (var source = capacity - 1; source >= 0; source--) {
      queuedLast = mux(
        (captureQueueOffset + Const(lane, width: arithmeticWidth))
            .eq(Const(source, width: arithmeticWidth)),
        queue.bufferedLanes.arrayElements[source].last,
        queuedLast,
      );
    }
    final endsOffer =
        queuedLast & Const(lane, width: arithmeticWidth).lt(initialOffer);
    offeredCount = mux(
      endsOffer,
      Const(lane + 1, width: arithmeticWidth),
      offeredCount,
    );
    offeredLast = endsOffer | offeredLast;
  }
  final captureOutput = ~this.start &
      (~outputOccupied | effectiveAdvance) &
      captureQueueCount.neq(0);

  presenting <= outputOccupied & ~this.start;
  canWrite <= this.start | queue.canWrite;
  bufferedCount <= queue.bufferedCount;
  invalidInputCount <= queue.invalidInputCount;

  Sequential(clk, reset: reset, resetValues: {
    firstOutputPending: 1,
    frameOutputOffset: 0,
    outputOccupied: 0,
    outputConsumed: 0,
    alignedFrame.data.validMask: 0,
    alignedFrame.last: 0,
    for (final lane in alignedFrame.data.lanes.arrayElements) lane: 0,
  }, [
    If(this.start, then: [
      firstOutputPending < 1,
      frameOutputOffset < this.outputOffset,
      outputOccupied < 0,
    ]),
    If(captureOutput, then: [
      firstOutputPending < 0,
      outputOccupied < 1,
      outputConsumed < offeredCount.getRange(0, queueCountWidth),
      alignedFrame.data.validMask <
          _outputMask(firstOutputOffset, offeredCount),
      alignedFrame.last < offeredLast,
      for (var destination = 0; destination < laneCount; destination++)
        alignedFrame.data.lanes.arrayElements[destination] <
            _outputLane(
              destination: destination,
              outputOffset: firstOutputOffset,
              offeredCount: offeredCount,
              queueOffset: captureQueueOffset,
              queueLanes: queue.bufferedLanes.arrayElements,
            ),
    ]),
    If(effectiveAdvance & ~captureOutput, then: [
      outputOccupied < 0,
    ]),
  ]);
}