diff --git a/core/main.c b/core/main.c index 75eee39..ddffa00 100644 --- a/core/main.c +++ b/core/main.c @@ -380,8 +380,6 @@ void kmain(unsigned long magic, unsigned long addr) pagingSetup(firstUsedByMem, lastUsedByMem); VGAMap(); - printf("[Setup] IRQ handlers\n"); - irqSetRoutineWrapped(IRQ_KEYBOARD, keyboard_do_irq); printf("[Setup] HW interrupts\n"); // Enabling the HW interrupts @@ -394,6 +392,10 @@ void kmain(unsigned long magic, unsigned long addr) printf("[Setup] allocation system\n"); areaInit(firstUsedByMem, lastUsedByMem, _stack_bottom, _stack_top); + printf("[Setup] IRQ handlers\n"); + keyboardSetup(); + irqSetRoutineWrapped(IRQ_KEYBOARD, keyboard_do_irq); + mmuContextSetup(); cpu_context_subsystem_setup(); diff --git a/core/syscall.c b/core/syscall.c index 18c757d..fe350a2 100644 --- a/core/syscall.c +++ b/core/syscall.c @@ -1,3 +1,4 @@ +#include "keyboard.h" #include "klibc.h" #include "stdarg.h" #include "syscall.h" @@ -24,6 +25,9 @@ int syscallExecute(int syscallId, const struct cpu_state *userCtx){ ret = syscallGet1arg(userCtx, &c); putc(c); break; + case SYSCALL_ID_READ: + ret = keyboardRead(); + break; default: printf("Unknon syscall id %d\n", syscallId); ret = -ENOENT; diff --git a/core/syscall.h b/core/syscall.h index 617387f..3eb77dc 100644 --- a/core/syscall.h +++ b/core/syscall.h @@ -6,6 +6,7 @@ #define SYSCALL_ID_EXIT 1 #define SYSCALL_ID_YOLO 2 #define SYSCALL_ID_PUTC 3 +#define SYSCALL_ID_READ 4 #ifdef __KERNEL__ int syscallExecute(int syscallId, const struct cpu_state *user_ctx); diff --git a/drivers/keyboard.c b/drivers/keyboard.c index d64600c..bda6b27 100644 --- a/drivers/keyboard.c +++ b/drivers/keyboard.c @@ -1,6 +1,18 @@ #include "keyboard.h" #include "io.h" #include "klibc.h" +#include "ringbuffer.h" + +#define KEYBOARD_BUFFER_SIZE 128 + +static ringbuffer_t buf; + +int keyboardSetup() +{ + buf = ringbufferCreate(KEYBOARD_BUFFER_SIZE); + + return 0; +} const char *scancode[128] = { /* 0 */ 0, @@ -308,6 +320,18 @@ void keyboard_do_irq() } } - if (key) + if (key){ printf(key); + ringbufferEnqueue(buf, *key); + } +} + +char keyboardRead() +{ + uint8_t c; + if (ringbufferDequeue(buf, &c)){ + return c; + } + + return 0; } diff --git a/drivers/keyboard.h b/drivers/keyboard.h index 9e4f3fe..2882943 100644 --- a/drivers/keyboard.h +++ b/drivers/keyboard.h @@ -6,3 +6,6 @@ #define KEYBOARD_DATA_PORT 0x60 void keyboard_do_irq(); + +int keyboardSetup(); +char keyboardRead(); diff --git a/userspace/kernel/syscall.h b/userspace/kernel/syscall.h index 617387f..3eb77dc 100644 --- a/userspace/kernel/syscall.h +++ b/userspace/kernel/syscall.h @@ -6,6 +6,7 @@ #define SYSCALL_ID_EXIT 1 #define SYSCALL_ID_YOLO 2 #define SYSCALL_ID_PUTC 3 +#define SYSCALL_ID_READ 4 #ifdef __KERNEL__ int syscallExecute(int syscallId, const struct cpu_state *user_ctx); diff --git a/userspace/libc.c b/userspace/libc.c index 442fed3..657a41b 100644 --- a/userspace/libc.c +++ b/userspace/libc.c @@ -512,3 +512,38 @@ void yolo() { syscall0(SYSCALL_ID_YOLO); } + +char readc() +{ + return syscall0(SYSCALL_ID_READ); +} + +char readcBlock() +{ + char c = 0; + do { + c = readc(); + } while (c == 0); + return c; +} + +int readline(char *buf, int size) +{ + int i = 0; + for (; i < size - 1; i++) { + char key = readcBlock(); + + if (key == '\n') + break; + + if(key == '\b' && i>=1){ + buf[i-1] = '\0'; + i-=2; + } + + buf[i] = key; + } + buf[i] = '\0'; + return i == (size-1); +} + diff --git a/userspace/libc.h b/userspace/libc.h index c93d8ba..91031e5 100644 --- a/userspace/libc.h +++ b/userspace/libc.h @@ -37,3 +37,6 @@ int syscall0(int id); void _exit(int status); void yolo(); +char readc(); +char readcBlock(); +int readline(char *buf, int size); diff --git a/userspace/main_user.c b/userspace/main_user.c index 7c6dae7..04eb2cf 100644 --- a/userspace/main_user.c +++ b/userspace/main_user.c @@ -1,12 +1,50 @@ #include "libc.h" +int func_yolo() +{ + yolo(); + return 0; +} + +int func_help() +{ + printf("\nAvailable Commands:\n"); + printf(" yolo\n"); + printf(" suicide\n"); + printf(" help\n"); + return 0; +} + +int func_suicide() +{ + printf("User is about to suicide\n"); + int *yolo = 0; + *yolo = 1; + return 0; +} + int main(int argc, char *argv[]) { (void)argc; (void)argv; - yolo(); - printf("User is about to suicide\n"); - int *yolo =0; - *yolo = 1; + char buf[64]; + printf("Shell staring. type \"help\" for help\n"); + while (1) { + printf(">"); + if (readline(buf, sizeof(buf))) + continue; + if (strcmp(buf, "yolo") == 0) { + func_yolo(); + continue; + } + if (strcmp(buf, "help") == 0) { + func_help(); + continue; + } + if (strcmp(buf, "suicide") == 0) { + func_suicide(); + continue; + } + } return 0; }