902aa402b3830b9c9aa26758390b6eb93b42a0f5
Stefan Schuermans added file headers

Stefan Schuermans authored 12 years ago

1) -- MIPS I system
Stefan Schuermans replace email address in he...

Stefan Schuermans authored 12 years ago

2) -- Copyright 2011-2012 Stefan Schuermans <stefan@blinkenarea.org>
Stefan Schuermans added file headers

Stefan Schuermans authored 12 years ago

3) -- Copyleft GNU public license V2 or later
4) --          http://www.gnu.org/copyleft/gpl.html
5) 
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

47) 
48) BEGIN
49) 
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

86)     BEGIN
87)         -- de-gray-code rotary inputs
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

88)         v_debounced := io_switches_from_slv(r_debounced);
89)         IF v_debounced.rot_b = '1' THEN
90)             IF v_debounced.rot_a = '1' THEN
91)                 n_rot_val <= "00";
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

92)             ELSE
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

94)             END IF;
95)         ELSE
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

98)             ELSE
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

100)             END IF;
101)         END IF;
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

102)     END PROCESS p_de_gray;
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

103) 
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

105)         VARIABLE v_delta: signed(1 DOWNTO 0);
106)     BEGIN
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

107)         v_delta := signed(r_rot_val) - signed(r_rot_prev);
108)         n_rot_cnt <= std_logic_vector(signed(r_rot_cnt) + v_delta);
109)     END PROCESS p_rot_cnt;
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

110) 
111)     p_sync: PROCESS(rst, clk)
112)     BEGIN
113)         IF rst = '1' THEN
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

114)             r_scale     <= 0;
115)             r_states    <= c_states_def;
116)             r_debounced <= c_state_def;
117)             r_rot_val   <= (OTHERS => '0');
118)             r_rot_prev  <= (OTHERS => '0');
119)             r_rot_cnt   <= (OTHERS => '0');
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

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

Stefan Schuermans authored 12 years ago

121)             r_scale     <= n_scale;
122)             r_states    <= n_states;
123)             r_debounced <= n_debounced;
124)             r_rot_val   <= n_rot_val;
125)             r_rot_prev  <= r_rot_val;
126)             r_rot_cnt   <= n_rot_cnt;
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

127)         END IF;
128)     END PROCESS p_sync;
129) 
130)     p_read: PROCESS(rst, clk)
131)     BEGIN
132)         IF rst = '1' THEN
133)             o_rd_data <= (OTHERS => '0');
134)         ELSIF rising_edge(clk) THEN
135)             IF i_addr = "0" THEN
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

136)                 o_rd_data <= r_debounced;
Stefan Schuermans implemented switches

Stefan Schuermans authored 12 years ago

137)             ELSE
Stefan Schuermans debounce switches

Stefan Schuermans authored 12 years ago

138)                 o_rd_data <= r_rot_cnt;