ReadyValidQueue<LogicType extends Logic> constructor

ReadyValidQueue<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required ReadyValidInterface<LogicType> upstream,
  4. required ReadyValidInterface<LogicType> downstream,
  5. required int depth,
  6. ReadyValidQueuePolicy policy = ReadyValidQueuePolicy.registered,
  7. ReadyValidQueueStorage storage = ReadyValidQueueStorage.registers,
  8. int? almostFullThreshold,
  9. int? almostEmptyThreshold,
  10. Logic? flush,
  11. bool blockIngressOnFlush = false,
  12. String name = 'ready_valid_queue',
})

Creates a ready/valid queue with explicit storage policy.

Implementation

ReadyValidQueue(
    {required Logic clk,
    required Logic reset,
    required ReadyValidInterface<LogicType> upstream,
    required ReadyValidInterface<LogicType> downstream,
    required this.depth,
    this.policy = ReadyValidQueuePolicy.registered,
    this.storage = ReadyValidQueueStorage.registers,
    int? almostFullThreshold,
    int? almostEmptyThreshold,
    Logic? flush,
    bool blockIngressOnFlush = false,
    String name = 'ready_valid_queue'})
    : almostFullThreshold = almostFullThreshold ?? depth,
      almostEmptyThreshold = almostEmptyThreshold ?? 0,
      super(
          definitionName: 'ReadyValidQueue_D${depth}_'
              '${policy == ReadyValidQueuePolicy.fallThrough ? 'F' : 'R'}_'
              '${storage == ReadyValidQueueStorage.ring ? 'Ring' : 'Regs'}_'
              'AF${almostFullThreshold ?? depth}_'
              'AE${almostEmptyThreshold ?? 0}_'
              '${blockIngressOnFlush && flush != null ? 'BlockIngress_' : ''}'
              '${upstream.data.name}',
          name: name) {
  if (depth < 1) {
    throw ArgumentError.value(depth, 'depth', 'Must be at least one.');
  }
  if (this.almostFullThreshold < 0 ||
      this.almostFullThreshold > depth ||
      this.almostEmptyThreshold < 0 ||
      this.almostEmptyThreshold > depth) {
    throw ArgumentError(
        'Queue thresholds must be between zero and depth inclusive.');
  }
  downstream.validatePayloadCompatibility(upstream.data);

  this.clk = addInput('clk', clk);
  this.reset = addInput('reset', reset);
  this.flush = flush == null ? Const(0) : addInput('flush', flush);
  this.upstream = addPairInterfacePorts(upstream, PairRole.consumer,
      uniquify: (original) => 'upstream_$original');
  this.downstream = addPairInterfaceFrom(
    IrrevocableReadyValidInterface<LogicType>(downstream.data.cloneTyped()),
    downstream,
    PairRole.provider,
    uniquify: (original) => 'downstream_$original',
  );
  final occupancyWidth = (depth + 1).log2Ceil;
  addOutput('occupancy', width: occupancyWidth);
  addOutput('empty');
  addOutput('full');
  addOutput('almostFull');
  addOutput('almostEmpty');

  final storageReset =
      (this.reset | this.flush).named('${name}_storage_reset');
  final blockIngress = blockIngressOnFlush && flush != null;
  final storageInput = blockIngress
      ? ReadyValidInterface(this.upstream.data,
          contract: this.upstream.contract)
      : this.upstream;
  if (blockIngress) {
    // Suppress fresh bypass offers as well as storage writes.
    storageInput.valid <= this.upstream.valid & ~this.flush;
    this.upstream.ready <= storageInput.ready & ~this.flush;
  }

  if (storage == ReadyValidQueueStorage.registers) {
    ReadyValidElasticPipeline<LogicType>(
        clk: this.clk,
        reset: storageReset,
        upstream: storageInput,
        downstream: this.downstream,
        depth: depth,
        fallThrough: policy == ReadyValidQueuePolicy.fallThrough,
        name: '${name}_storage');
  } else {
    final fifo = Fifo<LogicType>(
      this.clk,
      storageReset,
      writeEnable: storageInput.accepted,
      readEnable: this.downstream.accepted,
      writeData: storageInput.data,
      depth: depth,
      generateBypass: policy == ReadyValidQueuePolicy.fallThrough,
      name: '${name}_storage',
    );
    this.downstream.valid <=
        ~fifo.empty |
            (policy == ReadyValidQueuePolicy.fallThrough
                ? storageInput.valid
                : Const(0));
    this.downstream.data <= fifo.readData;
    storageInput.ready <= ~full | this.downstream.accepted;
  }

  final inputAccepted = this.upstream.accepted;
  final outputAccepted = this.downstream.accepted;
  final inputOnlyAccepted =
      (inputAccepted & ~outputAccepted).named('${name}_input_only_accepted');
  final outputOnlyAccepted =
      (~inputAccepted & outputAccepted).named('${name}_output_only_accepted');
  empty <= occupancy.eq(0);
  full <= occupancy.eq(depth);
  almostFull <=
      ~occupancy.lt(Const(this.almostFullThreshold, width: occupancyWidth));
  almostEmpty <=
      occupancy.lte(Const(this.almostEmptyThreshold, width: occupancyWidth));
  Sequential(this.clk, reset: storageReset, resetValues: {
    occupancy: 0
  }, [
    If(inputOnlyAccepted, then: [
      occupancy < occupancy + Const(1, width: occupancyWidth),
    ]),
    If(outputOnlyAccepted, then: [
      occupancy < occupancy - Const(1, width: occupancyWidth),
    ]),
  ]);
}