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 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 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 29 gorand
reg timer_done ;
202
reg timer_5usec ;
203 9 mihad
                         // NOTE: These two signals used to be one.  They
204
                         //       were split into two signals because of
205
                         //       shift key trapping.  With shift key
206
                         //       trapping, no event is generated externally,
207
                         //       but the "hold" data must still be cleared
208
                         //       anyway regardless, in preparation for the
209
                         //       next scan codes.
210
wire rx_output_event;    // Used only to clear: hold_released, hold_extended
211
wire rx_output_strobe;   // Used to produce the actual output.
212
 
213
wire tx_parity_bit;
214
wire rx_shifting_done;
215
wire tx_shifting_done;
216
 
217
reg [`TOTAL_BITS-1:0] q;
218
reg [3:0] m1_state;
219
reg [3:0] m1_next_state;
220
reg m2_state;
221
reg m2_next_state;
222
reg [3:0] bit_count;
223
reg enable_timer_60usec;
224
reg enable_timer_5usec;
225
reg [TIMER_60USEC_BITS_PP-1:0] timer_60usec_count;
226
reg [TIMER_5USEC_BITS_PP-1:0] timer_5usec_count;
227
reg ps2_clk_s;        // Synchronous version of this input
228
reg ps2_data_s;       // Synchronous version of this input
229
reg ps2_clk_hi_z;     // Without keyboard, high Z equals 1 due to pullups.
230
reg ps2_data_hi_z;    // Without keyboard, high Z equals 1 due to pullups.
231
 
232 24 primozs
reg ps2_clk_ms;
233
reg ps2_data_ms;
234
 
235 9 mihad
//--------------------------------------------------------------------------
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 25 primozs
  ps2_clk_ms <= #1 ps2_clk_i;
247
  ps2_data_ms <= #1 ps2_data_i;
248 24 primozs
 
249 25 primozs
  ps2_clk_s <= #1 ps2_clk_ms;
250
  ps2_data_s <= #1 ps2_data_ms;
251 24 primozs
 
252 9 mihad
end
253
 
254
// State register
255
always @(posedge clk)
256
begin : m1_state_register
257 25 primozs
  if (reset) m1_state <= #1 m1_rx_clk_h;
258
  else m1_state <= #1 m1_next_state;
259 9 mihad
end
260
 
261
// State transition logic
262
always @(m1_state
263
         or q
264
         or tx_shifting_done
265
         or tx_write
266
         or ps2_clk_s
267
         or ps2_data_s
268
         or timer_60usec_done
269
         or timer_5usec_done
270
         )
271
begin : m1_state_logic
272
 
273
  // Output signals default to this value, unless changed in a state condition.
274 25 primozs
  ps2_clk_hi_z <= #1 1;
275
  ps2_data_hi_z <= #1 1;
276
  tx_error_no_ack <= #1 0;
277
  enable_timer_60usec <= #1 0;
278
  enable_timer_5usec <= #1 0;
279 9 mihad
 
280
  case (m1_state)
281
 
282
    m1_rx_clk_h :
283
      begin
284 25 primozs
        enable_timer_60usec <= #1 1;
285
        if (tx_write) m1_next_state <= #1 m1_tx_reset_timer;
286
        else if (~ps2_clk_s) m1_next_state <= #1 m1_rx_falling_edge_marker;
287
        else m1_next_state <= #1 m1_rx_clk_h;
288 9 mihad
      end
289
 
290
    m1_rx_falling_edge_marker :
291
      begin
292 25 primozs
        enable_timer_60usec <= #1 0;
293
        m1_next_state <= #1 m1_rx_clk_l;
294 9 mihad
      end
295
 
296
    m1_rx_rising_edge_marker :
297
      begin
298 25 primozs
        enable_timer_60usec <= #1 0;
299
        m1_next_state <= #1 m1_rx_clk_h;
300 9 mihad
      end
301
 
302
 
303
    m1_rx_clk_l :
304
      begin
305 25 primozs
        enable_timer_60usec <= #1 1;
306
        if (tx_write) m1_next_state <= #1 m1_tx_reset_timer;
307
        else if (ps2_clk_s) m1_next_state <= #1 m1_rx_rising_edge_marker;
308
        else m1_next_state <= #1 m1_rx_clk_l;
309 9 mihad
      end
310
 
311
    m1_tx_reset_timer:
312
      begin
313 25 primozs
        enable_timer_60usec <= #1 0;
314
        m1_next_state <= #1 m1_tx_force_clk_l;
315 9 mihad
      end
316
 
317
    m1_tx_force_clk_l :
318
      begin
319 25 primozs
        enable_timer_60usec <= #1 1;
320
        ps2_clk_hi_z <= #1 0;  // Force the ps2_clk line low.
321
        if (timer_60usec_done) m1_next_state <= #1 m1_tx_first_wait_clk_h;
322
        else m1_next_state <= #1 m1_tx_force_clk_l;
323 9 mihad
      end
324
 
325
    m1_tx_first_wait_clk_h :
326
      begin
327 25 primozs
        enable_timer_5usec <= #1 1;
328
        ps2_data_hi_z <= #1 0;        // Start bit.
329 9 mihad
        if (~ps2_clk_s && timer_5usec_done)
330 25 primozs
          m1_next_state <= #1 m1_tx_clk_l;
331 9 mihad
        else
332 25 primozs
          m1_next_state <= #1 m1_tx_first_wait_clk_h;
333 9 mihad
      end
334
 
335
    // This state must be included because the device might possibly
336
    // delay for up to 10 milliseconds before beginning its clock pulses.
337
    // During that waiting time, we cannot drive the data (q[0]) because it
338
    // is possibly 1, which would cause the keyboard to abort its receive
339
    // and the expected clocks would then never be generated.
340
    m1_tx_first_wait_clk_l :
341
      begin
342 25 primozs
        ps2_data_hi_z <= #1 0;
343
        if (~ps2_clk_s) m1_next_state <= #1 m1_tx_clk_l;
344
        else m1_next_state <= #1 m1_tx_first_wait_clk_l;
345 9 mihad
      end
346
 
347
    m1_tx_wait_clk_h :
348
      begin
349 25 primozs
        enable_timer_5usec <= #1 1;
350
        ps2_data_hi_z <= #1 q[0];
351 9 mihad
        if (ps2_clk_s && timer_5usec_done)
352 25 primozs
          m1_next_state <= #1 m1_tx_rising_edge_marker;
353 9 mihad
        else
354 25 primozs
          m1_next_state <= #1 m1_tx_wait_clk_h;
355 9 mihad
      end
356
 
357
    m1_tx_rising_edge_marker :
358
      begin
359 25 primozs
        ps2_data_hi_z <= #1 q[0];
360
        m1_next_state <= #1 m1_tx_clk_h;
361 9 mihad
      end
362
 
363
    m1_tx_clk_h :
364
      begin
365 25 primozs
        ps2_data_hi_z <= #1 q[0];
366
        if (tx_shifting_done) m1_next_state <= #1 m1_tx_wait_ack;
367
        else if (~ps2_clk_s) m1_next_state <= #1 m1_tx_clk_l;
368
        else m1_next_state <= #1 m1_tx_clk_h;
369 9 mihad
      end
370
 
371
    m1_tx_clk_l :
372
      begin
373 25 primozs
        ps2_data_hi_z <= #1 q[0];
374
        if (ps2_clk_s) m1_next_state <= #1 m1_tx_wait_clk_h;
375
        else m1_next_state <= #1 m1_tx_clk_l;
376 9 mihad
      end
377
 
378
    m1_tx_wait_ack :
379
      begin
380
        if (~ps2_clk_s && ps2_data_s)
381 25 primozs
          m1_next_state <= #1 m1_tx_error_no_ack;
382 9 mihad
        else if (~ps2_clk_s && ~ps2_data_s)
383 25 primozs
          m1_next_state <= #1 m1_tx_done_recovery;
384
        else m1_next_state <= #1 m1_tx_wait_ack;
385 9 mihad
      end
386
 
387
    m1_tx_done_recovery :
388
      begin
389 25 primozs
        if (ps2_clk_s && ps2_data_s) m1_next_state <= #1 m1_rx_clk_h;
390
        else m1_next_state <= #1 m1_tx_done_recovery;
391 9 mihad
      end
392
 
393
    m1_tx_error_no_ack :
394
      begin
395 25 primozs
        tx_error_no_ack <= #1 1;
396
        if (ps2_clk_s && ps2_data_s) m1_next_state <= #1 m1_rx_clk_h;
397
        else m1_next_state <= #1 m1_tx_error_no_ack;
398 9 mihad
      end
399
 
400 25 primozs
    default : m1_next_state <= #1 m1_rx_clk_h;
401 9 mihad
  endcase
402
end
403
 
404
// State register
405
always @(posedge clk)
406
begin : m2_state_register
407 25 primozs
  if (reset) m2_state <= #1 m2_rx_data_ready_ack;
408
  else m2_state <= #1 m2_next_state;
409 9 mihad
end
410
 
411
// State transition logic
412
always @(m2_state or rx_output_strobe or rx_read)
413
begin : m2_state_logic
414
  case (m2_state)
415
    m2_rx_data_ready_ack:
416
          begin
417 25 primozs
            rx_data_ready <= #1 1'b0;
418
            if (rx_output_strobe) m2_next_state <= #1 m2_rx_data_ready;
419
            else m2_next_state <= #1 m2_rx_data_ready_ack;
420 9 mihad
          end
421
    m2_rx_data_ready:
422
          begin
423 25 primozs
            rx_data_ready <= #1 1'b1;
424
            if (rx_read) m2_next_state <= #1 m2_rx_data_ready_ack;
425
            else m2_next_state <= #1 m2_rx_data_ready;
426 9 mihad
          end
427 25 primozs
    default : m2_next_state <= #1 m2_rx_data_ready_ack;
428 9 mihad
  endcase
429
end
430
 
431
// This is the bit counter
432
always @(posedge clk)
433
begin
434
  if (   reset
435
      || rx_shifting_done
436
      || (m1_state == m1_tx_wait_ack)        // After tx is done.
437 25 primozs
      ) bit_count <= #1 0;  // normal reset
438 9 mihad
  else if (timer_60usec_done
439
           && (m1_state == m1_rx_clk_h)
440
           && (ps2_clk_s)
441 25 primozs
      ) bit_count <= #1 0;  // rx watchdog timer reset
442 9 mihad
  else if ( (m1_state == m1_rx_falling_edge_marker)   // increment for rx
443
           ||(m1_state == m1_tx_rising_edge_marker)   // increment for tx
444
           )
445 25 primozs
    bit_count <= #1 bit_count + 1;
446 9 mihad
end
447
// This signal is high for one clock at the end of the timer count.
448
assign rx_shifting_done = (bit_count == `TOTAL_BITS);
449
assign tx_shifting_done = (bit_count == `TOTAL_BITS-1);
450
 
451
// This is the signal which enables loading of the shift register.
452
// It also indicates "ack" to the device writing to the transmitter.
453
assign tx_write_ack_o = (  (tx_write && (m1_state == m1_rx_clk_h))
454
                         ||(tx_write && (m1_state == m1_rx_clk_l))
455
                         );
456
 
457
// This is the ODD parity bit for the transmitted word.
458
assign tx_parity_bit = ~^tx_data;
459
 
460
// This is the shift register
461
always @(posedge clk)
462
begin
463 25 primozs
  if (reset) q <= #1 0;
464
  else if (tx_write_ack_o) q <= #1 {1'b1,tx_parity_bit,tx_data,1'b0};
465 9 mihad
  else if ( (m1_state == m1_rx_falling_edge_marker)
466
           ||(m1_state == m1_tx_rising_edge_marker) )
467 25 primozs
    q <= #1 {ps2_data_s,q[`TOTAL_BITS-1:1]};
468 9 mihad
end
469
 
470
// This is the 60usec timer counter
471
always @(posedge clk)
472
begin
473 25 primozs
  if (~enable_timer_60usec) timer_60usec_count <= #1 0;
474 24 primozs
  else if ( timer_done && !timer_60usec_done)
475 25 primozs
         timer_60usec_count<= #1 timer_60usec_count +1;
476 24 primozs
  end
477
assign timer_60usec_done = (timer_60usec_count == (TIMER_60USEC_VALUE_PP ));
478 9 mihad
 
479 24 primozs
 
480
 
481
always @(posedge clk or posedge reset)
482 25 primozs
if (reset) timer_5usec <= #1 1;
483
else if (!enable_timer_60usec) timer_5usec <= #1 1;
484 24 primozs
else if (timer_5usec == devide_reg_i)
485
 begin
486 25 primozs
   timer_5usec <= #1 1;
487
   timer_done  <= #1 1;
488 24 primozs
  end
489
else
490
  begin
491 25 primozs
    timer_5usec<= #1 timer_5usec +1;
492
    timer_done  <= #1 0;
493 24 primozs
 end
494
 
495 9 mihad
// This is the 5usec timer counter
496
always @(posedge clk)
497
begin
498 25 primozs
  if (~enable_timer_5usec) timer_5usec_count <= #1 0;
499
  else if (~timer_5usec_done) timer_5usec_count <= #1 timer_5usec_count + 1;
500 9 mihad
end
501 24 primozs
assign timer_5usec_done = (timer_5usec_count == devide_reg_i - 1);
502 9 mihad
 
503
always @(posedge clk)
504
begin
505
  if (reset)
506
  begin
507 25 primozs
    rx_scan_code <= #1 0;
508 9 mihad
  end
509
  else if (rx_output_strobe)
510
  begin
511 25 primozs
    rx_scan_code <= #1 q[8:1];
512 9 mihad
  end
513
end
514
 
515
// Store the final rx output data only when all extend and release codes
516
// are received and the next (actual key) scan code is also ready.
517
// (the presence of rx_extended or rx_released refers to the
518
// the current latest scan code received, not the previously latched flags.)
519
assign rx_output_event  = rx_shifting_done ;
520
 
521
assign rx_output_strobe = rx_shifting_done ;
522
 
523 24 primozs
endmodule

powered by: WebSVN 2.1.0

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