CLR是复位控制输入端;ENA是使能控制输入端;LOAD是预置控制输入端;D[3..0]是4位并行数据输入端;DIR是加减控制输入端,当DIR=0时,计数器作加法操作,DIR=1时,计数器作减法操作;COUT是进/借位输出端。
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity Rev_Counter is
Port ( CLK : in STD_LOGIC;
CLR : in STD_LOGIC;
ENA : in STD_LOGIC;
LOAD : in STD_LOGIC;
D : in STD_LOGIC_VECTOR (3 downto 0);
DIR : in STD_LOGIC;
Q : out STD_LOGIC_VECTOR (3 downto 0);
COUT : out STD_LOGIC);
end Rev_Counter;
architecture Behavioral of Rev_Counter is
signal R : STD_LOGIC_VECTOR (3 downto 0);
begin
process(CLK, CLR)
begin
if CLR = '1' then
R <= "0000";
elsif CLK'event and CLK = '1' then
if ENA = '1' then
if LOAD = '1' then
R <= D;
else
if DIR = '0' then
if R = "1111" then
COUT <= '1';
else
COUT <= '0';
end if;
R <= R + 1;
else
if R = "0000" then
COUT <= '1';
else
COUT <= '0';
end if;
R <= R - 1;
end if;
end if;
end if;
end if;
end process;
Q <= R;
end Behavioral;