ReadyValidMultiResourceAllocationBoundary<LogicType extends Logic> constructor

ReadyValidMultiResourceAllocationBoundary<LogicType extends Logic>({
  1. required Logic quiesce,
  2. required IrrevocableReadyValidInterface<CompactedLanes<LogicType>> dispatch,
  3. required List<Logic> lanePools,
  4. required List<List<ReadyValidInterface<Logic>>> allocationOffers,
  5. required IrrevocableReadyValidInterface<CompactedLanes<AllocatedLane<LogicType>>> downstream,
  6. Logic? laneDemandMask,
  7. String name = 'ready_valid_multi_resource_allocation',
})

Creates an atomic multi-resource allocation boundary.

Implementation

ReadyValidMultiResourceAllocationBoundary({
  required Logic quiesce,
  required IrrevocableReadyValidInterface<CompactedLanes<LogicType>> dispatch,
  required List<Logic> lanePools,
  required List<List<ReadyValidInterface<Logic>>> allocationOffers,
  required IrrevocableReadyValidInterface<
          CompactedLanes<AllocatedLane<LogicType>>>
      downstream,
  Logic? laneDemandMask,
  super.name = 'ready_valid_multi_resource_allocation',
})  : laneCount = lanePools.length,
      poolCount = allocationOffers.length,
      tagWidth = _maximumOfferTagWidth(allocationOffers),
      super(
        definitionName: _multiResourceAllocationDefinitionName(dispatch,
            lanePools, allocationOffers, downstream, laneDemandMask != null),
      ) {
  if (laneCount < 1 ||
      poolCount < 1 ||
      allocationOffers.any((pool) => pool.isEmpty) ||
      tagWidth < 1) {
    throw ArgumentError(
        'Lanes, resource pools, and resource offers must be non-empty.');
  }
  final poolIdWidth = max(1, poolCount.log2Ceil);
  if (lanePools.any((pool) => pool.width < poolIdWidth) ||
      dispatch.data.laneCount != laneCount ||
      dispatch.data.countWidth < max(1, (laneCount + 1).log2Ceil) ||
      (laneDemandMask != null && laneDemandMask.width != laneCount)) {
    throw ArgumentError(
        'Lane, pool, demand, and dispatch shapes must match.');
  }
  if (allocationOffers
      .expand((pool) => pool)
      .any((offer) => offer.data.width < 1)) {
    throw ArgumentError('Resource offer tags must have positive width.');
  }
  if (downstream.data.laneCount != laneCount ||
      downstream.data.countWidth != dispatch.data.countWidth ||
      downstream.data.lanes.arrayElements.any((lane) =>
          lane.tagWidth != tagWidth ||
          lane.data.width != dispatch.data.lanes.elementWidth)) {
    throw ArgumentError(
        'Dispatch and allocated-dispatch lane shapes must match.');
  }
  for (var lane = 0; lane < laneCount; lane++) {
    if (!rohdPayloadsCompatible([
      dispatch.data.lanes.arrayElements[lane],
      downstream.data.lanes.arrayElements[lane].data
    ], ignoreConstantNames: true)) {
      throw ArgumentError('Corresponding lane payloads must be compatible.');
    }
  }

  dispatch.requireProviderComposableWith(ReadyValidContract.irrevocable,
      consumerName: name);
  downstream.requireConsumerComposableWith(ReadyValidContract.irrevocable,
      providerName: name);
  for (final pool in allocationOffers) {
    for (final offer in pool) {
      offer.requireProviderComposableWith(ReadyValidContract.irrevocable,
          consumerName: name);
    }
  }

  final localQuiesce = addInput('quiesce', quiesce);
  this.dispatch = addPairInterface(dispatch, PairRole.consumer,
      uniquify: (original) => 'dispatch_$original');
  this.downstream = addPairInterface(downstream, PairRole.provider,
      uniquify: (original) => 'downstream_$original');
  final localLanePools = [
    for (var lane = 0; lane < laneCount; lane++)
      addInput(
        'lanePool$lane',
        lanePools[lane],
        width: lanePools[lane].width,
      ),
  ];
  final localLaneDemandMask = laneDemandMask == null
      ? Const(-1, width: laneCount)
      : addInput('laneDemandMask', laneDemandMask, width: laneCount);
  this.allocationOffers = [
    for (var pool = 0; pool < poolCount; pool++)
      [
        for (var offer = 0; offer < allocationOffers[pool].length; offer++)
          addPairInterfaceFrom(
            IrrevocableReadyValidInterface<Logic>(
              cloneInterfacePayload(allocationOffers[pool][offer].data),
            ),
            allocationOffers[pool][offer],
            PairRole.consumer,
            uniquify: (original) => 'pool${pool}_offer${offer}_$original',
          ),
      ],
  ];

  allocationPlan = MultiPoolLaneAllocationPlan(
    validCount: this.dispatch.data.validCount,
    lanePools: localLanePools,
    offeredTags: [
      for (final pool in this.allocationOffers)
        [for (final offer in pool) offer.data],
    ],
    offerValidMasks: [
      for (final pool in this.allocationOffers)
        [for (final offer in pool) offer.valid].rconcat(),
    ],
    laneDemandMask: localLaneDemandMask,
    name: '${name}_plan',
  );

  final validLanes = [
    for (var lane = 0; lane < laneCount; lane++)
      this.dispatch.data.validCount.gt(
            Const(lane, width: this.dispatch.data.countWidth),
          ),
  ];
  final hasValidLanes = this.dispatch.data.validCount.neq(0);
  final invalidDispatchCount =
      ~hasValidLanes | allocationPlan.invalidValidCount;
  final planValid =
      allocationPlan.enoughOffers & ~invalidDispatchCount & ~localQuiesce;
  addOutput('allocationAccepted') <= this.downstream.accepted;
  addOutput('allocationRequired', width: laneCount) <=
      (localLaneDemandMask & validLanes.rconcat())
          .named('${name}_allocation_required');
  addOutput('acceptedAllocationLanes', width: laneCount) <=
      mux(
        this.downstream.accepted,
        localLaneDemandMask & validLanes.rconcat(),
        Const(0, width: laneCount),
      ).named('${name}_accepted_lanes');
  addOutput('invalidDispatchCount') <= invalidDispatchCount;
  addOutput('enoughOffers') <= allocationPlan.enoughOffers;
  addOutput('invalidLanePools', width: laneCount) <=
      allocationPlan.invalidLanePools;
  addOutput('invalidValidCount') <= allocationPlan.invalidValidCount;

  this.dispatch.ready <= this.downstream.ready & planValid;
  this.downstream.valid <= this.dispatch.valid & planValid;
  this.downstream.data.validCount <= this.dispatch.data.validCount;
  this.downstream.data.consumedMask <= this.dispatch.data.consumedMask;
  for (var lane = 0; lane < laneCount; lane++) {
    final source = this.dispatch.data.lanes.arrayElements[lane];
    final allocated = this.downstream.data.lanes.arrayElements[lane];
    allocated.data <= source;
    allocated.tag <= allocationPlan.laneTags[lane];
    allocated.allocated <= validLanes[lane] & localLaneDemandMask[lane];
  }

  for (var pool = 0; pool < poolCount; pool++) {
    for (var offer = 0; offer < this.allocationOffers[pool].length; offer++) {
      this.allocationOffers[pool][offer].ready <=
          this.downstream.accepted &
              allocationPlan.selectedOfferMasks[pool][offer];
    }
  }
}