Okay, here is a VHDL testbench for the axi_stream_adder entity. This testbench will:
- Instantiate the
axi_stream_adder (Device Under Test - DUT).
- Generate a clock signal (
ACLK).
- Generate an active-low reset signal (
ARESETN).
- Drive the input AXI-Stream interfaces (
S_AXIS_A, S_AXIS_B) with test data.
- Monitor the output AXI-Stream interface (
M_AXIS) and check the results.
- Include basic handshake scenarios, including waiting for ready signals and simulating backpressure.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
-- Testbench entity (no ports)
entity tb_axi_stream_adder is
end tb_axi_stream_adder;
architecture behavior of tb_axi_stream_adder is
-- Component declaration for the DUT
component axi_stream_adder is
Port (
ACLK : in std_logic;
ARESETN : in std_logic;
S_AXIS_A_TVALID : in std_logic;
S_AXIS_A_TREADY : out std_logic;
S_AXIS_A_TDATA : in std_logic_vector(7 downto 0);
S_AXIS_B_TVALID : in std_logic;
S_AXIS_B_TREADY : out std_logic;
S_AXIS_B_TDATA : in std_logic_vector(7 downto 0);
M_AXIS_TVALID : out std_logic;
M_AXIS_TREADY : in std_logic;
M_AXIS_TDATA : out std_logic_vector(7 downto 0)
);
end component;
-- Constants
constant CLK_PERIOD : time := 10 ns;
constant DATA_WIDTH : integer := 8;
-- Signals to connect to the DUT
signal tb_aclk : std_logic := '0';
signal tb_aresetn : std_logic := '0'; -- Start in reset
-- Input Stream A signals
signal tb_s_axis_a_tvalid : std_logic := '0';
signal tb_s_axis_a_tready : std_logic;
signal tb_s_axis_a_tdata : std_logic_vector(DATA_WIDTH-1 downto 0) := (others => '0');
-- Input Stream B signals
signal tb_s_axis_b_tvalid : std_logic := '0';
signal tb_s_axis_b_tready : std_logic;
signal tb_s_axis_b_tdata : std_logic_vector(DATA_WIDTH-1 downto 0) := (others => '0');
-- Output Stream signals
signal tb_m_axis_tvalid : std_logic;
signal tb_m_axis_tready : std_logic := '0'; -- Sink initially not ready
signal tb_m_axis_tdata : std_logic_vector(DATA_WIDTH-1 downto 0);
-- Simulation control
signal stop_sim : boolean := false;
begin
-- Instantiate the DUT
dut_inst : axi_stream_adder
port map (
ACLK => tb_aclk,
ARESETN => tb_aresetn,
S_AXIS_A_TVALID => tb_s_axis_a_tvalid,
S_AXIS_A_TREADY => tb_s_axis_a_tready,
S_AXIS_A_TDATA => tb_s_axis_a_tdata,
S_AXIS_B_TVALID => tb_s_axis_b_tvalid,
S_AXIS_B_TREADY => tb_s_axis_b_tready,
S_AXIS_B_TDATA => tb_s_axis_b_tdata,
M_AXIS_TVALID => tb_m_axis_tvalid,
M_AXIS_TREADY => tb_m_axis_tready,
M_AXIS_TDATA => tb_m_axis_tdata
);
-- Clock generation process
clk_process : process
begin
if not stop_sim then
tb_aclk <= '0';
wait for CLK_PERIOD / 2;
tb_aclk <= '1';
wait for CLK_PERIOD / 2;
else
wait; -- Stop clock when simulation ends
end if;
end process clk_process;
-- Reset generation process
reset_process : process
begin
tb_aresetn <= '0'; -- Assert reset
report "Reset Asserted" severity note;
wait for CLK_PERIOD * 5;
tb_aresetn <= '1'; -- Deassert reset
report "Reset Deasserted" severity note;
wait; -- Reset sequence finished
end process reset_process;
-- Stimulus process
stimulus_process : process
variable data_a_v : unsigned(DATA_WIDTH-1 downto 0);
variable data_b_v : unsigned(DATA_WIDTH-1 downto 0);
variable expected_sum_v : unsigned(DATA_WIDTH-1 downto 0);
begin
report "Starting Simulation" severity note;
-- Wait for reset to deassert
wait until tb_aresetn = '1';
wait until rising_edge(tb_aclk);
-- ** Test Case 1: Simple addition (10 + 5 = 15) **
report "Test Case 1: Simple Addition (10 + 5)" severity note;
data_a_v := to_unsigned(10, DATA_WIDTH);
data_b_v := to_unsigned(5, DATA_WIDTH);
expected_sum_v := data_a_v + data_b_v;
-- Prepare input data
tb_s_axis_a_tdata <= std_logic_vector(data_a_v);
tb_s_axis_b_tdata <= std_logic_vector(data_b_v);
tb_s_axis_a_tvalid <= '1';
tb_s_axis_b_tvalid <= '1';
-- Prepare to receive output
tb_m_axis_tready <= '1';
-- Wait for DUT to be ready for both inputs
wait until rising_edge(tb_aclk) and tb_s_axis_a_tready = '1' and tb_s_axis_b_tready = '1';
report "DUT ready for inputs A and B" severity note;
-- Inputs accepted by DUT on this clock edge
wait until rising_edge(tb_aclk);
tb_s_axis_a_tvalid <= '0'; -- Deassert valid after handshake
tb_s_axis_b_tvalid <= '0';
-- Wait for output data to be valid
wait until rising_edge(tb_aclk) and tb_m_axis_tvalid = '1';
report "DUT output valid" severity note;
-- Check the result
report "Checking result: Expected=" & integer'image(to_integer(expected_sum_v)) &
", Got=" & integer'image(to_integer(unsigned(tb_m_axis_tdata))) severity note;
assert tb_m_axis_tdata = std_logic_vector(expected_sum_v)
report "Test Case 1 FAILED: Incorrect sum!" severity failure;
-- Output accepted by TB on this clock edge
wait until rising_edge(tb_aclk);
tb_m_axis_tready <= '0'; -- Deassert ready after handshake (optional, good practice)
report "Test Case 1 PASSED" severity note;
wait for CLK_PERIOD * 2;
-- ** Test Case 2: Addition with output backpressure (20 + 30 = 50) **
report "Test Case 2: Addition with Output Backpressure (20 + 30)" severity note;
data_a_v := to_unsigned(20, DATA_WIDTH);
data_b_v := to_unsigned(30, DATA_WIDTH);
expected_sum_v := data_a_v + data_b_v;
-- Keep output sink NOT ready
tb_m_axis_tready <= '0';
-- Prepare input data
tb_s_axis_a_tdata <= std_logic_vector(data_a_v);
tb_s_axis_b_tdata <= std_logic_vector(data_b_v);
tb_s_axis_a_tvalid <= '1';
tb_s_axis_b_tvalid <= '1';
-- Wait for DUT to be ready for inputs
wait until rising_edge(tb_aclk) and tb_s_axis_a_tready = '1' and tb_s_axis_b_tready = '1';
report "DUT ready for inputs A and B" severity note;
-- Inputs accepted by DUT
wait until rising_edge(tb_aclk);
tb_s_axis_a_tvalid <= '0';
tb_s_axis_b_tvalid <= '0';
-- DUT should assert M_AXIS_TVALID, but we keep M_AXIS_TREADY low
wait until rising_edge(tb_aclk); -- Give DUT time to calculate
if tb_m_axis_tvalid = '1' then
report "DUT output is valid (as expected)" severity note;
else
report "DUT output not valid yet, waiting..." severity note;
wait until rising_edge(tb_aclk) and tb_m_axis_tvalid = '1';
report "DUT output is valid now" severity note;
end if;
-- DUT Input ready signals should be low now due to backpressure
assert tb_s_axis_a_tready = '0' and tb_s_axis_b_tready = '0'
report "Test Case 2 FAILED: Input READY signals not deasserted during backpressure!" severity failure;
report "Input READY signals correctly deasserted during backpressure" severity note;
-- Wait a few cycles to simulate backpressure
wait for CLK_PERIOD * 3;
-- Now make the sink ready
report "Asserting M_AXIS_TREADY to accept output" severity note;
tb_m_axis_tready <= '1';
-- Wait for the handshake to complete (M_AXIS_TVALID='1' and M_AXIS_TREADY='1')
wait until rising_edge(tb_aclk);
-- Check the result
report "Checking result: Expected=" & integer'image(to_integer(expected_sum_v)) &
", Got=" & integer'image(to_integer(unsigned(tb_m_axis_tdata))) severity note;
assert tb_m_axis_tdata = std_logic_vector(expected_sum_v)
report "Test Case 2 FAILED: Incorrect sum after backpressure!" severity failure;
-- Sink received data, deassert ready
wait until rising_edge(tb_aclk);
tb_m_axis_tready <= '0';
-- Check if DUT input ready signals are high again
assert tb_s_axis_a_tready = '1' and tb_s_axis_b_tready = '1'
report "Test Case 2 FAILED: Input READY signals not re-asserted after backpressure cleared!" severity failure;
report "Input READY signals correctly re-asserted" severity note;
report "Test Case 2 PASSED" severity note;
wait for CLK_PERIOD * 2;
-- ** Add more test cases as needed (e.g., max values, zero values, input backpressure) **
-- ...
-- End of simulation
report "Simulation Finished Successfully" severity note;
stop_sim <= true; -- Signal clock process to stop
wait; -- Wait indefinitely
end process stimulus_process;
end behavior;
Key Aspects of the Testbench:
- DUT Instantiation: The
axi_stream_adder is instantiated and connected to testbench signals.
- Clock and Reset: Standard processes generate the clock and an initial active-low reset pulse.
- Stimulus Process:
- Waits for reset to complete.
- Sends data by setting
TDATA and asserting TVALID.
- Waits for the DUT's
TREADY signal before considering the data sent (essential for the handshake).
- Deasserts
TVALID after the handshake (TVALID='1' and TREADY='1' on a clock edge).
- Simulates the receiver (sink) by controlling
M_AXIS_TREADY.
- Waits for the DUT's
M_AXIS_TVALID signal.
- Checks the received
M_AXIS_TDATA against the expected sum using assert.
- Handshake Verification:
- Basic: Test Case 1 shows a simple transaction where the receiver is always ready.
- Output Backpressure: Test Case 2 demonstrates stalling the output by keeping
tb_m_axis_tready low. It verifies that the DUT asserts M_AXIS_TVALID but waits, and importantly, deasserts its input S_AXIS_A/B_TREADY signals until the output is accepted. It then verifies that the input TREADY signals go high again once the output transaction completes.
- Clarity: Uses
report statements to show the simulation progress and assert statements with severity failure to stop the simulation on errors.
- Numeric Conversion: Uses
IEEE.NUMERIC_STD for easy conversion between integers and std_logic_vector for setting data and checking results.
How to Use:
- Save the adder code as
axi_stream_adder.vhd.
- Save the testbench code as
tb_axi_stream_adder.vhd.
- Compile both files using a VHDL simulator (like ModelSim, GHDL, Vivado Simulator, etc.). Make sure to compile the adder file first or ensure the library is correctly set up.
- Run the simulation on the testbench entity (
tb_axi_stream_adder).
- Observe the output messages and check for any assertion failures.