source: rtems/c/src/lib/libbsp/i386/pc386/console/inch.c @ 96d56b3

4.104.114.84.95
Last change on this file since 96d56b3 was 96d56b3, checked in by Joel Sherrill <joel.sherrill@…>, on 04/27/98 at 18:42:04

Update from Pedro Romano <pmcnr@…>.

  • Property mode set to 100644
File size: 8.0 KB
Line 
1/*-------------------------------------------------------------------------+
2| inch.c v1.1 - PC386 BSP - 1997/08/07
3+--------------------------------------------------------------------------+
4| (C) Copyright 1997 -
5| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
6|
7| http://pandora.ist.utl.pt
8|
9| Instituto Superior Tecnico * Lisboa * PORTUGAL
10+--------------------------------------------------------------------------+
11| Disclaimer:
12|
13| This file is provided "AS IS" without warranty of any kind, either
14| expressed or implied.
15+--------------------------------------------------------------------------+
16| This code is based on:
17|   inch.c,v 1.3 1995/12/19 20:07:25 joel Exp - go32 BSP
18| With the following copyright notice:
19| With the following copyright notice:
20| **************************************************************************
21| *  COPYRIGHT (c) 1989-1998.
22| *  On-Line Applications Research Corporation (OAR).
23| *  Copyright assigned to U.S. Government, 1994.
24| *
25| *  The license and distribution terms for this file may be
26| *  found in found in the file LICENSE in this distribution or at
27| *  http://www.OARcorp.com/rtems/license.html.
28| **************************************************************************
29|
30|  $Id$
31+--------------------------------------------------------------------------*/
32
33#include <bsp.h>
34#include <irq.h>
35
36/*-------------------------------------------------------------------------+
37| Constants
38+--------------------------------------------------------------------------*/
39#define KBD_CTL      0x61  /* -------------------------------- */
40#define KBD_DATA     0x60  /* Ports for PC keyboard controller */
41#define KBD_STATUS   0x64  /* -------------------------------- */
42
43#define KBD_BUF_SIZE 256
44
45/*-------------------------------------------------------------------------+
46| Global Variables
47+--------------------------------------------------------------------------*/
48static char key_map[] =
49{
50  0,033,'1','2','3','4','5','6','7','8','9','0','-','=','\b','\t',
51  'q','w','e','r','t','y','u','i','o','p','[',']',015,0x80,
52  'a','s','d','f','g','h','j','k','l',';',047,0140,0x80,
53  0134,'z','x','c','v','b','n','m',',','.','/',0x80,
54  '*',0x80,' ',0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
55  0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
56  0x80,0x80,0x80,'0',0177
57}; /* Keyboard scancode -> character map with no modifiers.       */
58
59static char shift_map[] =
60{
61  0,033,'!','@','#','$','%','^','&','*','(',')','_','+','\b','\t',
62  'Q','W','E','R','T','Y','U','I','O','P','{','}',015,0x80,
63  'A','S','D','F','G','H','J','K','L',':',042,'~',0x80,
64  '|','Z','X','C','V','B','N','M','<','>','?',0x80,
65  '*',0x80,' ',0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
66  0x80,0x80,0x80,0x80,'7','8','9',0x80,'4','5','6',0x80,
67  '1','2','3','0',177
68}; /* Keyboard scancode -> character map with SHIFT key modifier. */
69
70static char             kbd_buffer[KBD_BUF_SIZE];
71static rtems_unsigned16 kbd_first = 0;
72static rtems_unsigned16 kbd_last  = 0;
73
74
75/*-------------------------------------------------------------------------+
76|         Function: _IBMPC_scankey
77|      Description: This function can be called during a poll for input, or by
78|                   an ISR. Basically any time you want to process a keypress.
79| Global Variables: key_map, shift_map.
80|        Arguments: outChar - character read in case of a valid reading,
81|                   otherwise unchanged.
82|          Returns: TRUE in case a valid character has been read,
83|                   FALSE otherwise.
84+--------------------------------------------------------------------------*/
85rtems_boolean
86_IBMPC_scankey(char *outChar)
87{
88  unsigned char inChar;
89  static int alt_pressed   = 0;
90  static int ctrl_pressed  = 0;
91  static int shift_pressed = 0;
92  static int caps_pressed  = 0;
93  static int extended      = 0;
94
95  *outChar = NULL; /* default value if we return FALSE */
96
97  /* Read keyboard controller, toggle enable */
98  inport_byte(KBD_CTL, inChar);
99  outport_byte(KBD_CTL, inChar & ~0x80);
100  outport_byte(KBD_CTL, inChar | 0x80);
101  outport_byte(KBD_CTL, inChar & ~0x80);
102
103  /* See if it has data */
104  inport_byte(KBD_STATUS, inChar);
105  if ((inChar & 0x01) == 0)
106    return FALSE;
107
108  /* Read the data.  Handle nonsense with shift, control, etc. */
109  inport_byte(KBD_DATA, inChar);
110
111  if (extended)
112    extended--;
113
114  switch (inChar)
115  {
116    case 0xe0:
117      extended = 2;
118      return FALSE;
119      break;
120
121    case 0x38:
122      alt_pressed = 1;
123      return FALSE;
124      break;
125    case 0xb8:
126      alt_pressed = 0;
127      return FALSE;
128      break;
129
130    case 0x1d:
131      ctrl_pressed = 1;
132      return FALSE;
133      break;
134    case 0x9d:
135      ctrl_pressed = 0;
136      return FALSE;
137      break;
138
139    case 0x2a:
140      if (extended)
141        return FALSE;
142    case 0x36:
143      shift_pressed = 1;
144      return FALSE;
145      break;
146    case 0xaa:
147      if (extended)
148        return FALSE;
149    case 0xb6:
150      shift_pressed = 0;
151      return FALSE;
152      break;
153
154    case 0x3a:
155      caps_pressed = 1;
156      return FALSE;
157      break;
158    case 0xba:
159      caps_pressed = 0;
160      return FALSE;
161      break;
162
163    case 0x53:
164      if (ctrl_pressed && alt_pressed)
165        rtemsReboot(); /* ctrl+alt+del -> reboot */
166      break;
167
168    /*
169     * Ignore unrecognized keys--usually arrow and such
170     */
171    default:
172      if ((inChar & 0x80) || (inChar > 0x39))
173      /* High-bit on means key is being released, not pressed */
174        return FALSE;
175      break;
176  } /* switch */
177
178  /* Strip high bit, look up in our map */
179  inChar &= 0x7f;
180  if (ctrl_pressed)
181  {
182    *outChar = key_map[inChar];
183    *outChar &= 037;
184  }
185  else
186  {
187    *outChar = shift_pressed ? shift_map[inChar] : key_map[inChar];
188    if (caps_pressed)
189    {
190      if (*outChar >= 'A' && *outChar <= 'Z')
191        *outChar += 'a' - 'A';
192      else if (*outChar >= 'a' && *outChar <= 'z')
193        *outChar -= 'a' - 'A';
194    }
195  }
196
197  return TRUE;
198} /* _IBMPC_scankey */
199
200
201/*-------------------------------------------------------------------------+
202|         Function: _IBMPC_keyboard_isr
203|      Description: Interrupt Service Routine for keyboard (0x01) IRQ.
204| Global Variables: kbd_buffer, kbd_first, kbd_last.
205|        Arguments: vector - standard RTEMS argument - see documentation.
206|          Returns: standard return value - see documentation.
207+--------------------------------------------------------------------------*/
208rtems_isr
209_IBMPC_keyboard_isr(rtems_vector_number vector)
210{
211  if (_IBMPC_scankey(&kbd_buffer[kbd_last]))
212  {
213    /* Got one; save it if there is enough room in buffer. */
214    unsigned int next = (kbd_last + 1) % KBD_BUF_SIZE;
215
216    if (next != kbd_first)
217      kbd_last = next;
218  }
219
220  PC386_ackIrq(vector - PC386_IRQ_VECTOR_BASE); /* Mark interrupt as handled. */
221} /* _IBMPC_keyboard_isr */
222
223
224/*-------------------------------------------------------------------------+
225|         Function: _IBMPC_chrdy
226|      Description: Check keyboard ISR buffer and return character if not empty.
227| Global Variables: kbd_buffer, kbd_first, kbd_last.
228|        Arguments: c - character read if keyboard buffer not empty, otherwise
229|                   unchanged.
230|          Returns: TRUE if keyboard buffer not empty, FALSE otherwise.
231+--------------------------------------------------------------------------*/
232rtems_boolean
233_IBMPC_chrdy(char *c)
234{
235  /* FIX ME!!! It doesn't work without something like the following line.
236     Find out why! */
237  printk("");
238
239  /* Check buffer our ISR builds */
240  if (kbd_first != kbd_last)
241  {
242    *c = kbd_buffer[kbd_first];
243
244    kbd_first = (kbd_first + 1) % KBD_BUF_SIZE;
245    return TRUE;
246  }
247  else
248    return FALSE;
249} /* _IBMPC_chrdy */
250
251
252/*-------------------------------------------------------------------------+
253|         Function: _IBMPC_inch
254|      Description: Poll keyboard until a character is ready and return it.
255| Global Variables: None.
256|        Arguments: None.
257|          Returns: character read from keyboard.
258+--------------------------------------------------------------------------*/
259char
260_IBMPC_inch(void)
261{
262    char c;
263    while (!_IBMPC_chrdy(&c))
264      continue;
265
266    return c;
267} /* _IBMPC_inch */
Note: See TracBrowser for help on using the repository browser.