lowestFreeIndexAllocationGenerator function

List<Logic> lowestFreeIndexAllocationGenerator({
  1. required List<Logic> available,
  2. required Logic accepted,
  3. required Logic clk,
  4. required Logic reset,
  5. required String name,
})

Creates a lowest-index-first allocation grant.

Implementation

List<Logic> lowestFreeIndexAllocationGenerator(
    {required List<Logic> available,
    required Logic accepted,
    required Logic clk,
    required Logic reset,
    required String name}) {
  final grants =
      List.generate(available.length, (index) => Logic(name: '${name}_$index'));
  Combinational([
    CaseZ(available.rconcat(), conditionalType: ConditionalType.priority, [
      for (var index = 0; index < available.length; index++)
        CaseItem(
            Const(LogicValue.filled(available.length, LogicValue.z)
                .withSet(index, LogicValue.one)),
            [
              for (var grantIndex = 0;
                  grantIndex < available.length;
                  grantIndex++)
                grants[grantIndex] < (grantIndex == index ? 1 : 0)
            ])
    ], defaultItem: [
      for (final grant in grants) grant < 0
    ])
  ]);
  return grants;
}