From 11ced165e0df11cc3c889eb0cc402467361c421b Mon Sep 17 00:00:00 2001 From: Aleksa Vuckovic Date: Thu, 1 Sep 2022 23:45:47 +0200 Subject: timer & stdbuff --- kernel/src/devices/keyboard.c | 41 ++++++++++++++++++++++++++++++----------- kernel/src/devices/timer.c | 27 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 kernel/src/devices/timer.c (limited to 'kernel/src/devices') diff --git a/kernel/src/devices/keyboard.c b/kernel/src/devices/keyboard.c index f39f978..60f41b8 100644 --- a/kernel/src/devices/keyboard.c +++ b/kernel/src/devices/keyboard.c @@ -1,42 +1,61 @@ #include #include - #include #include #include #include #include - -#define KEYBOARD_DATA_PORT 0x60 -#define KEYBOARD_STATUS_PORT 0x64 +#include bool is_pressed[128]; -void keyboard_handler(void) +#include +#include +#define BUFFER_SIZE 10000 +stdbuff* keyboard_buffer; + +void keyboard_handler() { + if (keyboard_buffer == NULL) { + keyboard_buffer = init_buff(BUFFER_SIZE); + memset(keyboard_buffer->data, ' ', BUFFER_SIZE); + } + uint8_t status = inb(KEYBOARD_STATUS_PORT); if (!(status & 0x1)) { return; } + uint8_t keycode = inb(KEYBOARD_DATA_PORT); + if (keycode < keymap_len) { is_pressed[keycode] = true; if (keycode == KEY_SPACE) { - printf(" "); - + write_buff(keyboard_buffer, " ", 1); } else if (keycode == KEY_BACKSPACE) { if (curr_x != 0) - printf("\b \b"); + write_buff(keyboard_buffer, "\b \b", 3); } else if (keycode == KEY_ENTER) { - printf("%c", keymap[keycode]); + char* output = kalloc(sizeof(char) + 1); + output[0] = keymap[keycode]; + write_buff(keyboard_buffer, output, 1); + kfree(output); } else { + char* output = kalloc(sizeof(char) + 1); if (keymap[keycode] == ' ') return; if (is_pressed[KEY_LSHIFT] || is_pressed[KEY_RSHIFT]) - printf("%c", shift_keymap[keycode]); + output[0] = shift_keymap[keycode]; else - printf("%c", keymap[keycode]); + output[0] = keymap[keycode]; + write_buff(keyboard_buffer, output, 1); + kfree(output); } } else { is_pressed[keycode - 128] = false; } + + uint32_t len = (uint32_t)(keyboard_buffer->head - keyboard_buffer->tail); + char* print_buff = kalloc(len + 1); + read_buff(keyboard_buffer, print_buff, len); + printf("%s", print_buff); } diff --git a/kernel/src/devices/timer.c b/kernel/src/devices/timer.c new file mode 100644 index 0000000..1291219 --- /dev/null +++ b/kernel/src/devices/timer.c @@ -0,0 +1,27 @@ +#include +#include +#include + +uint32_t tick = 0; +uint32_t seconds = 0; + +void timer_handler() +{ + tick++; + if (tick > TICKS_PER_SECOND) { + tick = 0; + seconds++; + } +} + +void init_timer(uint32_t frequency) +{ + uint32_t divisor = 1193180 / frequency; + outb(0x43, 0x36); + + uint8_t l = (uint8_t)(divisor & 0xFF); + uint8_t h = (uint8_t)((divisor>>8) & 0xFF); + + outb(0x40, l); + outb(0x40, h); +} -- cgit v1.2.3