Rate Limiting in VHDL

Here’s the translation of the Go rate limiting example to VHDL, formatted in Markdown suitable for Hugo:

Rate limiting is an important mechanism for controlling resource utilization and maintaining quality of service. VHDL can support rate limiting with processes and signals.

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity rate_limiter is
    Port ( clk : in STD_LOGIC;
           reset : in STD_LOGIC;
           request : in STD_LOGIC;
           response : out STD_LOGIC);
end rate_limiter;

architecture Behavioral of rate_limiter is
    signal counter : integer range 0 to 5000000 := 0; -- 100ms at 50MHz clock
    signal limiter : STD_LOGIC := '0';
begin
    process(clk, reset)
    begin
        if reset = '1' then
            counter <= 0;
            limiter <= '0';
        elsif rising_edge(clk) then
            if counter = 5000000 then
                counter <= 0;
                limiter <= '1';
            else
                counter <= counter + 1;
                limiter <= '0';
            end if;
        end if;
    end process;

    process(clk, reset)
    begin
        if reset = '1' then
            response <= '0';
        elsif rising_edge(clk) then
            if request = '1' and limiter = '1' then
                response <= '1';
            else
                response <= '0';
            end if;
        end if;
    end process;
end Behavioral;

In this VHDL implementation, we create a basic rate limiter that allows one request every 100ms (assuming a 50MHz clock). Here’s how it works:

  1. We use a counter that counts up to 5,000,000 (100ms at 50MHz). When it reaches this value, it sets the limiter signal high for one clock cycle.

  2. The limiter signal acts as our rate limiter. It goes high once every 100ms, allowing one request to be processed.

  3. When a request comes in (indicated by the request signal going high) and the limiter is high, we set the response signal high for one clock cycle.

This implementation doesn’t allow for bursts like the original example, but it does implement basic rate limiting. To test this in a simulation or on actual hardware, you would need to create a testbench or top-level entity that generates requests and observes the responses.

To implement a bursty limiter in VHDL, you could use a more complex state machine that allows a certain number of requests to pass through quickly before enforcing the rate limit. This would require additional signals and logic to keep track of the burst count and timing.

Remember that VHDL is typically used for describing hardware, so concepts like channels and goroutines don’t have direct equivalents. Instead, we use processes and signals to model concurrent behavior and communication between different parts of the circuit.

Running this VHDL code on an FPGA or in a simulation would show the response signal going high at most once every 100ms, regardless of how often the request signal goes high. This effectively limits the rate of allowed requests.

-- Example usage in a testbench
entity testbench is
end testbench;

architecture tb of testbench is
    signal clk : STD_LOGIC := '0';
    signal reset : STD_LOGIC := '0';
    signal request : STD_LOGIC := '0';
    signal response : STD_LOGIC;
begin
    -- Instantiate the rate limiter
    uut: entity work.rate_limiter port map (clk, reset, request, response);

    -- Clock process
    clk_process: process
    begin
        clk <= '0';
        wait for 10 ns;
        clk <= '1';
        wait for 10 ns;
    end process;

    -- Stimulus process
    stim_process: process
    begin
        reset <= '1';
        wait for 100 ns;
        reset <= '0';
        
        -- Send requests every 50ns
        for i in 1 to 10 loop
            request <= '1';
            wait for 50 ns;
            request <= '0';
            wait for 50 ns;
        end loop;
        
        wait;
    end process;
end tb;

This testbench would demonstrate the rate limiting behavior, with the response signal going high at most once every 100ms, despite requests coming in more frequently.