AgeMatrixSelector constructor

AgeMatrixSelector({
  1. required Logic eligible,
  2. required Logic olderThan,
  3. String name = 'age_matrix_selector',
})

Creates an oldest-eligible age-matrix selector.

Implementation

AgeMatrixSelector({
  required Logic eligible,
  required Logic olderThan,
  super.name = 'age_matrix_selector',
})  : entryCount = eligible.width,
      super(
        definitionName: 'AgeMatrixSelector_E${eligible.width}',
      ) {
  if (entryCount < 1 || olderThan.width != entryCount * entryCount) {
    throw ArgumentError(
        'olderThan must contain one row-major bit per entry pair.');
  }
  final localEligible = addInput('eligible', eligible, width: entryCount);
  final localOlderThan =
      addInput('olderThan', olderThan, width: olderThan.width);
  final oldest = <Logic>[];
  for (var candidate = 0; candidate < entryCount; candidate++) {
    Logic olderThanAll = Const(1);
    for (var other = 0; other < entryCount; other++) {
      if (candidate == other) {
        continue;
      }
      olderThanAll = (olderThanAll &
              (~localEligible[other] |
                  localOlderThan[candidate * entryCount + other]))
          .named('${name}_${candidate}_older_through_$other');
    }
    oldest.add((localEligible[candidate] & olderThanAll)
        .named('${name}_oldest_$candidate'));
  }
  final indexWidth = max(1, (entryCount - 1).bitLength);
  Logic selected = Const(0, width: indexWidth);
  Logic seenOldest = Const(0);
  Logic multiple = Const(0);
  final selectedBits = <Logic>[];
  for (var index = 0; index < entryCount; index++) {
    final choose =
        (oldest[index] & ~seenOldest).named('${name}_choose_oldest_$index');
    selectedBits.add(choose);
    selected = mux(choose, Const(index, width: indexWidth), selected)
        .named('${name}_selected_index_$index');
    multiple = (multiple | (seenOldest & oldest[index]))
        .named('${name}_multiple_through_$index');
    seenOldest =
        (seenOldest | oldest[index]).named('${name}_seen_through_$index');
  }
  addOutput('valid') <= seenOldest;
  addOutput('selectedIndex', width: indexWidth) <= selected;
  addOutput('selectedOneHot', width: entryCount) <= selectedBits.rconcat();
  addOutput('multipleOldest') <= multiple;
}