library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity Counter is
Port ( clk : in STD_LOGIC;
clear : in STD_LOGIC;
load : in STD_LOGIC;
q : out STD_LOGIC_VECTOR(3 downto 0));
end Counter;
architecture Behavioral of Counter is
signal count : STD_LOGIC_VECTOR(3 downto 0) := "0000";
begin
process(clk, clear, load)
begin
if clear = '1' then
count <= "0000";
elsif rising_edge(clk) then
if load = '1' then
count <= q;
else
count <= count + 1;
end if;
end if;
end process;
q <= count;
end Behavioral;
仿真验证
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity Counter_TB is
end Counter_TB;
architecture Behavioral of Counter_TB is
component Counter
Port ( clk : in STD_LOGIC;
clear : in STD_LOGIC;
load : in STD_LOGIC;
q : out STD_LOGIC_VECTOR(3 downto 0));
end component;
signal clk, clear, load : STD_LOGIC := '0';
signal q : STD_LOGIC_VECTOR(3 downto 0);
begin
uut: Counter port map (clk, clear, load, q);
process
begin
wait for 10 ns;
clk <= not clk;
end process;
process
begin
wait for 100 ns;
clear <= '1';
wait for 50 ns;
clear <= '0';
wait for 150 ns;
load <= '1';
wait for 100 ns;
load <= '0';
wait;
end process;
end Behavioral;