GrayCumulativeCreditCdc constructor

GrayCumulativeCreditCdc({
  1. required Logic sourceClk,
  2. required Logic destinationClk,
  3. required Logic sourceReset,
  4. required Logic destinationReset,
  5. required Logic sourceCreditReturned,
  6. required Logic sourceQuiesce,
  7. required ReadyValidInterface<CreditReturnEvent> downstream,
  8. required int counterWidth,
  9. int? maximumOutstanding,
  10. int syncStages = 2,
  11. int pool = 0,
  12. String name = 'gray_cumulative_credit_cdc',
})

Creates a Gray cumulative credit crossing.

Implementation

GrayCumulativeCreditCdc(
    {required Logic sourceClk,
    required Logic destinationClk,
    required Logic sourceReset,
    required Logic destinationReset,
    required Logic sourceCreditReturned,
    required Logic sourceQuiesce,
    required ReadyValidInterface<CreditReturnEvent> downstream,
    required this.counterWidth,
    int? maximumOutstanding,
    this.syncStages = 2,
    this.pool = 0,
    String name = 'gray_cumulative_credit_cdc'})
    : maximumOutstanding =
          maximumOutstanding ?? (1 << (counterWidth - 1)) - 1,
      super(
          definitionName: 'GrayCumulativeCreditCdc_W$counterWidth'
              '_M${maximumOutstanding ?? (1 << (counterWidth - 1)) - 1}'
              '_S${syncStages}_P$pool',
          name: name) {
  if (counterWidth < 2) {
    throw ArgumentError.value(
        counterWidth, 'counterWidth', 'Must be at least two.');
  }
  final halfRange = 1 << (counterWidth - 1);
  if (this.maximumOutstanding < 1 || this.maximumOutstanding >= halfRange) {
    throw ArgumentError.value(this.maximumOutstanding, 'maximumOutstanding',
        'Must be positive and below half-range.');
  }
  if (syncStages < 2) {
    throw ArgumentError.value(
        syncStages, 'syncStages', 'Must be at least two.');
  }
  if (downstream.data.countWidth != counterWidth ||
      pool < 0 ||
      pool >= downstream.data.poolCount) {
    throw ArgumentError.value(downstream, 'downstream',
        'Must have countWidth $counterWidth and contain pool $pool.');
  }

  final localSourceClk = addInput('sourceClk', sourceClk);
  final localDestinationClk = addInput('destinationClk', destinationClk);
  final localSourceReset = addInput('sourceReset', sourceReset);
  final localDestinationReset =
      addInput('destinationReset', destinationReset);
  final localSourceCreditReturned =
      addInput('sourceCreditReturned', sourceCreditReturned);
  final localSourceQuiesce = addInput('sourceQuiesce', sourceQuiesce);
  addOutput('sourceReady');
  addOutput('sourceRejected');
  addOutput('sourceOutstanding', width: counterWidth);
  addOutput('sourceFull');
  addOutput('destinationDrained');
  this.downstream = addPairInterfaceFrom(
    IrrevocableReadyValidInterface<CreditReturnEvent>(
        downstream.data.clone()),
    downstream,
    PairRole.provider,
    uniquify: (original) => 'downstream_$original',
  );

  final sourceCount =
      Logic(name: '${name}_source_count', width: counterWidth);
  final sourceOnline = (~localSourceReset & ~localSourceQuiesce)
      .named('${name}_source_online');
  final sourceGray =
      BinaryToGrayConverter(sourceCount).gray.named('${name}_source_gray');

  final synchronizedSourceGray = Synchronizer(localDestinationClk,
          dataIn: sourceGray,
          reset: localDestinationReset,
          stages: syncStages,
          name: '${name}_source_gray_sync')
      .syncData;
  final synchronizedSourceOnline = Synchronizer(localDestinationClk,
          dataIn: sourceOnline,
          reset: localDestinationReset,
          stages: syncStages,
          name: '${name}_source_online_sync')
      .syncData;
  final synchronizedSourceCount =
      GrayToBinaryConverter(synchronizedSourceGray)
          .binary
          .named('${name}_synchronized_source_count');

  final observedCount =
      Logic(name: '${name}_observed_count', width: counterWidth);
  final destinationTracking = Logic(name: '${name}_destination_tracking');
  final offering = Logic(name: '${name}_offering');
  final offeredCount =
      Logic(name: '${name}_offered_count', width: counterWidth);
  final pendingCount = (synchronizedSourceCount - observedCount)
      .named('${name}_pending_count');
  final startOffer = (destinationTracking & ~offering & pendingCount.neq(0))
      .named('${name}_start_offer');
  final outputValid = (offering | startOffer).named('${name}_output_valid');
  final outputCount =
      mux(offering, offeredCount, pendingCount).named('${name}_output_count');
  final outputAccepted =
      (outputValid & this.downstream.ready).named('${name}_output_accepted');
  final observedAfterAccept =
      (observedCount + outputCount).named('${name}_observed_after_accept');
  final remainingAfterAccept = (synchronizedSourceCount - observedAfterAccept)
      .named('${name}_remaining_after_accept');
  final canStopTracking = (~synchronizedSourceOnline &
          ((~offering & pendingCount.eq(0)) |
              (outputAccepted & remainingAfterAccept.eq(0))))
      .named('${name}_can_stop_tracking');

  this.downstream.valid <= outputValid;
  this.downstream.data.count <= outputCount;
  this.downstream.data.pool <=
      Const(pool, width: this.downstream.data.pool.width);
  destinationDrained <=
      ~synchronizedSourceOnline & ~destinationTracking & ~offering;

  Sequential(localDestinationClk, reset: localDestinationReset, resetValues: {
    observedCount: 0,
    destinationTracking: 0,
    offering: 0,
    offeredCount: 0,
  }, [
    If(~destinationTracking, then: [
      If(synchronizedSourceOnline, then: [
        observedCount < synchronizedSourceCount,
        destinationTracking < 1,
      ])
    ], orElse: [
      If(outputAccepted, then: [
        observedCount < observedAfterAccept,
        offering < 0,
      ], orElse: [
        If(startOffer, then: [
          offering < 1,
          offeredCount < pendingCount,
        ])
      ]),
      If(canStopTracking, then: [
        destinationTracking < 0,
        offering < 0,
      ])
    ])
  ]);

  final destinationAcknowledged =
      (destinationTracking & synchronizedSourceOnline)
          .named('${name}_destination_acknowledged');
  final synchronizedDestinationAcknowledged = Synchronizer(localSourceClk,
          dataIn: destinationAcknowledged,
          reset: localSourceReset,
          stages: syncStages,
          name: '${name}_destination_ack_sync')
      .syncData;
  final observedGray = BinaryToGrayConverter(observedCount)
      .gray
      .named('${name}_observed_gray');
  final synchronizedObservedGray = Synchronizer(localSourceClk,
          dataIn: observedGray,
          reset: localSourceReset,
          stages: syncStages,
          name: '${name}_observed_gray_sync')
      .syncData;
  final synchronizedObservedCount =
      GrayToBinaryConverter(synchronizedObservedGray)
          .binary
          .named('${name}_synchronized_observed_count');
  final outstanding =
      (sourceCount - synchronizedObservedCount).named('${name}_outstanding');
  final capacityAvailable = outstanding
      .lt(Const(this.maximumOutstanding, width: counterWidth))
      .named('${name}_capacity_available');
  final ready =
      (sourceOnline & synchronizedDestinationAcknowledged & capacityAvailable)
          .named('${name}_ready');
  final sourceAccepted =
      (localSourceCreditReturned & ready).named('${name}_source_accepted');

  sourceReady <= ready;
  sourceRejected <= localSourceCreditReturned & ~ready;
  sourceOutstanding <= outstanding;
  sourceFull <= ~capacityAvailable;
  Sequential(localSourceClk, reset: localSourceReset, resetValues: {
    sourceCount: 0,
  }, [
    If(sourceAccepted, then: [
      sourceCount < sourceCount + Const(1, width: counterWidth),
    ])
  ]);
}