a060eadf72350188236b339dbe0de1e58ac26859
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

1) LIBRARY IEEE;
2) USE IEEE.STD_LOGIC_1164.ALL;
3) USE IEEE.NUMERIC_STD.ALL;
4) USE work.io_switches_pins.all;
5) 
6) ENTITY e_io_switches IS
7)     PORT (
8)         rst:            IN  std_logic;
9)         clk:            IN  std_logic;
10)         i_addr:         IN  std_logic_vector( 0 DOWNTO 0);
11)         o_rd_data:      OUT std_logic_vector(31 DOWNTO 0);
12)         pin_i_switches: IN  t_io_switches_pins
13)     );
14) END ENTITY e_io_switches;
15) 
16) ARCHITECTURE a_io_switches OF e_io_switches IS
17) 
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

18)     CONSTANT c_scale:  natural := 50000;
19)     CONSTANT c_states: natural := 5;
20) 
21)     SUBTYPE t_state  IS std_logic_vector(31 DOWNTO 0);
22)     TYPE    t_states IS ARRAY(0 TO c_states - 1) OF t_state;
23) 
24)     CONSTANT c_state_def:  t_state  := io_switches_to_slv(
25)                                          c_io_switches_pins_default);
26)     CONSTANT c_states_def: t_states := (OTHERS => c_state_def);
27) 
28)     SIGNAL n_scale: natural RANGE 0 TO c_scale - 1;
29)     SIGNAL r_scale: natural RANGE 0 TO c_scale - 1 := 0;
30) 
31)     SIGNAL n_states: t_states;
32)     SIGNAL r_states: t_states := c_states_def;
33) 
34)     SIGNAL n_debounced: t_state;
35)     SIGNAL r_debounced: t_state := c_state_def;
36) 
37)     SIGNAL n_rot_val:  std_logic_vector( 1 DOWNTO 0);
38)     SIGNAL r_rot_val:  std_logic_vector( 1 DOWNTO 0) := (OTHERS => '0');
39)     SIGNAL r_rot_prev: std_logic_vector( 1 DOWNTO 0) := (OTHERS => '0');
40)     SIGNAL n_rot_cnt:  std_logic_vector(31 DOWNTO 0);
41)     SIGNAL r_rot_cnt:  std_logic_vector(31 DOWNTO 0) := (OTHERS => '0');
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

42) 
43) BEGIN
44) 
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

45)     p_scale: PROCESS(r_scale)
46)     BEGIN
47)         IF r_scale >= c_scale - 1 THEN
48)             n_scale <= 0;
49)         ELSE
50)             n_scale <= r_scale + 1;
51)         END IF;
52)     END PROCESS p_scale;
53) 
54)     p_sample: PROCESS(pin_i_switches, r_scale, r_states)
55)     BEGIN
56)         IF r_scale = 0 THEN
57)             n_states(0) <= io_switches_to_slv(pin_i_switches);
58)             FOR i IN 1 TO c_states - 1 LOOP
59)                 n_states(i) <= r_states(i - 1);
60)             END LOOP;
61)         ELSE
62)             n_states <= r_states;
63)         END IF;
64)     END PROCESS p_sample;
65) 
66)     p_debounce: PROCESS(r_states, r_debounced)
67)         VARIABLE v_or:  t_state;
68)         VARIABLE v_and: t_state;
69)     BEGIN
70)         v_or  := r_states(0);
71)         v_and := r_states(0);
72)         FOR i IN 1 TO c_states - 1 LOOP
73)             v_or  := v_or OR r_states(i);
74)             v_and := v_and AND r_states(i);
75)         END LOOP;
76)         n_debounced <= (r_debounced AND v_or) OR v_and;
77)     END PROCESS p_debounce;
78) 
79)     p_de_gray: PROCESS(r_debounced)
80)         VARIABLE v_debounced: t_io_switches_pins;
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

81)     BEGIN
82)         -- de-gray-code rotary inputs
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

83)         v_debounced := io_switches_from_slv(r_debounced);
84)         IF v_debounced.rot_b = '1' THEN
85)             IF v_debounced.rot_a = '1' THEN
86)                 n_rot_val <= "00";
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

87)             ELSE
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

88)                 n_rot_val <= "11";
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

89)             END IF;
90)         ELSE
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

91)             IF v_debounced.rot_a = '1' THEN
92)                 n_rot_val <= "01";
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

93)             ELSE
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

94)                 n_rot_val <= "10";
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

95)             END IF;
96)         END IF;
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

97)     END PROCESS p_de_gray;
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

98) 
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

99)     p_rot_cnt: PROCESS(r_rot_val, r_rot_prev, r_rot_cnt)
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

100)         VARIABLE v_delta: signed(1 DOWNTO 0);
101)     BEGIN
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

102)         v_delta := signed(r_rot_val) - signed(r_rot_prev);
103)         n_rot_cnt <= std_logic_vector(signed(r_rot_cnt) + v_delta);
104)     END PROCESS p_rot_cnt;
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

105) 
106)     p_sync: PROCESS(rst, clk)
107)     BEGIN
108)         IF rst = '1' THEN
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

109)             r_scale     <= 0;
110)             r_states    <= c_states_def;
111)             r_debounced <= c_state_def;
112)             r_rot_val   <= (OTHERS => '0');
113)             r_rot_prev  <= (OTHERS => '0');
114)             r_rot_cnt   <= (OTHERS => '0');
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

115)         ELSIF rising_edge(clk) THEN
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

116)             r_scale     <= n_scale;
117)             r_states    <= n_states;
118)             r_debounced <= n_debounced;
119)             r_rot_val   <= n_rot_val;
120)             r_rot_prev  <= r_rot_val;
121)             r_rot_cnt   <= n_rot_cnt;
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

122)         END IF;
123)     END PROCESS p_sync;
124) 
125)     p_read: PROCESS(rst, clk)
126)     BEGIN
127)         IF rst = '1' THEN
128)             o_rd_data <= (OTHERS => '0');
129)         ELSIF rising_edge(clk) THEN
130)             IF i_addr = "0" THEN
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

131)                 o_rd_data <= r_debounced;
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

132)             ELSE
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

133)                 o_rd_data <= r_rot_cnt;