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

Subversion Repositories ps2

[/] [ps2/] [tags/] [rel_3/] [rtl/] [verilog/] [ps2_mouse.v] - Blame information for rev 51

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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