blob: bdeae47e164a5da1c374e9b8aee6380fc9dc4233 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
-- Author: Patrick Lehmann
--
-- A generic counter module used in the StopWatch example.
--
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use work.Utilities.all;
-- Generic modulo N counter
--
-- This component implements a generic modulo N counter with synchronous reset
-- and enable. It generates a wrap-around strobe signal when it roles from
-- MODULO-1 back to zero.
--
-- .. hint::
--
-- A modulo N counter counts binary from zero to N-1.
--
-- This component uses VHDL-2008 features like readback of ``out`` ports.
entity Counter is
generic (
MODULO : positive; -- Modulo value.
BITS : natural := log2(MODULO) -- Number of expected output bits.
);
port (
Clock : in std_logic; -- Component clock
Reset : in std_logic; -- Component reset (synchronous)
Enable : in std_logic; -- Component enable (synchronous)
Value : out unsigned(BITS - 1 downto 0); -- Current counter value
WrapAround : out std_logic -- Strobe output on change from MODULO-1 to zero
);
end entity;
-- Synthesizable and simulatable variant of a generic counter.
architecture rtl of Counter is
signal CounterValue : unsigned(log2(MODULO) - 1 downto 0) := (others => '0');
begin
process (Clock)
begin
if rising_edge(Clock) then
if ((Reset or WrapAround) = '1') then
CounterValue <= (others => '0');
elsif (Enable = '1') then
CounterValue <= CounterValue + 1;
end if;
end if;
end process;
Value <= resize(CounterValue, BITS);
WrapAround <= Enable when (CounterValue = MODULO - 1) else '0';
end architecture;
|