]> rtime.felk.cvut.cz Git - fpga/pwm.git/blob - counter.vhd
Wave_table initialization data format modified.
[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     clk_en   : in  std_logic;
18     reset    : in  std_logic;
19     count    : out std_logic_vector (WIDTH-1 downto 0);
20     event_ow : out std_logic);
21 end counter;
22
23 --------------------------------------------------------------------------------
24
25 architecture behavioral of counter is
26
27   signal eq_max : std_logic;            -- cnt is equal to MAX
28   signal cnt    : std_logic_vector (WIDTH-1 downto 0) := (others => '0');
29
30 --------------------------------------------------------------------------------
31
32 begin
33
34   assert MAX < 2**WIDTH
35     report "MAX is bigger then counter max. value."
36     severity error;
37
38   
39   count    <= cnt;
40   event_ow <= eq_max and clk_en;
41
42   
43   COUTER : process (clk, reset) is
44   begin
45     if rising_edge(clk) then
46       if reset = '1' then
47         cnt <= (others => '0');
48         
49       else
50         if clk_en = '1' then
51           if eq_max = '1' then
52             cnt <= (others => '0');
53           else
54             cnt <= cnt + 1;
55           end if;
56         end if;
57       end if;
58     end if;
59   end process;
60
61   
62   CNT_EQ_MAX : process (cnt) is
63   begin
64     if cnt = CONV_STD_LOGIC_VECTOR(MAX, WIDTH) then
65       eq_max <= '1';
66     else
67       eq_max <= '0';
68     end if;
69   end process;
70   
71 end behavioral;
72