]> rtime.felk.cvut.cz Git - fpga/virtex2/uart.git/blob - counter.vhd
+ README
[fpga/virtex2/uart.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 count width and maximum value can be set by generic
8 -- attributes.
9 --------------------------------------------------------------------------------
10
11 entity counter is
12   generic (
13     WIDTH : integer := 16;
14     MAX   : integer := 2**16 - 1);
15   port (
16     clk   : in  std_logic;
17     reset : in  std_ulogic;
18     count : out std_logic_vector (WIDTH-1 downto 0));
19 end counter;
20
21 --------------------------------------------------------------------------------
22
23 architecture behavioral of counter is
24
25   signal cnt : std_logic_vector (WIDTH-1 downto 0);
26
27 --------------------------------------------------------------------------------
28
29 begin
30
31   count <= cnt;
32   
33   
34   process (clk, reset) is
35   begin
36     if reset = '1' then
37       cnt <= (others => '0');
38
39     elsif clk'event and clk = '1' then
40       if cnt = CONV_STD_LOGIC_VECTOR(MAX, WIDTH) then
41         cnt <= (others => '0');
42       else
43         cnt <= cnt + 1;
44       end if;
45     end if;
46   end process;
47
48 end behavioral;
49