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

Subversion Repositories ps2

[/] [ps2/] [tags/] [rel_8/] [rtl/] [verilog/] [ps2_mouse.v] - Blame information for rev 25

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

powered by: WebSVN 2.1.0

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