ReadyValidLaneTagAllocator<LogicType extends Logic> constructor

ReadyValidLaneTagAllocator<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required ReadyValidInterface<CompactedLanes<LogicType>> upstream,
  4. required Logic allocationRequired,
  5. required List<ReadyValidInterface<Logic>> allocations,
  6. required List<Logic> allocationMasksAfterPrefixes,
  7. required ReadyValidInterface<CompactedLanes<AllocatedLane<LogicType>>> downstream,
  8. Logic? discardAllocations,
  9. String name = 'ready_valid_lane_tag_allocator',
})

Creates an atomic counted-lane tag binder.

Implementation

ReadyValidLaneTagAllocator(
    {required Logic clk,
    required Logic reset,
    required ReadyValidInterface<CompactedLanes<LogicType>> upstream,
    required Logic allocationRequired,
    required List<ReadyValidInterface<Logic>> allocations,
    required List<Logic> allocationMasksAfterPrefixes,
    required ReadyValidInterface<CompactedLanes<AllocatedLane<LogicType>>>
        downstream,
    Logic? discardAllocations,
    super.name = 'ready_valid_lane_tag_allocator'})
    : laneCount = upstream.data.laneCount,
      allocationLaneCount = allocations.length,
      tagWidth = allocations.isEmpty ? 0 : allocations.first.data.width,
      allocationCapacity = allocationMasksAfterPrefixes.isEmpty
          ? 0
          : allocationMasksAfterPrefixes.first.width,
      super(
          definitionName: _laneTagAllocatorDefinitionName(
              upstream,
              allocations,
              allocationMasksAfterPrefixes,
              downstream,
              discardAllocations != null)) {
  if (laneCount < 1 || allocationLaneCount < 1 || allocationCapacity < 1) {
    throw ArgumentError(
        'Payload lanes, allocation lanes, and capacity must be positive.');
  }
  if (allocationRequired.width != laneCount ||
      allocations.any((allocation) => allocation.data.width != tagWidth) ||
      (1 << tagWidth) < allocationCapacity ||
      allocationMasksAfterPrefixes.length != allocationLaneCount + 1 ||
      allocationMasksAfterPrefixes
          .any((mask) => mask.width != allocationCapacity) ||
      downstream.data.laneCount != laneCount ||
      downstream.data.countWidth != upstream.data.countWidth ||
      downstream.data.lanes.arrayElements.any((lane) =>
          lane.tagWidth != tagWidth ||
          lane.data.width != upstream.data.lanes.elementWidth)) {
    throw ArgumentError(
        'Lane, tag, count, payload, and ownership shapes must match.');
  }
  for (var lane = 0; lane < laneCount; lane++) {
    if (!rohdPayloadsCompatible([
      upstream.data.lanes.arrayElements[lane],
      downstream.data.lanes.arrayElements[lane].data
    ], ignoreConstantNames: true)) {
      throw ArgumentError('Corresponding lane payloads must be compatible.');
    }
  }
  upstream.requireProviderComposableWith(ReadyValidContract.irrevocable,
      consumerName: name);
  for (final allocation in allocations) {
    allocation.requireProviderComposableWith(ReadyValidContract.irrevocable,
        consumerName: name);
  }
  downstream.requireConsumerComposableWith(ReadyValidContract.irrevocable,
      providerName: name);

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  this.upstream = addPairInterface(upstream, PairRole.consumer,
      uniquify: (original) => 'upstream_$original');
  final localDiscardAllocations = discardAllocations == null
      ? Const(0)
      : addInput('discardAllocations', discardAllocations);
  final localAllocationRequired =
      addInput('allocationRequired', allocationRequired, width: laneCount);
  this.allocations = [
    for (var lane = 0; lane < allocationLaneCount; lane++)
      addPairInterfaceFrom(
        IrrevocableReadyValidInterface<Logic>(
            cloneInterfacePayload(allocations[lane].data)),
        allocations[lane],
        PairRole.consumer,
        uniquify: (original) => 'allocation_${lane}_$original',
      )
  ];
  final localAllocationMasks = [
    for (var prefix = 0;
        prefix < allocationMasksAfterPrefixes.length;
        prefix++)
      addInput('allocationMaskAfterPrefix$prefix',
          allocationMasksAfterPrefixes[prefix],
          width: allocationCapacity)
  ];
  this.downstream = addPairInterfaceFrom(
    IrrevocableReadyValidInterface<CompactedLanes<AllocatedLane<LogicType>>>(
      downstream.data.cloneTyped(),
    ),
    downstream,
    PairRole.provider,
    uniquify: (original) => 'downstream_$original',
  );

  final countWidth = max(this.upstream.data.countWidth,
      max(1, (max(laneCount, allocationLaneCount) + 1).log2Ceil));
  final validLanes = [
    for (var lane = 0; lane < laneCount; lane++)
      this
          .upstream
          .data
          .validCount
          .gt(Const(lane, width: this.upstream.data.countWidth))
          .named('${name}_lane_${lane}_valid')
  ];
  final effectiveDemand = [
    for (var lane = 0; lane < laneCount; lane++)
      (validLanes[lane] & localAllocationRequired[lane])
          .named('${name}_lane_${lane}_demands_allocation')
  ];
  // Excess counts historically demand all physical lanes and pass through
  // unchanged. Clamp only the planner's view, not the transported count.
  final count = this.upstream.data.validCount;
  final plan = MultiPoolLaneAllocationPlan(
    validCount:
        mux(count.gt(laneCount), Const(laneCount, width: count.width), count),
    lanePools: [for (var lane = 0; lane < laneCount; lane++) Const(0)],
    offeredTags: [
      [for (final allocation in this.allocations) allocation.data],
    ],
    offerValidMasks: [
      [for (final allocation in this.allocations) allocation.valid].rconcat(),
    ],
    laneDemandMask: localAllocationRequired,
    name: '${name}_plan',
  );
  final demandCount = plan.poolDemandCounts.single.zeroExtend(countWidth);
  final offerCount = plan.poolOfferCounts.single.zeroExtend(countWidth);

  final proposedOutput =
      this.downstream.data.cloneTyped(name: '${name}_proposed_output');
  final proposedOutputLanes = proposedOutput.lanes.arrayElements;
  final proposedAllocationSelection = plan.selectedOfferMasks.single;
  final locked = Logic(name: '${name}_locked');
  final lockedOutput =
      this.downstream.data.cloneTyped(name: '${name}_locked_output');
  final lockedAllocationSelection = Logic(
      name: '${name}_locked_allocation_selection',
      width: allocationLaneCount);
  final enoughAllocations = plan.enoughOffers;
  final proposedValid =
      this.upstream.valid & enoughAllocations & ~localDiscardAllocations;
  this.downstream.valid <=
      (locked | proposedValid) & ~localDiscardAllocations;
  this.downstream.data <= mux(locked, lockedOutput, proposedOutput);
  this.upstream.ready <= this.downstream.accepted & ~localDiscardAllocations;
  final transactionAccepted = this.downstream.accepted;
  for (var lane = 0; lane < allocationLaneCount; lane++) {
    final selected = mux(locked, lockedAllocationSelection[lane],
        proposedAllocationSelection[lane]);
    this.allocations[lane].ready <=
        localDiscardAllocations | (transactionAccepted & selected);
  }

  final outputLanes = proposedOutputLanes;
  for (var lane = 0; lane < laneCount; lane++) {
    outputLanes[lane].data <= this.upstream.data.lanes.arrayElements[lane];
    outputLanes[lane].tag <= plan.laneTags[lane];
    outputLanes[lane].allocated <= effectiveDemand[lane];
  }
  proposedOutput.validCount <= this.upstream.data.validCount;
  proposedOutput.consumedMask <= this.upstream.data.consumedMask;

  allocatedMasksAfterLanePrefixes = [
    addOutput('allocatedMaskAfterLanePrefix0', width: allocationCapacity)
  ];
  var ownership = localAllocationMasks.first;
  allocatedMasksAfterLanePrefixes.first <= ownership;
  for (var lane = 0; lane < laneCount; lane++) {
    final allocatedOneHot = [
      for (var index = allocationCapacity - 1; index >= 0; index--)
        transactionAccepted &
            this.downstream.data.lanes.arrayElements[lane].allocated &
            this.downstream.data.lanes.arrayElements[lane].tag.eq(index)
    ].concat();
    ownership = (ownership | allocatedOneHot)
        .named('${name}_ownership_after_lane_$lane');
    allocatedMasksAfterLanePrefixes.add(addOutput(
        'allocatedMaskAfterLanePrefix${lane + 1}',
        width: allocationCapacity)
      ..gets(ownership));
  }

  Logic duplicateOffers = Const(0);
  for (var first = 0; first < allocationLaneCount; first++) {
    for (var second = first + 1; second < allocationLaneCount; second++) {
      duplicateOffers = duplicateOffers |
          (this.allocations[first].valid &
              this.allocations[second].valid &
              this.allocations[first].data.eq(this.allocations[second].data));
    }
  }
  addOutput('allocationDemandCount', width: countWidth) <= demandCount;
  addOutput('allocationOfferCount', width: countWidth) <= offerCount;
  addOutput('invalidAllocationDemand', width: laneCount) <=
      localAllocationRequired & ~validLanes.rconcat();
  addOutput('insufficientAllocations') <=
      this.upstream.valid & ~locked & ~enoughAllocations;
  addOutput('duplicateAllocationOffers') <= duplicateOffers;

  Sequential(localClk, reset: localReset, resetValues: {
    locked: 0,
    lockedOutput: 0,
    lockedAllocationSelection: 0
  }, [
    If(localDiscardAllocations, then: [
      locked < 0
    ], orElse: [
      If(~locked & proposedValid & ~this.downstream.ready, then: [
        locked < 1,
        lockedOutput < proposedOutput,
        lockedAllocationSelection < proposedAllocationSelection
      ]),
      If(locked & transactionAccepted, then: [locked < 0])
    ])
  ]);
}