ScheduledCountedLaneWindow<LogicType extends Logic> constructor

ScheduledCountedLaneWindow<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required CompactedLanes<LogicType> inputLanes,
  4. required Logic load,
  5. required Logic write,
  6. required Logic advance,
  7. required Logic advanceCount,
  8. required Logic clear,
  9. int bufferBeats = 2,
  10. String name = 'scheduled_counted_lane_window',
})

Creates an explicitly scheduled counted-lane window.

Implementation

ScheduledCountedLaneWindow({
  required Logic clk,
  required Logic reset,
  required CompactedLanes<LogicType> inputLanes,
  required Logic load,
  required Logic write,
  required Logic advance,
  required Logic advanceCount,
  required Logic clear,
  this.bufferBeats = 2,
  String name = 'scheduled_counted_lane_window',
})  : laneCount = inputLanes.laneCount,
      capacity = inputLanes.laneCount * bufferBeats,
      super(
        definitionName: 'ScheduledCountedLaneWindow_L'
            '${inputLanes.laneCount}_B$bufferBeats'
            '_W${inputLanes.lanes.elementWidth}_A${advanceCount.width}_'
            '${payloadShapeIdentity(inputLanes)}',
        name: name,
      ) {
  if (bufferBeats < 1) {
    throw ArgumentError.value(
      bufferBeats,
      'bufferBeats',
      'Must retain at least one beat.',
    );
  }
  if (!rohdPayloadsCompatible(inputLanes.lanes.arrayElements,
      ignoreConstantNames: true)) {
    throw ArgumentError('Input lanes must have compatible payloads.');
  }

  clk = addInput('clk', clk);
  reset = addInput('reset', reset);
  this.inputLanes = addTypedInput('inputLanes', inputLanes);
  this.load = addInput('load', load);
  this.write = addInput('write', write);
  this.advance = addInput('advance', advance);
  this.advanceCount = addInput(
    'advanceCount',
    advanceCount,
    width: advanceCount.width,
  );
  this.clear = addInput('clear', clear);

  window = addTypedPayloadOutput(
    this,
    'window',
    CompactedLanes<LogicType>(
      this.inputLanes.lanes.cloneTyped(name: '${name}_window_lanes'),
      countWidth: this.inputLanes.countWidth,
      name: '${name}_window',
    ),
  );
  bufferedLanes = addTypedPayloadOutput(
    this,
    'bufferedLanes',
    TypedLogicArray<LogicType, LogicValue>(
      [capacity],
      this.inputLanes.lanes.arrayElements.first.cloneTyped,
      name: '${name}_buffered_lanes',
    ),
  );
  addOutput(
    'bufferedCount',
    width: widthFor(capacity + 1),
  );
  addOutput('canWrite');
  addOutput('invalidInputCount');
  addOutput('invalidAdvance');

  final countWidth = bufferedCount.width;
  final arithmeticWidth = max(
        countWidth,
        max(this.advanceCount.width, this.inputLanes.validCount.width),
      ) +
      1;
  final storedCount = Logic(name: '${name}_stored_count', width: countWidth);
  // Keep routed state packed to avoid mixed field and register RTL drivers.
  final storedLanes = List<Logic>.generate(
    capacity,
    (index) => Logic(
        name: '${name}_stored_lane_$index',
        width: this.inputLanes.lanes.elementWidth),
  );
  final current = storedCount.zeroExtend(arithmeticWidth);
  final replacing = (this.load & ~this.clear).named('${name}_replacing');
  final baseCurrent = mux(
    this.clear | replacing,
    Const(0, width: arithmeticWidth),
    current,
  );
  final offeredWide = mux(
    baseCurrent.lte(Const(laneCount, width: arithmeticWidth)),
    baseCurrent,
    Const(laneCount, width: arithmeticWidth),
  );
  final inputCount = this.inputLanes.validCount.zeroExtend(arithmeticWidth);
  final inputCountValid = inputCount.neq(0) &
      inputCount.lte(Const(laneCount, width: arithmeticWidth));
  final capacityForFullBeat =
      current.lte(Const(capacity - laneCount, width: arithmeticWidth));
  final appending =
      (this.write & ~this.load & ~this.clear & capacityForFullBeat)
          .named('${name}_appending');
  final inputAction = (replacing | appending).named('${name}_input_action');
  final acceptedInputCount = mux(
    inputAction & inputCountValid,
    inputCount,
    Const(0, width: arithmeticWidth),
  );
  final requestedAdvance = this.advanceCount.zeroExtend(arithmeticWidth);
  final advanceAction =
      (this.advance & ~this.clear & ~replacing).named('${name}_advance');
  final hasOfferedData = offeredWide.neq(0);
  final advanceValid = requestedAdvance.neq(0) &
      requestedAdvance.lte(offeredWide) &
      hasOfferedData;
  final clampedAdvance = mux(
    requestedAdvance.eq(0),
    Const(1, width: arithmeticWidth),
    mux(
      requestedAdvance.lte(offeredWide),
      requestedAdvance,
      offeredWide,
    ),
  );
  final consumedCount = mux(
    advanceAction & hasOfferedData,
    clampedAdvance,
    Const(0, width: arithmeticWidth),
  );
  final remainingCount = baseCurrent - consumedCount;
  final nextCount = remainingCount + acceptedInputCount;

  bufferedCount <= storedCount;
  canWrite <= capacityForFullBeat;
  invalidInputCount <=
      (this.load | this.write) & ~this.clear & ~inputCountValid;
  invalidAdvance <= advanceAction & ~advanceValid;
  window.validCount <=
      offeredWide.getRange(0, this.inputLanes.validCount.width);
  window.consumedMask <= Const(0, width: laneCount);
  for (var lane = 0; lane < laneCount; lane++) {
    window.lanes.arrayElements[lane] <= storedLanes[lane];
  }
  for (var lane = 0; lane < capacity; lane++) {
    bufferedLanes.arrayElements[lane] <= storedLanes[lane];
  }

  Sequential(clk, reset: reset, resetValues: {
    storedCount: 0,
    for (final lane in storedLanes) lane: 0,
  }, [
    storedCount < nextCount.getRange(0, countWidth),
    for (var destination = 0; destination < capacity; destination++)
      storedLanes[destination] <
          _nextLane(
            destination: destination,
            current: baseCurrent,
            consumedCount: consumedCount,
            remainingCount: remainingCount,
            inputAction: inputAction & inputCountValid,
            inputCount: inputCount,
            storedLanes: storedLanes,
            incomingLanes: this.inputLanes.lanes.arrayElements,
          ),
  ]);
}