In: Electrical Engineering
Using Behavorial VHDL, design a 4-bit up/down counter.
Vhdl code:
library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity UPDOWN_COUNTER is Port ( clk: in std_logic; -- clock input reset: in std_logic; -- reset input up_down: in std_logic; -- up or down counter: out std_logic_vector(3 downto 0) -- output 4-bit counter ); end UPDOWN_COUNTER; architecture Behavioral of UPDOWN_COUNTER is signal counter_updown: std_logic_vector(3 downto 0); begin -- down counter process(clk,reset) begin if(rising_edge(clk)) then if(reset='1') then counter_updown <= x"0"; elsif(up_down='1') then counter_updown <= counter_updown - x"1"; -- count down else counter_updown <= counter_updown + x"1"; -- count up end if; end if; end process; counter <= counter_updown; end Behavioral;
Testbench:
library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity tb_counters is end tb_counters; architecture Behavioral of tb_counters is component UPDOWN_COUNTER Port ( clk: in std_logic; -- clock input reset: in std_logic; -- reset input up_down: in std_logic; counter: out std_logic_vector(3 downto 0) -- output 4-bit counter ); end component; signal reset,clk,up_down: std_logic; signal counter:std_logic_vector(3 downto 0); begin dut: UPDOWN_COUNTER port map (clk => clk, reset=>reset, up_down => up_down, counter => counter); -- Clock process definitions clock_process :process begin clk <= '0'; wait for 10 ns; clk <= '1'; wait for 10 ns; end process; -- Stimulus process stim_proc: process begin -- hold reset state for 100 ns. reset <= '1'; up_down <= '0'; wait for 20 ns; reset <= '0'; wait for 300 ns; up_down <= '1'; wait; end process; end Behavioral;