1 |
2 |
amulder |
----------------------------------------------------------------------------------
|
2 |
|
|
-- Company:
|
3 |
|
|
-- Engineer: Aart Mulder
|
4 |
|
|
--
|
5 |
|
|
-- Create Date: 17:41:32 07/11/2012
|
6 |
|
|
-- Design Name:
|
7 |
|
|
-- Module Name: counter - Behavioral
|
8 |
|
|
-- Project Name: CCITT4
|
9 |
|
|
-- Note:
|
10 |
|
|
----------------------------------------------------------------------------------
|
11 |
|
|
|
12 |
|
|
library IEEE;
|
13 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
14 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
15 |
|
|
|
16 |
|
|
entity counter is
|
17 |
|
|
Generic (
|
18 |
|
|
COUNTER_WIDTH_G : integer := 8; -- Width of the counter
|
19 |
|
|
START_VALUE_G : integer := 1; -- Start value of the counter/The value loaded on an overflow
|
20 |
|
|
MAX_VALUE_G : integer := 255; -- Value where the counter overflows(Maximum)
|
21 |
|
|
ASYNCHRONOUS_RESET_G : boolean := true; -- Set to true to let the reset be processed on the clock
|
22 |
|
|
OVERFLOW_G : boolean := true -- Let the counter overflow
|
23 |
|
|
);
|
24 |
|
|
Port (
|
25 |
|
|
reset_i : in STD_LOGIC;
|
26 |
|
|
clk_i : in STD_LOGIC;
|
27 |
|
|
en_i : in STD_LOGIC;
|
28 |
|
|
cnt_o : out UNSIGNED (COUNTER_WIDTH_G-1 downto 0) := to_unsigned(START_VALUE_G, COUNTER_WIDTH_G);
|
29 |
|
|
overflow_o : out STD_LOGIC := '0' -- High on an overflow, i.e. when START_VALUE_G has been loaded
|
30 |
|
|
);
|
31 |
|
|
end counter;
|
32 |
|
|
|
33 |
|
|
architecture Behavioral of counter is
|
34 |
|
|
--The signal "cnt" is used because an output can't be read and configuring the output
|
35 |
|
|
--as buffer is not desired. Though the synthesiser will create a warning/info.
|
36 |
|
|
signal cnt : UNSIGNED (COUNTER_WIDTH_G-1 downto 0) := to_unsigned(START_VALUE_G, COUNTER_WIDTH_G);
|
37 |
|
|
begin
|
38 |
|
|
counter_process : process(reset_i, clk_i)
|
39 |
|
|
begin
|
40 |
|
|
if reset_i = '1' and ASYNCHRONOUS_RESET_G then
|
41 |
|
|
cnt <= to_unsigned(START_VALUE_G, COUNTER_WIDTH_G);
|
42 |
|
|
cnt_o <= to_unsigned(START_VALUE_G, COUNTER_WIDTH_G);
|
43 |
|
|
overflow_o <= '0';
|
44 |
|
|
elsif clk_i'event and clk_i = '1' then
|
45 |
|
|
overflow_o <= '0';
|
46 |
|
|
if reset_i = '1' then
|
47 |
|
|
cnt <= to_unsigned(START_VALUE_G, COUNTER_WIDTH_G);
|
48 |
|
|
cnt_o <= to_unsigned(START_VALUE_G, COUNTER_WIDTH_G);
|
49 |
|
|
overflow_o <= '0';
|
50 |
|
|
elsif en_i = '1' then
|
51 |
|
|
if cnt >= to_unsigned(MAX_VALUE_G, COUNTER_WIDTH_G) and OVERFLOW_G then
|
52 |
|
|
cnt <= to_unsigned(START_VALUE_G, COUNTER_WIDTH_G);
|
53 |
|
|
cnt_o <= to_unsigned(START_VALUE_G, COUNTER_WIDTH_G);
|
54 |
|
|
overflow_o <= '1';
|
55 |
|
|
elsif cnt >= to_unsigned(MAX_VALUE_G, COUNTER_WIDTH_G) and not OVERFLOW_G then
|
56 |
|
|
cnt <= cnt;
|
57 |
|
|
cnt_o <= cnt;
|
58 |
|
|
overflow_o <= '0';
|
59 |
|
|
else
|
60 |
|
|
cnt <= cnt + 1;
|
61 |
|
|
cnt_o <= cnt + 1;
|
62 |
|
|
end if;
|
63 |
|
|
end if;
|
64 |
|
|
end if;
|
65 |
|
|
end process counter_process;
|
66 |
|
|
end Behavioral;
|
67 |
|
|
|