source: rtems/c/src/lib/libbsp/i386/go32/console/inch.c @ 637df35

4.104.114.84.95
Last change on this file since 637df35 was 637df35, checked in by Joel Sherrill <joel.sherrill@…>, on 07/12/95 at 19:47:25

Ada95, gnat, go32

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 *  $Id$
3 */
4
5#include <pc.h>
6#include <go32.h>
7#include <bsp.h>
8#include <cpu.h>
9
10/*
11 * Ports for PC keyboard
12 */
13#define KBD_CTL 0x61
14#define KBD_DATA 0x60
15#define KBD_STATUS 0x64
16
17static char key_map[] = {
180,033,'1','2','3','4','5','6','7','8','9','0','-','=','\b','\t',
19'q','w','e','r','t','y','u','i','o','p','[',']',015,0x80,
20'a','s','d','f','g','h','j','k','l',';',047,0140,0x80,
210134,'z','x','c','v','b','n','m',',','.','/',0x80,
22'*',0x80,' ',0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
230x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
240x80,0x80,0x80,'0',0177
25};
26
27static char shift_map[] = {
280,033,'!','@','#','$','%','^','&','*','(',')','_','+','\b','\t',
29'Q','W','E','R','T','Y','U','I','O','P','{','}',015,0x80,
30'A','S','D','F','G','H','J','K','L',':',042,'~',0x80,
31'|','Z','X','C','V','B','N','M','<','>','?',0x80,
32'*',0x80,' ',0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
330x80,0x80,0x80,0x80,'7','8','9',0x80,'4','5','6',0x80,
34'1','2','3','0',177
35};
36
37extern int _IBMPC_Use_Go32_IO;
38
39#define KBD_BUF_SIZE    256
40static char             kbd_buffer[ KBD_BUF_SIZE ];
41static unsigned int     kbd_first = 0;
42static unsigned int     kbd_last  = 0;
43
44/* This function can be called during a poll for input, or by an ISR.   */
45/* Basically any time you want to process a keypress.                   */
46int _IBMPC_scankey( char * ch )
47{
48        unsigned char c;
49        unsigned char outch;
50        static int shift_pressed = 0;
51        static int ctrl_pressed = 0;
52        static int caps_pressed = 0;
53
54        /* Read keyboard controller, toggle enable */
55        inport_byte( KBD_CTL, c );
56        outport_byte( KBD_CTL, c & ~0x80 );
57        outport_byte( KBD_CTL, c | 0x80 );
58        outport_byte( KBD_CTL, c & ~0x80 );
59
60        /* See if it has data */
61        inport_byte( KBD_STATUS, c );
62        if ( ( c & 0x01 ) == 0 )
63            return 0;
64
65        /* Read the data.  Handle nonsense with shift, control, etc. */
66        inport_byte( KBD_DATA, c );
67        switch ( c ) {
68        case 0x36:
69        case 0x2a:
70                shift_pressed = 1;
71                return 0;
72        case 0x3a:
73                caps_pressed = 1;
74                return 0;
75        case 0x1d:
76                ctrl_pressed = 1;
77                return 0;
78        case 0xb6:
79        case 0xaa:
80                shift_pressed = 0;
81                return 0;
82        case 0xba:
83                caps_pressed = 0;
84                return 0;
85        case 0x9d:
86                ctrl_pressed = 0;
87                return 0;
88        /*
89         * Ignore unrecognized keys--usually arrow and such
90         */
91        default:
92                if ( c & 0x80 )
93                    /* High-bit on means key is being released, not pressed */
94                    return 0;
95                if ( c == 88 )
96                    /* F12 - abort */
97                    exit( 1 );
98                if ( c > 0x39 )  {
99                    return 0;
100                }
101        }
102
103        /* Strip high bit, look up in our map */
104        c &= 127;
105        if ( ctrl_pressed )  {
106            outch = key_map[c];
107            outch &= 037;
108        } else {
109            outch = shift_pressed ? shift_map[c] : key_map[c];
110            if ( caps_pressed )  {
111                if      ( outch >= 'A' && outch <= 'Z' )  outch += 'a' - 'A';
112                else if ( outch >= 'a' && outch <= 'z' )  outch -= 'a' - 'A';
113            }
114        }
115
116        *ch = outch;
117        return 1;
118}
119
120
121void _IBMPC_keyboard_isr( rtems_unsigned32 interrupt )
122{
123    if ( _IBMPC_scankey( & kbd_buffer[ kbd_last ] ) )  {
124        /* Got one; save it if there is enough room in buffer. */
125        unsigned int next = (kbd_last + 1) % KBD_BUF_SIZE;
126        if ( next != kbd_first )
127            kbd_last = next;
128    }
129
130    /* Mark interrupt as handled */
131    outport_byte( 0x20, 0x20 );
132}
133
134
135int _IBMPC_chrdy( char * ch )
136{
137    if ( _IBMPC_Use_Go32_IO )  {
138        /* Read keyboard via BIOS: raw mode. */
139        if ( kbhit() )  {
140            *ch = getkey();
141            return 1;
142        } else {
143            return 0;
144        }
145    } else {
146        /* Check buffer our ISR builds */
147        if ( kbd_first != kbd_last )  {
148            *ch = kbd_buffer[ kbd_first ];
149            kbd_first = (kbd_first + 1) % KBD_BUF_SIZE;
150            return 1;
151        } else {
152            return 0;
153        }
154    }
155}
156
157int _IBMPC_inch( void )
158{
159    char Ch;
160    while ( ! _IBMPC_chrdy( & Ch ) )
161        continue;
162    return Ch;
163}
Note: See TracBrowser for help on using the repository browser.