ReadyValidTokenBucket<LogicType extends Logic> constructor

ReadyValidTokenBucket<LogicType extends Logic>({
  1. required Logic clk,
  2. required Logic reset,
  3. required ReadyValidInterface<LogicType> upstream,
  4. required ReadyValidInterface<LogicType> downstream,
  5. required Logic cost,
  6. required int capacity,
  7. required int refillAmount,
  8. required int refillPeriod,
  9. int? initialTokens,
  10. TokenBucketRefillGenerator? refillGenerator,
  11. String name = 'ready_valid_token_bucket',
})

Creates a periodically refilled, saturating token bucket.

Implementation

ReadyValidTokenBucket(
    {required Logic clk,
    required Logic reset,
    required ReadyValidInterface<LogicType> upstream,
    required ReadyValidInterface<LogicType> downstream,
    required Logic cost,
    required this.capacity,
    required this.refillAmount,
    required this.refillPeriod,
    int? initialTokens,
    TokenBucketRefillGenerator? refillGenerator,
    String name = 'ready_valid_token_bucket'})
    : initialTokens = initialTokens ?? capacity,
      super(
          definitionName: 'ReadyValidTokenBucket_C${capacity}_R'
              '${refillAmount}_P${refillPeriod}_${upstream.data.name}',
          name: name) {
  if (!rohdPayloadsCompatible([upstream.data, downstream.data],
      ignoreConstantNames: true)) {
    throw ArgumentError('Token bucket payload shapes must be compatible.');
  }
  upstream.requireProviderComposableWith(ReadyValidContract.validThenReady,
      consumerName: '$name upstream');
  downstream.requireConsumerComposableWith(ReadyValidContract.readyAndValid,
      providerName: name);

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localCost = addInput('cost', cost, width: cost.width);
  this.upstream = addPairInterface(upstream, PairRole.consumer,
      uniquify: (original) => 'upstream_$original');
  this.downstream = addPairInterface(downstream, PairRole.provider,
      uniquify: (original) => 'downstream_$original');

  final spend = Logic(name: '${name}_spend');
  final core = TokenBucket(
      clk: localClk,
      reset: localReset,
      spend: spend,
      cost: localCost,
      capacity: capacity,
      refillAmount: refillAmount,
      refillPeriod: refillPeriod,
      initialTokens: this.initialTokens,
      refillGenerator: refillGenerator,
      name: '${name}_core');

  // The payload is not retained: the bucket gates a direct path between the
  // two boundaries. The consumer's readiness is that path's capacity, and
  // one spend is both the upstream acceptance and the downstream transfer.
  final consumerReady = Logic(name: '${name}_consumer_ready');
  final payload = this.upstream.data.cloneTyped(name: '${name}_payload');
  ReadyValidEgressAttachmentPolicy<LogicType>(
    requiresRetention: false,
    coreGatesTransfer: true,
  ).attach(
    downstream: this.downstream,
    offerValid: this.upstream.valid & core.canSpend,
    payload: payload,
    transfer: consumerReady,
  );
  ReadyValidIngressAttachmentPolicy<LogicType>(
    supportsSameCycleReplacement: false,
  ).attach(
    upstream: this.upstream,
    canAccept: consumerReady & core.canSpend,
    accept: spend,
    payload: payload,
  );

  addOutput('tokens', width: core.tokens.width) <= core.tokens;
  addOutput('rateLimited') <= this.upstream.valid & ~core.canSpend;
}