os-k/kaleid/kernel/io/keyb.c

143 lines
4.3 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Basic Keyboard Driver //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <kernel/base.h>
#include <kernel/iomisc.h>
#include <extras/buf.h>
char ScanCodes[100] = { 0 };
void KeybPrint(char code)
{
if (code) {
bputc(BStdOut, ScanCodes[(int)code]);
//bprintf(BStdOut, "%x ", code);
BStdOut->flusher(BStdOut);
}
}
void ScanCodesInit(void)
{
ScanCodes[0x02] = '1';
ScanCodes[0x03] = '2';
ScanCodes[0x04] = '3';
ScanCodes[0x05] = '4';
ScanCodes[0x06] = '5';
ScanCodes[0x07] = '6';
ScanCodes[0x08] = '7';
ScanCodes[0x09] = '8';
ScanCodes[0x0A] = '9';
ScanCodes[0x0B] = '0';
ScanCodes[0x0C] = ' ';
ScanCodes[0x0D] = '+';
ScanCodes[0x0E] = 0x8;
ScanCodes[0x0F] = '\t';
ScanCodes[0x10] = 'a';
ScanCodes[0x11] = 'z';
ScanCodes[0x12] = 'e';
ScanCodes[0x13] = 'r';
ScanCodes[0x14] = 't';
ScanCodes[0x15] = 'y';
ScanCodes[0x16] = 'u';
ScanCodes[0x17] = 'i';
ScanCodes[0x18] = 'o';
ScanCodes[0x19] = 'p';
ScanCodes[0x1A] = '^';
ScanCodes[0x1B] = '$';
ScanCodes[0x1E] = 'q';
ScanCodes[0x1F] = 's';
ScanCodes[0x20] = 'd';
ScanCodes[0x21] = 'f';
ScanCodes[0x22] = 'g';
ScanCodes[0x23] = 'h';
ScanCodes[0x24] = 'j';
ScanCodes[0x25] = 'k';
ScanCodes[0x26] = 'l';
ScanCodes[0x27] = 'm';
ScanCodes[0x28] = 'u';
ScanCodes[0x29] = '*';
ScanCodes[0x2C] = 'w';
ScanCodes[0x2D] = 'x';
ScanCodes[0x2E] = 'c';
ScanCodes[0x2F] = 'v';
ScanCodes[0x30] = 'b';
ScanCodes[0x31] = 'n';
ScanCodes[0x32] = ',';
ScanCodes[0x33] = ';';
ScanCodes[0x34] = ':';
ScanCodes[0x35] = '!';
ScanCodes[0x1C] = '\n';
ScanCodes[0x39] = ' ';
}
void KeybHandler(ISRFrame_t *regs)
{
char status;
char code = 0;
// write EOI
IoWriteByteOnPort(0x20, 0x20);
status = IoReadByteFromPort(0x64);
if (status & 0x01) {
code = IoReadByteFromPort(0x60);
if(code < 0) code = 0;
}
KeybPrint((int)code);
IoSendEOItoPIC(0x21);
}
void IoCreateInputBuffer(void)
{
error_t rc;
rc = BOpenPureBuf(&BStdIn, BS_RDWR, 4 * KB);
if (rc) KeStartPanic("Couldn't create BStdIn");
BEnableLineBuffering(BStdIn);
}
void IoEnableKeyb(void)
{
ulong flags = KePauseIRQs();
IdtRegisterIsr(KeybHandler, 0x21);
char readedInterruptConfig = IoReadByteFromPort(0x21);
IoWriteByteOnPort(0x21, 0xFD & readedInterruptConfig);
KeRestoreIRQs(flags);
IoEnableNMI();
ScanCodesInit();
IoCreateInputBuffer();
}