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

Subversion Repositories ps2

[/] [ps2/] [trunk/] [rtl/] [verilog/] [ps2_keyboard.v] - Blame information for rev 17

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

Line No. Rev Author Line
1 13 mihad
//-------------------------------------------------------------------------------------
2
//
3
// Author: John Clayton
4
// Date  : April 30, 2001
5
// Update: 4/30/01 copied this file from lcd_2.v (pared down).
6
// Update: 5/24/01 changed the first module from "ps2_keyboard_receiver"
7
//                 to "ps2_keyboard_interface"
8
// Update: 5/29/01 Added input synchronizing flip-flops.  Changed state
9
//                 encoding (m1) for good operation after part config.
10
// Update: 5/31/01 Added low drive strength and slow transitions to ps2_clk
11
//                 and ps2_data in the constraints file.  Added the signal
12
//                 "tx_shifting_done" as distinguished from "rx_shifting_done."
13
//                 Debugged the transmitter portion in the lab.
14
// Update: 6/01/01 Added horizontal tab to the ascii output.
15
// Update: 6/01/01 Added parameter TRAP_SHIFT_KEYS.
16
// Update: 6/05/01 Debugged the "debounce" timer functionality.
17
//                 Used 60usec timer as a "watchdog" timeout during
18
//                 receive from the keyboard.  This means that a keyboard
19
//                 can now be "hot plugged" into the interface, without
20
//                 messing up the bit_count, since the bit_count is reset
21
//                 to zero during periods of inactivity anyway.  This was
22
//                 difficult to debug.  I ended up using the logic analyzer,
23
//                 and had to scratch my head quite a bit.
24
// Update: 6/06/01 Removed extra comments before the input synchronizing
25
//                 flip-flops.  Used the correct parameter to size the
26
//                 5usec_timer_count.  Changed the name of this file from
27
//                 ps2.v to ps2_keyboard.v
28
// Update: 6/06/01 Removed "&& q[7:0]" in output_strobe logic.  Removed extra
29
//                 commented out "else" condition in the shift register and
30
//                 bit counter.
31
// Update: 6/07/01 Changed default values for 60usec timer parameters so that
32
//                 they correspond to 60usec for a 49.152MHz clock.
33
//
34
//
35
//
36
//
37
//
38
// Description
39
//-------------------------------------------------------------------------------------
40
// This is a state-machine driven serial-to-parallel and parallel-to-serial
41
// interface to the ps2 style keyboard interface.  The details of the operation
42
// of the keyboard interface were obtained from the following website:
43
//
44
//   http://www.beyondlogic.org/keyboard/keybrd.htm
45
//
46
// Some aspects of the keyboard interface are not implemented (e.g, parity
47
// checking for the receive side, and recognition of the various commands
48
// which the keyboard sends out, such as "power on selt test passed," "Error"
49
// and "Resend.")  However, if the user wishes to recognize these reply
50
// messages, the scan code output can always be used to extend functionality
51
// as desired.
52
//
53
// Note that the "Extended" (0xE0) and "Released" (0xF0) codes are recognized.
54
// The rx interface provides separate indicator flags for these two conditions
55
// with every valid character scan code which it provides.  The shift keys are
56
// also trapped by the interface, in order to provide correct uppercase ASCII
57
// characters at the ascii output, although the scan codes for the shift keys
58
// are still provided at the scan code output.  So, the left/right ALT keys
59
// can be differentiated by the presence of the rx_entended signal, while the
60
// left/right shift keys are differentiable by the different scan codes
61
// received.
62
//
63
// The interface to the ps2 keyboard uses ps2_clk clock rates of
64
// 30-40 kHz, dependent upon the keyboard itself.  The rate at which the state
65
// machine runs should be at least twice the rate of the ps2_clk, so that the
66
// states can accurately follow the clock signal itself.  Four times
67
// oversampling is better.  Say 200kHz at least.  The upper limit for clocking
68
// the state machine will undoubtedly be determined by delays in the logic
69
// which decodes the scan codes into ASCII equivalents.  The maximum speed
70
// will be most likely many megahertz, depending upon target technology.
71
// In order to run the state machine extremely fast, synchronizing flip-flops
72
// have been added to the ps2_clk and ps2_data inputs of the state machine.
73
// This avoids poor performance related to slow transitions of the inputs.
74
//
75
// Because this is a bi-directional interface, while reading from the keyboard
76
// the ps2_clk and ps2_data lines are used as inputs.  While writing to the
77
// keyboard, however (which may be done at any time.  If writing interrupts a
78
// read from the keyboard, the keyboard will buffer up its data, and send
79
// it later) both the ps2_clk and ps2_data lines are occasionally pulled low,
80
// and pullup resistors are used to bring the lines high again, by setting
81
// the drivers to high impedance state.
82
//
83
// The tx interface, for writing to the keyboard, does not provide any special
84
// pre-processing.  It simply transmits the 8-bit command value to the
85
// keyboard.
86
//
87
// Pullups MUST BE USED on the ps2_clk and ps2_data lines for this design,
88
// whether they be internal to an FPGA I/O pad, or externally placed.
89
// If internal pullups are used, they may be fairly weak, causing bounces
90
// due to crosstalk, etc.  There is a "debounce timer" implemented in order
91
// to eliminate erroneous state transitions which would occur based on bounce.
92
//
93
// Parameters are provided in order to configure and appropriately size the
94
// counter of a 60 microsecond timer used in the transmitter, depending on
95
// the clock frequency used.  The 60 microsecond period is guaranteed to be
96
// more than one period of the ps2_clk_s signal.
97
//
98
// Also, a smaller 5 microsecond timer has been included for "debounce".
99
// This is used because, with internal pullups on the ps2_clk and ps2_data
100
// lines, there is some bouncing around which occurs
101
//
102
// A parameter TRAP_SHIFT_KEYS allows the user to eliminate shift keypresses
103
// from producing scan codes (along with their "undefined" ASCII equivalents)
104
// at the output of the interface.  If TRAP_SHIFT_KEYS is non-zero, the shift
105
// key status will only be reported by rx_shift_key_on.  No ascii or scan
106
// codes will be reported for the shift keys.  This is useful for those who
107
// wish to use the ASCII data stream, and who don't want to have to "filter
108
// out" the shift key codes.
109
//
110
//-------------------------------------------------------------------------------------
111
 
112
 
113
// synopsys translate_off
114
`include "timescale.v"
115
// synopsys translate_on
116
`define TOTAL_BITS   11
117
`define RELEASE_CODE 16'hF0
118
 
119
module ps2_keyboard (
120
  clk,
121
  reset,
122
  ps2_clk_en_o_,
123
  ps2_data_en_o_,
124
  ps2_clk_i,
125
  ps2_data_i,
126
  rx_released,
127
  rx_scan_code,
128
  rx_data_ready,       // rx_read_o
129
  rx_read,             // rx_read_ack_i
130
  tx_data,
131
  tx_write,
132
  tx_write_ack_o,
133
  tx_error_no_keyboard_ack,
134
  translate
135
  );
136
 
137
// Parameters
138
 
139
// The timer value can be up to (2^bits) inclusive.
140
parameter TIMER_60USEC_VALUE_PP = 2950; // Number of sys_clks for 60usec.
141
parameter TIMER_60USEC_BITS_PP  = 12;   // Number of bits needed for timer
142
parameter TIMER_5USEC_VALUE_PP = 186;   // Number of sys_clks for debounce
143
parameter TIMER_5USEC_BITS_PP  = 8;     // Number of bits needed for timer
144
 
145
// State encodings, provided as parameters
146
// for flexibility to the one instantiating the module.
147
// In general, the default values need not be changed.
148
 
149
// State "m1_rx_clk_l" has been chosen on purpose.  Since the input
150
// synchronizing flip-flops initially contain zero, it takes one clk
151
// for them to update to reflect the actual (idle = high) status of
152
// the I/O lines from the keyboard.  Therefore, choosing 0 for m1_rx_clk_l
153
// allows the state machine to transition to m1_rx_clk_h when the true
154
// values of the input signals become present at the outputs of the
155
// synchronizing flip-flops.  This initial transition is harmless, and it
156
// eliminates the need for a "reset" pulse before the interface can operate.
157
 
158
parameter m1_rx_clk_h = 1;
159
parameter m1_rx_clk_l = 0;
160
parameter m1_rx_falling_edge_marker = 13;
161
parameter m1_rx_rising_edge_marker = 14;
162
parameter m1_tx_force_clk_l = 3;
163
parameter m1_tx_first_wait_clk_h = 10;
164
parameter m1_tx_first_wait_clk_l = 11;
165
parameter m1_tx_reset_timer = 12;
166
parameter m1_tx_wait_clk_h = 2;
167
parameter m1_tx_clk_h = 4;
168
parameter m1_tx_clk_l = 5;
169
parameter m1_tx_wait_keyboard_ack = 6;
170
parameter m1_tx_done_recovery = 7;
171
parameter m1_tx_error_no_keyboard_ack = 8;
172
parameter m1_tx_rising_edge_marker = 9;
173
parameter m2_rx_data_ready = 1;
174
parameter m2_rx_data_ready_ack = 0;
175
 
176
 
177
// I/O declarations
178
input clk;
179
input reset;
180
output ps2_clk_en_o_ ;
181
output ps2_data_en_o_ ;
182
input  ps2_clk_i ;
183
input  ps2_data_i ;
184
output rx_released;
185
output [7:0] rx_scan_code;
186
output rx_data_ready;
187
input rx_read;
188
input [7:0] tx_data;
189
input tx_write;
190
output tx_write_ack_o;
191
output tx_error_no_keyboard_ack;
192
input  translate ;
193
 
194
reg rx_released;
195
reg [7:0] rx_scan_code;
196
reg rx_data_ready;
197
reg tx_error_no_keyboard_ack;
198
 
199
// Internal signal declarations
200
wire timer_60usec_done;
201
wire timer_5usec_done;
202
wire released;
203
 
204
                         // NOTE: These two signals used to be one.  They
205
                         //       were split into two signals because of
206
                         //       shift key trapping.  With shift key
207
                         //       trapping, no event is generated externally,
208
                         //       but the "hold" data must still be cleared
209
                         //       anyway regardless, in preparation for the
210
                         //       next scan codes.
211
wire rx_output_event;    // Used only to clear: hold_released, hold_extended
212
wire rx_output_strobe;   // Used to produce the actual output.
213
 
214
wire tx_parity_bit;
215
wire rx_shifting_done;
216
wire tx_shifting_done;
217
 
218
reg [`TOTAL_BITS-1:0] q;
219
reg [3:0] m1_state;
220
reg [3:0] m1_next_state;
221
reg m2_state;
222
reg m2_next_state;
223
reg [3:0] bit_count;
224
reg enable_timer_60usec;
225
reg enable_timer_5usec;
226
reg [TIMER_60USEC_BITS_PP-1:0] timer_60usec_count;
227
reg [TIMER_5USEC_BITS_PP-1:0] timer_5usec_count;
228
reg hold_released;    // Holds prior value, cleared at rx_output_strobe
229
reg ps2_clk_s;        // Synchronous version of this input
230
reg ps2_data_s;       // Synchronous version of this input
231
reg ps2_clk_hi_z;     // Without keyboard, high Z equals 1 due to pullups.
232
reg ps2_data_hi_z;    // Without keyboard, high Z equals 1 due to pullups.
233
 
234
//--------------------------------------------------------------------------
235
// Module code
236
 
237
assign ps2_clk_en_o_  = ps2_clk_hi_z  ;
238
assign ps2_data_en_o_ = ps2_data_hi_z ;
239
 
240
// Input "synchronizing" logic -- synchronizes the inputs to the state
241
// machine clock, thus avoiding errors related to
242
// spurious state machine transitions.
243
always @(posedge clk)
244
begin
245
  ps2_clk_s <= ps2_clk_i;
246
  ps2_data_s <= ps2_data_i;
247
end
248
 
249
// State register
250
always @(posedge clk)
251
begin : m1_state_register
252
  if (reset) m1_state <= m1_rx_clk_h;
253
  else m1_state <= m1_next_state;
254
end
255
 
256
// State transition logic
257
always @(m1_state
258
         or q
259
         or tx_shifting_done
260
         or tx_write
261
         or ps2_clk_s
262
         or ps2_data_s
263
         or timer_60usec_done
264
         or timer_5usec_done
265
         )
266
begin : m1_state_logic
267
 
268
  // Output signals default to this value, unless changed in a state condition.
269
  ps2_clk_hi_z <= 1;
270
  ps2_data_hi_z <= 1;
271
  tx_error_no_keyboard_ack <= 0;
272
  enable_timer_60usec <= 0;
273
  enable_timer_5usec <= 0;
274
 
275
  case (m1_state)
276
 
277
    m1_rx_clk_h :
278
      begin
279
        enable_timer_60usec <= 1;
280
        if (tx_write) m1_next_state <= m1_tx_reset_timer;
281
        else if (~ps2_clk_s) m1_next_state <= m1_rx_falling_edge_marker;
282
        else m1_next_state <= m1_rx_clk_h;
283
      end
284
 
285
    m1_rx_falling_edge_marker :
286
      begin
287
        enable_timer_60usec <= 0;
288
        m1_next_state <= m1_rx_clk_l;
289
      end
290
 
291
    m1_rx_rising_edge_marker :
292
      begin
293
        enable_timer_60usec <= 0;
294
        m1_next_state <= m1_rx_clk_h;
295
      end
296
 
297
 
298
    m1_rx_clk_l :
299
      begin
300
        enable_timer_60usec <= 1;
301
        if (tx_write) m1_next_state <= m1_tx_reset_timer;
302
        else if (ps2_clk_s) m1_next_state <= m1_rx_rising_edge_marker;
303
        else m1_next_state <= m1_rx_clk_l;
304
      end
305
 
306
    m1_tx_reset_timer:
307
      begin
308
        enable_timer_60usec <= 0;
309
        m1_next_state <= m1_tx_force_clk_l;
310
      end
311
 
312
    m1_tx_force_clk_l :
313
      begin
314
        enable_timer_60usec <= 1;
315
        ps2_clk_hi_z <= 0;  // Force the ps2_clk line low.
316
        if (timer_60usec_done) m1_next_state <= m1_tx_first_wait_clk_h;
317
        else m1_next_state <= m1_tx_force_clk_l;
318
      end
319
 
320
    m1_tx_first_wait_clk_h :
321
      begin
322
        enable_timer_5usec <= 1;
323
        ps2_data_hi_z <= 0;        // Start bit.
324
        if (~ps2_clk_s && timer_5usec_done)
325
          m1_next_state <= m1_tx_clk_l;
326
        else
327
          m1_next_state <= m1_tx_first_wait_clk_h;
328
      end
329
 
330
    // This state must be included because the device might possibly
331
    // delay for up to 10 milliseconds before beginning its clock pulses.
332
    // During that waiting time, we cannot drive the data (q[0]) because it
333
    // is possibly 1, which would cause the keyboard to abort its receive
334
    // and the expected clocks would then never be generated.
335
    m1_tx_first_wait_clk_l :
336
      begin
337
        ps2_data_hi_z <= 0;
338
        if (~ps2_clk_s) m1_next_state <= m1_tx_clk_l;
339
        else m1_next_state <= m1_tx_first_wait_clk_l;
340
      end
341
 
342
    m1_tx_wait_clk_h :
343
      begin
344
        enable_timer_5usec <= 1;
345
        ps2_data_hi_z <= q[0];
346
        if (ps2_clk_s && timer_5usec_done)
347
          m1_next_state <= m1_tx_rising_edge_marker;
348
        else
349
          m1_next_state <= m1_tx_wait_clk_h;
350
      end
351
 
352
    m1_tx_rising_edge_marker :
353
      begin
354
        ps2_data_hi_z <= q[0];
355
        m1_next_state <= m1_tx_clk_h;
356
      end
357
 
358
    m1_tx_clk_h :
359
      begin
360
        ps2_data_hi_z <= q[0];
361
        if (tx_shifting_done) m1_next_state <= m1_tx_wait_keyboard_ack;
362
        else if (~ps2_clk_s) m1_next_state <= m1_tx_clk_l;
363
        else m1_next_state <= m1_tx_clk_h;
364
      end
365
 
366
    m1_tx_clk_l :
367
      begin
368
        ps2_data_hi_z <= q[0];
369
        if (ps2_clk_s) m1_next_state <= m1_tx_wait_clk_h;
370
        else m1_next_state <= m1_tx_clk_l;
371
      end
372
 
373
    m1_tx_wait_keyboard_ack :
374
      begin
375
        if (~ps2_clk_s && ps2_data_s)
376
          m1_next_state <= m1_tx_error_no_keyboard_ack;
377
        else if (~ps2_clk_s && ~ps2_data_s)
378
          m1_next_state <= m1_tx_done_recovery;
379
        else m1_next_state <= m1_tx_wait_keyboard_ack;
380
      end
381
 
382
    m1_tx_done_recovery :
383
      begin
384
        if (ps2_clk_s && ps2_data_s) m1_next_state <= m1_rx_clk_h;
385
        else m1_next_state <= m1_tx_done_recovery;
386
      end
387
 
388
    m1_tx_error_no_keyboard_ack :
389
      begin
390
        tx_error_no_keyboard_ack <= 1;
391
        if (ps2_clk_s && ps2_data_s) m1_next_state <= m1_rx_clk_h;
392
        else m1_next_state <= m1_tx_error_no_keyboard_ack;
393
      end
394
 
395
    default : m1_next_state <= m1_rx_clk_h;
396
  endcase
397
end
398
 
399
// State register
400
always @(posedge clk)
401
begin : m2_state_register
402
  if (reset) m2_state <= m2_rx_data_ready_ack;
403
  else m2_state <= m2_next_state;
404
end
405
 
406
// State transition logic
407
always @(m2_state or rx_output_strobe or rx_read)
408
begin : m2_state_logic
409
  case (m2_state)
410
    m2_rx_data_ready_ack:
411
          begin
412
            rx_data_ready <= 1'b0;
413
            if (rx_output_strobe) m2_next_state <= m2_rx_data_ready;
414
            else m2_next_state <= m2_rx_data_ready_ack;
415
          end
416
    m2_rx_data_ready:
417
          begin
418
            rx_data_ready <= 1'b1;
419
            if (rx_read) m2_next_state <= m2_rx_data_ready_ack;
420
            else m2_next_state <= m2_rx_data_ready;
421
          end
422
    default : m2_next_state <= m2_rx_data_ready_ack;
423
  endcase
424
end
425
 
426
// This is the bit counter
427
always @(posedge clk)
428
begin
429
  if (   reset
430
      || rx_shifting_done
431
      || (m1_state == m1_tx_wait_keyboard_ack)        // After tx is done.
432
      ) bit_count <= 0;  // normal reset
433
  else if (timer_60usec_done
434
           && (m1_state == m1_rx_clk_h)
435
           && (ps2_clk_s)
436
      ) bit_count <= 0;  // rx watchdog timer reset
437
  else if ( (m1_state == m1_rx_falling_edge_marker)   // increment for rx
438
           ||(m1_state == m1_tx_rising_edge_marker)   // increment for tx
439
           )
440
    bit_count <= bit_count + 1;
441
end
442
// This signal is high for one clock at the end of the timer count.
443
assign rx_shifting_done = (bit_count == `TOTAL_BITS);
444
assign tx_shifting_done = (bit_count == `TOTAL_BITS-1);
445
 
446
// This is the signal which enables loading of the shift register.
447
// It also indicates "ack" to the device writing to the transmitter.
448
assign tx_write_ack_o = (  (tx_write && (m1_state == m1_rx_clk_h))
449
                         ||(tx_write && (m1_state == m1_rx_clk_l))
450
                         );
451
 
452
// This is the ODD parity bit for the transmitted word.
453
assign tx_parity_bit = ~^tx_data;
454
 
455
// This is the shift register
456
always @(posedge clk)
457
begin
458
  if (reset) q <= 0;
459
  else if (tx_write_ack_o) q <= {1'b1,tx_parity_bit,tx_data,1'b0};
460
  else if ( (m1_state == m1_rx_falling_edge_marker)
461
           ||(m1_state == m1_tx_rising_edge_marker) )
462
    q <= {ps2_data_s,q[`TOTAL_BITS-1:1]};
463
end
464
 
465
// This is the 60usec timer counter
466
always @(posedge clk)
467
begin
468
  if (~enable_timer_60usec) timer_60usec_count <= 0;
469
  else if (~timer_60usec_done) timer_60usec_count <= timer_60usec_count + 1;
470
end
471
assign timer_60usec_done = (timer_60usec_count == (TIMER_60USEC_VALUE_PP - 1));
472
 
473
// This is the 5usec timer counter
474
always @(posedge clk)
475
begin
476
  if (~enable_timer_5usec) timer_5usec_count <= 0;
477
  else if (~timer_5usec_done) timer_5usec_count <= timer_5usec_count + 1;
478
end
479
assign timer_5usec_done = (timer_5usec_count == TIMER_5USEC_VALUE_PP - 1);
480
 
481
 
482
// Create the signals which indicate special scan codes received.
483
// These are the "unlatched versions."
484
assign released = (q[8:1] == `RELEASE_CODE) && rx_shifting_done && translate ;
485
 
486
// Store the special scan code status bits
487
// Not the final output, but an intermediate storage place,
488
// until the entire set of output data can be assembled.
489
always @(posedge clk)
490
begin
491
  if (reset || rx_output_event)
492
  begin
493
    hold_released <= 0;
494
  end
495
  else
496
  begin
497
    if (rx_shifting_done && released) hold_released <= 1;
498
  end
499
end
500
 
501
// Output the special scan code flags, the scan code and the ascii
502
always @(posedge clk)
503
begin
504
  if (reset)
505
  begin
506
    rx_released <= 0;
507
    rx_scan_code <= 0;
508
  end
509
  else if (rx_output_strobe)
510
  begin
511
    rx_released <= hold_released;
512
    rx_scan_code <= q[8:1];
513
  end
514
end
515
 
516
// Store the final rx output data only when all extend and release codes
517
// are received and the next (actual key) scan code is also ready.
518
// (the presence of rx_extended or rx_released refers to the
519
// the current latest scan code received, not the previously latched flags.)
520
assign rx_output_event  = (rx_shifting_done
521
                          && ~released
522
                          );
523
 
524
assign rx_output_strobe = (rx_shifting_done
525
                          && ~released
526
                          );
527
 
528
endmodule
529
 
530
//`undefine TOTAL_BITS
531
//`undefine EXTEND_CODE
532
//`undefine RELEASE_CODE
533
//`undefine LEFT_SHIFT
534
//`undefine RIGHT_SHIFT
535
 

powered by: WebSVN 2.1.0

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