OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [processor/] [VHDL/] [ext_modules/] [ext_key_matrix/] [debounce/] [debounce_fsm_beh.vhd] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 jlechner
-----------------------------------------------------------------------
2
-- This file is part of SCARTS.
3
-- 
4
-- SCARTS is free software: you can redistribute it and/or modify
5
-- it under the terms of the GNU General Public License as published by
6
-- the Free Software Foundation, either version 3 of the License, or
7
-- (at your option) any later version.
8
-- 
9
-- SCARTS is distributed in the hope that it will be useful,
10
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
-- GNU General Public License for more details.
13
-- 
14
-- You should have received a copy of the GNU General Public License
15
-- along with SCARTS.  If not, see <http://www.gnu.org/licenses/>.
16
-----------------------------------------------------------------------
17
 
18
 
19
library ieee;
20
use ieee.std_logic_1164.all;
21
use ieee.numeric_std.all;
22
 
23
architecture beh of debounce_fsm is
24
  constant CLK_PERIOD : time := 1E9 / CLK_FREQ * 1 ns;
25
  constant CNT_MAX : integer := TIMEOUT / CLK_PERIOD;
26
  type DEBOUNCE_FSM_STATE_TYPE is
27
    (REINIT0, REINIT1, IDLE0, TIMEOUT0, IDLE1, TIMEOUT1);
28
  signal debounce_fsm_state, debounce_fsm_state_next : DEBOUNCE_FSM_STATE_TYPE;
29
  signal cnt, cnt_next : integer range 0 to CNT_MAX;
30
begin
31
  next_state : process(debounce_fsm_state, i, reinit, reinit_value, cnt)
32
  begin
33
    debounce_fsm_state_next <= debounce_fsm_state;
34
    case debounce_fsm_state is
35
      when REINIT0 =>
36
        debounce_fsm_state_next <= TIMEOUT0;
37
      when REINIT1 =>
38
        debounce_fsm_state_next <= TIMEOUT1;
39
      when IDLE0 =>
40
        if reinit = '1' and reinit_value = '0' then
41
          debounce_fsm_state_next <= REINIT0;
42
        elsif reinit = '1' and reinit_value = '1' then
43
          debounce_fsm_state_next <= REINIT1;
44
        elsif i = '1' then
45
          debounce_fsm_state_next <= TIMEOUT0;
46
        end if;
47
      when TIMEOUT0 =>
48
        if reinit = '1' and reinit_value = '0' then
49
          debounce_fsm_state_next <= REINIT0;
50
        elsif reinit = '1' and reinit_value = '1' then
51
          debounce_fsm_state_next <= REINIT1;
52
        elsif i = '0' then
53
          debounce_fsm_state_next <= IDLE0;
54
        elsif cnt = CNT_MAX - 1 then
55
          debounce_fsm_state_next <= IDLE1;
56
        end if;
57
      when IDLE1 =>
58
        if reinit = '1' and reinit_value = '0' then
59
          debounce_fsm_state_next <= REINIT0;
60
        elsif reinit = '1' and reinit_value = '1' then
61
          debounce_fsm_state_next <= REINIT1;
62
        elsif i = '0' then
63
          debounce_fsm_state_next <= TIMEOUT1;
64
        end if;
65
      when TIMEOUT1 =>
66
        if reinit = '1' and reinit_value = '0' then
67
          debounce_fsm_state_next <= REINIT0;
68
        elsif reinit = '1' and reinit_value = '1' then
69
          debounce_fsm_state_next <= REINIT1;
70
        elsif i = '1' then
71
          debounce_fsm_state_next <= IDLE1;
72
        elsif cnt = CNT_MAX - 1 then
73
          debounce_fsm_state_next <= IDLE0;
74
        end if;
75
    end case;
76
  end process next_state;
77
 
78
  output : process(debounce_fsm_state, cnt)
79
  begin
80
    o <= RESET_VALUE;
81
    cnt_next <= 0;
82
 
83
    case debounce_fsm_state is
84
      when REINIT0 =>
85
        o <= '0';
86
      when REINIT1 =>
87
        o <= '1';
88
      when IDLE0 =>
89
        o <= '0';
90
      when TIMEOUT0 =>
91
        o <= '0';
92
        cnt_next <= cnt + 1;
93
      when IDLE1 =>
94
        o <= '1';
95
      when TIMEOUT1 =>
96
        o <= '1';
97
        cnt_next <= cnt + 1;
98
    end case;
99
  end process output;
100
 
101
  assert RESET_VALUE = '0' or RESET_VALUE = '1' report
102
    "RESET_VALUE may only be 0 or 1!" severity failure;
103
 
104
  sync : process(sys_clk, sys_res_n)
105
  begin
106
    if sys_res_n = '0' then
107
      if RESET_VALUE = '0' then
108
        debounce_fsm_state <= IDLE0;
109
      else
110
        debounce_fsm_state <= IDLE1;
111
      end if;
112
      cnt <= 0;
113
    elsif rising_edge(sys_clk) then
114
      debounce_fsm_state <= debounce_fsm_state_next;
115
      cnt <= cnt_next;
116
    end if;
117
  end process sync;
118
end architecture beh;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.