library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; use IEEE.NUMERIC_STD.ALL; -------------------------------------------------------------------------------- entity PS2_Controller is Port ( ps2_clk : in std_logic; -- linha de relogio PS2 ps2_data : in std_logic; -- linhda de dados PS2 rst : in std_logic; clk : in std_logic; LED : out std_logic; -- LED sinaliza recepcao scan_code : out std_logic_vector(7 downto 0); -- tecla pressionada busy : out std_logic; -- flag de sincronizacao LED2 : out std_logic; LED3 : out std_logic ); end PS2_Controller; -------------------------------------------------------------------------------- architecture Behavioral of PS2_Controller is signal byte : std_logic_vector(7 downto 0); -- byte PS2 recebido signal count33 : integer range 0 to 32; -- 33 bits recebidos no total signal busyIn,pulse : std_logic; signal improve_signal : std_logic_vector(7 downto 0); begin process(clk, rst) begin if rst = '0' then improve_signal <= (others => '0'); elsif rising_edge(clk) then improve_signal <= improve_signal(6 downto 0) & ps2_clk; if improve_signal = "10000000" then pulse <= '1'; else pulse <= '0'; end if; end if; end process; process (pulse,rst) begin if rst = '0' then count33 <= 0; busyIn <= '0'; scan_code <= (others => '0'); elsif rising_edge(pulse) then -- inicio de uma recepcao if (ps2_data = '0') then if (busyIn = '0') then busyIn <= '1'; count33 <= count33+1; -- esperar start bit else null; end if; else null; end if; case count33 is when 1 to 8 => count33 <= count33+1; byte(count33-1) <= ps2_data; when 9 => count33 <= count33+1; scan_code <= byte; LED <= '0'; when 10 => if (ps2_data = '1') then count33 <= count33+1; -- STOP BIT else null; end if; when 11 => if (ps2_data = '0') then count33 <= count33+1; -- START BIT else null; end if; when 12 to 20 => count33 <= count33+1; when 21 => if (ps2_data = '1') then count33 <= count33+1; -- STOP BIT else null; end if; when 22 => if (ps2_data = '0') then count33 <= count33+1; -- START BIT else null; end if; when 23 to 31 => count33 <= count33+1; when 32 => if (ps2_data = '1') then busyIn <= '0'; -- STOP BIT count33 <= 0; else null; end if; when others => null; end case; end if; end process; busy <= busyIn; LED <= '0'; LED2 <= '0'; LED3 <= '0'; end Behavioral;