]> rtime.felk.cvut.cz Git - fpga/pwm.git/blob - counter.vhd
Corrected mcc_master.
[fpga/pwm.git] / counter.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
5
6 --------------------------------------------------------------------------------
7 -- General counter which counter width and maximum value can be set by generic
8 -- attributes. When 'count' is equal to MAX value, 'event_ow' is high.
9 --------------------------------------------------------------------------------
10
11 entity counter is
12   generic (
13     WIDTH : integer;
14     MAX   : integer);
15   port (
16     clk      : in  std_logic;
17     reset    : in  std_logic;
18     count    : out std_logic_vector (WIDTH-1 downto 0);
19     event_ow : out std_logic);
20 end counter;
21
22 --------------------------------------------------------------------------------
23
24 architecture behavioral of counter is
25
26   signal eq_max : std_logic;            -- cnt is equal to MAX
27   signal cnt    : std_logic_vector (WIDTH-1 downto 0);
28
29 --------------------------------------------------------------------------------
30
31 begin
32
33   assert MAX < 2**WIDTH
34     report "MAX is bigger then counter max. value."
35     severity error;
36
37   
38   count    <= cnt;
39   event_ow <= eq_max;
40
41   
42   COUTER : process (clk, reset) is
43   begin
44     if reset = '1' then
45       cnt <= (others => '0');
46
47     elsif rising_edge(clk) then
48       if eq_max = '1' then
49         cnt <= (others => '0');
50       else
51         cnt <= cnt + 1;
52       end if;
53     end if;
54   end process;
55
56   
57   CNT_EQ_MAX : process (cnt) is
58   begin
59     if cnt = CONV_STD_LOGIC_VECTOR(MAX, WIDTH) then
60       eq_max <= '1';
61     else
62       eq_max <= '0';
63     end if;
64   end process;
65   
66 end behavioral;
67