QueueBank<LogicType extends Logic>.fromBindings constructor

QueueBank<LogicType extends Logic>.fromBindings({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic enqueue,
  4. required Logic enqueueQueue,
  5. required LogicType enqueueData,
  6. required Logic dequeue,
  7. required List<ReadyValidStorageQueueBinding<LogicType>> storageBindings,
  8. int outputBufferDepth = 1,
  9. Logic? flush,
  10. String name = 'queue_bank',
})

Creates a bank with one queue for each of storageBindings, which a parent has already resolved for the width of enqueueData.

Implementation

QueueBank.fromBindings({
  required Logic clk,
  required Logic reset,
  required Logic enqueue,
  required Logic enqueueQueue,
  required LogicType enqueueData,
  required Logic dequeue,
  required List<ReadyValidStorageQueueBinding<LogicType>> storageBindings,
  this.outputBufferDepth = 1,
  Logic? flush,
  super.name = 'queue_bank',
})  : queueCount = storageBindings.length,
      queueIndexWidth = max(1, storageBindings.length.log2Ceil),
      storageBindings = List.unmodifiable(storageBindings),
      capacity = storageBindings.fold(
          0,
          (total, binding) =>
              total + binding.contract.capacity + outputBufferDepth),
      super(
        definitionName: _definitionName(
          storageBindings,
          outputBufferDepth: outputBufferDepth,
          hasFlush: flush != null,
          payload: enqueueData,
        ),
      ) {
  if (queueCount == 0) {
    throw ArgumentError.value(
        storageBindings, 'storageBindings', 'Must not be empty.');
  }
  if (outputBufferDepth < 0) {
    throw ArgumentError.value(
        outputBufferDepth, 'outputBufferDepth', 'Must not be negative.');
  }
  if (storageBindings
      .any((binding) => binding.request.dataWidth != enqueueData.width)) {
    throw ArgumentError('Every storage binding must be resolved for the '
        'width of enqueueData.');
  }
  if (enqueueQueue.width != queueIndexWidth) {
    throw ArgumentError.value(enqueueQueue, 'enqueueQueue',
        'Must be $queueIndexWidth bits to select $queueCount queues.');
  }
  if (dequeue.width != queueCount) {
    throw ArgumentError.value(
        dequeue, 'dequeue', 'Must have one bit for each queue.');
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localFlush = flush == null ? null : addInput('flush', flush);
  final localEnqueue = addInput('enqueue', enqueue);
  final localQueue =
      addInput('enqueueQueue', enqueueQueue, width: queueIndexWidth);
  final localData = addTypedInput('enqueueData', enqueueData);
  final localDequeue = addInput('dequeue', dequeue, width: queueCount);
  addOutput('canEnqueue');
  addOutput('enqueueQueueValid');
  addOutput('headValid', width: queueCount);
  heads = UnmodifiableListView([
    for (var queue = 0; queue < queueCount; queue++)
      addTypedPayloadOutput(this, 'head$queue', localData),
  ]);
  final countWidth = max(1, (capacity + 1).log2Ceil);
  addOutput('nonEmptyQueues', width: queueCount);
  addOutput('fullQueues', width: queueCount);
  addOutput('occupancy', width: countWidth);
  addOutput('empty');
  addOutput('full');

  final selections = [
    for (var queue = 0; queue < queueCount; queue++)
      localQueue
          .eq(Const(queue, width: queueIndexWidth))
          .named('${name}_selected_queue_$queue'),
  ];
  final selectionValid =
      selections.rconcat().or().named('${name}_selection_valid');
  final queueEnqueues = [
    for (var queue = 0; queue < queueCount; queue++)
      Logic(name: '${name}_queue_${queue}_enqueue'),
  ];
  final queues = [
    for (var queue = 0; queue < queueCount; queue++)
      StorageQueue<LogicType>.fromBinding(
        clk: localClk,
        reset: localReset,
        flush: localFlush,
        enqueue: queueEnqueues[queue],
        enqueueData: localData,
        dequeue: localDequeue[queue],
        storageBinding: this.storageBindings[queue],
        outputBufferDepth: outputBufferDepth,
        name: '${name}_queue_$queue',
      ),
  ];
  queueOccupancies = UnmodifiableListView([
    for (var queue = 0; queue < queueCount; queue++)
      addOutput('queueOccupancy$queue', width: queues[queue].occupancy.width)
        ..gets(queues[queue].occupancy),
  ]);

  Logic selectedCanEnqueue = Const(0);
  for (var queue = queueCount - 1; queue >= 0; queue--) {
    // Gating with the queue's own condition keeps an unknown selection
    // from reaching the storage of a queue that cannot admit an entry.
    queueEnqueues[queue] <=
        localEnqueue & selections[queue] & queues[queue].canEnqueue;
    heads[queue] <= queues[queue].head;
    selectedCanEnqueue =
        mux(selections[queue], queues[queue].canEnqueue, selectedCanEnqueue);
  }
  enqueueQueueValid <= selectionValid;
  canEnqueue <= selectionValid & selectedCanEnqueue;
  headValid <= [for (final queue in queues) queue.headValid].rconcat();

  final nonEmptyBits = [for (final queue in queues) ~queue.empty];
  final fullBits = [for (final queue in queues) queue.full];
  nonEmptyQueues <= nonEmptyBits.rconcat();
  fullQueues <= fullBits.rconcat();
  empty <= ~nonEmptyBits.rconcat().or();
  full <= fullBits.rconcat().and();

  Logic aggregateOccupancy = Const(0, width: countWidth);
  for (final queue in queues) {
    aggregateOccupancy =
        (aggregateOccupancy + queue.occupancy.zeroExtend(countWidth))
            .named('${name}_aggregate_occupancy');
  }
  occupancy <= aggregateOccupancy;
}