BankedIndexPool constructor

BankedIndexPool({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic quiesce,
  4. required List<Logic> allocate,
  5. required List<Logic> release,
  6. required List<Logic> releaseIndices,
  7. required Logic restore,
  8. required Logic restoreMask,
  9. required int capacity,
  10. List<int>? initiallyAvailableIndices,
  11. MemoryBankingPolicy bankingPolicy = MemoryBankingPolicy.interleaved,
  12. IndexAllocationGenerator allocationGenerator = lowestFreeIndexAllocationGenerator,
  13. String allocationPolicyName = 'lowestFree',
  14. RestorableMultiIndexAllocatorFunctor? bankImplementation,
  15. String name = 'banked_index_pool',
})

Creates a banked global index pool driven by explicit actions.

allocate has one bit per bank. release has one bit per release lane, each with an indexWidth-bit entry of releaseIndices. restoreMask has one bit per index.

Each bank is a flat ReadyValidMultiIndexPool using allocationGenerator, unless bankImplementation supplies another allocator, which then owns its local allocation policy.

Implementation

BankedIndexPool(
    {required Logic clk,
    required Logic reset,
    required Logic quiesce,
    required List<Logic> allocate,
    required List<Logic> release,
    required List<Logic> releaseIndices,
    required Logic restore,
    required Logic restoreMask,
    required this.capacity,
    List<int>? initiallyAvailableIndices,
    MemoryBankingPolicy bankingPolicy = MemoryBankingPolicy.interleaved,
    IndexAllocationGenerator allocationGenerator =
        lowestFreeIndexAllocationGenerator,
    this.allocationPolicyName = 'lowestFree',
    RestorableMultiIndexAllocatorFunctor? bankImplementation,
    super.name = 'banked_index_pool'})
    : indexWidth = max(1, capacity.log2Ceil),
      allocationLaneCount = allocate.length,
      releaseLaneCount = release.length,
      layout = BankedMemoryLayout(
          entryCount: capacity,
          bankCount: allocate.length,
          policy: bankingPolicy),
      super(
          definitionName: _bankedIndexPoolCoreDefinitionName(
              capacity,
              allocate.length,
              release.length,
              initiallyAvailableIndices,
              bankingPolicy,
              allocationPolicyName,
              bankImplementation?.identity ?? 'flatBitmap')) {
  if (allocationLaneCount < 1 || releaseLaneCount < 1) {
    throw ArgumentError(
        'Allocation and release lane counts must be positive.');
  }
  if (releaseIndices.length != releaseLaneCount ||
      releaseIndices.any((index) => index.width != indexWidth) ||
      restoreMask.width != capacity) {
    throw ArgumentError(
        'Each release lane requires an index that represents every index, '
        'and the restore mask must contain one bit per index.');
  }
  if (bankImplementation != null &&
      (!identical(allocationGenerator, lowestFreeIndexAllocationGenerator) ||
          allocationPolicyName != 'lowestFree')) {
    throw ArgumentError(
        'A late-bound bank implementation owns its local allocation policy.');
  }
  final initialAvailability = initiallyAvailableIndices ??
      List<int>.generate(capacity, (index) => index);
  if (initialAvailability.toSet().length != initialAvailability.length ||
      initialAvailability.any((index) => index < 0 || index >= capacity)) {
    throw ArgumentError.value(
        initiallyAvailableIndices,
        'initiallyAvailableIndices',
        'Must contain unique indices in the configured capacity.');
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localQuiesce = addInput('quiesce', quiesce);
  final localAllocate = [
    for (var bank = 0; bank < allocationLaneCount; bank++)
      addInput('allocate$bank', allocate[bank]),
  ];
  final localRelease = [
    for (var lane = 0; lane < releaseLaneCount; lane++)
      addInput('release$lane', release[lane]),
  ];
  final localReleaseIndices = [
    for (var lane = 0; lane < releaseLaneCount; lane++)
      addInput('releaseIndex$lane', releaseIndices[lane], width: indexWidth),
  ];
  final localRestore = addInput('restore', restore);
  final localRestoreMask =
      addInput('restoreMask', restoreMask, width: capacity);

  allocationValid = [
    for (var bank = 0; bank < allocationLaneCount; bank++)
      addOutput('allocationValid$bank'),
  ];
  allocationIndices = [
    for (var bank = 0; bank < allocationLaneCount; bank++)
      addOutput('allocationIndex$bank', width: indexWidth),
  ];
  canRelease = [
    for (var lane = 0; lane < releaseLaneCount; lane++)
      addOutput('canRelease$lane'),
  ];
  addOutput('canRestore');

  // The bank allocators have a ready/valid contract, so each is driven
  // through internal interfaces that never reach this core's boundary.
  final bankAllocations = <ReadyAndValidInterface<Logic>>[];
  final bankReleases = <List<ReadyAndValidInterface<TagRelease>>>[];
  final bankRestores = <ReadyAndValidInterface<Logic>>[];
  banks = [];
  for (var bank = 0; bank < allocationLaneCount; bank++) {
    final depth = layout.bankEntryCount(bank);
    final localWidth = max(1, depth.log2Ceil);
    final allocation = ReadyAndValidInterface(
        Logic(name: '${name}_bank_${bank}_allocation', width: localWidth));
    final releasesForBank = [
      for (var lane = 0; lane < releaseLaneCount; lane++)
        ReadyAndValidInterface(TagRelease(
            tagWidth: localWidth, name: '${name}_bank_${bank}_release_$lane'))
    ];
    final bankRestore = ReadyAndValidInterface(
        Logic(name: '${name}_bank_${bank}_restore', width: depth));
    final availableRows = [
      for (final index in initialAvailability)
        if (layout.coordinateOf(index).bank == bank)
          layout.coordinateOf(index).row
    ];
    final child = bankImplementation == null
        ? ReadyValidMultiIndexPool(
            clk: localClk,
            reset: localReset,
            quiesce: localQuiesce,
            allocations: [allocation],
            releases: releasesForBank,
            restore: bankRestore,
            capacity: depth,
            initiallyAvailableIndices: availableRows,
            allocationGenerator: allocationGenerator,
            allocationPolicyName: allocationPolicyName,
            name: '${name}_bank_$bank')
        : bankImplementation(
            clk: localClk,
            reset: localReset,
            quiesce: localQuiesce,
            allocations: [allocation],
            releases: releasesForBank,
            restore: bankRestore,
            capacity: depth,
            initiallyAvailableIndices: availableRows,
            name: '${name}_bank_$bank');
    bankAllocations.add(allocation);
    bankReleases.add(releasesForBank);
    bankRestores.add(bankRestore);
    banks.add(child);
  }

  // A bank takes its offer only while it is valid, so the action needs no
  // further gating here.
  for (var bank = 0; bank < allocationLaneCount; bank++) {
    final localAllocation = bankAllocations[bank];
    Logic globalIndex = Const(0, width: indexWidth);
    for (var row = layout.bankEntryCount(bank) - 1; row >= 0; row--) {
      globalIndex = mux(
          localAllocation.data.eq(row),
          Const(layout.flatIndex(bank: bank, row: row), width: indexWidth),
          globalIndex);
    }
    allocationValid[bank] <= localAllocation.valid;
    allocationIndices[bank] <= globalIndex;
    localAllocation.ready <= localAllocate[bank];
  }

  final capacityFillsTagSpace = capacity == (1 << indexWidth);
  final releaseInRange = [
    for (var lane = 0; lane < releaseLaneCount; lane++)
      if (capacityFillsTagSpace)
        Const(1)
      else
        localReleaseIndices[lane]
            .lt(Const(capacity, width: indexWidth))
            .named('${name}_release_${lane}_in_range')
  ];
  for (var lane = 0; lane < releaseLaneCount; lane++) {
    var selectedReady = ~releaseInRange[lane];
    for (var bank = 0; bank < allocationLaneCount; bank++) {
      final bankRelease = bankReleases[bank][lane];
      Logic bankMatch = Const(0);
      Logic localRow = Const(0, width: bankRelease.data.tag.width);
      for (final index in layout.indicesInBank(bank)) {
        final coordinate = layout.coordinateOf(index);
        final matches = localReleaseIndices[lane].eq(index);
        bankMatch = bankMatch | matches;
        localRow = mux(
            matches, Const(coordinate.row, width: localRow.width), localRow);
      }
      bankRelease.valid <= localRelease[lane] & bankMatch;
      bankRelease.data.tag <= localRow;
      selectedReady = selectedReady | (bankMatch & bankRelease.ready);
    }
    canRelease[lane] <= selectedReady;
  }

  final allRestoreReady = bankRestores
      .map((bankRestore) => bankRestore.ready)
      .reduce((left, right) => left & right);
  canRestore <= allRestoreReady;
  for (var bank = 0; bank < allocationLaneCount; bank++) {
    bankRestores[bank].valid <= localRestore & allRestoreReady;
    bankRestores[bank].data <=
        [
          for (var row = layout.bankEntryCount(bank) - 1; row >= 0; row--)
            localRestoreMask[layout.flatIndex(bank: bank, row: row)]
        ].concat();
  }

  addOutput('allocatedMask', width: capacity);
  final countWidth = max(1, (capacity + 1).log2Ceil);
  addOutput('allocatedCount', width: countWidth);
  addOutput('availableCount', width: countWidth);
  addOutput('exhausted');
  addOutput('quiesced');
  addOutput('retainedAllocationLanes', width: allocationLaneCount);
  addOutput('invalidReleases', width: releaseLaneCount);
  addOutput('doubleReleases', width: releaseLaneCount);
  addOutput('invalidAllocationSelections', width: allocationLaneCount);
  addOutput('bankExhausted', width: allocationLaneCount);
  bankAvailableCounts = [
    for (var bank = 0; bank < allocationLaneCount; bank++)
      addOutput('bankAvailableCount$bank',
          width: banks[bank].availableCount.width)
  ];
  allocatedMasksAfterAllocationPrefixes = [
    for (var prefix = 0; prefix <= allocationLaneCount; prefix++)
      addOutput('allocatedMaskAfterAllocationPrefix$prefix', width: capacity)
  ];

  final currentAllocated = [
    for (var index = 0; index < capacity; index++)
      banks[layout.coordinateOf(index).bank]
          .allocatedMask[layout.coordinateOf(index).row]
  ].rconcat();
  allocatedMask <= currentAllocated;
  for (var prefix = 0; prefix <= allocationLaneCount; prefix++) {
    allocatedMasksAfterAllocationPrefixes[prefix] <=
        [
          for (var index = capacity - 1; index >= 0; index--)
            banks[layout.coordinateOf(index).bank]
                        .allocatedMasksAfterAllocationPrefixes[
                    layout.coordinateOf(index).bank < prefix ? 1 : 0]
                [layout.coordinateOf(index).row]
        ].concat();
  }
  final allocatedSum = banks
      .map((bank) => bank.allocatedCount.zeroExtend(countWidth))
      .reduce((left, right) => left + right);
  final availableSum = banks
      .map((bank) => bank.availableCount.zeroExtend(countWidth))
      .reduce((left, right) => left + right);
  allocatedCount <= allocatedSum;
  availableCount <= availableSum;
  exhausted <= availableSum.eq(0);
  quiesced <=
      banks
          .map((bank) => bank.quiesced)
          .reduce((left, right) => left & right);
  retainedAllocationLanes <=
      [for (final bank in banks) bank.retainedAllocationLanes[0]].rconcat();
  invalidReleases <=
      [
        for (var lane = releaseLaneCount - 1; lane >= 0; lane--)
          (canRelease[lane] & localRelease[lane])
                  .named('${name}_release_${lane}_taken') &
              ~releaseInRange[lane]
      ].concat();
  doubleReleases <=
      [
        for (var lane = releaseLaneCount - 1; lane >= 0; lane--)
          banks
              .map((bank) => bank.doubleReleases[lane])
              .reduce((left, right) => left | right)
      ].concat();
  invalidAllocationSelections <=
      [for (final bank in banks) bank.invalidAllocationSelections[0]]
          .rconcat();
  bankExhausted <= [for (final bank in banks) bank.exhausted].rconcat();
  for (var bank = 0; bank < allocationLaneCount; bank++) {
    bankAvailableCounts[bank] <= banks[bank].availableCount;
  }
}