Below is a testbench for the 8-bit AXI-Stream adder. The testbench simulates the behavior of the inputs and outputs, testing various scenarios to ensure the adder behaves as expected.
📂 VHDL Testbench Code
vhdl
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity axi_stream_adder_tb is
end axi_stream_adder_tb;
architecture Behavioral of axi_stream_adder_tb is
-- Clock period definition
constant CLK_PERIOD : time := 10 ns;
-- Component declaration for the Unit Under Test (UUT)
component axi_stream_adder
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;
-- Inputs
signal ACLK : std_logic := '0';
signal ARESETN : std_logic := '0';
signal S_AXIS_A_TVALID : std_logic := '0';
signal S_AXIS_A_TDATA : std_logic_vector(7 downto 0) := (others => '0');
signal S_AXIS_B_TVALID : std_logic := '0';
signal S_AXIS_B_TDATA : std_logic_vector(7 downto 0) := (others => '0');
signal M_AXIS_TREADY : std_logic := '0';
-- Outputs
signal S_AXIS_A_TREADY : std_logic;
signal S_AXIS_B_TREADY : std_logic;
signal M_AXIS_TVALID : std_logic;
signal M_AXIS_TDATA : std_logic_vector(7 downto 0);
begin
-- Instantiate the Unit Under Test (UUT)
uut: axi_stream_adder
port map (
ACLK => ACLK,
ARESETN => ARESETN,
S_AXIS_A_TVALID => S_AXIS_A_TVALID,
S_AXIS_A_TREADY => S_AXIS_A_TREADY,
S_AXIS_A_TDATA => S_AXIS_A_TDATA,
S_AXIS_B_TVALID => S_AXIS_B_TVALID,
S_AXIS_B_TREADY => S_AXIS_B_TREADY,
S_AXIS_B_TDATA => S_AXIS_B_TDATA,
M_AXIS_TVALID => M_AXIS_TVALID,
M_AXIS_TREADY => M_AXIS_TREADY,
M_AXIS_TDATA => M_AXIS_TDATA
);
-- Clock generation process
clk_process: process
begin
ACLK <= not ACLK after CLK_PERIOD / 2;
wait for CLK_PERIOD / 2;
end process;
-- Stimulus process
stim_proc: process
begin
-- Initial reset
ARESETN <= '0';
wait for 2 * CLK_PERIOD;
-- Deassert reset
ARESETN <= '1';
wait for CLK_PERIOD;
-- Test Case 1: Add 5 + 3 = 8
S_AXIS_A_TVALID <= '1';
S_AXIS_A_TDATA <= std_logic_vector(to_unsigned(5, 8));
S_AXIS_B_TVALID <= '1';
S_AXIS_B_TDATA <= std_logic_vector(to_unsigned(3, 8));
M_AXIS_TREADY <= '1'; -- Output ready to accept data
wait until M_AXIS_TVALID = '1'; -- Wait for result
wait for CLK_PERIOD;
assert unsigned(M_AXIS_TDATA) = 8 report "Test Case 1 Failed" severity error;
-- Clear input signals
S_AXIS_A_TVALID <= '0';
S_AXIS_B_TVALID <= '0';
M_AXIS_TREADY <= '0';
wait for CLK_PERIOD;
-- Test Case 2: Add 255 + 1 = 256 (overflow scenario)
S_AXIS_A_TVALID <= '1';
S_AXIS_A_TDATA <= std_logic_vector(to_unsigned(255, 8));
S_AXIS_B_TVALID <= '1';
S_AXIS_B_TDATA <= std_logic_vector(to_unsigned(1, 8));
M_AXIS_TREADY <= '1'; -- Output ready to accept data
wait until M_AXIS_TVALID = '1'; -- Wait for result
wait for CLK_PERIOD;
assert unsigned(M_AXIS_TDATA) = 0 report "Test Case 2 Failed" severity error; -- Overflow wraps around
-- Clear input signals
S_AXIS_A_TVALID <= '0';
S_AXIS_B_TVALID <= '0';
M_AXIS_TREADY <= '0';
wait for CLK_PERIOD;
-- Test Case 3: Add 128 + 128 = 256 (overflow scenario again)
S_AXIS_A_TVALID <= '1';
S_AXIS_A_TDATA <= std_logic_vector(to_unsigned(128, 8));
S_AXIS_B_TVALID <= '1';
S_AXIS_B_TDATA <= std_logic_vector(to_unsigned(128, 8));
M_AXIS_TREADY <= '1'; -- Output ready to accept data
wait until M_AXIS_TVALID = '1'; -- Wait for result
wait for CLK_PERIOD;
assert unsigned(M_AXIS_TDATA) = 0 report "Test Case 3 Failed" severity error; -- Overflow wraps around
-- Clear input signals
S_AXIS_A_TVALID <= '0';
S_AXIS_B_TVALID <= '0';
M_AXIS_TREADY <= '0';
wait for CLK_PERIOD;
-- End simulation
report "All tests completed" severity note;
wait;
end process;
end Behavioral;
🔍 Explanation of the Testbench
Clock Generation:
The clock signal ACLK toggles every 5 ns (CLK_PERIOD / 2).
Reset Behavior:
The reset signal ARESETN is active-low. It starts at '0' (reset asserted) for two clock cycles before being deasserted ('1').
Stimulus Process:
After reset, the testbench applies three test cases to validate the functionality of the adder:
Test Case 1: Adds 5 + 3 and checks if the result is 8.
Test Case 2: Adds 255 + 1 and checks for overflow. The result should wrap around to 0 due to the 8-bit limit.
Test Case 3: Adds 128 + 128, which also overflows and results in 0.
Handshake Protocol:
The testbench asserts S_AXIS_A_TVALID and S_AXIS_B_TVALID to indicate valid input data.
It waits for M_AXIS_TVALID to be high, indicating that the output is ready.
The testbench asserts M_AXIS_TREADY to indicate that the consumer is ready to accept the output data.
Assertions:
The testbench uses assert statements to verify that the output matches the expected result.
If an assertion fails, it raises an error with a descriptive message.
Simulation Completion:
At the end of the test cases, the testbench reports that all tests are completed using a report statement.
🧪 Running the Testbench
To run this testbench:
Compile both the axi_stream_adder and axi_stream_adder_tb files in your VHDL simulator.
Run the simulation.
Observe the waveforms and console output to verify the correctness of the design.
The simulation will show the following key behaviors:
Proper reset initialization.
Correct addition results.
Handling of overflow conditions.
Let me know if you need further assistance or additional test cases!