Add lib/mem.*.

This commit is contained in:
Alexander Myltsev 2022-12-13 19:12:31 +03:00
parent b0c53f6c83
commit 529ba03e50
4 changed files with 24 additions and 4 deletions

View File

@ -59,8 +59,8 @@ image.bin: mbr.bin fs.img
cat $^ >$@ cat $^ >$@
kernel.bin: kernel.o console.o drivers/vga.o drivers/keyboard.o \ kernel.bin: kernel.o console.o drivers/vga.o drivers/keyboard.o \
string.o drivers/ata.o cpu/vectors.o cpu/idt.o cpu/gdt.o drivers/uart.o \ drivers/ata.o cpu/vectors.o cpu/idt.o cpu/gdt.o drivers/uart.o \
fs/fs.o fs/fs.o lib/mem.o lib/string.o
$(LD) $(LDFLAGS) -o $@ -Ttext 0x1000 $^ $(LD) $(LDFLAGS) -o $@ -Ttext 0x1000 $^
%.o: %.c %.o: %.c

View File

@ -2,6 +2,7 @@
#include "../cpu/isr.h" #include "../cpu/isr.h"
#include "../console.h" #include "../console.h"
#include "port.h" #include "port.h"
#include "../lib/mem.h"
static const char sc_ascii[] = { static const char sc_ascii[] = {
'?', '?', '1', '2', '3', '4', '5', '6', '?', '?', '1', '2', '3', '4', '5', '6',
@ -11,11 +12,13 @@ static const char sc_ascii[] = {
'b', 'n', 'm', ',', '.', '/', '?', '?', '?', ' ', 'b', 'n', 'm', ',', '.', '/', '?', '?', '?', ' ',
}; };
enum { kbd_buf_capacity = 1024 };
static void interrupt_handler(registers_t *r) { static void interrupt_handler(registers_t *r) {
uint8_t scancode = port_byte_in(0x60); uint8_t scancode = port_byte_in(0x60);
if (scancode < sizeof(sc_ascii)) { if (scancode < sizeof(sc_ascii)) {
char c = sc_ascii[scancode]; char c = sc_ascii[scancode];
if (kbd_buf_size < 1024) { if (kbd_buf_size < kbd_buf_capacity) {
kbd_buf[kbd_buf_size++] = c; kbd_buf[kbd_buf_size++] = c;
} }
char string[] = {c, '\0'}; char string[] = {c, '\0'};
@ -27,7 +30,7 @@ char* kbd_buf;
unsigned kbd_buf_size; unsigned kbd_buf_size;
void init_keyboard() { void init_keyboard() {
kbd_buf = (char*)(1 << 20); kbd_buf = kmalloc(kbd_buf_capacity);
register_interrupt_handler(IRQ1, interrupt_handler); register_interrupt_handler(IRQ1, interrupt_handler);
} }

12
lib/mem.c Normal file
View File

@ -0,0 +1,12 @@
#include "mem.h"
static void* freeptr;
void* kmalloc(size_t size) {
if (!freeptr) {
freeptr = (void*)(1<<20);
}
void* result = freeptr;
freeptr += size;
return result;
}

5
lib/mem.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
typedef unsigned size_t;
void* kmalloc(size_t size);