feat(keyboard): implement generic ring buffer and add getkey

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
Julien CLEMENT 2021-07-26 10:41:51 +02:00
parent 0e43e73fc7
commit cf020eae84
7 changed files with 91 additions and 3 deletions

@ -35,7 +35,8 @@ OBJS = \
events/idt.o \
events/isr.o \
events/pic/pic.o \
events/pic/keyboard.o
events/pic/keyboard.o \
utils/ring_buffer.o
DEPS = $(OBJS:.o=.d)

@ -1,7 +1,26 @@
#include "keyboard.h"
#include "io.h"
#include "utils/ring_buffer.h"
static struct ring_buffer keyboard_buffer;
void keyboard_handler(void)
{
inb(KEYBOARD_IO);
u8 scan_code = inb(KEYBOARD_IO);
if (!(scan_code & 0x80))
ring_buffer_push(&keyboard_buffer, &scan_code);
}
int getkey(void)
{
u8 *key = ring_buffer_pop(&keyboard_buffer);
if (!key)
return -1;
return *key;
}
void init_keyboard(void)
{
ring_buffer_init(&keyboard_buffer, sizeof(u8));
}

@ -6,4 +6,8 @@
void keyboard_handler(void);
int getkey(void);
void init_keyboard(void);
#endif /* !KEYBOARD_H */

@ -2,6 +2,7 @@
#include "io.h"
#include "pic.h"
#include "stdio.h"
#include "keyboard.h"
static void send_icw1(void)
{
@ -41,12 +42,12 @@ void init_pic(void)
send_icw3();
send_icw4();
mask_irqs();
init_keyboard();
asm volatile("sti");
}
void acknowledge(uint32_t int_vector)
{
printf("acknowledging %d\r\n", int_vector);
uint8_t ocw2 = 0x20;
if (int_vector >= IDT_RESERVED_ENTRIES + 8)
outb(SLAVE_PIC_A, ocw2);

33
k/utils/ring_buffer.c Normal file

@ -0,0 +1,33 @@
#include "ring_buffer.h"
#include "string.h"
void ring_buffer_init(struct ring_buffer *buffer, u32 el_size)
{
buffer->start = 0;
buffer->end = 0;
buffer->element_size = el_size;
buffer->size = 0;
}
void ring_buffer_push(struct ring_buffer *buffer, void *el)
{
if (buffer->size >= RING_BUFFER_SIZE)
return;
char *dst = buffer->buffer + buffer->start;
memcpy(dst, el, buffer->element_size);
buffer->start = (buffer->start + buffer->element_size) % RING_BUFFER_SIZE;
buffer->size += buffer->element_size;
}
void *ring_buffer_pop(struct ring_buffer *buffer)
{
if (buffer->size == 0)
return NULL;
void *res = buffer->buffer + buffer->end;
buffer->end = (buffer->end + buffer->element_size) % RING_BUFFER_SIZE;
buffer->size -= buffer->element_size;
return res;
}

21
k/utils/ring_buffer.h Normal file

@ -0,0 +1,21 @@
#ifndef RING_BUFFER_H
#define RING_BUFFER_H
#define RING_BUFFER_SIZE 256
#include "k/types.h"
struct ring_buffer
{
char buffer[RING_BUFFER_SIZE];
u32 start;
u32 end;
u32 element_size;
u32 size;
};
void ring_buffer_init(struct ring_buffer *buffer, u32 el_size);
void ring_buffer_push(struct ring_buffer *buffer, void *el);
void *ring_buffer_pop(struct ring_buffer *buffer);
#endif /* !RING_BUFFER_H */

9
k/utils/utils.h Normal file

@ -0,0 +1,9 @@
#ifndef UTILS_H
#define UTILS_H
static void *cast(void *ptr)
{
return ptr;
}
#endif /* !UTILS_H */