]> 4ch.mooo.com Git - 16.git/blob - 16/modex16/dos_kb.c
modified: 16/modex16/dos_kb.c
[16.git] / 16 / modex16 / dos_kb.c
1 /* Thanks to Alex Russell for example code */
2 /* Thanks to Gary Neal for example code */
3 #include "dos_kb.h"
4
5 // keyboard buffer
6 static byte key[NUM_SCANCODES]; // pressed
7 static byte kea[NUM_SCANCODES]; // released
8
9 #ifdef __cplusplus              /* Function must be declared C style */
10 extern "C" {
11 #endif
12 static void interrupt (far *oldkb)(void) = NULL; /* BIOS keyboard handler */
13 #ifdef __cplusplus
14 }
15 #endif
16
17 /*
18  * Comment out the following #define if you don't want the testing main()
19  * to be included.
20  */
21 //#define TESTING
22 #define TESTING2
23
24 /*****************NEW KEYBOARD 09h ISR***********************/
25 void interrupt newkb(void){
26         byte kee;
27         register char qx;
28
29         kee = inp(0x60);        /* Read the keyboard scan code */
30
31         /* Clear keyboard controller on XT machines */
32         qx = inp(0x61);    /* Get keyboard control register */
33         qx |= 0x82;
34         outp(0x61, qx);    /* Toggle acknowledge bit high */
35         qx &= 0x7F;
36         outp(0x61, qx);    /* Toggle acknowledge bit low */
37
38         /* Interpret the scan code and set our flags */
39         #ifdef TESTING2
40         printf("%d[%d]\n",kee,key[kee]);
41         #endif
42         if(kee & 0x80)
43                 key[kee & 0x7F] = 0; // a key is released
44         else
45                 key[kee] = kea[kee] = 1; // a key is pressed
46
47         /* Acknowledge the interrupt to the programmable interrupt controller */
48         outp(0x20, 0x20);      /* Signal non specific end of interrupt */
49 }
50
51 /* ---------------------- init_keyboard() ---------------- April 17,1993 */
52 /* restore the bios keyboard handler */
53 /* ---------------------- deinit_keyboard() -------------- April 17,1993 */
54 void setkb(int vq){
55         int i;  /* Index variable */
56         if(!vq){ // deinitiation
57                 /* Abort if our function pointer has no valid address */
58                 if(oldkb == NULL) return;
59                 /* Set address in our function pointer in interrupt vector table */
60                 _dos_setvect(9, oldkb);
61                 /* Reset our function pointer to contain no valid address */
62                 oldkb = NULL;
63                 #ifdef TESTING2
64                 /* Print the key heap */
65                 printf("\n");
66                 for(i=0; i<NUM_SCANCODES; i++){
67                         if(i==NUM_SCANCODES/2) printf("================================\n");
68                         printf("%03d[%d][%d]",i+1,key[i],kea[i]);
69                         if(key[i]==1)printf("====");
70                         printf(",\n");
71                 }
72                 #endif
73         }else if(vq == 1){ // initiation
74                 byte far *lock_key;
75
76                 /* Abort if our function pointer has a valid address. */
77                 if(oldkb != NULL) return;
78
79                 /* Clear the keyboard buttons state arrays */
80                 for(i = 0; i < NUM_SCANCODES; i++)
81                         key[i] = kea[i] = 0;
82
83                 /* save old BIOS key board handler */
84                 oldkb = _dos_getvect(9);
85
86                 // turn off num-lock via BIOS
87                 lock_key = MK_FP(0x040, 0x017); // Pointing to the address of the bios shift state keys
88                 *lock_key&=(~(16 | 32 | 64)); // toggle off the locks by changing the values of the 4th, 5th, and 6th bits of the address byte of 0040:0017
89                 oldkb();        // call BIOS keyhandler to change keyboard lights
90
91                 /* setup our own handler */
92                 _dos_setvect(9, newkb);
93         }
94 }
95
96 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
97  * keyp                                                       *
98  *                                                                       *
99  * Returns the status of the key requested.                             *
100  * The status is 1 if the key is pressed or has been pressed since the     *
101  * last call to this function for that particular key.               *
102 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
103 int keyp(byte c){
104         register char retVal;
105
106         /* Key value in range of keyboard keys available */
107         c &= 0x7F;
108
109         /* Get the status of the key requested */
110         retVal = key[c] | kea[c];
111
112         /* Reset the was pressed status for the requested key */
113         kea[c] = 0;
114
115         /* Return the requested key's state */
116         return retVal;
117 }
118
119
120 /*
121  * The library testing routines follows below.
122  */
123
124 #ifdef TESTING
125
126 /*
127  * Library test (program) entry point.
128  */
129
130 void main(void)
131 {
132         byte q;
133         setkb(1);
134         while(!keyp(1))
135         {
136                 keyp(q);
137         }
138         setkb(0);
139 }
140
141 #endif