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

Subversion Repositories ps2

[/] [ps2/] [tags/] [rel_1/] [rtl/] [verilog/] [ps2_keyboard.v] - Blame information for rev 13

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
`resetall
115
`include "timescale.v"
116
// synopsys translate_on
117
`define TOTAL_BITS   11
118
`define RELEASE_CODE 16'hF0
119
 
120
module ps2_keyboard (
121
  clk,
122
  reset,
123
  ps2_clk_en_o_,
124
  ps2_data_en_o_,
125
  ps2_clk_i,
126
  ps2_data_i,
127
  rx_released,
128
  rx_scan_code,
129
  rx_data_ready,       // rx_read_o
130
  rx_read,             // rx_read_ack_i
131
  tx_data,
132
  tx_write,
133
  tx_write_ack_o,
134
  tx_error_no_keyboard_ack,
135
  translate
136
  );
137
 
138
// Parameters
139
 
140
// The timer value can be up to (2^bits) inclusive.
141
parameter TIMER_60USEC_VALUE_PP = 2950; // Number of sys_clks for 60usec.
142
parameter TIMER_60USEC_BITS_PP  = 12;   // Number of bits needed for timer
143
parameter TIMER_5USEC_VALUE_PP = 186;   // Number of sys_clks for debounce
144
parameter TIMER_5USEC_BITS_PP  = 8;     // Number of bits needed for timer
145
 
146
// State encodings, provided as parameters
147
// for flexibility to the one instantiating the module.
148
// In general, the default values need not be changed.
149
 
150
// State "m1_rx_clk_l" has been chosen on purpose.  Since the input
151
// synchronizing flip-flops initially contain zero, it takes one clk
152
// for them to update to reflect the actual (idle = high) status of
153
// the I/O lines from the keyboard.  Therefore, choosing 0 for m1_rx_clk_l
154
// allows the state machine to transition to m1_rx_clk_h when the true
155
// values of the input signals become present at the outputs of the
156
// synchronizing flip-flops.  This initial transition is harmless, and it
157
// eliminates the need for a "reset" pulse before the interface can operate.
158
 
159
parameter m1_rx_clk_h = 1;
160
parameter m1_rx_clk_l = 0;
161
parameter m1_rx_falling_edge_marker = 13;
162
parameter m1_rx_rising_edge_marker = 14;
163
parameter m1_tx_force_clk_l = 3;
164
parameter m1_tx_first_wait_clk_h = 10;
165
parameter m1_tx_first_wait_clk_l = 11;
166
parameter m1_tx_reset_timer = 12;
167
parameter m1_tx_wait_clk_h = 2;
168
parameter m1_tx_clk_h = 4;
169
parameter m1_tx_clk_l = 5;
170
parameter m1_tx_wait_keyboard_ack = 6;
171
parameter m1_tx_done_recovery = 7;
172
parameter m1_tx_error_no_keyboard_ack = 8;
173
parameter m1_tx_rising_edge_marker = 9;
174
parameter m2_rx_data_ready = 1;
175
parameter m2_rx_data_ready_ack = 0;
176
 
177
 
178
// I/O declarations
179
input clk;
180
input reset;
181
output ps2_clk_en_o_ ;
182
output ps2_data_en_o_ ;
183
input  ps2_clk_i ;
184
input  ps2_data_i ;
185
output rx_released;
186
output [7:0] rx_scan_code;
187
output rx_data_ready;
188
input rx_read;
189
input [7:0] tx_data;
190
input tx_write;
191
output tx_write_ack_o;
192
output tx_error_no_keyboard_ack;
193
input  translate ;
194
 
195
reg rx_released;
196
reg [7:0] rx_scan_code;
197
reg rx_data_ready;
198
reg tx_error_no_keyboard_ack;
199
 
200
// Internal signal declarations
201
wire timer_60usec_done;
202
wire timer_5usec_done;
203
wire released;
204
 
205
                         // NOTE: These two signals used to be one.  They
206
                         //       were split into two signals because of
207
                         //       shift key trapping.  With shift key
208
                         //       trapping, no event is generated externally,
209
                         //       but the "hold" data must still be cleared
210
                         //       anyway regardless, in preparation for the
211
                         //       next scan codes.
212
wire rx_output_event;    // Used only to clear: hold_released, hold_extended
213
wire rx_output_strobe;   // Used to produce the actual output.
214
 
215
wire tx_parity_bit;
216
wire rx_shifting_done;
217
wire tx_shifting_done;
218
 
219
reg [`TOTAL_BITS-1:0] q;
220
reg [3:0] m1_state;
221
reg [3:0] m1_next_state;
222
reg m2_state;
223
reg m2_next_state;
224
reg [3:0] bit_count;
225
reg enable_timer_60usec;
226
reg enable_timer_5usec;
227
reg [TIMER_60USEC_BITS_PP-1:0] timer_60usec_count;
228
reg [TIMER_5USEC_BITS_PP-1:0] timer_5usec_count;
229
reg hold_released;    // Holds prior value, cleared at rx_output_strobe
230
reg ps2_clk_s;        // Synchronous version of this input
231
reg ps2_data_s;       // Synchronous version of this input
232
reg ps2_clk_hi_z;     // Without keyboard, high Z equals 1 due to pullups.
233
reg ps2_data_hi_z;    // Without keyboard, high Z equals 1 due to pullups.
234
 
235
//--------------------------------------------------------------------------
236
// Module code
237
 
238
assign ps2_clk_en_o_  = ps2_clk_hi_z  ;
239
assign ps2_data_en_o_ = ps2_data_hi_z ;
240
 
241
// Input "synchronizing" logic -- synchronizes the inputs to the state
242
// machine clock, thus avoiding errors related to
243
// spurious state machine transitions.
244
always @(posedge clk)
245
begin
246
  ps2_clk_s <= ps2_clk_i;
247
  ps2_data_s <= ps2_data_i;
248
end
249
 
250
// State register
251
always @(posedge clk)
252
begin : m1_state_register
253
  if (reset) m1_state <= m1_rx_clk_h;
254
  else m1_state <= m1_next_state;
255
end
256
 
257
// State transition logic
258
always @(m1_state
259
         or q
260
         or tx_shifting_done
261
         or tx_write
262
         or ps2_clk_s
263
         or ps2_data_s
264
         or timer_60usec_done
265
         or timer_5usec_done
266
         )
267
begin : m1_state_logic
268
 
269
  // Output signals default to this value, unless changed in a state condition.
270
  ps2_clk_hi_z <= 1;
271
  ps2_data_hi_z <= 1;
272
  tx_error_no_keyboard_ack <= 0;
273
  enable_timer_60usec <= 0;
274
  enable_timer_5usec <= 0;
275
 
276
  case (m1_state)
277
 
278
    m1_rx_clk_h :
279
      begin
280
        enable_timer_60usec <= 1;
281
        if (tx_write) m1_next_state <= m1_tx_reset_timer;
282
        else if (~ps2_clk_s) m1_next_state <= m1_rx_falling_edge_marker;
283
        else m1_next_state <= m1_rx_clk_h;
284
      end
285
 
286
    m1_rx_falling_edge_marker :
287
      begin
288
        enable_timer_60usec <= 0;
289
        m1_next_state <= m1_rx_clk_l;
290
      end
291
 
292
    m1_rx_rising_edge_marker :
293
      begin
294
        enable_timer_60usec <= 0;
295
        m1_next_state <= m1_rx_clk_h;
296
      end
297
 
298
 
299
    m1_rx_clk_l :
300
      begin
301
        enable_timer_60usec <= 1;
302
        if (tx_write) m1_next_state <= m1_tx_reset_timer;
303
        else if (ps2_clk_s) m1_next_state <= m1_rx_rising_edge_marker;
304
        else m1_next_state <= m1_rx_clk_l;
305
      end
306
 
307
    m1_tx_reset_timer:
308
      begin
309
        enable_timer_60usec <= 0;
310
        m1_next_state <= m1_tx_force_clk_l;
311
      end
312
 
313
    m1_tx_force_clk_l :
314
      begin
315
        enable_timer_60usec <= 1;
316
        ps2_clk_hi_z <= 0;  // Force the ps2_clk line low.
317
        if (timer_60usec_done) m1_next_state <= m1_tx_first_wait_clk_h;
318
        else m1_next_state <= m1_tx_force_clk_l;
319
      end
320
 
321
    m1_tx_first_wait_clk_h :
322
      begin
323
        enable_timer_5usec <= 1;
324
        ps2_data_hi_z <= 0;        // Start bit.
325
        if (~ps2_clk_s && timer_5usec_done)
326
          m1_next_state <= m1_tx_clk_l;
327
        else
328
          m1_next_state <= m1_tx_first_wait_clk_h;
329
      end
330
 
331
    // This state must be included because the device might possibly
332
    // delay for up to 10 milliseconds before beginning its clock pulses.
333
    // During that waiting time, we cannot drive the data (q[0]) because it
334
    // is possibly 1, which would cause the keyboard to abort its receive
335
    // and the expected clocks would then never be generated.
336
    m1_tx_first_wait_clk_l :
337
      begin
338
        ps2_data_hi_z <= 0;
339
        if (~ps2_clk_s) m1_next_state <= m1_tx_clk_l;
340
        else m1_next_state <= m1_tx_first_wait_clk_l;
341
      end
342
 
343
    m1_tx_wait_clk_h :
344
      begin
345
        enable_timer_5usec <= 1;
346
        ps2_data_hi_z <= q[0];
347
        if (ps2_clk_s && timer_5usec_done)
348
          m1_next_state <= m1_tx_rising_edge_marker;
349
        else
350
          m1_next_state <= m1_tx_wait_clk_h;
351
      end
352
 
353
    m1_tx_rising_edge_marker :
354
      begin
355
        ps2_data_hi_z <= q[0];
356
        m1_next_state <= m1_tx_clk_h;
357
      end
358
 
359
    m1_tx_clk_h :
360
      begin
361
        ps2_data_hi_z <= q[0];
362
        if (tx_shifting_done) m1_next_state <= m1_tx_wait_keyboard_ack;
363
        else if (~ps2_clk_s) m1_next_state <= m1_tx_clk_l;
364
        else m1_next_state <= m1_tx_clk_h;
365
      end
366
 
367
    m1_tx_clk_l :
368
      begin
369
        ps2_data_hi_z <= q[0];
370
        if (ps2_clk_s) m1_next_state <= m1_tx_wait_clk_h;
371
        else m1_next_state <= m1_tx_clk_l;
372
      end
373
 
374
    m1_tx_wait_keyboard_ack :
375
      begin
376
        if (~ps2_clk_s && ps2_data_s)
377
          m1_next_state <= m1_tx_error_no_keyboard_ack;
378
        else if (~ps2_clk_s && ~ps2_data_s)
379
          m1_next_state <= m1_tx_done_recovery;
380
        else m1_next_state <= m1_tx_wait_keyboard_ack;
381
      end
382
 
383
    m1_tx_done_recovery :
384
      begin
385
        if (ps2_clk_s && ps2_data_s) m1_next_state <= m1_rx_clk_h;
386
        else m1_next_state <= m1_tx_done_recovery;
387
      end
388
 
389
    m1_tx_error_no_keyboard_ack :
390
      begin
391
        tx_error_no_keyboard_ack <= 1;
392
        if (ps2_clk_s && ps2_data_s) m1_next_state <= m1_rx_clk_h;
393
        else m1_next_state <= m1_tx_error_no_keyboard_ack;
394
      end
395
 
396
    default : m1_next_state <= m1_rx_clk_h;
397
  endcase
398
end
399
 
400
// State register
401
always @(posedge clk)
402
begin : m2_state_register
403
  if (reset) m2_state <= m2_rx_data_ready_ack;
404
  else m2_state <= m2_next_state;
405
end
406
 
407
// State transition logic
408
always @(m2_state or rx_output_strobe or rx_read)
409
begin : m2_state_logic
410
  case (m2_state)
411
    m2_rx_data_ready_ack:
412
          begin
413
            rx_data_ready <= 1'b0;
414
            if (rx_output_strobe) m2_next_state <= m2_rx_data_ready;
415
            else m2_next_state <= m2_rx_data_ready_ack;
416
          end
417
    m2_rx_data_ready:
418
          begin
419
            rx_data_ready <= 1'b1;
420
            if (rx_read) m2_next_state <= m2_rx_data_ready_ack;
421
            else m2_next_state <= m2_rx_data_ready;
422
          end
423
    default : m2_next_state <= m2_rx_data_ready_ack;
424
  endcase
425
end
426
 
427
// This is the bit counter
428
always @(posedge clk)
429
begin
430
  if (   reset
431
      || rx_shifting_done
432
      || (m1_state == m1_tx_wait_keyboard_ack)        // After tx is done.
433
      ) bit_count <= 0;  // normal reset
434
  else if (timer_60usec_done
435
           && (m1_state == m1_rx_clk_h)
436
           && (ps2_clk_s)
437
      ) bit_count <= 0;  // rx watchdog timer reset
438
  else if ( (m1_state == m1_rx_falling_edge_marker)   // increment for rx
439
           ||(m1_state == m1_tx_rising_edge_marker)   // increment for tx
440
           )
441
    bit_count <= bit_count + 1;
442
end
443
// This signal is high for one clock at the end of the timer count.
444
assign rx_shifting_done = (bit_count == `TOTAL_BITS);
445
assign tx_shifting_done = (bit_count == `TOTAL_BITS-1);
446
 
447
// This is the signal which enables loading of the shift register.
448
// It also indicates "ack" to the device writing to the transmitter.
449
assign tx_write_ack_o = (  (tx_write && (m1_state == m1_rx_clk_h))
450
                         ||(tx_write && (m1_state == m1_rx_clk_l))
451
                         );
452
 
453
// This is the ODD parity bit for the transmitted word.
454
assign tx_parity_bit = ~^tx_data;
455
 
456
// This is the shift register
457
always @(posedge clk)
458
begin
459
  if (reset) q <= 0;
460
  else if (tx_write_ack_o) q <= {1'b1,tx_parity_bit,tx_data,1'b0};
461
  else if ( (m1_state == m1_rx_falling_edge_marker)
462
           ||(m1_state == m1_tx_rising_edge_marker) )
463
    q <= {ps2_data_s,q[`TOTAL_BITS-1:1]};
464
end
465
 
466
// This is the 60usec timer counter
467
always @(posedge clk)
468
begin
469
  if (~enable_timer_60usec) timer_60usec_count <= 0;
470
  else if (~timer_60usec_done) timer_60usec_count <= timer_60usec_count + 1;
471
end
472
assign timer_60usec_done = (timer_60usec_count == (TIMER_60USEC_VALUE_PP - 1));
473
 
474
// This is the 5usec timer counter
475
always @(posedge clk)
476
begin
477
  if (~enable_timer_5usec) timer_5usec_count <= 0;
478
  else if (~timer_5usec_done) timer_5usec_count <= timer_5usec_count + 1;
479
end
480
assign timer_5usec_done = (timer_5usec_count == TIMER_5USEC_VALUE_PP - 1);
481
 
482
 
483
// Create the signals which indicate special scan codes received.
484
// These are the "unlatched versions."
485
assign released = (q[8:1] == `RELEASE_CODE) && rx_shifting_done && translate ;
486
 
487
// Store the special scan code status bits
488
// Not the final output, but an intermediate storage place,
489
// until the entire set of output data can be assembled.
490
always @(posedge clk)
491
begin
492
  if (reset || rx_output_event)
493
  begin
494
    hold_released <= 0;
495
  end
496
  else
497
  begin
498
    if (rx_shifting_done && released) hold_released <= 1;
499
  end
500
end
501
 
502
// Output the special scan code flags, the scan code and the ascii
503
always @(posedge clk)
504
begin
505
  if (reset)
506
  begin
507
    rx_released <= 0;
508
    rx_scan_code <= 0;
509
  end
510
  else if (rx_output_strobe)
511
  begin
512
    rx_released <= hold_released;
513
    rx_scan_code <= q[8:1];
514
  end
515
end
516
 
517
// Store the final rx output data only when all extend and release codes
518
// are received and the next (actual key) scan code is also ready.
519
// (the presence of rx_extended or rx_released refers to the
520
// the current latest scan code received, not the previously latched flags.)
521
assign rx_output_event  = (rx_shifting_done
522
                          && ~released
523
                          );
524
 
525
assign rx_output_strobe = (rx_shifting_done
526
                          && ~released
527
                          );
528
 
529
endmodule
530
 
531
//`undefine TOTAL_BITS
532
//`undefine EXTEND_CODE
533
//`undefine RELEASE_CODE
534
//`undefine LEFT_SHIFT
535
//`undefine RIGHT_SHIFT
536
 

powered by: WebSVN 2.1.0

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