Repo: https://github.com/novavita/Novix
[branch is work/rizet+pontaoski/keyboard-input]
I've been working quite a bit into getting keyboard input working. I've gotten help on some botched interrupts, and a broken multiboot header. The project was pretty much a trainwreck. They had written large portions of code without being able to test it. Multiboot header was broken, wouldn't boot. Booted, but triple faulted. Interrupts were all over the place. I've been working for the last week or two on making the OS functional. I've pretty much restored the broken OS, you can boot it and interrupts are working. I set out to create features. I decided to undertake the task of keyboard input. And now, here I am.
Like I said, my interrupts are working, however I feel like I am making some sort of mistake in the keyboard input. When I press a key, something is printed. Pressing different keys results in different things being printed. Cool. I went to the stage to convert the scan codes into characters, and my mistakes were shown to me.
I've linked a screenshot below where I press A on the keyboard.
My interrupt handler(s) (GDT.cpp)
Code: Select all
void InterruptHandler(Registers registers)
{
if (registers.interruptNumber == 13)
return;
Terminal& terminal = Terminal::instance();
if (registers.interruptNumber == 6)
{
uint8_t keycode = inb(0x60);
if (keycode < 0 || keycode == prevKeyCode || keycode == 32)
return;
writeHex(keycode);
terminal << ", " << keycode << ", " << getChar(keycode) << Terminal::EOL;
prevKeyCode = keycode;
}
terminal << "Interrupt: ";
terminal << registers.interruptNumber;
terminal << Terminal::EOL;
}
void ISRHandler(Registers registers)
{
outb(0xA0, 0x20);
InterruptHandler(registers);
outb(0x20, 0x20);
}
void IRQHandler(Registers registers)
{
if (registers.interruptNumber >= 40) outb(0xA0, 0x20); /* slave */
InterruptHandler(registers);
outb(0x20, 0x20); /* master */
}
Code: Select all
char getChar(uint8_t keycode)
{
switch (keycode)
{
case 0x49101:
return 'a';
case 159:
return 's';
case 160:
return 'd';
case 161:
return 'f';
default:
return ' ';
}
}
void writeHex(int n)
{
int tmp;
Terminal::instance().write("0x");
bool noZeroes = true;
int i;
for (i = 28; i > 0; i -= 4)
{
tmp = (n >> i) & 0xF;
if (tmp == 0 && noZeroes)
{
continue;
}
if (tmp >= 0xA)
{
noZeroes = false;
Terminal::instance().write(tmp-0xA+'a');
}
else
{
noZeroes = false;
Terminal::instance().write(tmp+'0');
}
}
tmp = n & 0xF;
if (tmp >= 0xA)
{
Terminal::instance().write(tmp-0xA+'a');
}
else
{
Terminal::instance().write(tmp+'0');
}
}