]> 4ch.mooo.com Git - 16.git/blob - src/lib/dos_comm.c
modified: .gitignore
[16.git] / src / lib / dos_comm.c
1 /* Thanks to Alex Russell for example code */
2 /* Thanks to Gary Neal for example code */\r
3 #include "src\lib\dos_comm.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 */\r
10 extern "C" {\r
11 #endif
12 static void interrupt (far *oldkb)(void) = NULL; /* BIOS keyboard handler */
13 #ifdef __cplusplus\r
14 }\r
15 #endif\r
16 /*****************NEW KEYBOARD 09h ISR***********************/\r
17 void interrupt newkb(void){
18         byte kee;
19         register char qx;\r
20
21         kee = inp(0x60);        /* Read the keyboard scan code */
22
23         /* Clear keyboard controller on XT machines */\r
24         qx = inp(0x61);           /* Get keyboard control register */\r
25         qx |= 0x82;\r
26         outp(0x61, qx);           /* Toggle acknowledge bit high */\r
27         qx &= 0x7F;\r
28         outp(0x61, qx);           /* Toggle acknowledge bit low */
29
30         /* Interpret the scan code and set our flags */
31 //tt    printf("%d[%d]\n",kee,key[kee]);
32         if(kee & 0x80)\r
33                 key[kee & 0x7F] = 0; // a key is released\r
34         else\r
35                 key[kee] = kea[kee] = 1; // a key is pressed
36
37         /* Acknowledge the interrupt to the programmable interrupt controller */\r
38         outp(0x20, 0x20);      /* Signal non specific end of interrupt */\r
39 }\r
40 \r
41 /* ---------------------- init_keyboard() ---------------- April 17,1993 */\r
42 /* restore the bios keyboard handler */\r
43 /* ---------------------- deinit_keyboard() -------------- April 17,1993 */\r
44 void setkb(int vq){
45         int i;  /* Index variable */\r
46         if(!vq){ // deinitiation
47                 /* Abort if our function pointer has no valid address */\r
48                 if(oldkb == NULL) return;
49                 /* Set address in our function pointer in interrupt vector table */
50                 _dos_setvect(9, oldkb);
51                 /* Reset our function pointer to contain no valid address */\r
52                 oldkb = NULL;
53                 /* Print the key heap */
54                 printf("\n");
55                 for(i=0; i<NUM_SCANCODES; i++){
56                         if(i==NUM_SCANCODES/2) printf("================================\n");
57                         printf("%03d[%d][%d]",i+1,key[i],kea[i]);
58                         if(key[i]==1)printf("====");
59                         printf(",\n");
60                 }
61         }else if(vq == 1){ // initiation
62                 byte far *lock_key;
63
64                 /* Abort if our function pointer has a valid address. */\r
65                 if(oldkb != NULL) return;\r
66
67                 /* Clear the keyboard buttons state arrays */\r
68                 for(i = 0; i < NUM_SCANCODES; i++)\r
69                         key[i] = kea[i] = 0;
70 \r
71                 /* save old BIOS key board handler */\r
72                 oldkb = _dos_getvect(9);\r
73 \r
74                 // turn off num-lock via BIOS\r
75                 lock_key = MK_FP(0x040, 0x017); // Pointing to the address of the bios shift state keys\r
76                 *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\r
77                 oldkb();        // call BIOS keyhandler to change keyboard lights\r
78
79                 /* setup our own handler */\r
80                 _dos_setvect(9, newkb);\r
81         }\r
82 }\r
83 \r
84 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\\r
85  * keyp                                                              *\r
86  *                                                                         *\r
87  * Returns the status of the key requested.                                *\r
88  * The status is 1 if the key is pressed or has been pressed since the     *\r
89  * last call to this function for that particular key.                     *\r
90 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */\r
91 int keyp(byte c){\r
92         register char retVal;\r
93 \r
94         /* Key value in range of keyboard keys available */\r
95         c &= 0x7F;\r
96
97         /* Get the status of the key requested */\r
98         retVal = key[c] | kea[c];\r
99 \r
100         /* Reset the was pressed status for the requested key */\r
101         kea[c] = 0;
102
103         /* Return the requested key's state */\r
104         return retVal;\r
105 }\r