library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; -------------------------------------------------------------------------------- -- General counter which counter width and maximum value can be set by generic -- attributes. When 'count' is equal to MAX value, 'event_ow' is high. -------------------------------------------------------------------------------- entity counter is generic ( WIDTH : integer; MAX : integer); port ( clk : in std_logic; clk_en : in std_logic; reset : in std_logic; count : out std_logic_vector (WIDTH-1 downto 0); event_ow : out std_logic); end counter; -------------------------------------------------------------------------------- architecture behavioral of counter is signal eq_max : std_logic; -- cnt is equal to MAX signal cnt : std_logic_vector (WIDTH-1 downto 0) := (others => '0'); -------------------------------------------------------------------------------- begin assert MAX < 2**WIDTH report "MAX is bigger then counter max. value." severity error; count <= cnt; event_ow <= eq_max and clk_en; COUTER : process (clk, reset) is begin if rising_edge(clk) then if reset = '1' then cnt <= (others => '0'); else if clk_en = '1' then if eq_max = '1' then cnt <= (others => '0'); else cnt <= cnt + 1; end if; end if; end if; end if; end process; CNT_EQ_MAX : process (cnt) is begin if cnt = CONV_STD_LOGIC_VECTOR(MAX, WIDTH) then eq_max <= '1'; else eq_max <= '0'; end if; end process; end behavioral;