Rate Limiter
Medium · Sequential · SystemVerilog · VHDL
A premium problem. Subscribe to open it in the editor, run the testbench and see where your design lands.
Design a token bucket rate limiter.
A token bucket admits requests at an average rate set by its refills while still allowing bursts up to its size: every request spends tokens from the bucket, and tokens come back at a fixed rate.
The bucket holds up to BUCKET_DEPTH tokens. In a cycle with refill_tick high, REFILL_RATE tokens are added, up to BUCKET_DEPTH. Each cycle req_count tokens are requested, and the grant is the smaller of req_count and the tokens held after that cycle's refill; the granted tokens leave the bucket.
granted is the number of tokens granted and level the number held after the grant. Both are registered: they show the cycle before.
resetn is a synchronous, active-low reset; it fills the bucket, so level = BUCKET_DEPTH, and clears granted.
- Arbitration & flow control