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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity debouncer is
generic(
DEBOUNCE_CYCLES: integer
);
port(
i_clk: in std_logic;
i_data: in std_logic;
o_data: out std_logic := '0'
);
end entity debouncer;
architecture arch_debouncer of debouncer is
signal cnt: integer := 0;
signal current_value: std_logic := '0';
begin
process(i_clk)
begin
if rising_edge(i_clk) then
if i_data = current_value then
cnt <= 0;
else
if cnt = DEBOUNCE_CYCLES then
cnt <= 0;
current_value <= i_data;
else
cnt <= cnt + 1;
end if;
end if;
o_data <= current_value;
end if;
end process;
end architecture arch_debouncer;
library ieee;
use ieee.numeric_std.all;
use ieee.std_logic_1164.all;
entity ps2 is
port(
i_clk: in std_logic := 'X';
i_resend: in std_logic := '0';
io_ps2_data: inout std_logic := 'Z';
io_ps2_clk: inout std_logic := 'Z'
);
end entity ps2;
architecture arch_ps2 of ps2 is
component debouncer is
generic(
DEBOUNCE_CYCLES: integer
);
port(
i_clk: in std_logic;
i_data: in std_logic;
o_data: out std_logic := '0'
);
end component debouncer;
component ps2_comm is
port(
i_clk: in std_logic := 'X';
i_ps2_data: in std_logic := '0';
i_ps2_clk: in std_logic := '0';
o_ps2_data: out std_logic := '0';
o_ps2_data_we: out std_logic := '0';
o_ps2_clk: out std_logic := '0';
o_ps2_clk_we: out std_logic := '0';
o_msg: out std_logic_vector(7 downto 0);
o_data_available: out std_logic := '0';
i_msg: in std_logic_vector(7 downto 0);
i_we: in std_logic := '0'
);
end component ps2_comm;
signal data_in: std_logic := '0';
signal clk_in: std_logic := '0';
signal data_out: std_logic := '0';
signal clk_out: std_logic := '0';
signal d_data_in: std_logic := '0';
signal d_clk_in: std_logic := '0';
signal data_we: std_logic := '0';
signal clk_we: std_logic := '0';
signal msg_in: std_logic_vector(7 downto 0);
signal msg_out: std_logic_vector(7 downto 0);
signal da: std_logic := '0';
signal we: std_logic := '0';
signal d_resend: std_logic := '0';
signal old_resend: std_logic := '0';
begin
deb_data: debouncer
generic map(
DEBOUNCE_CYCLES => 50 -- 1µs
)
port map(
i_clk => i_clk,
i_data => data_in,
o_data => d_data_in
);
deb_clk: debouncer
generic map(
DEBOUNCE_CYCLES => 50 -- 1µs
)
port map(
i_clk => i_clk,
i_data => clk_in,
o_data => d_clk_in
);
ps2: ps2_comm
port map(
i_clk => i_clk,
i_ps2_data => d_data_in,
i_ps2_clk => d_clk_in,
o_ps2_data => data_out,
o_ps2_data_we => data_we,
o_ps2_clk => clk_out,
o_ps2_clk_we => clk_we,
o_msg => msg_in,
o_data_available => da,
i_msg => msg_out,
i_we => we
);
deb_resend: debouncer
generic map(
DEBOUNCE_CYCLES => 5000000 -- 100ms
)
port map(
i_clk => i_clk,
i_data => i_resend,
o_data => d_resend
);
process(i_clk)
begin
if rising_edge(i_clk) then
data_in <= io_ps2_data;
clk_in <= io_ps2_clk;
if data_we = '1' then
io_ps2_data <= data_out;
else
io_ps2_data <= 'Z';
end if;
if clk_we = '1' then
io_ps2_clk <= clk_out;
else
io_ps2_clk <= 'Z';
end if;
old_resend <= d_resend;
if old_resend = '0' and d_resend = '1' then
we <= '1';
msg_out <= "11111111";
else
we <= '0';
end if;
end if;
end process;
end architecture arch_ps2; |
Partager