StorageQueue<LogicType extends Logic>.fromBinding constructor

StorageQueue<LogicType extends Logic>.fromBinding({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic enqueue,
  4. required LogicType enqueueData,
  5. required Logic dequeue,
  6. required ReadyValidStorageQueueBinding<LogicType> storageBinding,
  7. int outputBufferDepth = 1,
  8. int? almostFullThreshold,
  9. int? almostEmptyThreshold,
  10. Logic? flush,
  11. String name = 'storage_queue',
})

Creates a queue from a storage implementation that a parent has already resolved for the width of enqueueData.

storageDepth and requestedStorageLatency are those of the request from which storageBinding was resolved.

Implementation

StorageQueue.fromBinding({
  required Logic clk,
  required Logic reset,
  required Logic enqueue,
  required LogicType enqueueData,
  required Logic dequeue,
  required this.storageBinding,
  this.outputBufferDepth = 1,
  int? almostFullThreshold,
  int? almostEmptyThreshold,
  Logic? flush,
  super.name = 'storage_queue',
})  : storageDepth = storageBinding.request.depth,
      requestedStorageLatency = storageBinding.request.requestedOutputLatency,
      capacity = storageBinding.contract.capacity + outputBufferDepth,
      almostFullThreshold = almostFullThreshold ??
          storageBinding.contract.capacity + outputBufferDepth,
      almostEmptyThreshold = almostEmptyThreshold ?? 0,
      super(
        definitionName: _definitionName(
          storageBinding,
          outputBufferDepth: outputBufferDepth,
          almostFullThreshold: almostFullThreshold,
          almostEmptyThreshold: almostEmptyThreshold,
          hasFlush: flush != null,
          payload: enqueueData,
        ),
      ) {
  if (outputBufferDepth < 0) {
    throw ArgumentError.value(
        outputBufferDepth, 'outputBufferDepth', 'Must not be negative.');
  }
  if (this.almostFullThreshold < 0 ||
      this.almostFullThreshold > capacity ||
      this.almostEmptyThreshold < 0 ||
      this.almostEmptyThreshold > capacity) {
    throw ArgumentError('Watermarks must be within the total capacity.');
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localFlush = flush == null ? Const(0) : addInput('flush', flush);
  final localEnqueue = addInput('enqueue', enqueue);
  final localData = addTypedInput('enqueueData', enqueueData);
  final localDequeue = addInput('dequeue', dequeue);
  addOutput('canEnqueue');
  addOutput('headValid');
  head = addTypedPayloadOutput(this, 'head', localData);
  final countWidth = max(1, (capacity + 1).log2Ceil);
  addOutput('occupancy', width: countWidth);
  addOutput('empty');
  addOutput('full');
  addOutput('almostFull');
  addOutput('almostEmpty');

  // The late-bound storage and the output stages are built on handshakes,
  // so each action is presented to them as one side of an internal one.
  final storageReset =
      (localReset | localFlush).named('${name}_storage_reset');
  final storageInput =
      ReadyAndValidInterface<LogicType>(localData.cloneTyped());
  final storageOutput =
      ReadyAndValidInterface<LogicType>(localData.cloneTyped());
  storageBinding.instantiate(
      clk: localClk,
      reset: localReset,
      flush: localFlush,
      upstream: storageInput,
      downstream: storageOutput,
      name: '${name}_storage');
  storageInput.valid <= localEnqueue & ~localFlush;
  storageInput.data <= localData;
  canEnqueue <= storageInput.ready & ~localFlush;

  final ReadyValidInterface<LogicType> offer;
  if (outputBufferDepth == 0) {
    offer = storageOutput;
  } else {
    offer = IrrevocableReadyValidInterface<LogicType>(localData.cloneTyped());
    ReadyValidElasticPipeline<LogicType>(
        clk: localClk,
        reset: storageReset,
        upstream: storageOutput,
        downstream: offer,
        depth: outputBufferDepth,
        name: '${name}_front_buffer');
  }
  headValid <= offer.valid;
  head <= offer.data;
  offer.ready <= localDequeue;

  final enqueueTaken =
      (localEnqueue & canEnqueue).named('${name}_enqueue_taken');
  final dequeueTaken =
      (localDequeue & headValid).named('${name}_dequeue_taken');
  final enqueueOnlyTaken =
      (enqueueTaken & ~dequeueTaken).named('${name}_enqueue_only_taken');
  final dequeueOnlyTaken =
      (~enqueueTaken & dequeueTaken).named('${name}_dequeue_only_taken');
  empty <= occupancy.eq(0);
  full <= occupancy.eq(capacity);
  almostFull <=
      occupancy.gte(Const(this.almostFullThreshold, width: countWidth));
  almostEmpty <=
      occupancy.lte(Const(this.almostEmptyThreshold, width: countWidth));
  Sequential(localClk, reset: storageReset, resetValues: {
    occupancy: 0
  }, [
    If(enqueueOnlyTaken, then: [occupancy < occupancy + 1]),
    If(dequeueOnlyTaken, then: [occupancy < occupancy - 1]),
  ]);
}