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

Subversion Repositories zet86

[/] [zet86/] [trunk/] [soc/] [keyb/] [rtl/] [ps2_keyb.v] - Blame information for rev 49

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

Line No. Rev Author Line
1 43 zeus
/*
2
 *  PS2 Wishbone 8042 compatible keyboard controller
3
 *
4
 *  Copyright (c) 2009  Zeus Gomez Marmolejo <zeus@opencores.org>
5
 *  adapted from the opencores keyboard controller from John Clayton
6
 *
7
 *  This file is part of the Zet processor. This processor is free
8
 *  hardware; you can redistribute it and/or modify it under the terms of
9
 *  the GNU General Public License as published by the Free Software
10
 *  Foundation; either version 3, or (at your option) any later version.
11
 *
12
 *  Zet is distrubuted in the hope that it will be useful, but WITHOUT
13
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15
 *  License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with Zet; see the file COPYING. If not, see
19
 *  <http://www.gnu.org/licenses/>.
20
 */
21
 
22
`timescale 1ns/100ps
23
 
24
`define TOTAL_BITS   11
25
`define RELEASE_CODE 16'hF0
26
`define LEFT_SHIFT   16'h12
27
`define RIGHT_SHIFT  16'h59
28
 
29
module ps2_keyb (
30
    // Wishbone slave interface
31
    input            wb_clk_i,
32
    input            wb_rst_i,
33
    output reg [7:0] wb_dat_o,   // scancode
34
    output reg       wb_tgc_o,   // intr
35
    input            wb_tgc_i,   // inta
36
 
37
    // PS2 PAD signals
38
    inout            ps2_clk_,
39
    inout            ps2_data_
40
  );
41
 
42
  // Parameter declarations
43
  // The timer value can be up to (2^bits) inclusive.
44
  parameter TIMER_60USEC_VALUE_PP = 1920; // Number of sys_clks for 60usec.
45
  parameter TIMER_60USEC_BITS_PP  = 11;   // Number of bits needed for timer
46
  parameter TIMER_5USEC_VALUE_PP  = 186;  // Number of sys_clks for debounce
47
  parameter TIMER_5USEC_BITS_PP   = 8;    // Number of bits needed for timer
48
  parameter TRAP_SHIFT_KEYS_PP    = 0;    // Default: No shift key trap.
49
 
50
  // State encodings, provided as parameters
51
  // for flexibility to the one instantiating the module.
52
  // In general, the default values need not be changed.
53
 
54
  // State "m1_rx_clk_l" has been chosen on purpose.  Since the input
55
  // synchronizing flip-flops initially contain zero, it takes one clk
56
  // for them to update to reflect the actual (idle = high) status of
57
  // the I/O lines from the keyboard.  Therefore, choosing 0 for m1_rx_clk_l
58
  // allows the state machine to transition to m1_rx_clk_h when the true
59
  // values of the input signals become present at the outputs of the
60
  // synchronizing flip-flops.  This initial transition is harmless, and it
61
  // eliminates the need for a "reset" pulse before the interface can operate.
62
  parameter m1_rx_clk_h = 1;
63
  parameter m1_rx_clk_l = 0;
64
  parameter m1_rx_falling_edge_marker = 13;
65
  parameter m1_rx_rising_edge_marker = 14;
66
  parameter m1_tx_force_clk_l = 3;
67
  parameter m1_tx_first_wait_clk_h = 10;
68
  parameter m1_tx_first_wait_clk_l = 11;
69
  parameter m1_tx_reset_timer = 12;
70
  parameter m1_tx_wait_clk_h = 2;
71
  parameter m1_tx_clk_h = 4;
72
  parameter m1_tx_clk_l = 5;
73
  parameter m1_tx_wait_keyboard_ack = 6;
74
  parameter m1_tx_done_recovery = 7;
75
  parameter m1_tx_error_no_keyboard_ack = 8;
76
  parameter m1_tx_rising_edge_marker = 9;
77
 
78
  // Nets and registers
79
  wire rx_output_event;
80
  wire rx_output_strobe;
81
  wire rx_shifting_done;
82
  wire tx_shifting_done;
83
  wire timer_60usec_done;
84
  wire timer_5usec_done;
85
 
86
  wire released;
87
 
88
  wire [6:0] xt_code;
89
 
90
  reg [3:0] bit_count;
91
  reg [3:0] m1_state;
92
  reg [3:0] m1_next_state;
93
 
94
  reg ps2_clk_hi_z;     // Without keyboard, high Z equals 1 due to pullups.
95
  reg ps2_data_hi_z;    // Without keyboard, high Z equals 1 due to pullups.
96
  reg ps2_clk_s;        // Synchronous version of this input
97
  reg ps2_data_s;       // Synchronous version of this input
98
 
99
  reg enable_timer_60usec;
100
  reg enable_timer_5usec;
101
  reg [TIMER_60USEC_BITS_PP-1:0] timer_60usec_count;
102
  reg [TIMER_5USEC_BITS_PP-1:0] timer_5usec_count;
103
 
104
  reg [`TOTAL_BITS-1:0] q;
105
 
106
  reg hold_released;    // Holds prior value, cleared at rx_output_strobe
107
 
108
  // Module instantiation
109
  translate_8042 tr0 (
110
    .at_code (q[7:1]),
111
    .xt_code (xt_code)
112
  );
113
 
114
  // Continuous assignments
115
  // This signal is high for one clock at the end of the timer count.
116
  assign rx_shifting_done = (bit_count == `TOTAL_BITS);
117
  assign tx_shifting_done = (bit_count == `TOTAL_BITS-1);
118
 
119
  assign rx_output_event  = (rx_shifting_done
120
                          && ~released
121
                          );
122
  assign rx_output_strobe = (rx_shifting_done
123
                          && ~released
124
                          && ( (TRAP_SHIFT_KEYS_PP == 0)
125
                               || ( (q[8:1] != `RIGHT_SHIFT)
126
                                    &&(q[8:1] != `LEFT_SHIFT)
127
                                  )
128
                             )
129
                          );
130
 
131
  assign ps2_clk_  = ps2_clk_hi_z  ? 1'bZ : 1'b0;
132
  assign ps2_data_ = ps2_data_hi_z ? 1'bZ : 1'b0;
133
 
134
  assign timer_60usec_done =
135
    (timer_60usec_count == (TIMER_60USEC_VALUE_PP - 1));
136
  assign timer_5usec_done = (timer_5usec_count == TIMER_5USEC_VALUE_PP - 1);
137
 
138
  // Create the signals which indicate special scan codes received.
139
  // These are the "unlatched versions."
140
  //assign extended = (q[8:1] == `EXTEND_CODE) && rx_shifting_done;
141
  assign released = (q[8:1] == `RELEASE_CODE) && rx_shifting_done;
142
 
143
  // Behaviour
144
  // intr
145
  always @(posedge wb_clk_i)
146
    wb_tgc_o <= wb_rst_i ? 1'b0
147
      : ((rx_output_strobe & !wb_tgc_i) ? 1'b1
148
      : (wb_tgc_o ? !wb_tgc_i : 1'b0));
149
 
150
  // This is the shift register
151
  always @(posedge wb_clk_i)
152
    if (wb_rst_i) q <= 0;
153
    //  else if (((m1_state == m1_rx_clk_h) && ~ps2_clk_s)
154
    else if ( (m1_state == m1_rx_falling_edge_marker)
155
             ||(m1_state == m1_tx_rising_edge_marker) )
156
        q <= {ps2_data_s,q[`TOTAL_BITS-1:1]};
157
 
158
  // This is the 60usec timer counter
159
  always @(posedge wb_clk_i)
160
    if (~enable_timer_60usec) timer_60usec_count <= 0;
161
    else if (~timer_60usec_done) timer_60usec_count <= timer_60usec_count + 1;
162
 
163
  // This is the 5usec timer counter
164
  always @(posedge wb_clk_i)
165
    if (~enable_timer_5usec) timer_5usec_count <= 0;
166
    else if (~timer_5usec_done) timer_5usec_count <= timer_5usec_count + 1;
167
 
168
  // Input "synchronizing" logic -- synchronizes the inputs to the state
169
  // machine clock, thus avoiding errors related to
170
  // spurious state machine transitions.
171
  //
172
  // Since the initial state of registers is zero, and the idle state
173
  // of the ps2_clk and ps2_data lines is "1" (due to pullups), the
174
  // "sense" of the ps2_clk_s signal is inverted from the true signal.
175
  // This allows the state machine to "come up" in the correct
176
  always @(posedge wb_clk_i)
177
  begin
178
    ps2_clk_s <= ps2_clk_;
179
    ps2_data_s <= ps2_data_;
180
  end
181
 
182
  // State transition logic
183
  always @(m1_state
184
           or q
185
           or tx_shifting_done
186
           or ps2_clk_s
187
           or ps2_data_s
188
           or timer_60usec_done
189
           or timer_5usec_done
190
          )
191
    begin : m1_state_logic
192
 
193
    // Output signals default to this value,
194
    //  unless changed in a state condition.
195
    ps2_clk_hi_z  <= 1;
196
    ps2_data_hi_z <= 1;
197
    enable_timer_60usec <= 0;
198
    enable_timer_5usec  <= 0;
199
 
200
    case (m1_state)
201
 
202
      m1_rx_clk_h :
203
      begin
204
        enable_timer_60usec <= 1;
205
        if (~ps2_clk_s)
206
          m1_next_state <= m1_rx_falling_edge_marker;
207
        else m1_next_state <= m1_rx_clk_h;
208
      end
209
 
210
      m1_rx_falling_edge_marker :
211
      begin
212
        enable_timer_60usec <= 0;
213
        m1_next_state <= m1_rx_clk_l;
214
      end
215
 
216
      m1_rx_rising_edge_marker :
217
      begin
218
        enable_timer_60usec <= 0;
219
        m1_next_state <= m1_rx_clk_h;
220
      end
221
 
222
      m1_rx_clk_l :
223
      begin
224
        enable_timer_60usec <= 1;
225
        if (ps2_clk_s)
226
          m1_next_state <= m1_rx_rising_edge_marker;
227
        else m1_next_state <= m1_rx_clk_l;
228
      end
229
 
230
      m1_tx_reset_timer :
231
      begin
232
        enable_timer_60usec <= 0;
233
        m1_next_state <= m1_tx_force_clk_l;
234
      end
235
 
236
      m1_tx_force_clk_l :
237
      begin
238
        enable_timer_60usec <= 1;
239
        ps2_clk_hi_z <= 0;  // Force the ps2_clk line low.
240
        if (timer_60usec_done)
241
          m1_next_state <= m1_tx_first_wait_clk_h;
242
        else m1_next_state <= m1_tx_force_clk_l;
243
      end
244
 
245
      m1_tx_first_wait_clk_h :
246
      begin
247
        enable_timer_5usec <= 1;
248
        ps2_data_hi_z <= 0;        // Start bit.
249
        if (~ps2_clk_s && timer_5usec_done)
250
          m1_next_state <= m1_tx_clk_l;
251
        else
252
          m1_next_state <= m1_tx_first_wait_clk_h;
253
      end
254
 
255
      // This state must be included because the device might possibly
256
      // delay for up to 10 milliseconds before beginning its clock pulses.
257
      // During that waiting time, we cannot drive the data (q[0]) because it
258
      // is possibly 1, which would cause the keyboard to abort its receive
259
      // and the expected clocks would then never be generated.
260
      m1_tx_first_wait_clk_l :
261
      begin
262
        ps2_data_hi_z <= 0;
263
        if (~ps2_clk_s) m1_next_state <= m1_tx_clk_l;
264
        else m1_next_state <= m1_tx_first_wait_clk_l;
265
      end
266
 
267
      m1_tx_wait_clk_h :
268
      begin
269
        enable_timer_5usec <= 1;
270
        ps2_data_hi_z <= q[0];
271
        if (ps2_clk_s && timer_5usec_done)
272
          m1_next_state <= m1_tx_rising_edge_marker;
273
        else
274
          m1_next_state <= m1_tx_wait_clk_h;
275
      end
276
 
277
      m1_tx_rising_edge_marker :
278
      begin
279
        ps2_data_hi_z <= q[0];
280
        m1_next_state <= m1_tx_clk_h;
281
      end
282
 
283
      m1_tx_clk_h :
284
      begin
285
        ps2_data_hi_z <= q[0];
286
        if (tx_shifting_done) m1_next_state <= m1_tx_wait_keyboard_ack;
287
        else if (~ps2_clk_s) m1_next_state <= m1_tx_clk_l;
288
        else m1_next_state <= m1_tx_clk_h;
289
      end
290
 
291
      m1_tx_clk_l :
292
      begin
293
        ps2_data_hi_z <= q[0];
294
        if (ps2_clk_s) m1_next_state <= m1_tx_wait_clk_h;
295
        else m1_next_state <= m1_tx_clk_l;
296
      end
297
 
298
      m1_tx_wait_keyboard_ack :
299
      begin
300
        if (~ps2_clk_s && ps2_data_s)
301
          m1_next_state <= m1_tx_error_no_keyboard_ack;
302
        else if (~ps2_clk_s && ~ps2_data_s)
303
          m1_next_state <= m1_tx_done_recovery;
304
        else m1_next_state <= m1_tx_wait_keyboard_ack;
305
      end
306
 
307
      m1_tx_done_recovery :
308
      begin
309
        if (ps2_clk_s && ps2_data_s) m1_next_state <= m1_rx_clk_h;
310
        else m1_next_state <= m1_tx_done_recovery;
311
      end
312
 
313
      m1_tx_error_no_keyboard_ack :
314
      begin
315
        if (ps2_clk_s && ps2_data_s) m1_next_state <= m1_rx_clk_h;
316
        else m1_next_state <= m1_tx_error_no_keyboard_ack;
317
      end
318
 
319
      default : m1_next_state <= m1_rx_clk_h;
320
    endcase
321
  end
322
 
323
  // State register
324
  always @(posedge wb_clk_i)
325
  begin : m1_state_register
326
    if (wb_rst_i) m1_state <= m1_rx_clk_h;
327
    else m1_state <= m1_next_state;
328
  end
329
 
330
  // wb_dat_o - scancode
331
  always @(posedge wb_clk_i)
332
    if (wb_rst_i) wb_dat_o <= 8'b0;
333
    else wb_dat_o <=
334
      (rx_output_strobe && q[8:1]) ? (q[8] ? q[8:1]
335
        : {hold_released,xt_code})
336
     : wb_dat_o;
337
 
338
  // This is the bit counter
339
  always @(posedge wb_clk_i)
340
    begin
341
      if (wb_rst_i
342
         || rx_shifting_done
343
         || (m1_state == m1_tx_wait_keyboard_ack) // After tx is done.
344
         ) bit_count <= 0;  // normal reset
345
      else if (timer_60usec_done
346
               && (m1_state == m1_rx_clk_h)
347
               && (ps2_clk_s)
348
              ) bit_count <= 0;  // rx watchdog timer reset
349
      else if ( (m1_state == m1_rx_falling_edge_marker) // increment for rx
350
              ||(m1_state == m1_tx_rising_edge_marker)  // increment for tx
351
              )
352
        bit_count <= bit_count + 1;
353
  end
354
 
355
  // Store the special scan code status bits
356
  // Not the final output, but an intermediate storage place,
357
  // until the entire set of output data can be assembled.
358
  always @(posedge wb_clk_i)
359
    if (wb_rst_i || rx_output_event) hold_released <= 0;
360
    else if (rx_shifting_done && released) hold_released <= 1;
361
 
362
endmodule
363
 
364
 
365
module translate_8042 (
366
    input      [6:0] at_code,
367
    output reg [6:0] xt_code
368
  );
369
 
370
  // Behaviour
371
  always @(at_code)
372
    case (at_code)
373
      7'h00: xt_code <= 7'h7f;
374
      7'h01: xt_code <= 7'h43;
375
      7'h02: xt_code <= 7'h41;
376
      7'h03: xt_code <= 7'h3f;
377
      7'h04: xt_code <= 7'h3d;
378
      7'h05: xt_code <= 7'h3b;
379
      7'h06: xt_code <= 7'h3c;
380
      7'h07: xt_code <= 7'h58;
381
      7'h08: xt_code <= 7'h64;
382
      7'h09: xt_code <= 7'h44;
383
      7'h0a: xt_code <= 7'h42;
384
      7'h0b: xt_code <= 7'h40;
385
      7'h0c: xt_code <= 7'h3e;
386
      7'h0d: xt_code <= 7'h0f;
387
      7'h0e: xt_code <= 7'h29;
388
      7'h0f: xt_code <= 7'h59;
389
      7'h10: xt_code <= 7'h65;
390
      7'h11: xt_code <= 7'h38;
391
      7'h12: xt_code <= 7'h2a;
392
      7'h13: xt_code <= 7'h70;
393
      7'h14: xt_code <= 7'h1d;
394
      7'h15: xt_code <= 7'h10;
395
      7'h16: xt_code <= 7'h02;
396
      7'h17: xt_code <= 7'h5a;
397
      7'h18: xt_code <= 7'h66;
398
      7'h19: xt_code <= 7'h71;
399
      7'h1a: xt_code <= 7'h2c;
400
      7'h1b: xt_code <= 7'h1f;
401
      7'h1c: xt_code <= 7'h1e;
402
      7'h1d: xt_code <= 7'h11;
403
      7'h1e: xt_code <= 7'h03;
404
      7'h1f: xt_code <= 7'h5b;
405
      7'h20: xt_code <= 7'h67;
406
      7'h21: xt_code <= 7'h2e;
407
      7'h22: xt_code <= 7'h2d;
408
      7'h23: xt_code <= 7'h20;
409
      7'h24: xt_code <= 7'h12;
410
      7'h25: xt_code <= 7'h05;
411
      7'h26: xt_code <= 7'h04;
412
      7'h27: xt_code <= 7'h5c;
413
      7'h28: xt_code <= 7'h68;
414
      7'h29: xt_code <= 7'h39;
415
      7'h2a: xt_code <= 7'h2f;
416
      7'h2b: xt_code <= 7'h21;
417
      7'h2c: xt_code <= 7'h14;
418
      7'h2d: xt_code <= 7'h13;
419
      7'h2e: xt_code <= 7'h06;
420
      7'h2f: xt_code <= 7'h5d;
421
      7'h30: xt_code <= 7'h69;
422
      7'h31: xt_code <= 7'h31;
423
      7'h32: xt_code <= 7'h30;
424
      7'h33: xt_code <= 7'h23;
425
      7'h34: xt_code <= 7'h22;
426
      7'h35: xt_code <= 7'h15;
427
      7'h36: xt_code <= 7'h07;
428
      7'h37: xt_code <= 7'h5e;
429
      7'h38: xt_code <= 7'h6a;
430
      7'h39: xt_code <= 7'h72;
431
      7'h3a: xt_code <= 7'h32;
432
      7'h3b: xt_code <= 7'h24;
433
      7'h3c: xt_code <= 7'h16;
434
      7'h3d: xt_code <= 7'h08;
435
      7'h3e: xt_code <= 7'h09;
436
      7'h3f: xt_code <= 7'h5f;
437
      7'h40: xt_code <= 7'h6b;
438
      7'h41: xt_code <= 7'h33;
439
      7'h42: xt_code <= 7'h25;
440
      7'h43: xt_code <= 7'h17;
441
      7'h44: xt_code <= 7'h18;
442
      7'h45: xt_code <= 7'h0b;
443
      7'h46: xt_code <= 7'h0a;
444
      7'h47: xt_code <= 7'h60;
445
      7'h48: xt_code <= 7'h6c;
446
      7'h49: xt_code <= 7'h34;
447
      7'h4a: xt_code <= 7'h35;
448
      7'h4b: xt_code <= 7'h26;
449
      7'h4c: xt_code <= 7'h27;
450
      7'h4d: xt_code <= 7'h19;
451
      7'h4e: xt_code <= 7'h0c;
452
      7'h4f: xt_code <= 7'h61;
453
      7'h50: xt_code <= 7'h6d;
454
      7'h51: xt_code <= 7'h73;
455
      7'h52: xt_code <= 7'h28;
456
      7'h53: xt_code <= 7'h74;
457
      7'h54: xt_code <= 7'h1a;
458
      7'h55: xt_code <= 7'h0d;
459
      7'h56: xt_code <= 7'h62;
460
      7'h57: xt_code <= 7'h6e;
461
      7'h58: xt_code <= 7'h3a;
462
      7'h59: xt_code <= 7'h36;
463
      7'h5a: xt_code <= 7'h1c;
464
      7'h5b: xt_code <= 7'h1b;
465
      7'h5c: xt_code <= 7'h75;
466
      7'h5d: xt_code <= 7'h2b;
467
      7'h5e: xt_code <= 7'h63;
468
      7'h5f: xt_code <= 7'h76;
469
      7'h60: xt_code <= 7'h55;
470
      7'h61: xt_code <= 7'h56;
471
      7'h62: xt_code <= 7'h77;
472
      7'h63: xt_code <= 7'h78;
473
      7'h64: xt_code <= 7'h79;
474
      7'h65: xt_code <= 7'h7a;
475
      7'h66: xt_code <= 7'h0e;
476
      7'h67: xt_code <= 7'h7b;
477
      7'h68: xt_code <= 7'h7c;
478
      7'h69: xt_code <= 7'h4f;
479
      7'h6a: xt_code <= 7'h7d;
480
      7'h6b: xt_code <= 7'h4b;
481
      7'h6c: xt_code <= 7'h47;
482
      7'h6d: xt_code <= 7'h7e;
483
      7'h6e: xt_code <= 7'h7f;
484
      7'h6f: xt_code <= 7'h6f;
485
      7'h70: xt_code <= 7'h52;
486
      7'h71: xt_code <= 7'h53;
487
      7'h72: xt_code <= 7'h50;
488
      7'h73: xt_code <= 7'h4c;
489
      7'h74: xt_code <= 7'h4d;
490
      7'h75: xt_code <= 7'h48;
491
      7'h76: xt_code <= 7'h01;
492
      7'h77: xt_code <= 7'h45;
493
      7'h78: xt_code <= 7'h57;
494
      7'h79: xt_code <= 7'h4e;
495
      7'h7a: xt_code <= 7'h51;
496
      7'h7b: xt_code <= 7'h4a;
497
      7'h7c: xt_code <= 7'h37;
498
      7'h7d: xt_code <= 7'h49;
499
      7'h7e: xt_code <= 7'h46;
500
      7'h7f: xt_code <= 7'h54;
501
    endcase
502
endmodule

powered by: WebSVN 2.1.0

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