WrappingAgeSelector constructor

WrappingAgeSelector({
  1. required Logic eligible,
  2. required List<Logic> ages,
  3. int? maximumLiveEntries,
  4. String name = 'wrapping_age_selector',
})

Creates a wrapping virtual-age selector.

Implementation

WrappingAgeSelector({
  required Logic eligible,
  required List<Logic> ages,
  int? maximumLiveEntries,
  super.name = 'wrapping_age_selector',
})  : entryCount = eligible.width,
      ageWidth = ages.isEmpty ? 0 : ages.first.width,
      maximumLiveEntries = maximumLiveEntries ?? eligible.width,
      super(
        definitionName: 'WrappingAgeSelector_'
            'E${eligible.width}_A${ages.isEmpty ? 0 : ages.first.width}_'
            'L${maximumLiveEntries ?? eligible.width}',
      ) {
  if (entryCount < 1 ||
      ages.length != entryCount ||
      ages.isEmpty ||
      ages.any((age) => age.width != ageWidth)) {
    throw ArgumentError(
        'A non-empty, equally sized age is required for every entry.');
  }
  if (ageWidth < 2 ||
      this.maximumLiveEntries < 1 ||
      this.maximumLiveEntries >= (1 << (ageWidth - 1))) {
    throw ArgumentError.value(this.maximumLiveEntries, 'maximumLiveEntries',
        'Must fit in less than half the wrapping age space.');
  }

  final localEligible = addInput('eligible', eligible, width: entryCount);
  final localAges = [
    for (var index = 0; index < entryCount; index++)
      addInput('age$index', ages[index], width: ageWidth)
  ];
  final indexWidth = max(1, (entryCount - 1).bitLength);
  Logic found = Const(0);
  Logic selected = Const(0, width: indexWidth);
  Logic selectedAge = Const(0, width: ageWidth);
  for (var index = 0; index < entryCount; index++) {
    final candidateOlder = isOlderWrappingAge(
        candidateAge: localAges[index],
        selectedAge: selectedAge,
        name: '${name}_entry_$index');
    final choose = (localEligible[index] & (~found | candidateOlder))
        .named('${name}_choose_$index');
    selected = mux(choose, Const(index, width: indexWidth), selected)
        .named('${name}_selected_index_$index');
    selectedAge = mux(choose, localAges[index], selectedAge)
        .named('${name}_selected_age_$index');
    found = (found | localEligible[index]).named('${name}_found_$index');
  }

  addOutput('valid') <= found;
  addOutput('selectedIndex', width: indexWidth) <= selected;
  addOutput('selectedOneHot', width: entryCount);
  selectedOneHot <=
      List.generate(
          entryCount,
          (index) => (found & selected.eq(index))
              .named('${name}_selected_one_hot_$index')).rconcat();
}