]> 4ch.mooo.com Git - 16.git/blob - src/lib/dos_comm.c
modified: 16/modex16/scroll.c
[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
32         printf("%d[%d]\n",kee,key[kee]);
33         if(kee & 0x80)\r
34                 key[kee & 0x7F] = 0; // a key is released\r
35         else\r
36                 key[kee] = kea[kee] = 1; // a key is pressed
37
38         /* Acknowledge the interrupt to the programmable interrupt controller */\r
39         outp(0x20, 0x20);      /* Signal non specific end of interrupt */\r
40 }\r
41 \r
42 /* ---------------------- init_keyboard() ---------------- April 17,1993 */\r
43 /* restore the bios keyboard handler */\r
44 /* ---------------------- deinit_keyboard() -------------- April 17,1993 */\r
45 void setkb(int vq){
46         int i;  /* Index variable */\r
47         if(!vq){ // deinitiation
48                 /* Abort if our function pointer has no valid address */\r
49                 if(oldkb == NULL) return;
50                 /* Set address in our function pointer in interrupt vector table */
51                 _dos_setvect(9, oldkb);
52                 /* Reset our function pointer to contain no valid address */\r
53                 oldkb = NULL;
54                 /* Print the key heap */
55                 printf("\n");
56                 for(i=0; i<NUM_SCANCODES; i++){
57                         if(i==NUM_SCANCODES/2) printf("================================\n");
58                         printf("%03d[%d][%d]",i+1,key[i],kea[i]);
59                         if(key[i]==1)printf("====");
60                         printf(",\n");
61                 }
62         }else if(vq == 1){ // initiation
63                 byte far *lock_key;
64
65                 /* Abort if our function pointer has a valid address. */\r
66                 if(oldkb != NULL) return;\r
67
68                 /* Clear the keyboard buttons state arrays */\r
69                 for(i = 0; i < NUM_SCANCODES; i++)\r
70                         key[i] = kea[i] = 0;
71 \r
72                 /* save old BIOS key board handler */\r
73                 oldkb = _dos_getvect(9);\r
74 \r
75                 // turn off num-lock via BIOS\r
76                 lock_key = MK_FP(0x040, 0x017); // Pointing to the address of the bios shift state keys\r
77                 *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
78                 oldkb();        // call BIOS keyhandler to change keyboard lights\r
79
80                 /* setup our own handler */\r
81                 _dos_setvect(9, newkb);\r
82         }\r
83 }\r
84 \r
85 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\\r
86  * keyp                                                              *\r
87  *                                                                         *\r
88  * Returns the status of the key requested.                                *\r
89  * The status is 1 if the key is pressed or has been pressed since the     *\r
90  * last call to this function for that particular key.                     *\r
91 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */\r
92 int keyp(byte c){\r
93         register char retVal;\r
94 \r
95         /* Key value in range of keyboard keys available */\r
96         c &= 0x7F;\r
97
98         /* Get the status of the key requested */\r
99         retVal = key[c] | kea[c];\r
100 \r
101         /* Reset the was pressed status for the requested key */\r
102         kea[c] = 0;
103
104         /* Return the requested key's state */\r
105         return retVal;\r
106 }\r