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

Subversion Repositories ps2core

[/] [ps2core/] [trunk/] [sim/] [vhdl/] [ps2mouse_tb.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 generic UART for mice/keyboard, Low level Testbench
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_tb is
27
end ps2mouse_tb;
28
 
29
architecture sim of ps2mouse_tb is
30
 
31
        procedure PS2SendByte(byte             : in    std_logic_vector(7 downto 0);
32
                               signal PS2_clk  : inout std_logic;
33
                               signal PS2_data : inout std_logic) is
34
        begin
35
                --wait until (PS2_clk = 'H');
36
                for i in 0 to 10 loop
37
                        if i = 0 then
38
                                PS2_Data <= '0';
39
                        elsif i = 9 then
40
                                PS2_Data <= not (Byte(0) xor
41
                                                 Byte(1) xor
42
                                                 Byte(2) xor
43
                                                 Byte(3) xor
44
                                                 Byte(4) xor
45
                                                 Byte(5) xor
46
                                                 Byte(6) xor
47
                                                 Byte(7));
48
                        elsif i = 10 then
49
                                PS2_Data <= 'H';
50
                        else
51
                                if Byte(i - 1) = '1' then
52
                                        PS2_Data <= '1';
53
                                else
54
                                        PS2_Data <= '0';
55
                                end if;
56
                        end if;
57
                        wait for 20 us;
58
                        PS2_Clk <= '0';
59
                        wait for 20 us;
60
                        PS2_Clk <= '1';
61
                end loop;
62
                PS2_Clk <= 'H';
63
        end;
64
 
65
        procedure PS2RecvByte(byte             : out   std_logic_vector(7 downto 0);
66
                               signal PS2_clk  : inout std_logic;
67
                               signal PS2_data : inout std_logic) is
68
                variable parity : std_logic;
69
                variable buf    : std_logic_vector(7 downto 0);
70
        begin
71
                PS2_Data <= 'H';
72
                for i in 0 to 10 loop
73
                        wait for 20 us;
74
                        PS2_Clk <= '0';
75
                        if i = 0 then
76
                                if PS2_data /= '0' then
77
                                        write(output, string'("Warning, not start bit from Host"));
78
                                end if;
79
                        elsif i = 9 then
80
                                parity := To_X01(PS2_data);
81
                        elsif i = 10 then
82
                                PS2_Data <= '0';  -- Ack
83
                        else
84
                                buf(i - 1) := To_X01(PS2_data);
85
                        end if;
86
                        wait for 20 us;
87
                        PS2_Clk <= '1';
88
                end loop;
89
 
90
                if parity /= not (buf(0) xor buf(1) xor buf(2) xor buf(3) xor
91
                                  buf(4) xor buf(5) xor buf(6) xor buf(7)) then
92
                        write(output, string'("Waring, parity check error in host data"));
93
                end if;
94
                Byte     := buf;
95
                PS2_Clk  <= 'H';
96
                PS2_Data <= 'H';
97
        end;
98
 
99
        procedure PS2Write(signal req, rw : out std_logic;
100
                            signal ack    : in  std_logic) is
101
        begin
102
                wait for 20 us;
103
                req <= '1';
104
                rw  <= '0';
105
                wait until ack = '1';
106
                req <= '0' after 10 ns;
107
                rw  <= '1' after 10 ns;
108
        end;
109
 
110
 
111
        function stdvec_to_str(inp : std_logic_vector) return string is
112
                variable temp : string(inp'left+1 downto 1) := (others => 'X');
113
        begin
114
                for i in inp'reverse_range loop
115
                        if (inp(i) = '1') then
116
                                temp(i+1) := '1';
117
                        elsif (inp(i) = '0') then
118
                                temp(i+1) := '0';
119
                        end if;
120
                end loop;
121
                return temp;
122
        end function stdvec_to_str;
123
 
124
 
125
        signal stop   : boolean                      := false;
126
        signal listen, read, send : boolean             := false;
127
        signal clk    : std_logic                    := '0';
128
        signal rst    : std_logic;
129
        signal cmd_in : std_logic_vector(7 downto 0) := (others => '0');
130
 
131
        signal PS2_clk  : std_logic := 'H';
132
        signal PS2_data : std_logic := 'H';
133
begin
134
 
135
        u0 : entity mouse_test(rtl)
136
                port map(clk_i      => clk, rst_i => rst,
137
                         ps2_clk_io => PS2_Clk, ps2_data_io => PS2_Data);
138
 
139
        -- Clk generation
140
        process
141
        begin
142
                clk <= not clk;         -- 50Mhz clk
143
                wait for 10 ns;
144
                if stop then
145
                        wait;
146
                end if;
147
        end process;
148
        rst  <= '0', '1' after 1 ns;
149
        stop <= true     after 25 ms;
150
        process
151
                variable data_in : std_logic_vector(7 downto 0) := (others => '0');
152
                variable cmd     : integer;
153
        begin
154
                --wait for 100 us;
155
                -- Send BAT cycle successful, 0xAA
156
                --PS2SendByte("10101010", PS2_clk, PS2_data);
157
                -- Send PS/2 device ID, 0x00
158
                --PS2SendByte("00000000", PS2_clk, PS2_data);
159
                wait for 10 us;
160
                   listen <= true;
161
                wait;
162
        end process;
163
 
164
        process
165
                variable data_in : std_logic_vector(7 downto 0) := (others => '0');
166
                variable cmd     : integer;
167
        begin
168
                wait on PS2_clk;
169
                if PS2_clk = '0' then --and listen and not send then
170
                        wait on PS2_clk;
171
                    PS2RecvByte(data_in, PS2_clk, PS2_data);
172
                    write(output, string'("Received data from host : "));
173
                    write(output, stdvec_to_str(data_in));
174
                        send <= true;
175
 
176
        elsif PS2_clk /= '0' and send then
177
                        wait for 1 ms;
178
                        PS2SendByte("00001000", PS2_clk, PS2_data);
179
                        PS2SendByte("00000100", PS2_clk, PS2_data);
180
                        PS2SendByte("00000101", PS2_clk, PS2_data);
181
                end if;
182
                if stop then
183
                        wait;
184
                end if;
185
        end process;
186
 
187
end sim;

powered by: WebSVN 2.1.0

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