?? mouse.vhd
字號:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity mouse is
Port (
clk : in std_logic;
reset : in std_logic;
ps2_clk : inout std_logic;
ps2_data : inout std_logic;
left_button : out std_logic;
right_button : out std_logic;
mousex: buffer std_logic_vector(9 downto 0);
mousey: buffer std_logic_vector(9 downto 0);
data_ready : out std_logic;-- rx_read_o
error_no_ack : out std_logic
);
end mouse;
architecture Behavioral of mouse is
constant TOTAL_BITS : integer :=33; -- 數(shù)據(jù)包位數(shù)
constant WATCHDOG : integer :=320; -- 400usec所需sys_clk脈沖數(shù)
--constant DEBOUNCE_TIMER : integer := 2;
--type m1statetype is ( m1_clk_h, m1_falling_edge, m1_falling_wait,
-- m1_clk_l, m1_rising_edge, m1_rising_wait);
type m2statetype is (m2_reset, m2_wait, m2_gather, m2_verify, m2_use, m2_hold_clk_l,
m2_data_low_1, m2_data_high_1, m2_data_low_2, m2_data_high_2, m2_data_low_3,
m2_data_high_3, m2_error_no_ack, m2_await_response);
--signal m1_state,m1_next_state : m1statetype;
signal m2_state,m2_next_state : m2statetype;
--signal m3_state,m3_next_state : std_logic;
signal watchdog_timer_done : std_logic;--命令傳輸超時標志
signal q : std_logic_vector(TOTAL_BITS-1 downto 0);--位序列
signal bitcount : std_logic_vector(5 downto 0);--位計數(shù)器
signal watchdog_timer_count : std_logic_vector(8 downto 0); --等待時間
--signal debounce_timer_count : std_logic_vector(1 downto 0);
signal ps2_clk_hi_z : std_logic;
signal ps2_data_hi_z : std_logic;
signal fallsig,risesig : std_logic_vector(2 downto 0);
signal clean_clk : std_logic; -- 從m1跟隨ps2_clk反向輸出
signal rise,n_rise : std_logic; -- m1狀態(tài)機輸出數(shù)據(jù)
signal fall,n_fall : std_logic; -- m1狀態(tài)機輸出數(shù)據(jù)
signal output_strobe : std_logic; -- 鎖存數(shù)據(jù)到輸出寄存器
signal packet_good : std_logic; -- 檢查數(shù)據(jù)是否有效
--signal x_increment : std_logic_vector(8 downto 0);
--signal y_increment : std_logic_vector(7 downto 0);
signal mouseyy : std_logic_vector(9 downto 0);
begin
ps2_clk <= '0' when ps2_clk_hi_z='0' else 'Z';
ps2_data <= '0' when ps2_data_hi_z='0' else 'Z';
------------------------------------
--檢測ps2clk上升沿和下降沿
------------------------------------
detect_ps2clkfall : process(clk,reset,ps2_clk)
begin
if reset='0' then
fallsig <= "000";
elsif clk'event and clk='1' then
fallsig(0) <= ps2_clk;
fallsig(1) <= fallsig(0);
fallsig(2) <= fallsig(1);
end if;
end process;
fall <= '1' when fallsig="110" else '0';
detect_ps2clkrise : process(clk,reset,ps2_clk)
begin
if reset='0' then
risesig <= "000";
elsif clk'event and clk='1' then
risesig(0) <= ps2_clk;
risesig(1) <= risesig(0);
risesig(2) <= risesig(1);
end if;
end process;
rise <= '1' when risesig="001" else '0';
------------------m2 狀態(tài)
m2statech: process (reset, clk)
begin
if (reset='0') then
m2_state <= m2_reset;
elsif (clk'event and clk='1') then
m2_state <= m2_next_state;
end if;
end process;
--m2 狀態(tài)傳輸邏輯
m2statetr: process (m2_state, q, fall,rise,watchdog_timer_done,bitcount,ps2_data,packet_good)
begin
-- 輸出信號的缺省值
ps2_clk_hi_z <= '1';
ps2_data_hi_z <= '1';
error_no_ack <= '0';
output_strobe <= '0';
case m2_state is
when m2_reset => -- 復位后向鼠標發(fā)送命令字
m2_next_state <= m2_hold_clk_l;
when m2_wait =>
if (fall='1') then
m2_next_state <= m2_gather;
else
m2_next_state <= m2_wait;
end if;
when m2_gather =>
if ((watchdog_timer_done='1') and (bitcount=TOTAL_BITS))then
m2_next_state <= m2_verify;
else
m2_next_state <= m2_gather;
end if;
when m2_verify =>
--if (bitcount < TOTAL_BITS) then --替換 " packet_good='1' "
--m2_next_state <= m2_wait;
--else
m2_next_state <= m2_use;
--end if;
when m2_use =>
output_strobe <= '1';
m2_next_state <= m2_wait;
-- 用狀態(tài)機的9個狀態(tài)實現(xiàn)命令字傳輸,使鼠標進入"streaming"模式,
-- 并等待鼠標正確應答
when m2_hold_clk_l =>
ps2_clk_hi_z <= '0'; -- 啟動看門狗!
if (watchdog_timer_done='1') then
m2_next_state <= m2_data_low_1;
else
m2_next_state <= m2_hold_clk_l;
end if;
when m2_data_low_1 =>
ps2_data_hi_z <= '0'; -- 數(shù)據(jù)位 開始位, d[0] and d[1]
if (fall='1' and (bitcount = 2)) then
m2_next_state <= m2_data_high_1;
else
m2_next_state <= m2_data_low_1;
end if;
when m2_data_high_1 =>
ps2_data_hi_z <= '1'; -- 數(shù)據(jù)位 d[2]
if (fall='1' and (bitcount = 3)) then
m2_next_state <= m2_data_low_2;
else
m2_next_state <= m2_data_high_1;
end if;
when m2_data_low_2 =>
ps2_data_hi_z <= '0'; -- 數(shù)據(jù)位 d[3]
if (fall='1' and (bitcount = 4)) then
m2_next_state <= m2_data_high_2;
else
m2_next_state <= m2_data_low_2;
end if;
when m2_data_high_2 =>
ps2_data_hi_z <= '1'; -- 數(shù)據(jù)位 d[4],d[5],d[6],d[7]
if (fall='1' and (bitcount = 8)) then
m2_next_state <= m2_data_low_3;
else
m2_next_state <= m2_data_high_2;
end if;
when m2_data_low_3 =>
ps2_data_hi_z <= '0'; -- 奇偶校驗位
if (fall='1') then
m2_next_state <= m2_data_high_3;
else
m2_next_state <= m2_data_low_3;
end if;
when m2_data_high_3 =>
ps2_data_hi_z <= '1'; -- 允許鼠標拉成低電平(ACK脈沖)
if (fall='1' and (ps2_data='1')) then
m2_next_state <= m2_error_no_ack;
elsif (fall='1' and (ps2_data='0')) then
m2_next_state <= m2_await_response;
else
m2_next_state <= m2_data_high_3;
end if;
when m2_error_no_ack =>
error_no_ack <= '1';
m2_next_state <= m2_error_no_ack;
-- 為了鼠標正確進入"streaming"模式,狀態(tài)極必須等待足夠長的時間,
-- 確保鼠標正確應答0xFA。
when m2_await_response =>
--if (bitcount = 22) then
m2_next_state <= m2_verify;
--else
-- m2_next_state <= m2_await_response;
--end if;
when others => m2_next_state <= m2_wait;
end case;
end process;-----------------------------m2 狀態(tài)結(jié)束
--位計數(shù)器
bitcoun: process (reset, clk)
begin
if (reset='0') then
bitcount <= (others=>'0'); -- normal reset
elsif (clk'event and clk='1') then
if (fall='1') then
bitcount <= bitcount + 1;
elsif (watchdog_timer_done='1') then
bitcount <= (others=>'0'); -- rx watchdog timer reset
end if;
end if;
end process;
-- 數(shù)據(jù)移位寄存器
dataseq: process (reset, clk)
begin
if (reset='0') then
q <= (others=>'0');
elsif (clk'event and clk='1') then
if (fall='1') then
q <= ps2_data & q(TOTAL_BITS-1 downto 1);
end if;
end if;
end process;
-- 看門狗時間計數(shù)器
watchcount: process (reset,rise,fall, clk)
begin
if ((reset='0') or (rise='1') or (fall='1')) then
watchdog_timer_count <= (others=>'0');
elsif (clk'event and clk='1') then
if (watchdog_timer_done='0') then
watchdog_timer_count <= watchdog_timer_count + 1;
end if;
end if;
end process;
watchdog_timer_done <= '1' when (watchdog_timer_count=WATCHDOG-1) else '0';
-- 接收數(shù)據(jù)包有效標志
packet_good <= '1';
-- 輸出數(shù)據(jù)
outdata: process (reset, clk)
begin
if (reset='0') then
left_button <= '0';
right_button <= '0';
--x_increment <= (others=>'0');
--y_increment <= (others=>'0');
elsif (clk'event and clk='1') then
if (output_strobe='1') then
left_button <= q(1);
right_button <= q(2);
--x_increment <= '0' & q(19 downto 12);
mouseyy <= not (q(6) & q(6) & q(30 downto 23)) + "1";
end if;
end if;
end process;
cordinatex: process (reset, clk)
begin
if (reset='0') then
mousex <= "0110010000"; -- 400
elsif (clk'event and clk='1') then
if (output_strobe='1') then
if ((mousex >= 797 and q(5)='0') or (mousex <= 2 and q(5)='1')) then
mousex <= mousex;
else
mousex <= mousex + (q(5) & q(5) & q(19 downto 12));--q(5):xsign q(6):ysign
end if;
end if;
end if;
end process;
cordinatey: process (reset, clk)
begin
if (reset='0') then
mousey <= "0100101100"; -- 300
elsif (clk'event and clk='1') then
if (output_strobe='1') then
if ((mousey >= 597 and q(6)='1') or (mousey <= 2 and q(6)='0')) then
mousey <= mousey;
else
mousey <= mousey + mouseyy; --(q(6) & q(6) & q(30 downto 23));
end if;
end if;
end if;
end process;
--mousey <= "1001010101"-mouseyy;
data_ready <= output_strobe;
end Behavioral;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -