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

Subversion Repositories System09

[/] [System09/] [trunk/] [rtl/] [VHDL/] [ps2_keyboard.vhd] - Blame information for rev 159

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

Line No. Rev Author Line
1 99 davidgb
--===========================================================================--
2
--                                                                           --
3
--  ps2_keyboard.vhd - Synthesizable PS/2 Keyboard Interface                 --
4
--                                                                           --
5
--===========================================================================--
6 19 dilbert57
--
7 99 davidgb
--  File name      : ps2_keyboard.vhd
8 19 dilbert57
--
9 99 davidgb
--  Purpose        : Implements a PS/2 Keyboard Interface
10
--                  
11
--  Dependencies   : ieee.std_logic_1164
12
--                   ieee.std_logic_unsigned
13
--                   ieee.std_logic_arith
14
--                   ieee.numeric_std
15
--
16
--  Author         : Original Verilog version by John Clayton
17
--                   Converted to VHDL by John E. Kent
18
--
19
--  Email          : dilbert57@opencores.org      
20
--
21
--  Web            : http://opencores.org/project,system09
22
--
23
--  Description     :
24
--
25 19 dilbert57
-- This is a state-machine driven serial-to-parallel and parallel-to-serial
26
-- interface to the ps2 style keyboard interface.  The details of the operation
27
-- of the keyboard interface were obtained from the following website:
28
--
29 99 davidgb
--   http://www.beyondlogic.org/keyboard/keybrd.htm
30 19 dilbert57
--
31
-- Some aspects of the keyboard interface are not implemented (e.g, parity
32
-- checking for the receive side, and recognition of the various commands
33
-- which the keyboard sends out, such as "power on selt test passed," "Error"
34
-- and "Resend.")  However, if the user wishes to recognize these reply
35
-- messages, the scan code output can always be used to extend functionality
36
-- as desired.
37
--
38
-- Note that the "Extended" (0xE0) and "Released" (0xF0) codes are recognized.
39
-- The rx interface provides separate indicator flags for these two conditions
40
-- with every valid character scan code which it provides.  The shift keys are
41
-- also trapped by the interface, in order to provide correct uppercase ASCII
42
-- characters at the ascii output, although the scan codes for the shift keys
43
-- are still provided at the scan code output.  So, the left/right ALT keys
44
-- can be differentiated by the presence of the rx_entended signal, while the
45
-- left/right shift keys are differentiable by the different scan codes
46
-- received.
47
--
48
-- The interface to the ps2 keyboard uses ps2_clk clock rates of
49
-- 30-40 kHz, dependent upon the keyboard itself.  The rate at which the state
50
-- machine runs should be at least twice the rate of the ps2_clk, so that the
51
-- states can accurately follow the clock signal itself.  Four times 
52
-- oversampling is better.  Say 200kHz at least.  The upper limit for clocking
53
-- the state machine will undoubtedly be determined by delays in the logic 
54
-- which decodes the scan codes into ASCII equivalents.  The maximum speed
55
-- will be most likely many megahertz, depending upon target technology.
56
-- In order to run the state machine extremely fast, synchronizing flip-flops
57
-- have been added to the ps2_clk and ps2_data inputs of the state machine.
58
-- This avoids poor performance related to slow transitions of the inputs.
59
-- 
60
-- Because this is a bi-directional interface, while reading from the keyboard
61
-- the ps2_clk and ps2_data lines are used as inputs.  While writing to the
62
-- keyboard, however (which may be done at any time.  If writing interrupts a
63
-- read from the keyboard, the keyboard will buffer up its data, and send
64
-- it later) both the ps2_clk and ps2_data lines are occasionally pulled low,
65
-- and pullup resistors are used to bring the lines high again, by setting
66
-- the drivers to high impedance state.
67
--
68
-- The tx interface, for writing to the keyboard, does not provide any special
69
-- pre-processing.  It simply transmits the 8-bit command value to the
70
-- keyboard.
71
--
72
-- Pullups MUST BE USED on the ps2_clk and ps2_data lines for this design,
73
-- whether they be internal to an FPGA I/O pad, or externally placed.
74
-- If internal pullups are used, they may be fairly weak, causing bounces
75
-- due to crosstalk, etc.  There is a "debounce timer" implemented in order
76
-- to eliminate erroneous state transitions which would occur based on bounce.
77
-- 
78
-- Parameters are provided in order to configure and appropriately size the
79
-- counter of a 60 microsecond timer used in the transmitter, depending on
80
-- the clock frequency used.  The 60 microsecond period is guaranteed to be
81
-- more than one period of the ps2_clk_s signal.
82
--
83
-- Also, a smaller 5 microsecond timer has been included for "debounce".
84
-- This is used because, with internal pullups on the ps2_clk and ps2_data
85
-- lines, there is some bouncing around which occurs
86
--
87
-- A parameter TRAP_SHIFT_KEYS allows the user to eliminate shift keypresses
88
-- from producing scan codes (along with their "undefined" ASCII equivalents)
89
-- at the output of the interface.  If TRAP_SHIFT_KEYS is non-zero, the shift
90 99 davidgb
-- key status will only be reported by rx_shift_on.  No ascii or scan
91 19 dilbert57
-- codes will be reported for the shift keys.  This is useful for those who
92
-- wish to use the ASCII data stream, and who don't want to have to "filter
93
-- out" the shift key codes.
94 99 davidgb
-- 
95
--  Copyright (C) 2001 - 2010 John Clayton and John Kent
96
--
97
--  This program is free software: you can redistribute it and/or modify
98
--  it under the terms of the GNU General Public License as published by
99
--  the Free Software Foundation, either version 3 of the License, or
100
--  (at your option) any later version.
101
--
102
--  This program is distributed in the hope that it will be useful,
103
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
104
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
105
--  GNU General Public License for more details.
106
--
107
--  You should have received a copy of the GNU General Public License
108
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
109
--
110
--===========================================================================--
111
--                                                                           --
112
--                              Revision  History                            --
113
--                                                                           --
114
--===========================================================================--
115
--
116
-- Author: John Clayton
117
-- 2001-04-30 copied this file from lcd_2.v (pared down).
118
-- 2001-05-24 changed the first module from "ps2_keyboard_receiver"
119
--            to "ps2_keyboard_interface"
120
-- 2001-05-29 Added input synchronizing flip-flops.  Changed state
121
--            encoding (m1) for good operation after part config.
122
-- 2001-05-31 Added low drive strength and slow transitions to ps2_clk
123
--            and ps2_data in the constraints file.  Added the signal
124
--            "tx_shifting_done" as distinguished from "rx_shifting_done."
125
--            Debugged the transmitter portion in the lab.
126
-- 2001-06-01 Added horizontal tab to the ascii output.
127
-- 2001-06-01 Added parameter TRAP_SHIFT_KEYS.
128
-- 2001-06-05 Debugged the "debounce" timer functionality.
129
--            Used 60usec timer as a "watchdog" timeout during
130
--            receive from the keyboard.  This means that a keyboard
131
--            can now be "hot plugged" into the interface, without
132
--            messing up the bit_count, since the bit_count is reset
133
--            to zero during periods of inactivity anyway.  This was
134
--            difficult to debug.  I ended up using the logic analyzer,
135
--            and had to scratch my head quite a bit.
136
-- 2001-06-06 Removed extra comments before the input synchronizing
137
--            flip-flops.  Used the correct parameter to size the
138
--            5usec_timer_count.  Changed the name of this file from
139
--            ps2.v to ps2_keyboard.v
140
-- 2001-06/06 Removed "&& q[7:0]" in output_strobe logic.  Removed extra
141
--            commented out "else" condition in the shift register and
142
--            bit counter.
143
-- 2001-06-07 Changed default values for 60usec timer parameters so that
144
--            they correspond to 60usec for a 49.152MHz clock.
145
--
146
-- Author: John Kent
147 118 dilbert57
--2001-02-10 Converted to VHDL
148 99 davidgb
-- 2004-09-11 Added ctrl key 
149
--            Changed undefined key codes to x"ff"
150
--            Reversed clock polarity
151
-- 2004-10-18 Added ctrl keys to ASCII ROM
152
--            Added CAPS Lock toggle.
153
-- 2007-02-06 Added Generic Clock parameter
154
-- 2010-05-31 Revised header, added GPL
155
-- 2010-06-17 Change some signal names for consistancy
156 118 dilbert57
-- 2010-10-24 Rearranged code to prevent shift key outputting characters
157 19 dilbert57
--
158
--
159
---------------------------------------------------------------------------------------
160
 
161
library ieee;
162
   use ieee.std_logic_1164.all;
163 99 davidgb
   use ieee.std_logic_arith.all;
164
   use ieee.std_logic_unsigned.all;
165 19 dilbert57
   use ieee.numeric_std.all;
166 118 dilbert57
--library unisim;
167
--   use unisim.vcomponents.all;
168 19 dilbert57
 
169 99 davidgb
entity ps2_keyboard is
170 19 dilbert57
  generic (
171 99 davidgb
  CLK_FREQ_MHZ   : integer
172 19 dilbert57
  );
173
 
174
  port(
175
  clk             : in    std_logic;
176
  reset           : in    std_logic;
177 99 davidgb
  rx_data         : out   std_logic_vector(7 downto 0);
178
  rx_read         : in    std_logic;
179
  rx_data_ready   : out   std_logic;
180 19 dilbert57
  rx_extended     : out   std_logic;
181
  rx_released     : out   std_logic;
182 99 davidgb
  rx_shift_on     : out   std_logic;
183 19 dilbert57
  tx_data         : in    std_logic_vector(7 downto 0);
184
  tx_write        : in    std_logic;
185 99 davidgb
  tx_data_empty   : out   std_logic;
186
  tx_error            : out   std_logic;
187
  ps2_clk         : inout std_logic;
188
  ps2_data        : inout std_logic
189 19 dilbert57
  );
190 99 davidgb
end ps2_keyboard;
191 19 dilbert57
 
192
-------------------------------------------------------------------------------
193
-- Architecture for ps2 keyboard interface
194
-------------------------------------------------------------------------------
195 99 davidgb
architecture rtl of ps2_keyboard is
196 19 dilbert57
  -----------------------------------------------------------------------------
197
 
198
 
199
constant TOTAL_BITS   : integer := 11;
200
constant EXTEND_CODE  : integer := 16#E0#;
201
constant RELEASE_CODE : integer := 16#F0#;
202
constant LEFT_SHIFT   : integer := 16#12#;
203
constant RIGHT_SHIFT  : integer := 16#59#;
204
constant CTRL_CODE    : integer := 16#14#;
205 118 dilbert57
constant LEFT_ALT     : integer := 16#11#;
206
constant CAPS_CODE    : integer := 16#58#;
207
constant SCROLL_LOCK  : integer := 16#7E#;
208
constant NUM_LOCK     : integer := 16#77#;
209 19 dilbert57
 
210
 
211
-- constants
212
 
213
-- The timer value can be up to (2^bits) inclusive.
214
-- Values for 49.152 MHz clock
215
--constant TIMER_60USEC_VALUE_PP : integer := 2950; -- Number of sys_clks for 60usec.
216
--constant TIMER_60USEC_BITS_PP  : integer := 12;   -- Number of bits needed for timer
217
--constant TIMER_5USEC_VALUE_PP  : integer := 186;   -- Number of sys_clks for debounce
218
--constant TIMER_5USEC_BITS_PP   : integer := 8;     -- Number of bits needed for timer
219
 
220
-- Values for  12.5 MHz Clock
221
--constant TIMER_60USEC_VALUE_PP : integer := 750; -- Number of sys_clks for 60usec.
222
--constant TIMER_60USEC_BITS_PP  : integer := 10;  -- Number of bits needed for timer
223
--constant TIMER_5USEC_VALUE_PP  : integer := 62;  -- Number of sys_clks for debounce
224
--constant TIMER_5USEC_BITS_PP   : integer := 6;   -- Number of bits needed for timer
225
 
226
-- Values for  25 MHz Clock
227
--constant TIMER_60USEC_VALUE_PP : integer := 1500; -- Number of sys_clks for 60usec.
228
--constant TIMER_60USEC_BITS_PP  : integer := 11;   -- Number of bits needed for timer
229
--constant TIMER_5USEC_VALUE_PP  : integer := 125;  -- Number of sys_clks for debounce
230
--constant TIMER_5USEC_BITS_PP   : integer := 7;    -- Number of bits needed for timer
231
 
232
-- Values for  generic Clock up to 50 MHz
233 118 dilbert57
constant TIMER_60USEC_VALUE_PP : integer := CLK_FREQ_MHZ * 60;  -- Number of clock cycles for 60usec.
234 19 dilbert57
constant TIMER_60USEC_BITS_PP  : integer := 12;                 -- Number of bits needed for timer
235 118 dilbert57
constant TIMER_5USEC_VALUE_PP  : integer := CLK_FREQ_MHZ * 5;   -- Number of clock cycles for debounce
236 19 dilbert57
constant TIMER_5USEC_BITS_PP   : integer := 8;                  -- Number of bits needed for timer
237
 
238
constant TRAP_SHIFT_KEYS_PP    : integer := 1;   -- Default: No shift key trap.
239
 
240
-- State encodings, provided as constants
241
-- for flexibility to the one instantiating the module.
242
-- In general, the default values need not be changed.
243
 
244
-- State "m1_rx_clk_l" has been chosen on purpose.  Since the input
245
-- synchronizing flip-flops initially contain zero, it takes one clk
246
-- for them to update to reflect the actual (idle = high) status of
247
-- the I/O lines from the keyboard.  Therefore, choosing 0 for m1_rx_clk_l
248
-- allows the state machine to transition to m1_rx_clk_h when the true
249
-- values of the input signals become present at the outputs of the
250
-- synchronizing flip-flops.  This initial transition is harmless, and it
251
-- eliminates the need for a "reset" pulse before the interface can operate.
252
 
253
type m1_type is ( m1_rx_clk_h, m1_rx_clk_l,
254
                  m1_tx_wait_clk_h, m1_tx_force_clk_l,
255
                                                m1_tx_clk_h, m1_tx_clk_l,
256
                                                m1_tx_wait_keyboard_ack, m1_tx_done_recovery,
257 99 davidgb
                                                m1_tx_error, m1_tx_rising_edge_marker,
258 19 dilbert57
                                                m1_tx_first_wait_clk_h, m1_tx_first_wait_clk_l, m1_tx_reset_timer,
259
                  m1_rx_falling_edge_marker, m1_rx_rising_edge_marker );
260
 
261
-- Internal signal declarations
262
signal timer_60usec_done  : std_logic;
263
signal timer_5usec_done   : std_logic;
264
signal extended           : std_logic;
265
signal released           : std_logic;
266
signal shift_key_on       : std_logic;
267
signal ctrl_key_on        : std_logic;
268
signal caps_key_on        : std_logic;
269
 
270
                         -- NOTE: These two signals used to be one.  They
271
                         --       were split into two signals because of
272
                         --       shift key trapping.  With shift key
273
                         --       trapping, no event is generated externally,
274
                         --       but the "hold" data must still be cleared
275
                         --       anyway regardless, in preparation for the
276
                         --       next scan codes.
277
signal rx_output_event    : std_logic;    -- Used only to clear: hold_released, hold_extended
278
signal rx_output_strobe   : std_logic;   -- Used to produce the actual output.
279
 
280
signal tx_parity_bit      : std_logic;
281
signal rx_shifting_done   : std_logic;
282
signal tx_shifting_done   : std_logic;
283
signal shift_key_plus_code: std_logic_vector(8 downto 0);
284
 
285
signal q                  : std_logic_vector(TOTAL_BITS-1 downto 0);
286
signal m1_state           : m1_type;
287
signal m1_next_state      : m1_type;
288
signal bit_count          : std_logic_vector(3 downto 0);
289
signal enable_timer_60usec: std_logic;
290
signal enable_timer_5usec : std_logic;
291
signal timer_60usec_count : std_logic_vector(TIMER_60USEC_BITS_PP-1 downto 0);
292
signal timer_5usec_count  : std_logic_vector(TIMER_5USEC_BITS_PP-1 downto 0);
293
signal ascii              : std_logic_vector(7 downto 0);      -- "REG" type only because a case statement is used.
294
signal left_shift_key     : std_logic;
295
signal right_shift_key    : std_logic;
296
signal hold_extended      : std_logic;  -- Holds prior value, cleared at rx_output_strobe
297
signal hold_released      : std_logic;  -- Holds prior value, cleared at rx_output_strobe
298
signal ps2_clk_s          : std_logic;  -- Synchronous version of this input
299
signal ps2_data_s         : std_logic;  -- Synchronous version of this input
300
signal ps2_clk_hi_z       : std_logic;  -- Without keyboard, high Z equals 1 due to pullups.
301
signal ps2_data_hi_z      : std_logic;  -- Without keyboard, high Z equals 1 due to pullups.
302 99 davidgb
signal tx_data_empty_o    : std_logic;
303 19 dilbert57
 
304
--
305
-- key lookup table
306
--
307
component keymap_rom
308
    Port (
309 99 davidgb
    clk      : in  std_logic;
310
    rst      : in  std_logic;
311
    cs       : in  std_logic;
312
    rw       : in  std_logic;
313
    addr     : in  std_logic_vector (8 downto 0);
314
    data_in  : in  std_logic_vector (7 downto 0);
315
    data_out : out std_logic_vector (7 downto 0)
316 19 dilbert57
    );
317
end component;
318
 
319
begin
320
 
321
my_key_map : keymap_rom
322
     Port map (
323 99 davidgb
          clk      => clk,
324
          rst      => reset,
325
          cs       => '1',
326
          rw       => '1',
327
          addr     => shift_key_plus_code,
328
          data_in  => "00000000",
329
          data_out => ascii
330 19 dilbert57
          );
331
 
332
----------------------------------------------------------------------------
333
-- Module code
334
-- assign ps2_clk = ps2_clk_hi_z?1'bZ:1'b0;
335
-- assign ps2_data = ps2_data_hi_z?1'bZ:1'b0;
336
--
337
ps2_direction : process( ps2_clk_hi_z, ps2_data_hi_z )
338
begin
339 99 davidgb
 
340 19 dilbert57
  if( ps2_clk_hi_z = '1' ) then
341
     ps2_clk <= 'Z';
342
  else
343
     ps2_clk <= '0';
344 99 davidgb
  end if;
345
 
346 19 dilbert57
  if( ps2_data_hi_z = '1' ) then
347
     ps2_data <= 'Z';
348
  else
349
     ps2_data <= '0';
350 99 davidgb
  end if;
351
 
352 19 dilbert57
end process;
353
 
354
-- Input "synchronizing" logic -- synchronizes the inputs to the state
355
-- machine clock, thus avoiding errors related to
356
-- spurious state machine transitions.
357
ps2_synch : process(clk, ps2_clk, ps2_data)
358
begin
359
  if clk'event and clk='0' then
360
    ps2_clk_s <= ps2_clk;
361
    ps2_data_s <= ps2_data;
362
  end if;
363
end process;
364
 
365
-- State register
366
m1_state_register : process( clk, reset, m1_state )
367
begin
368
  if clk'event and clk='0' then
369
    if (reset = '1') then
370
            m1_state <= m1_rx_clk_h;
371
    else
372
            m1_state <= m1_next_state;
373
    end if;
374
  end if;
375
end process;
376
 
377
m1_state_logic : process( m1_state, q,
378
                          tx_shifting_done, tx_write,
379 118 dilbert57
                                                  ps2_clk_s, ps2_data_s,
380
                                                  timer_60usec_done, timer_5usec_done )
381 19 dilbert57
begin
382
  -- Output signals default to this value, unless changed in a state condition.
383 118 dilbert57
        ps2_clk_hi_z             <= '1';
384
        ps2_data_hi_z            <= '1';
385
        tx_error                 <= '0';
386
        enable_timer_60usec      <= '0';
387
        enable_timer_5usec       <= '0';
388 19 dilbert57
 
389 118 dilbert57
        case (m1_state) is
390
        --
391
        -- receive clock transitions
392
        --
393
        when m1_rx_clk_h =>
394
                enable_timer_60usec <= '1';
395
                if (tx_write = '1') then
396
                        m1_next_state <= m1_tx_reset_timer;
397
                elsif (ps2_clk_s = '0') then
398
                        m1_next_state <= m1_rx_falling_edge_marker;
399
                else
400
                        m1_next_state <= m1_rx_clk_h;
401 19 dilbert57
        end if;
402
 
403 118 dilbert57
        when m1_rx_falling_edge_marker =>
404
                enable_timer_60usec <= '0';
405
                m1_next_state <= m1_rx_clk_l;
406 19 dilbert57
 
407 118 dilbert57
        when m1_rx_clk_l =>
408
                enable_timer_60usec <= '1';
409
                if (tx_write = '1') then
410
                        m1_next_state <= m1_tx_reset_timer;
411
                elsif (ps2_clk_s = '1') then
412
                        m1_next_state <= m1_rx_rising_edge_marker;
413
                else
414
                        m1_next_state <= m1_rx_clk_l;
415
                end if;
416
 
417
        when m1_rx_rising_edge_marker =>
418
                enable_timer_60usec <= '0';
419
                m1_next_state <= m1_rx_clk_h;
420
 
421
        --
422
        -- write to keyboard (Tx)
423
        --
424
        when m1_tx_reset_timer =>
425
                enable_timer_60usec <= '0';
426
                m1_next_state <= m1_tx_force_clk_l;
427 19 dilbert57
 
428 118 dilbert57
        when m1_tx_force_clk_l =>
429
                enable_timer_60usec <= '1';
430
                ps2_clk_hi_z <= '0';  -- Force the ps2_clk line low.
431
                if (timer_60usec_done = '1') then
432
                        m1_next_state <= m1_tx_first_wait_clk_h;
433
                else
434
                        m1_next_state <= m1_tx_force_clk_l;
435
                end if;
436 19 dilbert57
 
437 118 dilbert57
        when m1_tx_first_wait_clk_h =>
438
                enable_timer_5usec <= '1';
439
                ps2_data_hi_z <= '0';        -- Start bit.
440
                if (ps2_clk_s = '0') and (timer_5usec_done = '1') then
441
                        m1_next_state <= m1_tx_clk_l;
442
                else
443
                        m1_next_state <= m1_tx_first_wait_clk_h;
444
                end if;
445 19 dilbert57
 
446
    -- This state must be included because the device might possibly
447
    -- delay for up to 10 milliseconds before beginning its clock pulses.
448
    -- During that waiting time, we cannot drive the data (q[0]) because it
449
    -- is possibly 1, which would cause the keyboard to abort its receive
450
    -- and the expected clocks would then never be generated.
451 118 dilbert57
        when m1_tx_first_wait_clk_l =>
452
                ps2_data_hi_z <= '0';
453
                if (ps2_clk_s = '0') then
454
                        m1_next_state <= m1_tx_clk_l;
455
                else
456
                        m1_next_state <= m1_tx_first_wait_clk_l;
457
                end if;
458 19 dilbert57
 
459 118 dilbert57
        when m1_tx_wait_clk_h =>
460
                enable_timer_5usec <= '1';
461
                ps2_data_hi_z <= q(0);
462
                if (ps2_clk_s = '1') and (timer_5usec_done = '1') then
463
                        m1_next_state <= m1_tx_rising_edge_marker;
464
                else
465
                        m1_next_state <= m1_tx_wait_clk_h;
466
                end if;
467 19 dilbert57
 
468 118 dilbert57
        when m1_tx_rising_edge_marker =>
469
                ps2_data_hi_z <= q(0);
470
                m1_next_state <= m1_tx_clk_h;
471 19 dilbert57
 
472 118 dilbert57
        when m1_tx_clk_h =>
473
                ps2_data_hi_z <= q(0);
474
                if (tx_shifting_done = '1') then
475
                        m1_next_state <= m1_tx_wait_keyboard_ack;
476
                elsif (ps2_clk_s = '0') then
477
                        m1_next_state <= m1_tx_clk_l;
478
                else
479
                        m1_next_state <= m1_tx_clk_h;
480
                end if;
481 19 dilbert57
 
482 118 dilbert57
        when m1_tx_clk_l =>
483
                ps2_data_hi_z <= q(0);
484
                if (ps2_clk_s = '1') then
485
                        m1_next_state <= m1_tx_wait_clk_h;
486
                else
487
                        m1_next_state <= m1_tx_clk_l;
488
                end if;
489 19 dilbert57
 
490 118 dilbert57
        when m1_tx_wait_keyboard_ack =>
491
                if (ps2_clk_s = '0') and (ps2_data_s = '1') then
492
                        m1_next_state <= m1_tx_error;
493
                elsif (ps2_clk_s = '0') and (ps2_data_s = '0') then
494
                        m1_next_state <= m1_tx_done_recovery;
495
                else
496
                        m1_next_state <= m1_tx_wait_keyboard_ack;
497
                end if;
498 19 dilbert57
 
499 118 dilbert57
        when m1_tx_done_recovery =>
500
                if (ps2_clk_s = '1') and (ps2_data_s = '1') then
501
                        m1_next_state <= m1_rx_clk_h;
502
                else
503
                        m1_next_state <= m1_tx_done_recovery;
504
                end if;
505 19 dilbert57
 
506 118 dilbert57
        when m1_tx_error =>
507
                tx_error <= '1';
508
                if (ps2_clk_s = '1') and (ps2_data_s ='1') then
509
                        m1_next_state <= m1_rx_clk_h;
510
                else
511
                        m1_next_state <= m1_tx_error;
512
                end if;
513 19 dilbert57
 
514 118 dilbert57
        when others =>
515
                m1_next_state <= m1_rx_clk_h;
516
        end case;
517 19 dilbert57
end process;
518 118 dilbert57
 
519
--
520 19 dilbert57
-- This is the bit counter
521 118 dilbert57
--
522 19 dilbert57
bit_counter: process(clk, reset, m1_state, bit_count )
523
begin
524 118 dilbert57
        if clk'event and clk = '0' then
525
                if ( reset = '1' ) or ( rx_shifting_done = '1' ) or
526
                        (m1_state = m1_tx_wait_keyboard_ack) then       -- After tx is done.
527
                        bit_count <= "0000";  -- normal reset
528
                elsif (timer_60usec_done = '1' ) and
529
                        ( m1_state = m1_rx_clk_h )      and
530
                        ( ps2_clk_s = '1' ) then
531
                        bit_count <= "0000";  -- rx watchdog timer reset
532
                elsif ( m1_state = m1_rx_falling_edge_marker ) or  -- increment for rx
533
                        (m1_state = m1_tx_rising_edge_marker) then  -- increment for tx
534
                        bit_count <= bit_count + 1;
535
                end if;
536
        end if;
537
 
538
        if (bit_count = TOTAL_BITS) then
539
                rx_shifting_done <= '1';
540
        else
541
                rx_shifting_done <= '0';
542
        end if;
543
 
544
        if (bit_count = (TOTAL_BITS-1)) then
545
                tx_shifting_done <= '1';
546
        else
547
                tx_shifting_done <= '0';
548
        end if;
549 19 dilbert57
end process;
550
 
551 118 dilbert57
assign: process( bit_count, tx_write, m1_state, tx_data_empty_o, m1_state )
552 19 dilbert57
begin
553 118 dilbert57
        --
554
        -- This is the signal which enables loading of the shift register.
555
        -- It also indicates "ack" to the device writing to the transmitter.
556
        --
557
        if      ((tx_write = '1') and (m1_state = m1_rx_clk_h)) or
558
                ((tx_write = '1') and (m1_state = m1_rx_clk_l)) then
559
                tx_data_empty_o <= '1';
560
        else
561
                tx_data_empty_o <= '0';
562
        end if;
563
        tx_data_empty <= tx_data_empty_o;
564 19 dilbert57
end process;
565
 
566
-- This is the shift register
567 99 davidgb
q_shift : process(clk, tx_data_empty_o, tx_parity_bit, tx_data,
568 19 dilbert57
                  m1_state, q, ps2_data_s, rx_shifting_done )
569
begin
570 118 dilbert57
        --
571
        -- This is the ODD parity bit for the transmitted word.
572
        -- assign tx_parity_bit = ~^tx_data;
573
        --
574
        tx_parity_bit <= not( tx_data(7) xor tx_data(6) xor tx_data(5) xor tx_data(4) xor
575
                                                tx_data(3) xor tx_data(2) xor tx_data(1) xor tx_data(0) );
576
 
577
        if clk'event and clk='0' then
578
                if (reset = '1') then
579
                        q <= (others=>'0');
580
                elsif (tx_data_empty_o = '1') then
581
                        q <= "1" & tx_parity_bit & tx_data & "0";
582
                elsif ( (m1_state = m1_rx_falling_edge_marker)  or
583
                        (m1_state = m1_tx_rising_edge_marker) ) then
584
                        q <= ps2_data_s & q((TOTAL_BITS-1) downto 1);
585
                end if;
586
        end if;
587
 
588 19 dilbert57
end process;
589 118 dilbert57
 
590
--
591 19 dilbert57
-- This is the 60usec timer counter
592 118 dilbert57
--
593 19 dilbert57
timer60usec: process(clk, enable_timer_60usec, timer_60usec_count)
594
begin
595 118 dilbert57
        if clk'event and clk = '0' then
596
                if (enable_timer_60usec = '0') then
597
                        timer_60usec_count <= (others => '0');
598
                elsif (timer_60usec_done = '0') then
599
                        timer_60usec_count <= timer_60usec_count + 1;
600
                end if;
601
        end if;
602 19 dilbert57
 
603 118 dilbert57
        if (timer_60usec_count = (TIMER_60USEC_VALUE_PP - 1)) then
604
                timer_60usec_done <= '1';
605
        else
606
                timer_60usec_done <= '0';
607
        end if;
608 19 dilbert57
end process;
609 118 dilbert57
 
610
--
611 19 dilbert57
-- This is the 5usec timer counter
612 118 dilbert57
--
613 19 dilbert57
timer5usec : process(clk, enable_timer_5usec, timer_5usec_count )
614
begin
615 118 dilbert57
        if clk'event and clk = '0' then
616
                if (enable_timer_5usec = '0') then
617
                        timer_5usec_count <= (others => '0');
618
                elsif (timer_5usec_done = '0') then
619
                        timer_5usec_count <= timer_5usec_count + 1;
620
                end if;
621
        end if;
622 19 dilbert57
 
623 118 dilbert57
        if( timer_5usec_count = (TIMER_5USEC_VALUE_PP - 1)) then
624
                timer_5usec_done <= '1';
625
        else
626
                timer_5usec_done <= '0';
627
        end if;
628 19 dilbert57
end process;
629 118 dilbert57
 
630
--
631
-- Create the signals which indicate special scan codes received.
632
-- These are the "unlatched versions."
633
--
634
extend_release_decode : process( q, rx_shifting_done, extended, released )
635
begin
636
        if (q(8 downto 1) = EXTEND_CODE) and (rx_shifting_done = '1') then
637
                extended <= '1';
638
        else
639
                extended <= '0';
640
        end if;
641
 
642
        if (q(8 downto 1) = RELEASE_CODE) and (rx_shifting_done = '1') then
643
                released <= '1';
644
        else
645
                released <= '0';
646
        end if;
647
 
648
        if (rx_shifting_done = '1') and (extended = '0') and (released = '0') then
649
                rx_output_event <= '1';
650
        else
651
                rx_output_event <= '0';
652
        end if;
653
 
654
end process;
655 19 dilbert57
 
656 118 dilbert57
--
657 19 dilbert57
-- Store the special scan code status bits
658
-- Not the final output, but an intermediate storage place,
659
-- until the entire set of output data can be assembled.
660 118 dilbert57
--
661 19 dilbert57
special_scan : process(clk, reset, rx_output_event, rx_shifting_done, extended, released )
662
begin
663 118 dilbert57
        if clk'event and clk='0' then
664
                if (reset = '1') or (rx_output_event = '1')     then
665
                        hold_extended <= '0';
666
                        hold_released <= '0';
667
                else
668
                        if (rx_shifting_done = '1') and (extended = '1') then
669
                                hold_extended <= '1';
670
                        end if;
671
                        if (rx_shifting_done = '1') and (released = '1') then
672
                                hold_released <= '1';
673
                        end if;
674
                end     if;
675
        end if;
676
 
677 19 dilbert57
end process;
678
 
679 118 dilbert57
--
680
-- convert scan code to ascii code
681
--
682
scan_to_ascii : process( shift_key_on, caps_key_on, q )
683
begin
684
        shift_key_plus_code <= shift_key_on & caps_key_on & q(7 downto 1);
685
end process;
686
 
687
--
688 19 dilbert57
-- These bits contain the status of the two shift keys
689 118 dilbert57
--
690 19 dilbert57
left_shift_proc : process(clk, reset, q, rx_shifting_done, hold_released )
691
begin
692 118 dilbert57
        if clk'event and clk = '0' then
693
                if (reset = '1') then
694
                        left_shift_key <= '0';
695
                elsif (q(8 downto 1) = LEFT_SHIFT) and (rx_shifting_done = '1') then
696
                        left_shift_key <= not hold_released;
697
                end if;
698
        end if;
699 19 dilbert57
end process;
700
 
701
right_shift_proc : process(clk, reset, q, rx_shifting_done, hold_released )
702
begin
703 118 dilbert57
        if clk'event and clk = '0' then
704
                if (reset = '1') then
705
                        right_shift_key <= '0';
706
                elsif (q(8 downto 1) = RIGHT_SHIFT) and (rx_shifting_done = '1') then
707
                        right_shift_key <= not hold_released;
708
                end if;
709
        end if;
710 19 dilbert57
end process;
711 118 dilbert57
 
712
shift_proc : process( left_shift_key, right_shift_key, shift_key_on, caps_key_on, q )
713
begin
714
        shift_key_on <= left_shift_key or right_shift_key;
715
        rx_shift_on  <= shift_key_on;
716
end process;
717 19 dilbert57
 
718
--
719
-- Control keys
720
--
721
ctrl_proc : process(clk, reset, q, rx_shifting_done, hold_released )
722
begin
723 118 dilbert57
        if clk'event and clk = '0' then
724
                if (reset = '1') then
725
                        ctrl_key_on <= '0';
726
                elsif (q(8 downto 1) = CTRL_CODE) and (rx_shifting_done = '1') then
727
                        ctrl_key_on <= not hold_released;
728
                end if;
729
        end if;
730 19 dilbert57
end process;
731
 
732
--
733
-- Caps lock
734
--
735
caps_proc : process(clk, reset, q, rx_shifting_done, hold_released, caps_key_on )
736
begin
737 118 dilbert57
        if clk'event and clk = '0' then
738
                if (reset = '1') then
739
                        caps_key_on <= '0';
740
                elsif (q(8 downto 1) = CAPS_CODE) and (rx_shifting_done = '1') then
741
                        if (hold_released = '0') then
742
                                caps_key_on <= not caps_key_on;
743
                        end if;
744
                end if;
745
        end if;
746 19 dilbert57
end process;
747 118 dilbert57
 
748
--
749 19 dilbert57
-- Output the special scan code flags, the scan code and the ascii
750 118 dilbert57
--
751
special_scan_proc : process(clk, reset, rx_output_strobe,
752
                                                        hold_extended, hold_released,
753
                                                        ascii, ctrl_key_on )
754 19 dilbert57
begin
755 118 dilbert57
        if clk'event and clk = '0' then
756
                if (reset = '1')        then
757
                        rx_extended <= '0';
758
                        rx_released <= '0';
759
                        rx_data <= (others=>'0');
760
                elsif (rx_output_strobe = '1') then
761
                        rx_extended <= hold_extended;
762
                        rx_released <= hold_released;
763
                        if ctrl_key_on = '1' then
764
                                rx_data <= ascii and x"1f";
765
                        else
766
                                rx_data <= ascii;
767
                        end if;
768
                end if;
769
        end if;
770 19 dilbert57
end process;
771 118 dilbert57
 
772
--
773 19 dilbert57
-- Store the final rx output data only when all extend and release codes
774
-- are received and the next (actual key) scan code is also ready.
775
-- (the presence of rx_extended or rx_released refers to the
776
-- the current latest scan code received, not the previously latched flags.)
777 118 dilbert57
--
778 19 dilbert57
rx_output_proc : process( clk, reset,
779
                          rx_shifting_done, rx_output_strobe,
780 118 dilbert57
                          extended, released,
781
                          hold_extended, hold_released,
782
                                                  q, ascii, rx_read )
783
begin
784
        if clk'event and clk = '0' then
785
                if reset = '1' then
786
                        rx_output_strobe <= '0';
787
                elsif (rx_shifting_done = '1') and (rx_output_strobe = '0') and
788
                        (extended = '0') and (released = '0') and
789
                        (hold_released = '0' ) and
790
                        (ascii /= "00000000" ) then
791
--              ((TRAP_SHIFT_KEYS_PP = 0) or
792
--              ( (q(8 downto 1) /= RIGHT_SHIFT) and 
793
--                (q(8 downto 1) /= LEFT_SHIFT) and
794
--                (q(8 downto 1) /= CTRL_CODE) ) )then
795
                        rx_output_strobe <= '1';
796
                elsif rx_read = '1' then
797
                        rx_output_strobe <= '0';
798
                end if;
799
        end if;
800
        rx_data_ready <= rx_output_strobe;
801 19 dilbert57
end process;
802 118 dilbert57
 
803
--
804
-- This part translates the scan code into an ASCII value...   
805
-- Only the ASCII codes which I considered important have been included.
806
-- if you want more, just add the appropriate case statement lines...   
807
-- (You will need to know the keyboard scan codes you wish to assign.)
808
-- The entries are listed in ascending order of ASCII value.   
809
--
810 19 dilbert57
--shift_map : process( shift_key_plus_code )
811
--begin
812
--  case shift_key_plus_code is
813
--  when x"066" => ascii <= x"08";  -- Backspace ("backspace" key)
814
--  when x"166" => ascii <= x"08";  -- Backspace ("backspace" key)
815
--  when x"00d" => ascii <= x"09";  -- Horizontal Tab
816
--  when x"10d" => ascii <= x"09";  -- Horizontal Tab
817
--  when x"05a" => ascii <= x"0d";  -- Carriage return ("enter" key)
818
--  when x"15a" => ascii <= x"0d";  -- Carriage return ("enter" key)
819
--  when x"076" => ascii <= x"1b";  -- Escape ("esc" key)
820
--  when x"176" => ascii <= x"1b";  -- Escape ("esc" key)
821
--  when x"029" => ascii <= x"20";  -- Space
822
--  when x"129" => ascii <= x"20";  -- Space
823
--  when x"116" => ascii <= x"21";  -- !
824
--  when x"152" => ascii <= x"22";  -- "
825
--  when x"126" => ascii <= x"23";  -- #
826
--  when x"125" => ascii <= x"24";  -- $
827
--  when x"12e" => ascii <= x"25";  --
828
--  when x"13d" => ascii <= x"26";  --
829
--  when x"052" => ascii <= x"27";  --
830
--  when x"146" => ascii <= x"28";  --
831
--  when x"145" => ascii <= x"29";  --
832
--  when x"13e" => ascii <= x"2a";  -- *
833
--  when x"155" => ascii <= x"2b";  -- +
834
--  when x"041" => ascii <= x"2c";  -- ,
835
--  when x"04e" => ascii <= x"2d";  -- -
836
--  when x"049" => ascii <= x"2e";  -- .
837
--  when x"04a" => ascii <= x"2f";  -- /
838
--  when x"045" => ascii <= x"30";  -- 0
839
--  when x"016" => ascii <= x"31";  -- 1
840
--  when x"01e" => ascii <= x"32";  -- 2
841
--  when x"026" => ascii <= x"33";  -- 3
842
--  when x"025" => ascii <= x"34";  -- 4
843
--  when x"02e" => ascii <= x"35";  -- 5
844
--  when x"036" => ascii <= x"36";  -- 6
845
--  when x"03d" => ascii <= x"37";  -- 7
846
--  when x"03e" => ascii <= x"38";  -- 8
847
--  when x"046" => ascii <= x"39";  -- 9
848
--  when x"14c" => ascii <= x"3a";  -- :
849
--  when x"04c" => ascii <= x"3b";  -- ;
850
--  when x"141" => ascii <= x"3c";  -- <
851
--  when x"055" => ascii <= x"3d";  -- =
852
--  when x"149" => ascii <= x"3e";  -- >
853
--  when x"14a" => ascii <= x"3f";  -- ?
854
--  when x"11e" => ascii <= x"40";  -- @
855
--  when x"11c" => ascii <= x"41";  -- A
856
--  when x"132" => ascii <= x"42";  -- B
857
--  when x"121" => ascii <= x"43";  -- C
858
--  when x"123" => ascii <= x"44";  -- D
859
--  when x"124" => ascii <= x"45";  -- E
860
--  when x"12b" => ascii <= x"46";  -- F
861
--  when x"134" => ascii <= x"47";  -- G
862
--  when x"133" => ascii <= x"48";  -- H
863
--  when x"143" => ascii <= x"49";  -- I
864
--  when x"13b" => ascii <= x"4a";  -- J
865
--  when x"142" => ascii <= x"4b";  -- K
866
--  when x"14b" => ascii <= x"4c";  -- L
867
--  when x"13a" => ascii <= x"4d";  -- M
868
--  when x"131" => ascii <= x"4e";  -- N
869
--  when x"144" => ascii <= x"4f";  -- O
870
--  when x"14d" => ascii <= x"50";  -- P
871
--  when x"115" => ascii <= x"51";  -- Q
872
--  when x"12d" => ascii <= x"52";  -- R
873
--  when x"11b" => ascii <= x"53";  -- S
874
--  when x"12c" => ascii <= x"54";  -- T
875
--  when x"13c" => ascii <= x"55";  -- U
876
--  when x"12a" => ascii <= x"56";  -- V
877
--  when x"11d" => ascii <= x"57";  -- W
878
--  when x"122" => ascii <= x"58";  -- X
879
--  when x"135" => ascii <= x"59";  -- Y
880
--  when x"11a" => ascii <= x"5a";  -- Z
881
--  when x"054" => ascii <= x"5b";  -- [
882
--  when x"05d" => ascii <= x"5c";  -- \
883
--  when x"05b" => ascii <= x"5d";  -- ]
884
--  when x"136" => ascii <= x"5e";  -- ^
885
--  when x"14e" => ascii <= x"5f";  -- _    
886
--  when x"00e" => ascii <= x"60";  -- `
887
--  when x"01c" => ascii <= x"61";  -- a
888
--  when x"032" => ascii <= x"62";  -- b
889
--  when x"021" => ascii <= x"63";  -- c
890
--  when x"023" => ascii <= x"64";  -- d
891
--  when x"024" => ascii <= x"65";  -- e
892
--  when x"02b" => ascii <= x"66";  -- f
893
--  when x"034" => ascii <= x"67";  -- g
894
--  when x"033" => ascii <= x"68";  -- h
895
--  when x"043" => ascii <= x"69";  -- i
896
--  when x"03b" => ascii <= x"6a";  -- j
897
--  when x"042" => ascii <= x"6b";  -- k
898
--  when x"04b" => ascii <= x"6c";  -- l
899
--  when x"03a" => ascii <= x"6d";  -- m
900
--  when x"031" => ascii <= x"6e";  -- n
901
--  when x"044" => ascii <= x"6f";  -- o
902
--  when x"04d" => ascii <= x"70";  -- p
903
--  when x"015" => ascii <= x"71";  -- q
904
--  when x"02d" => ascii <= x"72";  -- r
905
--  when x"01b" => ascii <= x"73";  -- s
906
--  when x"02c" => ascii <= x"74";  -- t
907
--  when x"03c" => ascii <= x"75";  -- u
908
--  when x"02a" => ascii <= x"76";  -- v
909
--  when x"01d" => ascii <= x"77";  -- w
910
--  when x"022" => ascii <= x"78";  -- x
911
--  when x"035" => ascii <= x"79";  -- y
912
--  when x"01a" => ascii <= x"7a";  -- z
913
--  when x"154" => ascii <= x"7b";  -- {
914
--  when x"15d" => ascii <= x"7c";  -- |
915
--  when x"15b" => ascii <= x"7d";  -- }
916
--  when x"10e" => ascii <= x"7e";  -- ~
917
--  when x"071" => ascii <= x"7f";  -- (Delete OR DEL on numeric keypad)
918
--  when x"171" => ascii <= x"7f";  -- (Delete OR DEL on numeric keypad)
919 118 dilbert57
--  when others => ascii <= x"00";  -- 0xff used for unlisted characters.
920 19 dilbert57
--  end case;
921
--end process;
922 118 dilbert57
 
923 19 dilbert57
 
924
end rtl;

powered by: WebSVN 2.1.0

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