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
- User
please give me a VHDL model of an 8 bit adder with axi streaming handshake
- Assistant
Below is a VHDL model of an 8-bit adder with AXI-Stream handshake. The AXI-Stream protocol includes
tvalid,tready, andtdatasignals for data transfer. This design assumes that the adder receives two 8-bit inputs (aandb) 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
-
Inputs and Outputs:
s_axis_tdata_aands_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.
-
Handshake Logic:
- The module waits for both
s_axis_tvalidands_axis_treadyto be'1'before accepting new input data. - After computing the sum, it sets
m_axis_tvalidto'1'to indicate that the output is valid. - The module waits for
m_axis_treadyto be'1'before clearing the output valid flag and allowing new inputs.
- The module waits for both
-
Addition:
- The sum is computed using the
+operator on the two 8-bit inputs.
- The sum is computed using the
-
Reset:
- On reset (
reset_n = '0'), all internal states are cleared, and the module is ready to accept new inputs.
- On reset (
Simulation Example
To test this module, you can create a simple testbench that simulates the following scenario:
- Drive
s_axis_tdata_aands_axis_tdata_bwith random 8-bit values. - Assert
s_axis_tvalidto indicate valid input data. - Monitor
s_axis_treadyto ensure the module is ready to accept data. - Observe
m_axis_tdata,m_axis_tvalid, andm_axis_treadyto 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!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.