#include #include "keyboard.h" #define KeybCmdWriteLED 0xed #define KeybCmdEcho 0xee #define KeybCmdSetScancodeSet 0xf0 #define KeybCmdGetID 0xf2 #define KeybCmdSetRepeatDelay 0xf3 #define KeybCmdEnable 0xf4 #define KeybCmdSetDefaultDisable 0xf5 #define KeybCmdSetDefault 0xf6 #define KeybCmdResend 0xfe #define KeybCmdReset 0xff volatile uint8 KeyState[16]; volatile uint8 KeyboardLastStatus; volatile uint8 KeyboardLastScancode; uint8 KeyboardScancodeSet = 2; // Byte map: // 0 If set, next code is break // 1 'Gray' key // 2 'Weird' key (Pause/Break) // 3 Scroll // 4 Num // 5 Caps // 6 If set, LEDs changed uint8 KeyboardModifiers; const char KeyboardMap[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\t', '`', 0, 0, 0, 0, 0, 0, 'q', '1', 0, 0, 0, 'z', 's', 'a', 'w', '2', 0, 0, 'c', 'x', 'd', 'e', '4', '3', 0, 0, ' ', 'v', 'f', 't', 'r', '5', 0, 0, 'n', 'b', 'h', 'g', 'y', '6', 0, 0, 0, 'm', 'j', 'u', '7', '8', 0, 0, ',', 'k', 'i', 'o', '0', '9', 0, 0, '.', '/', 'l', ';', 'p', '-', 0, 0, 0, '\'', 0, '[', '=', 0, 0, 0, 0, '\n', ']', 0, '\\', 0, 0, 0, 0, 0, 0, 0, 0, '\b', 0, 0, '1', '/', '4', '7', '\n', 0, 0, '0', '.', '2', '5', '6', '8', 0, 0, 0, '+', '3', '-', '*', '9', 0, 0 }; const char KeyboardMapShift[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\t', '~', 0, 0, 0, 0, 0, 0, 'Q', '!', 0, 0, 0, 'Z', 'S', 'A', 'W', '@', 0, 0, 'C', 'X', 'D', 'E', '$', '#', 0, 0, ' ', 'V', 'F', 'T', 'R', '%', 0, 0, 'N', 'B', 'H', 'G', 'Y', '^', 0, 0, 0, 'M', 'J', 'U', '&', '*', 0, 0, '<', 'K', 'I', 'O', ')', '(', 0, 0, '>', '?', 'L', ':', 'P', '_', 0, 0, 0, '\"', 0, '{', '+', 0, 0, 0, 0, '\n', '}', 0, '|', 0, 0, 0, 0, 0, 0, 0, 0, '\b', 0, 0, '1', '/', '4', '7', '\n', 0, 0, '0', '.', '2', '5', '6', '8', 0, 0, 0, '+', '3', '-', '*', '9', 0, 0 }; void KeyboardSetKeyStatus (uint8 scancode, uint8 status) { int32 index = scancode>>3, pos = scancode & 0x7; if (status) KeyState[index] |= 1<>3, pos = scancode & 0x7; return KeyState[index] & (1< 0); status |= (num > 0) << 1; status |= (caps > 0) << 2; KeyboardWaitOutport(); outportb (0x60, KeybCmdWriteLED); KeyboardWaitOutport(); outportb (0x60, status); } /*************************************** * Set repeat rate/delay * *************************************** Values for inter-character delay (bits 4-0) (characters per second; default is 10.9) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 ----+----+----+----+----+----+----+----+---- 0 |30.0|26.7|24.0|21.8|20.0|18.5|17.1|16.0 8 |15.0|13.3|12.0|10.9|10.0|9.2 |8.6 |8.0 16 |7.5 |6.7 |6.0 |5.5 |5.0 |4.6 |4.3 |4.0 24 |3.7 |3.3 |3.0 |2.7 |2.5 |2.3 |2.1 |2.0 Values for delay: (miliseconds; default is 500) 0 | 1 | 2 | 3 -----+-----+-----+----- 250 | 500 | 750 | 1000 ***************************************/ void KeyboardSetRepeatRate(uint8 rate, uint8 delay) { if (rate>3 || delay>31) return; uint8 out = rate<<5 | delay; KeyboardWaitOutport(); outportb(0x60, KeybCmdSetRepeatDelay); KeyboardWaitOutport(); outportb(0x60, out); } /*************************************** * Set scancode set * *************************************** 1 Set to scancode set 1 2 Set to scancode set 2 3 Set to scancode set 3 ***************************************/ void KeyboardSetScancodeSet(uint8 set) { if (set>3 || set <= 0) return; KeyboardWaitOutport(); outportb (0x60, KeybCmdSetScancodeSet); KeyboardWaitOutport(); outportb (0x60, set); KeyboardScancodeSet = set; } void KeyboardWaitOutport() { int fail_safe=200000; while ((inportb(0x64)&2)!=0 && fail_safe>0) fail_safe--; } void KeyboardWaitInport() { int fail_safe=200000; while ((inportb(0x64)&1)==0 && fail_safe>0) fail_safe--; } void KeyboardInstallA() { KeyboardWaitOutport(); outportb(0x60, KeybCmdReset); // Reset kb // Initialize variables KeyboardLastStatus = 0; KeyboardModifiers = 0; int32 i; for (i = 0; i < 16; i++) KeyState[i] = 0; } void KeyboardInstallB() { // Wait for BAT test results KeyboardWaitInport(); unsigned char temp; do temp = inportb(0x60); while (temp!=0xAA && temp!=0xFC); // Error if (temp == 0xFC) return; // Set new repeat rate KeyboardSetRepeatRate(1, 11); // Set scancode set 2 KeyboardSetScancodeSet(2); // Set new scancode set KeyboardWaitOutport(); outportb(0x64, 0x20); // Get "Command unsigned char" do { temp = inportb(0x60); } while (temp==0xFA || temp==0xAA); temp &= ~(1<<6); // Unset bit6: disable conversion KeyboardWaitOutport(); outportb(0x64, 0x60); // Function to write cmd unsigned char KeyboardWaitOutport(); outportb(0x60, temp); // Send it }