ReadyValidFifoIdTracker<LogicType extends Logic> constructor

ReadyValidFifoIdTracker<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required ReadyValidInterface<Tagged<LogicType>> upstream,
  4. required Logic popMask,
  5. required int idCount,
  6. required int laneCount,
  7. required int capacity,
  8. Logic? flush,
  9. String name = 'ready_valid_fifo_id_tracker',
})

Creates a FIFO-backed dense ID presence tracker.

Implementation

ReadyValidFifoIdTracker({
  required Logic clk,
  required Logic reset,
  required ReadyValidInterface<Tagged<LogicType>> upstream,
  required Logic popMask,
  required this.idCount,
  required this.laneCount,
  required this.capacity,
  Logic? flush,
  super.name = 'ready_valid_fifo_id_tracker',
})  : referenceCountWidth = max(1, (capacity + 1).log2Ceil),
      super(
        definitionName: 'ReadyValidFifoIdTracker_'
            'I${idCount}_L${laneCount}_C${capacity}_'
            '${flush == null ? 'NoFlush' : 'Flush'}_'
            '${upstream.data.data.name}_'
            '${payloadShapeIdentity(upstream.data)}',
      ) {
  if (idCount < 2 ||
      idCount != (1 << upstream.data.tagWidth) ||
      popMask.width != laneCount ||
      capacity < laneCount) {
    throw ArgumentError(
      'idCount must match the complete tag space, popMask must match '
      'laneCount, and capacity must be at least laneCount.',
    );
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localPopMask = addInput('popMask', popMask, width: laneCount);
  final localFlush = flush == null ? Const(0) : addInput('flush', flush);
  this.upstream = addPairInterface(
    upstream,
    PairRole.consumer,
    uniquify: (original) => 'upstream_$original',
  );

  final fifo = ReadyValidThermometerPopFifo<Tagged<LogicType>>(
    clk: localClk,
    reset: localReset,
    flush: localFlush,
    upstream: this.upstream,
    popMask: localPopMask,
    laneCount: laneCount,
    capacity: capacity,
    name: '${name}_fifo',
  );
  head = addTypedPayloadOutput(this, 'head', fifo.head);
  head <= fifo.head;
  addOutput('headValid') <= fifo.headValid;
  addOutput('acceptedPopMask', width: laneCount) <= fifo.acceptedPopMask;
  addOutput('acceptedPopCount', width: fifo.acceptedPopCount.width) <=
      fifo.acceptedPopCount;
  addOutput('invalidPopMask') <= fifo.invalidPopMask;
  addOutput('occupancy', width: fifo.occupancy.width) <= fifo.occupancy;
  addOutput('empty') <= fifo.empty;
  addOutput('full') <= fifo.full;
  addOutput('idPresent', width: idCount);

  final referenceCounts = [
    for (var id = 0; id < idCount; id++)
      Logic(
        name: '${name}_reference_count_$id',
        width: referenceCountWidth,
      ),
  ];
  idPresent <=
      [
        for (final count in referenceCounts) count.neq(0),
      ].reversed.toList(growable: false).concat();

  final arithmeticWidth = max(
        referenceCountWidth,
        max(1, (laneCount + 1).log2Ceil),
      ) +
      1;
  final pushAccepted = this.upstream.accepted.named('${name}_push_accepted');
  final storageReset =
      (localReset | localFlush).named('${name}_storage_reset');
  Sequential(localClk, reset: storageReset, resetValues: {
    for (final count in referenceCounts) count: 0,
  }, [
    for (var id = 0; id < idCount; id++)
      () {
        final pushForId = (pushAccepted &
                this
                    .upstream
                    .data
                    .tag
                    .eq(Const(id, width: this.upstream.data.tagWidth)))
            .zeroExtend(arithmeticWidth);
        Logic popForId = Const(0, width: arithmeticWidth);
        for (var lane = 0; lane < laneCount; lane++) {
          popForId = (popForId +
                  (fifo.acceptedPopMask[lane] &
                          fifo.head.lanes.arrayElements[lane].tag.eq(
                            Const(
                              id,
                              width: this.upstream.data.tagWidth,
                            ),
                          ))
                      .zeroExtend(arithmeticWidth))
              .named('${name}_id_${id}_pop_count_$lane');
        }
        final nextCount = (referenceCounts[id].zeroExtend(arithmeticWidth) +
                pushForId -
                popForId)
            .named('${name}_id_${id}_next_count');
        return referenceCounts[id] <
            nextCount.slice(referenceCountWidth - 1, 0);
      }(),
  ]);
}