-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio_ps2_keyboard.vhd
77 lines (68 loc) · 2.25 KB
/
io_ps2_keyboard.vhd
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
-- -----------------------------------------------------------------------
--
-- FPGA 64
--
-- A fully functional commodore 64 implementation in a single FPGA
--
-- -----------------------------------------------------------------------
-- Copyright 2005-2008 by Peter Wendrich ([email protected])
-- http://www.syntiac.com/fpga64.html
-- -----------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.ALL;
entity io_ps2_keyboard is
port (
clk: in std_logic;
kbd_clk: in std_logic;
kbd_dat: in std_logic;
interrupt: out std_logic;
scanCode: out unsigned(7 downto 0)
);
end io_ps2_keyboard;
architecture Behavioral of io_ps2_keyboard is
signal clk_reg: std_logic;
signal clk_waitNextBit: std_logic;
signal clk_filter: integer range 0 to 15;
signal shift_reg: unsigned(10 downto 0) := (others => '0');
signal bitsCount: integer range 0 to 10 := 0;
signal timeout: integer range 0 to 5000 := 0; -- 2* 50 us at 50 Mhz
begin
process(clk)
begin
if rising_edge(clk) then
-- Interrupt is edge triggered. Only 1 clock high.
interrupt <= '0';
-- Timeout if keyboard does not send anymore.
if timeout /= 0 then
timeout <= timeout - 1;
else
bitsCount <= 0;
end if;
-- Filter glitches on the clock
if (clk_reg /= kbd_clk) then
clk_filter <= 15; -- Wait 15 ticks
clk_reg <= kbd_clk; -- Store clock edge to detect changes
clk_waitNextBit <= '0'; -- Next bit comming up...
elsif (clk_filter /= 0) then
-- Wait for clock to stabilise
-- Clock must be stable before we sample the data line.
clk_filter <= clk_filter - 1;
elsif (clk_reg = '1') and (clk_waitNextBit = '0') then
-- We have a stable clock, so assume stable data too.
clk_waitNextBit <= '1';
-- Move data into shift register
shift_reg <= kbd_dat & shift_reg(10 downto 1);
timeout <= 5000;
if bitsCount < 10 then
bitsCount <= bitsCount + 1;
else
-- 10 bits received. Output new scancode
bitsCount <= 0;
interrupt <= '1';
scanCode <= shift_reg(9 downto 2);
end if;
end if;
end if;
end process;
end Behavioral;