57 lines
1.3 KiB
C
57 lines
1.3 KiB
C
|
#include <dos.h>
|
||
|
#include <conio.h>
|
||
|
|
||
|
#include "keyboard.h"
|
||
|
|
||
|
unsigned char keystateBits[16];
|
||
|
struct KeyboardKeydownState keyboardKeydownState;
|
||
|
|
||
|
void far (interrupt *int9Save)();
|
||
|
|
||
|
void populateKeyboardKeydownState() {
|
||
|
keyboardKeydownState.KEY_W = keystateBits[2] & 0x02;
|
||
|
keyboardKeydownState.KEY_A = keystateBits[3] & 0x40;
|
||
|
keyboardKeydownState.KEY_S = keystateBits[3] & 0x80;
|
||
|
keyboardKeydownState.KEY_D = keystateBits[4] & 0x01;
|
||
|
}
|
||
|
|
||
|
void far interrupt keyboardHandler(void) {
|
||
|
unsigned char rawcode, scancode, ksbByte, temp;
|
||
|
unsigned char mask = 1;
|
||
|
|
||
|
_disable();
|
||
|
|
||
|
rawcode = inp(0x60);
|
||
|
|
||
|
scancode = rawcode & 0x7f;
|
||
|
mask = 1 << (scancode & 0x07);
|
||
|
ksbByte = scancode >> 3;
|
||
|
|
||
|
// https://github.com/id-Software/wolf3d/blob/05167784ef009d0d0daefe8d012b027f39dc8541/WOLFSRC/ID_IN.C#L152-L154
|
||
|
outp(0x61, (temp = inp(0x61)) | 0x80);
|
||
|
outp(0x61, temp);
|
||
|
|
||
|
/* break */
|
||
|
if (rawcode & 0x80) {
|
||
|
mask = 0xff - mask;
|
||
|
keystateBits[ksbByte] &= mask;
|
||
|
/* make */
|
||
|
} else {
|
||
|
keystateBits[ksbByte] |= mask;
|
||
|
}
|
||
|
|
||
|
// acknowledge we're done with the interrupt
|
||
|
outp(0x20, 0x20);
|
||
|
|
||
|
_enable();
|
||
|
}
|
||
|
|
||
|
void installKeyboardHandler() {
|
||
|
int9Save = _dos_getvect(9);
|
||
|
_dos_setvect(9, keyboardHandler);
|
||
|
}
|
||
|
|
||
|
void uninstallKeyboardHandler() {
|
||
|
_dos_setvect(9, int9Save);
|
||
|
}
|