Token Bucket Rate Limiter
Medium · Sequential · SystemVerilog
Design a token bucket rate limiter. The bucket holds at most {BUCKET_DEPTH} tokens and starts full after reset.
Each rising edge applies refill, then consume: if {refill_tick} is high, {REFILL_RATE} tokens are added, saturating at {BUCKET_DEPTH}; then {granted} $= \min(${req_count}$,$ post-refill level$)$ tokens are consumed and {level} decreases by {granted}. Both outputs are registered — they show the result of the operation from the previous edge.
On reset ({resetn} low, synchronous), {level} is set to {BUCKET_DEPTH} and {granted} clears to $0$.