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

Subversion Repositories ps2

[/] [ps2/] [tags/] [rel_6/] [bench/] [verilog/] [ps2_keyboard_model.v] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mihad
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  ps2_keyboard_model.v                                        ////
4
////                                                              ////
5
////  This file is part of the "ps2" project                      ////
6
////  http://www.opencores.org/cores/ps2/                         ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - mihad@opencores.org                                   ////
10
////      - Miha Dolenc                                           ////
11
////                                                              ////
12
////  All additional information is avaliable in the README.txt   ////
13
////  file.                                                       ////
14
////                                                              ////
15
////                                                              ////
16
//////////////////////////////////////////////////////////////////////
17
////                                                              ////
18
//// Copyright (C) 2000 Miha Dolenc, mihad@opencores.org          ////
19
////                                                              ////
20
//// This source file may be used and distributed without         ////
21
//// restriction provided that this copyright statement is not    ////
22
//// removed from the file and that any derivative work contains  ////
23
//// the original copyright notice and the associated disclaimer. ////
24
////                                                              ////
25
//// This source file is free software; you can redistribute it   ////
26
//// and/or modify it under the terms of the GNU Lesser General   ////
27
//// Public License as published by the Free Software Foundation; ////
28
//// either version 2.1 of the License, or (at your option) any   ////
29
//// later version.                                               ////
30
////                                                              ////
31
//// This source is distributed in the hope that it will be       ////
32
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
33
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
34
//// PURPOSE.  See the GNU Lesser General Public License for more ////
35
//// details.                                                     ////
36
////                                                              ////
37
//// You should have received a copy of the GNU Lesser General    ////
38
//// Public License along with this source; if not, download it   ////
39
//// from http://www.opencores.org/lgpl.shtml                     ////
40
////                                                              ////
41
//////////////////////////////////////////////////////////////////////
42
//
43
// CVS Revision History
44
//
45
// $Log: not supported by cvs2svn $
46
//
47
 
48
`include "timescale.v"
49
 
50
module ps2_keyboard_model
51
(
52
    kbd_clk_io,
53
    kbd_data_io,
54
    last_char_received_o,
55
    char_valid_o
56
);
57
 
58
parameter [31:0] kbd_clk_period = 50000; // chould be between 33 and 50 us to generate the clock between 30 and 20 kHz
59
 
60
inout kbd_clk_io,
61
      kbd_data_io ;
62
 
63
output [7:0] last_char_received_o ;
64
reg    [7:0] last_char_received_o ;
65
 
66
output char_valid_o ;
67
reg    char_valid_o ;
68
 
69
reg kbd_clk,
70
    kbd_data ;
71
 
72
assign kbd_clk_io  = kbd_clk ? 1'bz : 1'b0 ;
73
assign kbd_data_io = kbd_data ? 1'bz : 1'b0 ;
74
 
75
reg receiving ;
76
initial
77
begin
78
    kbd_clk  = 1'b1 ;
79
    kbd_data = 1'b1 ;
80
 
81
    last_char_received_o = 0 ;
82
    char_valid_o         = 0 ;
83
 
84
    receiving = 0 ;
85
end
86
 
87
always@(kbd_data_io or kbd_clk_io)
88
begin
89
    // check if host is driving keyboard data low and doesn't drive clock
90
    if ( !kbd_data_io && kbd_data && kbd_clk_io)
91
    begin
92
        // wait for half of clock period
93
        #(kbd_clk_period/2) ;
94
 
95
        // state hasn't changed - host wishes to send data - go receiving
96
        if ( !kbd_data_io && kbd_data && kbd_clk_io)
97
            kbd_receive_char(last_char_received_o) ;
98
    end
99
end
100
 
101
task kbd_send_char ;
102
    input [7:0] char ;
103
    output      transmited_ok ;
104
    output      severe_error ;
105
    reg   [10:0] tx_reg ;
106
    integer i ;
107
begin:main
108
    severe_error  = 1'b0 ;
109
    transmited_ok = 1'b0 ;
110
 
111
    wait ( !receiving ) ;
112
 
113
    tx_reg = { 1'b1, !(^char), char, 1'b0 } ;
114
 
115
    fork
116
    begin:wait_for_idle
117
        wait( (kbd_clk_io === 1'b1) && (kbd_data_io === 1'b1) ) ;
118
    //    disable timeout ;
119
    end
120
    /*begin:timeout
121
        #(256 * kbd_clk_period) ;
122
        $display("Error! Keyboard bus did not go idle in 256 keyboard clock cycles time!") ;
123
        severe_error  = 1'b1 ;
124
        transmited_ok = 1'b0 ;
125
        disable main ;
126
    end*/
127
    join
128
 
129
    #(kbd_clk_period/2) ;
130
    if ( !kbd_clk_io )
131
    begin
132
        transmited_ok = 1'b0 ;
133
        kbd_data = 1'b1 ;
134
        disable main ;
135
    end
136
 
137
    i = 0 ;
138
    while ( i < 11 )
139
    begin
140
        kbd_data = tx_reg[i] ;
141
 
142
        #(kbd_clk_period/2) ;
143
 
144
        if ( !kbd_clk_io )
145
        begin
146
            transmited_ok = 1'b0 ;
147
            kbd_data = 1'b1 ;
148
            disable main ;
149
        end
150
 
151
        kbd_clk = 1'b0 ;
152
 
153
        i = i + 1 ;
154
 
155
        #(kbd_clk_period/2) ;
156
        kbd_clk = 1'b1 ;
157
    end
158
 
159
    if ( i == 11 )
160
        transmited_ok = 1'b1 ;
161
end
162
endtask // kbd_send_char
163
 
164
task kbd_receive_char;
165
    output [7:0] char ;
166
    reg          parity ;
167
    integer i ;
168
    reg          stop_bit_received ;
169
begin:main
170
    i = 0 ;
171
    stop_bit_received = 1 ;
172
    receiving = 1 ;
173
    #(kbd_clk_period/2) ;
174
 
175
    while ( i < 11 )
176
    begin
177
 
178
        if ( !kbd_clk_io )
179
        begin
180
            receiving = 0 ;
181
            disable main ;
182
        end
183
 
184
        kbd_clk = 1'b0 ;
185
 
186
        #(kbd_clk_period/2) ;
187
 
188
        kbd_clk = 1'b1 ;
189
 
190
        if ( i > 0 )
191
        begin
192
            if ( i <= 8 )
193
                char[i - 1] = kbd_data_io ;
194
            else if ( i == 9 )
195
            begin
196
                parity = kbd_data_io ;
197
                if ( parity !== ( !(^char) ) )
198
                    $display("Invalid parity bit received") ;
199
            end
200
            else
201
            begin
202
                if ( kbd_data_io !== 1'b1 )
203
                begin
204
                    i = i - 1 ;
205
                    stop_bit_received = 0 ;
206
                end
207
                else
208
                begin
209
                    kbd_data = 1'b0 ;
210
                end
211
            end
212
        end
213
 
214
        i = i + 1 ;
215
        #(kbd_clk_period/2) ;
216
    end
217
 
218
    if ( !kbd_clk_io )
219
    begin
220
        receiving = 0 ;
221
        disable main ;
222
    end
223
 
224
    kbd_clk  = 1'b0 ;
225
 
226
    #(kbd_clk_period/2) ;
227
    kbd_clk  <= 1'b1 ;
228
    kbd_data <= 1'b1 ;
229
 
230
    if ( stop_bit_received )
231
    begin
232
        char_valid_o = !char_valid_o ;
233
    end
234
 
235
    receiving = 0 ;
236
end
237
endtask // kbd_receive_char
238
 
239
endmodule // ps2_keyboard_model

powered by: WebSVN 2.1.0

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