Skip to content
The Chat Archive
qwen-max-2025-01-25· Alibaba (Qwen) LMArena Human Preference 140K

please give me a VHDL model of an 8 bit adder with axi streaming handshake

  1. User

    please give me a VHDL model of an 8 bit adder with axi streaming handshake

  2. Assistant

    Below is a VHDL model of an 8-bit adder with AXI-Stream handshake. The AXI-Stream protocol includes tvalid, tready, and tdata signals for data transfer. This design assumes that the adder receives two 8-bit inputs (a and b) via AXI-Stream, computes their sum, and outputs the result on another AXI-Stream interface.

    VHDL Code for 8-Bit Adder with AXI-Stream Handshake

    library IEEE;
    use IEEE.STD_LOGIC_1164.ALL;
    use IEEE.STD_LOGIC_ARITH.ALL;
    use IEEE.STD_LOGIC_UNSIGNED.ALL;
    
    entity axi_stream_adder is
        Port (
            -- Clock and reset
            clk       : in  std_logic;
            reset_n   : in  std_logic;  -- Active low reset
            
            -- Input AXI-Stream Interface
            s_axis_tdata_a : in  std_logic_vector(7 downto 0);
            s_axis_tdata_b : in  std_logic_vector(7 downto 0);
            s_axis_tvalid  : in  std_logic;
            s_axis_tready  : out std_logic;
            
            -- Output AXI-Stream Interface
            m_axis_tdata   : out std_logic_vector(7 downto 0);
            m_axis_tvalid  : out std_logic;
            m_axis_tready  : in  std_logic
        );
    end axi_stream_adder;
    
    architecture Behavioral of axi_stream_adder is
    
        -- Internal signals
        signal input_ready   : std_logic := '1';  -- Indicates readiness to accept new inputs
        signal output_valid  : std_logic := '0';  -- Indicates valid output data
        signal sum           : std_logic_vector(7 downto 0);  -- Sum of inputs
    
    begin
    
        -- AXI-Stream handshake logic
        process(clk, reset_n)
        begin
            if reset_n = '0' then
                input_ready <= '1';
                output_valid <= '0';
                sum <= (others => '0');
            elsif rising_edge(clk) then
                -- Input handshake: Accept new data when ready and valid
                if s_axis_tvalid = '1' and input_ready = '1' then
                    sum <= s_axis_tdata_a + s_axis_tdata_b;  -- Perform addition
                    output_valid <= '1';  -- Mark output as valid
                    input_ready <= '0';   -- Stop accepting new inputs until output is consumed
                end if;
    
                -- Output handshake: Wait for downstream module to be ready
                if output_valid = '1' and m_axis_tready = '1' then
                    output_valid <= '0';  -- Clear output valid flag
                    input_ready <= '1';   -- Allow new inputs to be accepted
                end if;
            end if;
        end process;
    
        -- Assign outputs
        s_axis_tready <= input_ready;
        m_axis_tdata <= sum;
        m_axis_tvalid <= output_valid;
    
    end Behavioral;
    

    Explanation of the Code

    1. Inputs and Outputs:

      • s_axis_tdata_a and s_axis_tdata_b: Two 8-bit input streams.
      • s_axis_tvalid: Indicates that the input data is valid.
      • s_axis_tready: Indicates that the module is ready to accept new input data.
      • m_axis_tdata: 8-bit output stream containing the sum of the inputs.
      • m_axis_tvalid: Indicates that the output data is valid.
      • m_axis_tready: Indicates that the downstream module is ready to accept the output data.
    2. Handshake Logic:

      • The module waits for both s_axis_tvalid and s_axis_tready to be '1' before accepting new input data.
      • After computing the sum, it sets m_axis_tvalid to '1' to indicate that the output is valid.
      • The module waits for m_axis_tready to be '1' before clearing the output valid flag and allowing new inputs.
    3. Addition:

      • The sum is computed using the + operator on the two 8-bit inputs.
    4. Reset:

      • On reset (reset_n = '0'), all internal states are cleared, and the module is ready to accept new inputs.

    Simulation Example

    To test this module, you can create a simple testbench that simulates the following scenario:

    1. Drive s_axis_tdata_a and s_axis_tdata_b with random 8-bit values.
    2. Assert s_axis_tvalid to indicate valid input data.
    3. Monitor s_axis_tready to ensure the module is ready to accept data.
    4. Observe m_axis_tdata, m_axis_tvalid, and m_axis_tready to verify the correctness of the output.

    Notes

    • This implementation assumes that the downstream module will eventually assert m_axis_tready. If this is not guaranteed, additional logic may be required to handle backpressure.
    • The design uses synchronous logic, which is suitable for FPGA or ASIC implementations.

    Let me know if you need further clarification or enhancements!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.