--=====================================================
-- Проект 05. Двоичный счётчик с разрешением
-- Счётчик 0..255 со сбросом, разрешением счёта
-- и сигналом переполнения.
--=====================================================
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;                               -- арифметика над векторами

entity counter8 is
    port (
        clk     : in  std_logic;
        rst     : in  std_logic;
        en      : in  std_logic;                        -- разрешение счёта
        count   : out std_logic_vector(7 downto 0);
        carry   : out std_logic                         -- переполнение
    );
end entity counter8;

architecture rtl of counter8 is
    signal cnt : unsigned(7 downto 0) := (others => '0');
begin
    process (clk, rst)
    begin
        if rst = '1' then
            cnt <= (others => '0');
        elsif rising_edge(clk) then
            if en = '1' then
                cnt <= cnt + 1;                         -- переполнение произойдёт само
            end if;
        end if;
    end process;

    count <= std_logic_vector(cnt);                     -- приведение типа для выхода
    carry <= '1' when cnt = 255 and en = '1' else '0';
end architecture rtl;
