MultiAgeMatrixTracker constructor

MultiAgeMatrixTracker({
  1. required Logic clk,
  2. required Logic reset,
  3. required List<ReadyValidInterface<Logic>> allocations,
  4. required ReadyValidInterface<Logic> releaseMask,
  5. required int entryCount,
  6. DuplicateAgeAllocationPolicy duplicateAllocationPolicy = DuplicateAgeAllocationPolicy.ignore,
  7. RegisterFileFunctor? storageImplementation,
  8. String name = 'multi_age_matrix_tracker',
})

Creates an ordered multi-allocation age matrix.

storageImplementation selects the backing for matrix rows. The default remains synthesizable flop state.

Implementation

MultiAgeMatrixTracker(
    {required Logic clk,
    required Logic reset,
    required List<ReadyValidInterface<Logic>> allocations,
    required ReadyValidInterface<Logic> releaseMask,
    required this.entryCount,
    this.duplicateAllocationPolicy = DuplicateAgeAllocationPolicy.ignore,
    RegisterFileFunctor? storageImplementation,
    super.name = 'multi_age_matrix_tracker'})
    : indexWidth = allocations.isEmpty ? 0 : allocations.first.data.width,
      allocationLaneCount = allocations.length,
      super(
          definitionName: 'MultiAgeMatrixTracker_E${entryCount}_'
              'A${allocations.length}_W'
              '${allocations.isEmpty ? 0 : allocations.first.data.width}_'
              'D${duplicateAllocationPolicy.name}'
              '${storageImplementation == null ? '' : '_'}'
              '${storageImplementation?.identity ?? ''}') {
  if (entryCount < 1 || allocationLaneCount < 1) {
    throw ArgumentError(
        'Entry count and allocation lane count must be positive.');
  }
  final minimumIndexWidth = max(1, entryCount.log2Ceil);
  if (indexWidth < minimumIndexWidth ||
      allocations.any((allocation) => allocation.data.width != indexWidth) ||
      releaseMask.data.width != entryCount) {
    throw ArgumentError(
        'Allocation indices and the release mask must match the entry set.');
  }
  for (final allocation in allocations) {
    allocation.requireProviderComposableWith(ReadyValidContract.readyAndValid,
        consumerName: name);
  }
  releaseMask.requireProviderComposableWith(ReadyValidContract.readyAndValid,
      consumerName: name);

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  this.allocations = [
    for (var lane = 0; lane < allocationLaneCount; lane++)
      addPairInterface(allocations[lane], PairRole.consumer,
          uniquify: (original) => 'allocation_${lane}_$original')
  ];
  this.releaseMask = addPairInterface(releaseMask, PairRole.consumer,
      uniquify: (original) => 'release_mask_$original');
  for (final allocation in this.allocations) {
    allocation.ready <= Const(1);
  }
  this.releaseMask.ready <= Const(1);

  final valid = [
    for (var index = 0; index < entryCount; index++)
      Logic(name: '${name}_valid_$index')
  ];
  final matrix = [
    for (var older = 0; older < entryCount; older++)
      [
        for (var younger = 0; younger < entryCount; younger++)
          Logic(name: '${name}_${older}_older_than_$younger')
      ]
  ];
  final acceptedReleaseMask = mux(this.releaseMask.accepted,
          this.releaseMask.data, Const(0, width: entryCount))
      .named('${name}_accepted_release_mask');
  var workingValid = [
    for (var index = 0; index < entryCount; index++)
      (valid[index] & ~acceptedReleaseMask[index])
          .named('${name}_valid_${index}_after_release')
  ];
  var workingMatrix = [
    for (var older = 0; older < entryCount; older++)
      [
        for (var younger = 0; younger < entryCount; younger++)
          (matrix[older][younger] &
                  workingValid[older] &
                  workingValid[younger])
              .named('${name}_${older}_older_${younger}_after_release')
      ]
  ];

  final capacityFillsIndexSpace = entryCount == (1 << indexWidth);
  Logic inRange(Logic index, String operation) => capacityFillsIndexSpace
      ? Const(1)
      : index
          .lt(Const(entryCount, width: indexWidth))
          .named('${name}_${operation}_in_range');
  final invalidAllocationLanes = <Logic>[];
  final duplicateAllocationLanes = <Logic>[];
  for (var lane = 0; lane < allocationLaneCount; lane++) {
    final accepted = this.allocations[lane].accepted;
    final allocationInRange =
        inRange(this.allocations[lane].data, 'allocation_$lane');
    Logic wasValid = Const(0);
    for (var index = entryCount - 1; index >= 0; index--) {
      wasValid = mux(this.allocations[lane].data.eq(index),
          workingValid[index], wasValid);
    }
    final duplicate = accepted & allocationInRange & wasValid;
    final validAllocation = accepted &
        allocationInRange &
        (duplicateAllocationPolicy == DuplicateAgeAllocationPolicy.ignore
            ? ~wasValid
            : Const(1));
    invalidAllocationLanes.add(accepted & ~allocationInRange);
    duplicateAllocationLanes.add(duplicate);
    final allocated = [
      for (var index = 0; index < entryCount; index++)
        validAllocation & this.allocations[lane].data.eq(index)
    ];
    workingMatrix = [
      for (var older = 0; older < entryCount; older++)
        [
          for (var younger = 0; younger < entryCount; younger++)
            if (older == younger)
              Const(0)
            else
              mux(
                      allocated[younger],
                      workingValid[older],
                      mux(allocated[older], Const(0),
                          workingMatrix[older][younger]))
                  .named('${name}_${older}_older_${younger}_after_lane_$lane')
        ]
    ];
    workingValid = [
      for (var index = 0; index < entryCount; index++)
        (workingValid[index] | allocated[index])
            .named('${name}_valid_${index}_after_lane_$lane')
    ];
  }

  addOutput('validEntries', width: entryCount) <= valid.rconcat();
  addOutput('olderThan', width: entryCount * entryCount) <=
      matrix.expand((row) => row).toList().rconcat();
  addOutput('invalidAllocations', width: allocationLaneCount) <=
      invalidAllocationLanes.rconcat();
  addOutput('duplicateAllocations', width: allocationLaneCount) <=
      duplicateAllocationLanes.rconcat();
  addOutput('doubleReleaseMask', width: entryCount) <=
      acceptedReleaseMask & ~valid.rconcat();

  Sequential(localClk, reset: localReset, resetValues: {
    for (final entry in valid) entry: 0,
    if (storageImplementation == null)
      for (final row in matrix)
        for (final entry in row) entry: 0
  }, [
    for (var index = 0; index < entryCount; index++)
      valid[index] < workingValid[index],
    if (storageImplementation == null)
      for (var older = 0; older < entryCount; older++)
        for (var younger = 0; younger < entryCount; younger++)
          matrix[older][younger] < workingMatrix[older][younger]
  ]);
  if (storageImplementation != null) {
    final rows = [
      for (var older = 0; older < entryCount; older++)
        Logic(name: '${name}_matrix_row_$older', width: entryCount)
    ];
    for (var older = 0; older < entryCount; older++) {
      for (var younger = 0; younger < entryCount; younger++) {
        matrix[older][younger] <= rows[older][younger];
      }
    }
    connectRegisterFileBank(
      clk: localClk,
      reset: localReset,
      current: rows,
      next: [for (final row in workingMatrix) row.rconcat()],
      resetValues: List.filled(
          entryCount, LogicValue.filled(entryCount, LogicValue.zero)),
      implementation: storageImplementation,
      name: '${name}_matrix_storage',
    );
  }
}