-
Notifications
You must be signed in to change notification settings - Fork 7
/
ps2_keyboard.v
277 lines (267 loc) · 5.78 KB
/
ps2_keyboard.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
////////////////////////////////////////////////////////////////////////////////
// Project Name: CoCo3FPGA Version 3.0
// File Name: ps2_keyboard.v
//
// CoCo3 in an FPGA
//
// Revision: 3.0 08/15/15
////////////////////////////////////////////////////////////////////////////////
//
// CPU section copyrighted by John Kent
// The FDC co-processor copyrighted Daniel Wallner.
//
////////////////////////////////////////////////////////////////////////////////
//
// Color Computer 3 compatible system on a chip
//
// Version : 3.0
//
// Copyright (c) 2008 Gary Becker ([email protected])
//
// All rights reserved
//
// Redistribution and use in source and synthezised forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// Redistributions in synthesized form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// Neither the name of the author nor the names of other contributors may
// be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Please report bugs to the author, but before you do so, please
// make sure that this is not a derivative work and that
// you have the latest version of this file.
//
// The latest version of this file can be found at:
// http://groups.yahoo.com/group/CoCo3FPGA
//
// File history :
//
// 1.0 Full Release
// 2.0 Partial Release
// 3.0 Full Release
////////////////////////////////////////////////////////////////////////////////
// Gary Becker
////////////////////////////////////////////////////////////////////////////////
module ps2_keyboard (
CLK,
RESET_N,
PS2_CLK,
PS2_DATA,
RX_PRESSED,
RX_EXTENDED,
RX_SCAN
);
input CLK;
input RESET_N;
input PS2_CLK;
input PS2_DATA;
output RX_PRESSED;
reg RX_PRESSED;
output RX_EXTENDED;
reg RX_EXTENDED;
output [7:0] RX_SCAN;
reg [7:0] RX_SCAN;
reg KB_CLK;
reg KB_DATA;
reg KB_CLK_B;
reg KB_DATA_B;
reg PRESSED_N;
reg EXTENDED;
reg [2:0] BIT;
reg [3:0] STATE;
reg [7:0] SCAN;
wire PARITY;
reg [10:0] TIMER;
reg KILLER;
wire RESET_X;
// Double buffer
always @ (posedge CLK)
begin
KB_CLK_B <= PS2_CLK;
KB_DATA_B <= PS2_DATA;
KB_CLK <= KB_CLK_B;
KB_DATA <= KB_DATA_B;
end
assign PARITY = ~(((SCAN[0]^SCAN[1])
^(SCAN[2]^SCAN[3]))
^((SCAN[4]^SCAN[5])
^(SCAN[6]^SCAN[7])));
assign RESET_X = RESET_N & KILLER;
always @ (negedge CLK or negedge RESET_N)
if(!RESET_N)
begin
KILLER <= 1'b1;
TIMER <= 11'h000;
end
else
case(TIMER)
11'h000:
begin
KILLER <= 1'b1;
if(STATE != 4'h0)
TIMER <= 11'h001;
end
11'h7FD:
begin
KILLER <= 1'b0;
TIMER <= 11'h7FE;
end
default:
if(STATE == 4'h0)
TIMER <= 11'h000;
else
TIMER <= TIMER + 1'b1;
endcase
always @ (posedge CLK or negedge RESET_X)
begin
if(!RESET_X)
begin
STATE <= 4'h0;
SCAN <= 8'h00;
BIT <= 3'b000;
RX_SCAN <= 8'h00;
RX_PRESSED <= 1'b0;
RX_EXTENDED <= 1'b0;
PRESSED_N <= 1'b0;
EXTENDED <= 1'b0;
end
else
begin
case (STATE)
4'h0: // Hunt for start bit
begin
BIT <= 3'b000;
RX_SCAN <= 8'h00;
RX_PRESSED <= 1'b0;
RX_EXTENDED <= 1'b0;
if(~KB_DATA & ~KB_CLK) //look for start bit
STATE <= 4'h1;
end
4'h1: // next bit
begin
if(KB_CLK)
STATE <= 4'h2;
end
4'h2: // Hunt for Bit
begin
if(~KB_CLK)
begin
SCAN[BIT] <= KB_DATA;
BIT <= BIT + 1'b1;
if(BIT == 3'b111)
STATE <= 4'h3;
else
STATE <= 4'h1;
end
end
4'h3: // Hunt for Bit
begin
if(KB_CLK)
STATE <= 4'h4;
end
4'h4: // Test parity
begin
if(~KB_CLK)
begin
if(KB_DATA == PARITY)
STATE <= 4'h5;
else
begin
PRESSED_N <= 1'b0;
EXTENDED <= 1'b0;
SCAN <= 8'h00;
STATE <= 4'hF;
end
end
end
4'h5: // Look for Stop bit
begin
if(KB_CLK)
STATE <= 4'h6;
end
4'h6: // Stop bit
begin
if(~KB_CLK)
STATE <= 4'h7;
end
4'h7:
begin
if(SCAN ==8'hE0)
begin
EXTENDED <= 1'b1;
STATE <= 4'hF;
end
else
if(SCAN == 8'hF0)
begin
PRESSED_N <= 1'b1;
STATE <= 4'hF;
end
else
begin
RX_SCAN <= SCAN;
RX_PRESSED <= ~PRESSED_N;
RX_EXTENDED <= EXTENDED;
PRESSED_N <= 1'b0;
EXTENDED <= 1'b0;
SCAN <= 8'h00;
STATE <= 4'hF;
end
end
4'h8:
begin
STATE <= 4'h9;
end
4'h9:
begin
STATE <= 4'hA;
end
4'hA:
begin
STATE <= 4'hB;
end
4'hB:
begin
STATE <= 4'hC;
end
4'hC:
begin
STATE <= 4'hD;
end
4'hD:
begin
STATE <= 4'hE;
end
4'hE:
begin
STATE <= 4'hF;
end
4'hF:
begin
if(KB_CLK)
STATE <= 4'h0;
end
endcase
end
end
endmodule