TokenBucket constructor

TokenBucket({
  1. required Logic clk,
  2. required Logic reset,
  3. required Logic spend,
  4. required Logic cost,
  5. required int capacity,
  6. required int refillAmount,
  7. required int refillPeriod,
  8. int? initialTokens,
  9. TokenBucketRefillGenerator? refillGenerator,
  10. String name = 'token_bucket',
})

Creates a saturating token bucket driven by a spend action.

Implementation

TokenBucket(
    {required Logic clk,
    required Logic reset,
    required Logic spend,
    required Logic cost,
    required this.capacity,
    required this.refillAmount,
    required this.refillPeriod,
    int? initialTokens,
    TokenBucketRefillGenerator? refillGenerator,
    super.name = 'token_bucket'})
    : initialTokens = initialTokens ?? capacity,
      super(
          definitionName: 'TokenBucket_C${capacity}_R${refillAmount}_'
              'P${refillPeriod}_I${initialTokens ?? capacity}_'
              'W${cost.width}_'
              '${refillGenerator == null ? 'Periodic' : 'Custom'}') {
  if (capacity < 1) {
    throw ArgumentError.value(capacity, 'capacity', 'Must be at least one.');
  }
  if (refillAmount < 1 || refillAmount > capacity) {
    throw ArgumentError.value(
        refillAmount, 'refillAmount', 'Must be between one and capacity.');
  }
  if (refillPeriod < 1) {
    throw ArgumentError.value(
        refillPeriod, 'refillPeriod', 'Must be at least one cycle.');
  }
  if (this.initialTokens < 0 || this.initialTokens > capacity) {
    throw ArgumentError.value(this.initialTokens, 'initialTokens',
        'Must be between zero and capacity.');
  }

  final localClk = addInput('clk', clk);
  final localReset = addInput('reset', reset);
  final localSpend = addInput('spend', spend);
  final localCost = addInput('cost', cost, width: cost.width);

  final tokenWidth = (capacity + 1).log2Ceil;
  final tokenRegister = Logic(name: '${name}_tokens', width: tokenWidth);
  final accepted = Logic(name: '${name}_accepted');
  final effectiveRefillGenerator = refillGenerator ??
      periodicTokenBucketRefillGenerator(
          refillAmount: refillAmount, refillPeriod: refillPeriod);
  final refill = effectiveRefillGenerator(
          tokens: tokenRegister,
          accepted: accepted,
          clk: localClk,
          reset: localReset,
          tokenWidth: tokenWidth,
          name: name)
      .named('${name}_refill');
  if (refill.width != tokenWidth) {
    throw ArgumentError.value(refillGenerator, 'refillGenerator',
        'Must produce a $tokenWidth-bit token supply.');
  }
  final calculationWidth = max(tokenWidth, localCost.width) + 1;
  final refilledWide = (tokenRegister.zeroExtend(calculationWidth) +
          refill.zeroExtend(calculationWidth))
      .named('${name}_refilled_wide');
  final refilledTokens = mux(
          refilledWide.lt(Const(capacity + 1, width: calculationWidth)),
          refilledWide.getRange(0, tokenWidth),
          Const(capacity, width: tokenWidth))
      .named('${name}_refilled_tokens');
  final hasTokens = (~refilledTokens
          .zeroExtend(calculationWidth)
          .lt(localCost.zeroExtend(calculationWidth)))
      .named('${name}_has_tokens');
  accepted <= localSpend & hasTokens;

  addOutput('canSpend') <= hasTokens;
  addOutput('tokens', width: tokenWidth) <= tokenRegister;

  Sequential(localClk, reset: localReset, resetValues: {
    tokenRegister: this.initialTokens
  }, [
    If(accepted, then: [
      tokenRegister <
          (refilledTokens.zeroExtend(calculationWidth) -
                  localCost.zeroExtend(calculationWidth))
              .getRange(0, tokenWidth)
    ], orElse: [
      tokenRegister < refilledTokens
    ])
  ]);
}