aboutsummaryrefslogtreecommitdiffstats
path: root/testsuite/pyunit/dom/examples/StopWatch/Debouncer.vhdl
diff options
context:
space:
mode:
authorPatrick Lehmann <Patrick.Lehmann@plc2.de>2022-12-19 22:11:44 +0100
committerPatrick Lehmann <Patrick.Lehmann@plc2.de>2022-12-23 23:44:14 +0100
commit633373d0054f551158cdf668c464646bb9e6af27 (patch)
treebc989fb2ce39865cb529ea1aa2fd957ec2d0310f /testsuite/pyunit/dom/examples/StopWatch/Debouncer.vhdl
parent5853a37df7c9468a01d62f7b2eeee7d9773e72ca (diff)
downloadghdl-633373d0054f551158cdf668c464646bb9e6af27.tar.gz
ghdl-633373d0054f551158cdf668c464646bb9e6af27.tar.bz2
ghdl-633373d0054f551158cdf668c464646bb9e6af27.zip
Added StopWatch example for DOM and documentation testing.
Diffstat (limited to 'testsuite/pyunit/dom/examples/StopWatch/Debouncer.vhdl')
-rw-r--r--testsuite/pyunit/dom/examples/StopWatch/Debouncer.vhdl58
1 files changed, 58 insertions, 0 deletions
diff --git a/testsuite/pyunit/dom/examples/StopWatch/Debouncer.vhdl b/testsuite/pyunit/dom/examples/StopWatch/Debouncer.vhdl
new file mode 100644
index 000000000..18207c7f1
--- /dev/null
+++ b/testsuite/pyunit/dom/examples/StopWatch/Debouncer.vhdl
@@ -0,0 +1,58 @@
+-- Author: Patrick Lehmann
+-- License: MIT
+--
+-- 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;
+
+
+entity Debouncer is
+ generic (
+ CLOCK_PERIOD : time := 10 ns;
+ DEBOUNCE_TIME : time := 3 ms;
+
+ BITS : positive
+ );
+ port (
+ Clock : in std_logic;
+
+ Input : in std_logic_vector(BITS - 1 downto 0);
+ Output : out std_logic_vector(BITS - 1 downto 0) := (others => '0')
+ );
+end entity;
+
+architecture rtl of Debouncer is
+ constant DEBOUNCE_COUNTER_MAX : positive := DEBOUNCE_TIME / (CLOCK_PERIOD* ite(IS_SIMULATION, 20, 1));
+ constant DEBOUNCE_COUNTER_BITS : positive := log2(DEBOUNCE_COUNTER_MAX);
+
+begin
+ assert false report "CLOCK_PERIOD: " & time'image(CLOCK_PERIOD);
+ assert false report "DEBOUNCE_TIME: " & time'image(DEBOUNCE_TIME);
+ --assert false report "DEBOUNCE_COUNTER_MAX: " & to_string(10 ns);
+ --assert false report "INTEGER'high: " & integer'image(integer'high);
+
+ genBits: for i in Input'range generate
+ signal DebounceCounter : signed(DEBOUNCE_COUNTER_BITS downto 0) := to_signed(DEBOUNCE_COUNTER_MAX - 3, DEBOUNCE_COUNTER_BITS + 1);
+ begin
+ process (Clock)
+ begin
+ if rising_edge(Clock) then
+ -- restart counter, whenever Input(i) was unstable within DEBOUNCE_TIME_MS
+ if (Input(i) /= Output(i)) then
+ DebounceCounter <= DebounceCounter - 1;
+ else
+ DebounceCounter <= to_signed(DEBOUNCE_COUNTER_MAX - 3, DebounceCounter'length);
+ end if;
+
+ -- latch input bit, if input was stable for DEBOUNCE_TIME_MS
+ if (DebounceCounter(DebounceCounter'high) = '1') then
+ Output(i) <= Input(i);
+ end if;
+ end if;
+ end process;
+ end generate;
+end architecture;