From d907df625f33cb36257f713ffa067d563474691b Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Mon, 11 Mar 2024 19:58:07 +0100 Subject: [PATCH] Added keyboard support (hell yeah) --- src/boot.asm | 6 ++--- src/kernel/entry.asm | 3 +-- src/kernel/idt.c | 1 - src/kernel/irq.c | 10 ++++----- src/kernel/keyboard.c | 52 +++++++++++++++++++++++++++++++++++++++++++ src/kernel/keyboard.h | 25 +++++++++++++++++++++ src/kernel/main.c | 7 +++++- src/kernel/screen.c | 8 +++++++ 8 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 src/kernel/keyboard.c create mode 100644 src/kernel/keyboard.h diff --git a/src/boot.asm b/src/boot.asm index 52c8c7d..227c76a 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -20,11 +20,9 @@ start: ; Setup screen mov ax, 3 ; 80x25 Text mode int 0x10 - mov ah, 1 ; Hide cursor - mov cx, 0x2607 - int 0x10 - mov byte [0xb800], 'A' + mov si, msg + call puts .read: ; Location mov bx, KERNEL_LOC diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm index 2e24798..dfb276b 100644 --- a/src/kernel/entry.asm +++ b/src/kernel/entry.asm @@ -9,8 +9,7 @@ ;; Calls the main function in 'main.c' _start: call main - cli - hlt + jmp $ ;; If we used cli, hlt we wouldn't have interrupts :/ ;; ;; --- IDT --- diff --git a/src/kernel/idt.c b/src/kernel/idt.c index 9aaa593..da5ae64 100644 --- a/src/kernel/idt.c +++ b/src/kernel/idt.c @@ -31,6 +31,5 @@ void setupIdt() { idtp.base = (uint32_t)&idt; idtLoad(); - sti(); } diff --git a/src/kernel/irq.c b/src/kernel/irq.c index d1c310d..3a1a234 100644 --- a/src/kernel/irq.c +++ b/src/kernel/irq.c @@ -14,16 +14,16 @@ void uninstallIrqHandler(uint32_t irq) { // I have no clue how or what this does, but it makes everything work void irqRemap(void) { - outb(0xA0, 0x11); + outb(0xa0, 0x11); outb(0x20, 0x11); outb(0x21, 0x20); - outb(0xA1, 0x28); + outb(0xa1, 0x28); outb(0x21, 0x04); - outb(0xA1, 0x02); + outb(0xa1, 0x02); outb(0x21, 0x01); - outb(0xA1, 0x01); + outb(0xa1, 0x01); outb(0x21, 0x0); - outb(0xA1, 0x0); + outb(0xa1, 0x0); } void irqHandler(struct regs* r) { diff --git a/src/kernel/keyboard.c b/src/kernel/keyboard.c new file mode 100644 index 0000000..d8bd122 --- /dev/null +++ b/src/kernel/keyboard.c @@ -0,0 +1,52 @@ +#include "keyboard.h" +#include "irq.h" +#include "idt.h" +#include "system.h" +#include "screen.h" +#include "stdint.h" + +const char US_Keyboard[2][128] = { + { + 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', + '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', + 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', + 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, + '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', // Numpad + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }, + { + 0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t', + 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0, 'A', 'S', + 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|', 'Z', 'X', 'C', 'V', + 'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + } +}; + +uint8_t shift = 0; + +void keyboardHandler() { + uint8_t scancode; + scancode = inb(0x60); + + if (scancode == LEFT_SHIFT_PRESS || scancode == RIGHT_SHIFT_PRESS) shift = 1; + else if (scancode == LEFT_SHIFT_RELEASE || scancode == RIGHT_SHIFT_RELEASE) shift = 0; + + char c = US_Keyboard[shift][scancode]; + + if (scancode & 0x80) { // Keyup + } else if (c != 0) { + putc(c, 0x07); + } +} + +char* read(char terminator, uint8_t attr) { + // TODO: This +} + +void setupKeyboard() { + installIrqHandler(1, keyboardHandler); +} + diff --git a/src/kernel/keyboard.h b/src/kernel/keyboard.h new file mode 100644 index 0000000..51eacae --- /dev/null +++ b/src/kernel/keyboard.h @@ -0,0 +1,25 @@ +#ifndef KEYBOARD_H +#define KEYBOARD_H + +#include "stdint.h" + +// Keycodes +#define LEFT_SHIFT_PRESS 0x2a +#define LEFT_SHIFT_RELEASE (LEFT_SHIFT_PRESS | 0x80) +#define RIGHT_SHIFT_PRESS 0x36 +#define RIGHT_SHIFT_RELEASE (RIGHT_SHIFT_PRESS | 0x80) +#define LEFT_CTRL_PRESS 0x1D +#define LEFT_CTRL_RELEASE (LEFT_CTRL_PRESS | 0x80) +#define RIGHT_CTRL_PRESS 0x1D +#define RIGHT_CTRL_RELEASE (RIGHT_CTRL_PRESS | 0x80) +#define LEFT_ALT_PRESS 0x38 +#define LEFT_ALT_RELEASE (LEFT_ALT_PRESS | 0x80) +#define RIGHT_ALT_PRESS 0x38 +#define RIGHT_ALT_RELEASE (RIGHT_ALT_PRESS | 0x80) + +#define MAX_BUFFER_SIZE 256 + +void setupKeyboard(); +char* read(char terminator, uint8_t attr); + +#endif diff --git a/src/kernel/main.c b/src/kernel/main.c index d4dae1e..70a8a90 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -2,14 +2,19 @@ #include "idt.h" #include "isrs.h" #include "irq.h" +#include "keyboard.h" +#include "system.h" void main() { // Interrupts setupIdt(); setupIsrs(); setupIrqs(); + sti(); + + setupKeyboard(); clear(0x0f); - print("kern.\n", 0x0f); + print("kern.\n\n", 0x0f); } diff --git a/src/kernel/screen.c b/src/kernel/screen.c index 095df7a..133f716 100644 --- a/src/kernel/screen.c +++ b/src/kernel/screen.c @@ -8,6 +8,12 @@ void putc(char c, uint8_t attr) { uint16_t line = cursor - (cursor % COLS); setCursorPos(line + COLS); return; + } else if (c == '\b') { + cursor--; + int mem = VIDMEM + cursor * 2; + *(char*)(mem) = ' '; + *(char*)(mem+1) = attr; + return; } int mem = VIDMEM + cursor * 2; @@ -48,8 +54,10 @@ void clear(uint8_t attr) { *(char*)(VIDMEM + i) = ' '; *(char*)(VIDMEM + i + 1) = attr; } + cursor = 0; } + void printHex(uint8_t d, uint8_t attr) { char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; uint8_t low = d & 0x0f;