ReadyValidTaggedJoin<FirstLogicType extends Logic, SecondLogicType extends Logic> constructor

ReadyValidTaggedJoin<FirstLogicType extends Logic, SecondLogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required ReadyValidInterface<Logic> reserve,
  4. required ReadyValidInterface<Tagged<FirstLogicType>> first,
  5. required ReadyValidInterface<Tagged<SecondLogicType>> second,
  6. required ReadyValidInterface<Tagged<JoinedPayload<FirstLogicType, SecondLogicType>>> downstream,
  7. required int capacity,
  8. ReadyValidInterface<TagCancel>? cancel,
  9. String name = 'ready_valid_tagged_join',
})

Creates a bounded two-input tagged rendezvous.

Implementation

ReadyValidTaggedJoin({
  required Logic clk,
  required Logic reset,
  required ReadyValidInterface<Logic> reserve,
  required ReadyValidInterface<Tagged<FirstLogicType>> first,
  required ReadyValidInterface<Tagged<SecondLogicType>> second,
  required ReadyValidInterface<
          Tagged<JoinedPayload<FirstLogicType, SecondLogicType>>>
      downstream,
  required this.capacity,
  ReadyValidInterface<TagCancel>? cancel,
  super.name = 'ready_valid_tagged_join',
})  : tagWidth = reserve.data.width,
      super(
        definitionName: 'ReadyValidTaggedJoin_C${capacity}_'
            '${payloadShapesIdentity([
              reserve.data,
              first.data,
              second.data,
              downstream.data,
              if (cancel != null) cancel.data,
            ])}',
      ) {
  if (capacity < 1) {
    throw ArgumentError.value(capacity, 'capacity', 'Must be positive.');
  }
  if (tagWidth < max(1, capacity.log2Ceil) ||
      first.data.tagWidth != tagWidth ||
      second.data.tagWidth != tagWidth ||
      downstream.data.tagWidth != tagWidth ||
      (cancel != null && cancel.data.tagWidth != tagWidth)) {
    throw ArgumentError(
        'All tags must match and represent every reservation.');
  }
  if (!rohdPayloadsCompatible([first.data.data, downstream.data.data.first],
          ignoreConstantNames: true) ||
      !rohdPayloadsCompatible([second.data.data, downstream.data.data.second],
          ignoreConstantNames: true)) {
    throw ArgumentError('Joined operand payloads must be compatible.');
  }
  reserve.requireProviderComposableWith(ReadyValidContract.validThenReady,
      consumerName: '$name reserve');
  first.requireProviderComposableWith(ReadyValidContract.readyAndValid,
      consumerName: '$name first');
  second.requireProviderComposableWith(ReadyValidContract.readyAndValid,
      consumerName: '$name second');
  cancel?.requireProviderComposableWith(ReadyValidContract.readyAndValid,
      consumerName: '$name cancel');
  downstream.requireConsumerComposableWith(ReadyValidContract.irrevocable,
      providerName: name);

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  this.reserve = addPairInterface(reserve, PairRole.consumer,
      uniquify: (original) => 'reserve_$original');
  this.first = addPairInterface(first, PairRole.consumer,
      uniquify: (original) => 'first_$original');
  this.second = addPairInterface(second, PairRole.consumer,
      uniquify: (original) => 'second_$original');
  final localCancel = this.cancel = cancel == null
      ? null
      : addPairInterface(cancel, PairRole.consumer,
          uniquify: (original) => 'cancel_$original');
  this.downstream = addPairInterfaceFrom(
    IrrevocableReadyValidInterface(downstream.data.cloneTyped()),
    downstream,
    PairRole.provider,
    uniquify: (original) => 'downstream_$original',
  );
  addOutput('reservedTags', width: capacity);
  addOutput('invalidReservation');
  addOutput('invalidFirst');
  addOutput('invalidSecond');
  addOutput('invalidCancel');
  addOutput('cancelTooLate');
  addOutput('cancelApplied');

  Logic matches(Logic tag) => [
        for (var index = 0; index < capacity; index++) tag.eq(index),
      ].rconcat();
  final active = ~localReset;
  final reserveMatches = matches(this.reserve.data);
  this.reserve.ready <= active & (reserveMatches & ~reservedTags).or();
  invalidReservation <= active & this.reserve.valid & ~reserveMatches.or();
  if (localCancel != null) {
    localCancel.ready <= active;
  }
  final selectedTag = Logic(name: 'selected_tag', width: tagWidth);
  final releasedTags = Logic(name: 'released_tags', width: capacity);
  final canceledTags = Logic(name: 'canceled_tags', width: capacity);

  ReadyValidMultiTaggedCompletionTable<OperandType>
      operandTable<OperandType extends Logic>(
          ReadyValidInterface<Tagged<OperandType>> operand,
          Logic invalid,
          String side) {
    final write = ReadyAndValidInterface(operand.data.cloneTyped());
    final release =
        ReadyAndValidInterface(Logic(name: 'release_mask', width: capacity));
    final noCancel =
        ReadyAndValidInterface(Logic(name: 'no_cancel', width: capacity));
    noCancel.valid <= Const(0);
    noCancel.data <= Const(0, width: capacity);
    final table = ReadyValidMultiTaggedCompletionTable(
      clk: localClk,
      reset: localReset,
      writes: [write],
      releaseMask: release,
      cancellationMask: noCancel,
      lookupTags: [selectedTag],
      capacity: capacity,
      name: '${name}_${side}_operands',
    );
    final operandMatches = matches(operand.data.tag);
    final slotAvailable =
        (operandMatches & reservedTags & ~table.occupiedTags).or();
    operand.ready <= active;
    invalid <= operand.accepted & ~slotAvailable;
    write.valid <=
        operand.accepted &
            slotAvailable &
            ~(canceledTags & operandMatches).or();
    write.data <= operand.data;
    release.data <= releasedTags & table.occupiedTags;
    release.valid <= release.data.or();
    return table;
  }

  final left = operandTable(this.first, invalidFirst, 'first');
  final right = operandTable(this.second, invalidSecond, 'second');
  final pairs =
      reservedTags & left.occupiedTags & right.occupiedTags & ~canceledTags;
  final arbiter = PolicyArbiter(
    [for (var index = 0; index < capacity; index++) active & pairs[index]],
    clk: localClk,
    reset: localReset,
    policy: roundRobinArbitrationPolicy,
    advanceMode: ArbitrationAdvanceMode.onAccepted,
    retentionPolicy: ArbitrationRetentionPolicy.holdUntilAccepted,
    accepted: this.downstream.ready,
    name: '${name}_pairs',
  );
  final cancelMatches = localCancel == null
      ? Const(0, width: capacity)
      : matches(localCancel.data.tag);
  final cancelAccepted = localCancel?.accepted ?? Const(0);
  final cancelReserved = (cancelMatches & reservedTags).or();
  final cancelHeld =
      arbiter.locked & (cancelMatches & arbiter.retainedOwner).or();
  invalidCancel <= cancelAccepted & ~cancelReserved;
  cancelTooLate <= cancelAccepted & cancelHeld;
  cancelApplied <= cancelAccepted & cancelReserved & ~cancelHeld;
  canceledTags <= cancelMatches & cancelApplied.replicate(capacity);
  releasedTags <= arbiter.acceptedGrant | canceledTags;

  selectedTag <= arbiter.selection.oneHotToBinary().zeroExtend(tagWidth);
  this.downstream.valid <= arbiter.grants.rconcat().or();
  this.downstream.data.tag <= selectedTag;
  this.downstream.data.data.first <= left.lookupData.single;
  this.downstream.data.data.second <= right.lookupData.single;
  Sequential(localClk, reset: localReset, resetValues: {
    reservedTags: 0
  }, [
    reservedTags <
        ((reservedTags & ~releasedTags) |
            (reserveMatches & this.reserve.accepted.replicate(capacity))),
  ]);
}