Search code examples
vhdlcountersimulationvivadotest-bench

VHDL counter simulated using a test bench giving 'Uninitialized' for the output, how is this resolved?


Below is a counter that is designed to represent an 8 bit binary number with 8 LEDs, it is being simulated using a test bench, however when running the simulation the output simply shows UU for the led.

Here is the main entity that I wish to test:

use IEEE.STD_LOGIC_1164.all;
use IEEE.STD_LOGIC_unsigned.all;

entity Lab_3_Source_File is
 generic(N_BITS : integer := 8);
 port(
 btnd : in STD_LOGIC ;
 clk : in STD_LOGIC;
 led : out STD_LOGIC_VECTOR(7 downto 0)
 );
end Lab_3_Source_File;

architecture counter of Lab_3_Source_File is
signal count: STD_LOGIC_VECTOR(7 downto 0);

begin

process(clk, btnd)
begin
 if btnd = '1' then
 count <= (others => '0');
 elsif rising_edge(clk) then
 count <= count + 1;
 end if;
end process;

 led <= count;
end counter;

Here is the test bench that I have tried to map to the main entity:

use IEEE.STD_LOGIC_1164.ALL;


entity Count_TestBench is

end Count_TestBench;

architecture Behavioral of Count_TestBench is                               
                   
    signal btnd, clk : STD_LOGIC;
    signal led : STD_LOGIC_VECTOR(7 downto 0);
begin


   UUT : entity work.Lab_3_Source_File port map (btnd => btnd,clk => clk,led => led);
 
    process
    begin
        btnd<='1';
        wait for 1 ns;
        btnd<='0';    
        led<= (others => '0');           
        for i in 1 to 100 loop
            clk<='1';
            wait for 10 ns;
            clk<='0';
            wait for 10 ns;
            led<=led;
        end loop;          
    end process;
end Behavioral;

Please could somebody help me understand how to enable the simulation to display the led output incrementing?

EDIT:

Set btnd to 1 with a 1ns wait in the test bench to initialise the led, following the answer from mkrieger1, the led output is still at U following this change.


Solution

  • count is not initialized inside Lab_3_Source_File until btnd is set to '1', which it isn't in the testbench.

    Since the led output is driven by count, it is also uninitialized. The uninitialized value of the led output of Lab_3_Source_File is then assigned to the led signal in the testbench.

    So, to fix this, you need to set btnd to '1' once for a non-zero duration in the testbench, before setting it to '0' again (otherwise led is held at "00000000" constantly).