ReadyValidTagTranslator<RequestType extends Logic, ResponseType extends Logic> constructor

ReadyValidTagTranslator<RequestType extends Logic, ResponseType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required ReadyValidInterface<Tagged<RequestType>> upstreamRequests,
  4. required ReadyValidInterface<Tagged<RequestType>> downstreamRequests,
  5. required ReadyValidInterface<Tagged<ResponseType>> downstreamResponses,
  6. required ReadyValidInterface<Tagged<ResponseType>> upstreamResponses,
  7. required int maxInFlight,
  8. ReadyValidInterface<TagCancel>? cancellation,
  9. Logic? requestDomain,
  10. List<int>? compactTagDomains,
  11. int? timeoutCycles,
  12. TransactionCompletionPredicate<ResponseType> responseCompletion = completesTransactionOnEveryBeat,
  13. String responseCompletionName = 'everyBeat',
  14. IndexAllocationGenerator allocationGenerator = lowestFreeIndexAllocationGenerator,
  15. String allocationPolicyName = 'lowestFree',
  16. String name = 'ready_valid_tag_translator',
})

Creates a bounded tag translator.

Give each custom responseCompletion policy a distinct, stable SystemVerilog-safe responseCompletionName for its generated definition.

Implementation

ReadyValidTagTranslator(
    {required Logic clk,
    required Logic reset,
    required ReadyValidInterface<Tagged<RequestType>> upstreamRequests,
    required ReadyValidInterface<Tagged<RequestType>> downstreamRequests,
    required ReadyValidInterface<Tagged<ResponseType>> downstreamResponses,
    required ReadyValidInterface<Tagged<ResponseType>> upstreamResponses,
    required this.maxInFlight,
    ReadyValidInterface<TagCancel>? cancellation,
    Logic? requestDomain,
    List<int>? compactTagDomains,
    int? timeoutCycles,
    TransactionCompletionPredicate<ResponseType> responseCompletion =
        completesTransactionOnEveryBeat,
    String responseCompletionName = 'everyBeat',
    IndexAllocationGenerator allocationGenerator =
        lowestFreeIndexAllocationGenerator,
    this.allocationPolicyName = 'lowestFree',
    String name = 'ready_valid_tag_translator'})
    : compactTagWidth = max(1, maxInFlight.log2Ceil),
      super(
          definitionName: 'ReadyValidTagTranslator_N${maxInFlight}_'
              '${upstreamRequests.data.tag.width}_'
              '${downstreamRequests.data.tag.width}_'
              'P${allocationPolicyName}_C${responseCompletionName}_'
              'Timeout${timeoutCycles ?? 0}_'
              'Domains${compactTagDomains?.join('x') ?? 'none'}_'
              '${_cancellationIdentity(cancellation)}_'
              '${payloadShapesIdentity([
                upstreamRequests.data,
                downstreamRequests.data,
                downstreamResponses.data,
                upstreamResponses.data
              ])}',
          name: name) {
  if (maxInFlight < 1) {
    throw ArgumentError.value(
        maxInFlight, 'maxInFlight', 'Must be positive.');
  }
  if (!RegExp(r'^[A-Za-z][A-Za-z0-9_]*$').hasMatch(allocationPolicyName)) {
    throw ArgumentError.value(allocationPolicyName, 'allocationPolicyName',
        'Must be a valid SystemVerilog identifier fragment.');
  }
  if (!identical(allocationGenerator, lowestFreeIndexAllocationGenerator) &&
      allocationPolicyName == 'lowestFree') {
    throw ArgumentError.value(allocationPolicyName, 'allocationPolicyName',
        'A custom allocation generator requires a distinct policy name.');
  }
  if (!RegExp(r'^[A-Za-z][A-Za-z0-9_]*$').hasMatch(responseCompletionName)) {
    throw ArgumentError.value(
        responseCompletionName,
        'responseCompletionName',
        'Must be a valid SystemVerilog identifier fragment.');
  }
  if (!identical(responseCompletion, completesTransactionOnEveryBeat) &&
      responseCompletionName == 'everyBeat') {
    throw ArgumentError.value(
        responseCompletionName,
        'responseCompletionName',
        'A custom completion predicate requires a distinct policy name.');
  }
  if (downstreamRequests.data.tag.width != compactTagWidth ||
      downstreamResponses.data.tag.width != compactTagWidth ||
      upstreamResponses.data.tag.width != upstreamRequests.data.tag.width) {
    throw ArgumentError(
        'Compact tags must have width $compactTagWidth and upstream '
        'request and response tag widths must match.');
  }
  if (!rohdPayloadsCompatible(
          [upstreamRequests.data.data, downstreamRequests.data.data],
          ignoreConstantNames: true) ||
      !rohdPayloadsCompatible(
          [downstreamResponses.data.data, upstreamResponses.data.data],
          ignoreConstantNames: true)) {
    throw ArgumentError('Request and response payloads must be compatible.');
  }
  if ((requestDomain == null) != (compactTagDomains == null)) {
    throw ArgumentError(
        'requestDomain and compactTagDomains must be provided together.');
  }
  if (compactTagDomains != null &&
      (compactTagDomains.length != maxInFlight ||
          compactTagDomains.any((domain) => domain < 0))) {
    throw ArgumentError.value(compactTagDomains, 'compactTagDomains',
        'Must provide one non-negative domain for every compact tag.');
  }
  if (timeoutCycles != null && timeoutCycles < 1) {
    throw ArgumentError.value(
        timeoutCycles, 'timeoutCycles', 'Must be at least one.');
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  if (cancellation != null &&
      cancellation.data.tag.width != upstreamRequests.data.tag.width) {
    throw ArgumentError.value(cancellation, 'cancellation',
        'Must have the same width as the upstream request tag.');
  }
  cancellation?.requireProviderComposableWith(
      ReadyValidContract.readyAndValid,
      consumerName: '$name cancellation');
  this.cancellation = cancellation == null
      ? null
      : addPairInterface(cancellation, PairRole.consumer,
          uniquify: (original) => 'cancellation_$original');
  if (this.cancellation != null) {
    this.cancellation!.ready <= Const(1);
  }
  final localCancelValid = this.cancellation?.accepted ?? Const(0);
  final localCancelTag = this.cancellation == null
      ? Const(0, width: upstreamRequests.data.tag.width)
      : this.cancellation!.data.tag;
  final domainWidth = compactTagDomains == null
      ? 1
      : max(1, (compactTagDomains.reduce(max) + 1).log2Ceil);
  if (requestDomain != null && requestDomain.width != domainWidth) {
    throw ArgumentError.value(requestDomain, 'requestDomain',
        'Must have width $domainWidth for compactTagDomains.');
  }
  final localRequestDomain = requestDomain == null
      ? Const(0, width: domainWidth)
      : addInput('requestDomain', requestDomain, width: domainWidth);
  this.upstreamRequests = addPairInterface(
      upstreamRequests, PairRole.consumer,
      uniquify: (original) => 'upstream_request_$original');
  this.downstreamRequests = addPairInterface(
      downstreamRequests, PairRole.provider,
      uniquify: (original) => 'downstream_request_$original');
  this.downstreamResponses = addPairInterface(
      downstreamResponses, PairRole.consumer,
      uniquify: (original) => 'downstream_response_$original');
  this.upstreamResponses = addPairInterface(
      upstreamResponses, PairRole.provider,
      uniquify: (original) => 'upstream_response_$original');
  addOutput('invalidResponse');
  addOutput('invalidRequestDomain');
  addOutput('cancelAccepted');
  addOutput('cancelMiss');
  addOutput('canceledResponse');
  addOutput('timedOut');
  addOutput('timedOutCompactTags', width: maxInFlight);
  addOutput('timedOutCallerTag', width: this.upstreamRequests.data.tag.width);
  addOutput('timedOutResponse');

  final timedOutState =
      List.generate(maxInFlight, (index) => Logic(name: 'timed_out_$index'));
  final lifecycleAllocation = ReadyAndValidInterface<Tagged<Logic>>(Tagged(
      Logic(name: 'callerTag', width: this.upstreamRequests.data.tag.width),
      tagWidth: compactTagWidth,
      name: 'tagMapping'));
  final lifecycleCancel = ReadyAndValidInterface<TagCancel>(
      TagCancel(tagWidth: compactTagWidth, name: 'mappingCancellation'));
  final lifecycleCancellationMask = ReadyAndValidInterface<Logic>(
      Logic(name: 'cancellationMask', width: maxInFlight));
  final lifecycleRelease = ReadyAndValidInterface<TagRelease>(
      TagRelease(tagWidth: compactTagWidth, name: 'mappingRelease'));
  final lifecycle = TaggedLifecycleTracker<Logic>(
      clk: localClk,
      reset: localReset,
      allocation: lifecycleAllocation,
      cancel: lifecycleCancel,
      cancellationMask: lifecycleCancellationMask,
      release: lifecycleRelease,
      lookupTag: this.downstreamResponses.data.tag,
      capacity: maxInFlight,
      name: '${name}_lifecycle');
  lifecycleCancel.valid <= Const(0);
  lifecycleCancel.data.tag <= Const(0, width: compactTagWidth);
  lifecycleCancel.data.reason <= Const(0);
  final requestAccepted = Logic(name: '${name}_request_accepted');
  final domainMatches = List.generate(
      maxInFlight,
      (index) => (compactTagDomains == null
              ? Const(1)
              : localRequestDomain
                  .eq(Const(compactTagDomains[index], width: domainWidth)))
          .named('${name}_domain_matches_$index'));
  final responseMatches = List.generate(
      maxInFlight, (index) => Logic(name: 'response_match_$index'));
  Combinational([
    for (var index = 0; index < maxInFlight; index++)
      responseMatches[index] <
          lifecycle.liveTags[index] &
              this
                  .downstreamResponses
                  .data
                  .tag
                  .eq(Const(index, width: compactTagWidth)),
  ]);
  final responseComplete =
      responseCompletion(this.downstreamResponses.data.data)
          .named('${name}_response_complete');
  if (responseComplete.width != 1) {
    throw ArgumentError.value(responseCompletion, 'responseCompletion',
        'Must produce a one-bit completion predicate.');
  }
  final responseCompleted = (this.downstreamResponses.valid &
          responseMatches.rconcat().or() &
          this.downstreamResponses.ready &
          responseComplete)
      .named('${name}_response_completed');
  final allocateGrant = allocationGenerator(
      available: List.generate(
          maxInFlight,
          (index) =>
              (~lifecycle.liveTags[index] |
                  (responseCompleted & responseMatches[index])) &
              domainMatches[index]),
      accepted: requestAccepted,
      clk: localClk,
      reset: localReset,
      name: '${name}_allocation');
  if (allocateGrant.length != maxInFlight) {
    throw ArgumentError.value(allocationGenerator, 'allocationGenerator',
        'Must produce one grant bit for each compact tag.');
  }
  final cancelMatches = List.generate(
      maxInFlight, (index) => Logic(name: 'cancel_match_$index'));

  Combinational([
    for (var index = 0; index < maxInFlight; index++)
      cancelMatches[index] <
          lifecycle.liveTags[index] &
              ~lifecycle.canceledTags[index] &
              lifecycle.entryData[index].eq(localCancelTag),
  ]);

  final allocationAvailable =
      allocateGrant.rconcat().or().named('${name}_allocation_available');
  this.downstreamRequests.valid <=
      this.upstreamRequests.valid & allocationAvailable;
  this.upstreamRequests.ready <=
      this.downstreamRequests.ready & allocationAvailable;
  this.downstreamRequests.data.data <= this.upstreamRequests.data.data;
  Logic compactTag = Const(0, width: compactTagWidth);
  for (var index = maxInFlight - 1; index >= 0; index--) {
    compactTag = mux(allocateGrant[index],
            Const(index, width: compactTagWidth), compactTag)
        .named('${name}_compact_tag_$index');
  }
  this.downstreamRequests.data.tag <= compactTag;
  final domainAvailable =
      domainMatches.rconcat().or().named('${name}_domain_available');
  invalidRequestDomain <= this.upstreamRequests.valid & ~domainAvailable;

  final activeResponseMatches = List.generate(
      maxInFlight,
      (index) => (responseMatches[index] & ~lifecycle.canceledTags[index])
          .named('${name}_active_response_matches_$index'));
  final canceledResponseMatches = List.generate(
      maxInFlight,
      (index) => (responseMatches[index] & lifecycle.canceledTags[index])
          .named('${name}_canceled_response_matches_$index'));
  final timedOutResponseMatches = List.generate(
      maxInFlight,
      (index) => (canceledResponseMatches[index] & timedOutState[index])
          .named('${name}_timed_out_response_matches_$index'));
  final responseMapped =
      activeResponseMatches.rconcat().or().named('${name}_response_mapped');
  final responseCanceled = canceledResponseMatches
      .rconcat()
      .or()
      .named('${name}_response_canceled');
  final responseTimedOut = timedOutResponseMatches
      .rconcat()
      .or()
      .named('${name}_response_timed_out');
  this.upstreamResponses.valid <=
      this.downstreamResponses.valid & responseMapped;
  this.downstreamResponses.ready <=
      this.upstreamResponses.ready | ~responseMapped | responseCanceled;
  this.upstreamResponses.data.data <= this.downstreamResponses.data.data;
  var restoredTag = lifecycle.entryData.last;
  for (var index = maxInFlight - 2; index >= 0; index--) {
    restoredTag = mux(activeResponseMatches[index],
            lifecycle.entryData[index], restoredTag)
        .named('${name}_restored_tag_$index');
  }
  this.upstreamResponses.data.tag <= restoredTag;
  invalidResponse <=
      this.downstreamResponses.valid & ~responseMapped & ~responseCanceled;
  canceledResponse <= this.downstreamResponses.valid & responseCanceled;
  timedOutResponse <= this.downstreamResponses.valid & responseTimedOut;

  requestAccepted <=
      this.upstreamRequests.valid & this.upstreamRequests.ready;
  final responseAccepted = (this.downstreamResponses.valid &
          (responseMapped | responseCanceled) &
          this.downstreamResponses.ready)
      .named('${name}_response_accepted');
  final cancellationMapped =
      cancelMatches.rconcat().or().named('${name}_cancellation_mapped');
  // A completing response wins a same-edge cancellation race for that tag.
  final cancellationAccepted =
      (localCancelValid & cancellationMapped & ~responseAccepted)
          .named('${name}_cancellation_accepted');
  final timeoutWidth =
      timeoutCycles == null ? 1 : max(1, timeoutCycles.log2Ceil);
  final timeoutCounters = timeoutCycles == null
      ? <Logic>[]
      : List.generate(
          maxInFlight,
          (index) =>
              Logic(name: '${name}_timeout_$index', width: timeoutWidth));
  final timedOutEntries = timeoutCycles == null
      ? List<Logic>.filled(maxInFlight, Const(0))
      : List.generate(
          maxInFlight,
          (index) => (lifecycle.liveTags[index] &
                  ~lifecycle.canceledTags[index] &
                  timeoutCounters[index].eq(timeoutCycles - 1) &
                  ~(responseCompleted & responseMatches[index]))
              .named('${name}_timed_out_entry_$index'));
  final timeoutOccurred =
      timedOutEntries.rconcat().or().named('${name}_timeout_occurred');
  Logic timeoutCallerTag =
      Const(0, width: this.upstreamRequests.data.tag.width);
  for (var index = maxInFlight - 1; index >= 0; index--) {
    timeoutCallerTag = mux(timedOutEntries[index], lifecycle.entryData[index],
            timeoutCallerTag)
        .named('${name}_timeout_caller_tag_$index');
  }
  cancelAccepted <= cancellationAccepted;
  cancelMiss <= localCancelValid & ~cancellationMapped;
  timedOut <= timeoutOccurred;
  timedOutCompactTags <= timedOutEntries.rconcat();
  timedOutCallerTag <= timeoutCallerTag;
  lifecycleAllocation.valid <= requestAccepted;
  lifecycleAllocation.data.data <= this.upstreamRequests.data.tag;
  lifecycleAllocation.data.tag <= compactTag;
  lifecycleCancellationMask.valid <= cancellationAccepted | timeoutOccurred;
  lifecycleCancellationMask.data <=
      List.generate(
          maxInFlight,
          (index) =>
              (cancellationAccepted & cancelMatches[index]) |
              timedOutEntries[index]).rconcat();
  lifecycleRelease.valid <= responseCompleted;
  lifecycleRelease.data.tag <= this.downstreamResponses.data.tag;
  Sequential(localClk, reset: localReset, resetValues: {
    for (final entry in timedOutState) entry: 0,
    for (final counter in timeoutCounters) counter: 0
  }, [
    for (var index = 0; index < maxInFlight; index++) ...[
      timedOutState[index] <
          (timedOutState[index] | timedOutEntries[index]) &
              ~(responseCompleted & responseMatches[index]),
      if (timeoutCycles != null)
        If(requestAccepted & allocateGrant[index], then: [
          timeoutCounters[index] < 0,
        ], orElse: [
          If(responseCompleted & responseMatches[index], then: [
            timeoutCounters[index] < 0,
          ], orElse: [
            If(
                lifecycle.liveTags[index] &
                    ~lifecycle.canceledTags[index] &
                    ~timeoutCounters[index].eq(timeoutCycles - 1),
                then: [
                  timeoutCounters[index] <
                      timeoutCounters[index] + Const(1, width: timeoutWidth),
                ])
          ])
        ]),
    ]
  ]);
}