OpenCores
URL https://opencores.org/ocsvn/ps2core/ps2core/trunk

Subversion Repositories ps2core

[/] [ps2core/] [trunk/] [sim/] [vhdl/] [ps2_mouse.vhd] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 danielqg
-------------------------------------------------------------------------------
2
-- Title      : PS/2 interface Testbench
3
-- Project    :
4
-------------------------------------------------------------------------------
5
-- File       : ps2.vhd
6
-- Author     : Daniel Quintero <danielqg@infonegocio.com>
7
-- Company    : Itoo Software
8
-- Created    : 2003-04-14
9
-- Last update: 2003-10-30
10
-- Platform   : VHDL'87
11
-------------------------------------------------------------------------------
12
-- Description: PS/2 mice model
13
-------------------------------------------------------------------------------
14
--  This code is distributed under the terms and conditions of the
15
--  GNU General Public License
16
-------------------------------------------------------------------------------
17
-- Revisions  :
18
-- Date        Version  Author  Description
19
-- 2003-04-14  1.0      daniel  Created
20
-------------------------------------------------------------------------------
21
 
22
library IEEE;
23
use IEEE.std_logic_1164.all;
24
use std.textio.all;
25
 
26
entity ps2mouse is
27
        port (
28
                PS2_clk  : inout std_logic;
29
                PS2_data : inout std_logic);
30
end ps2mouse;
31
 
32
architecture sim of ps2mouse is
33
 
34
        procedure PS2SendByte(byte             : in    std_logic_vector(7 downto 0);
35
                               signal PS2_clk  : inout std_logic;
36
                               signal PS2_data : inout std_logic) is
37
        begin
38
                --wait until (PS2_clk = 'H');
39
                for i in 0 to 10 loop
40
                        if i = 0 then
41
                                PS2_Data <= '0';
42
                        elsif i = 9 then
43
                                PS2_Data <= not (Byte(0) xor
44
                                                 Byte(1) xor
45
                                                 Byte(2) xor
46
                                                 Byte(3) xor
47
                                                 Byte(4) xor
48
                                                 Byte(5) xor
49
                                                 Byte(6) xor
50
                                                 Byte(7));
51
                        elsif i = 10 then
52
                                PS2_Data <= 'H';
53
                        else
54
                                if Byte(i - 1) = '1' then
55
                                        PS2_Data <= '1';
56
                                else
57
 
58
                                end if;
59
                        end if;
60
                        wait for 20 us;
61
                        PS2_Clk <= '0';
62
                        wait for 20 us;
63
                        PS2_Clk <= '1';
64
                end loop;
65
                PS2_Clk <= 'H';
66
        end;
67
 
68
        procedure PS2RecvByte(byte             : out   std_logic_vector(7 downto 0);
69
                               signal PS2_clk  : inout std_logic;
70
                               signal PS2_data : inout std_logic) is
71
                variable parity : std_logic;
72
                variable buf    : std_logic_vector(7 downto 0);
73
        begin
74
                PS2_Data <= 'H';
75
                for i in 0 to 10 loop
76
                        wait for 20 us;
77
                        PS2_Clk <= '0';
78
                        if i = 0 then
79
                                if PS2_data /= '0' then
80
                                        write(output, string'("Warning, not start bit from Host"));
81
                                end if;
82
                        elsif i = 9 then
83
                                parity := To_X01(PS2_data);
84
                        elsif i = 10 then
85
                                PS2_Data <= '0';  -- Ack
86
                        else
87
                                buf(i - 1) := To_X01(PS2_data);
88
                        end if;
89
                        wait for 20 us;
90
                        PS2_Clk <= '1';
91
                end loop;
92
 
93
                if parity /= not (buf(0) xor buf(1) xor buf(2) xor buf(3) xor
94
                                  buf(4) xor buf(5) xor buf(6) xor buf(7)) then
95
                        write(output, string'("Waring, parity check error in host data"));
96
                end if;
97
                Byte     := buf;
98
                PS2_Clk  <= 'H';
99
                PS2_Data <= 'H';
100
        end;
101
 
102
        procedure PS2Write(signal req, rw : out std_logic;
103
                            signal ack    : in  std_logic) is
104
        begin
105
                wait for 20 us;
106
                req <= '1';
107
                rw  <= '0';
108
                wait until ack = '1';
109
                req <= '0' after 10 ns;
110
                rw  <= '1' after 10 ns;
111
        end;
112
 
113
 
114
        function stdvec_to_str(inp : std_logic_vector) return string is
115
                variable temp : string(inp'left+1 downto 1) := (others => 'X');
116
        begin
117
                for i in inp'reverse_range loop
118
                        if (inp(i) = '1') then
119
                                temp(i+1) := '1';
120
                        elsif (inp(i) = '0') then
121
                                temp(i+1) := '0';
122
                        end if;
123
                end loop;
124
                return temp;
125
        end function stdvec_to_str;
126
 
127
 
128
        signal stop               : boolean                      := false;
129
        signal listen, read, send : boolean                      := false;
130
        signal clk                : std_logic                    := '0';
131
        signal rst                : std_logic;
132
        signal cmd_in             : std_logic_vector(7 downto 0) := (others => '0');
133
 
134
begin
135
        process
136
                variable data_in : std_logic_vector(7 downto 0) := (others => '0');
137
                variable cmd     : integer;
138
        begin
139
                --wait for 100 us;
140
                 -- Send BAT cycle successful, 0xAA
141
                --PS2SendByte("10101010", PS2_clk, PS2_data);
142
                -- Send PS/2 device ID, 0x00
143
                --PS2SendByte("00000000", PS2_clk, PS2_data);
144
                wait for 10 us;
145
                   listen <= true;
146
                wait;
147
        end process;
148
 
149
        process
150
                variable data_in : std_logic_vector(7 downto 0) := (others => '0');
151
                variable cmd     : integer;
152
        begin
153
                PS2_clk <= 'H';
154
                PS2_data <= 'H';
155
                wait on PS2_clk;
156
                if PS2_clk = '0' then --and listen and not send then
157
                        wait on PS2_clk;
158
                    PS2RecvByte(data_in, PS2_clk, PS2_data);
159
                    write(output, string'("PS2MOUSE - Received data from host : "));
160
                    write(output, stdvec_to_str(data_in));
161
                        send <= true;
162
                elsif PS2_clk /= '0' and send then
163
                        wait for 1 ms;
164
                        PS2SendByte("00001000", PS2_clk, PS2_data);
165
                        PS2SendByte("00000100", PS2_clk, PS2_data);
166
                        PS2SendByte("00000101", PS2_clk, PS2_data);
167
                        wait;
168
                end if;
169
        end process;
170
 
171
 
172
end sim;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.