Here is a VHDL model and testbench for a four-bit binary counter. Run the simulation and observe the behavior in the timing diagram.
1
www.edaplayground.com/x/2ww2-- Testbench for two-bit binary counter
library ieee;
use ieee.std_logic_1164.all;
entity counter2_tb is
end counter2_tb;
architecture testbench of counter2_tb is
-- Declare UUT
component counter2 is
port( clk: in std_logic;
X: out std_logic_vector(1 downto 0) );
end component;
-- Declare and initialize signals
constant CLK_PERIOD: time := 100ns; -- 1
signal clk: std_logic := '0';
signal X: std_logic_vector(1 downto 0) := "00";
begin
-- Instantiate UUT and wire to signals
uut: counter2 port map
clk => clk,
X => X );
-- Generate stimuli
clk_proc: process -- 2
begin
clk <= '1';
wait for CLK_PERIOD/2;
clk <= '0';
wait for CLK_PERIOD/2;
end process clk_proc;
--stim_proc: process -- 3
--begin
--end process stim_proc;
end testbench;
time, which can be expressed in seconds (s), milliseconds (ms), microseconds (us), and nanoseconds (ns). Here, assigning CLK_PERIOD the value 100ns establishes a 10MHz clock.clk signal HIGH, waits for half the clock period (50ns), then sets it LOW, and waits for half the clock period. The process resumes from the top when the wait time is up, and repeats without end, generating a clock with 100ns period.www.edaplayground.com/x/2ww2-- Testbench for binary counter with reset and enable
library ieee;
use ieee.std_logic_1164.all;
entity counter_reseten_tb is
end counter_reseten_tb;
architecture testbench of counter_reseten_tb is
-- Declare UUT
component counter_reseten is
port( clk: in std_logic;
reset: in std_logic;
CE: in std_logic;
y: out std_logic_vector(7 downto 0) );
end component;
-- Declare and initialize signals
constant CLK_PERIOD: time := 100ns;
signal clk: std_logic := '0';
signal reset, CE: std_logic := '0';
signal y: std_logic_vector(7 downto 0) := x"00";
begin
-- Instantiate UUT and wire to signals
uut: counter_reseten port map
clk => clk,
reset => reset,
CE => CE,
y => y );
-- Generate stimuli
-- These processes run concurrently.
clk_proc: process
begin
clk <= '1';
wait for CLK_PERIOD/2;
clk <= '0';
wait for CLK_PERIOD/2;
end process clk_proc;
stim_proc: process
begin
CE <= '0'; reset <= '1'; -- Hold in reset
wait for 2*CLK_PERIOD; -- for two clock cycles
reset <= '0'; -- Release reset, but still holding
wait for 2*CLK_PERIOD;
CE <= '1'; -- Enable counting
wait for 5*CLK_PERIOD;
reset <= '1'; -- Reset while counting
wait for 2*CLK_PERIOD;
reset <= '0'; -- Start counting again
wait for 4*CLK_PERIOD;
CE <= '0'; -- Hold while counting
wait for 4*CLK_PERIOD;
CE <= '1'; -- Re-enable and
wait; -- let it run
end process stim_proc;
end testbench;
www.edaplayground.com/x/4WH9