diff --git a/Makefile b/Makefile index 8fe1023..50c1786 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ GCCPATH = /opt/gcc-arm/bin CC = $(GCCPATH)/aarch64-none-elf-gcc LD = $(GCCPATH)/aarch64-none-elf-ld OBJCOPY = $(GCCPATH)/aarch64-none-elf-objcopy -GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles +GCCFLAGS = -Wall -O2 -ffreestanding -fno-builtin -nostdinc -nostdlib -nostartfiles OUTDIR = ./out/bin diff --git a/include/memory.h b/include/memory.h new file mode 100644 index 0000000..19536fd --- /dev/null +++ b/include/memory.h @@ -0,0 +1,8 @@ +#pragma once + +#define NULL ((void*)0) + +void heap_init(); + +void* malloc(); +void free(void* ptr); \ No newline at end of file diff --git a/src/convert.c b/src/convert.c index 6633e52..3a94342 100644 --- a/src/convert.c +++ b/src/convert.c @@ -1,5 +1,6 @@ #include "convert.h" #include "io.h" +#include "memory.h" int stoi(const char* string) { @@ -29,7 +30,9 @@ char* ultoa(unsigned long number, char* string, int base) return string; } - volatile char* temp = (volatile char*)0x40000000; + char* buffer = (char*)malloc(); + char* temp = buffer; + int i = 0; if(number == 0) { @@ -39,7 +42,12 @@ char* ultoa(unsigned long number, char* string, int base) { for(; number > 0; number /= base) { - temp[i++] = '0' + (number % base); + uint8_t digit = number % base; + + if(digit < 10) + temp[i++] = '0' + digit; + else + temp[i++] = 'A' + digit; } } @@ -51,6 +59,7 @@ char* ultoa(unsigned long number, char* string, int base) string[--i] = *temp++; } + free(buffer); return string; } diff --git a/src/kernel.c b/src/kernel.c index 2906bb7..540cdad 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,6 +1,7 @@ #include "io.h" #include "convert.h" #include "string.h" +#include "memory.h" void print_clippy(); char* get_user_input(char* buffer); @@ -12,7 +13,8 @@ void main() char buffer[1024]; uart_init(); - + heap_init(); + print_clippy(); uart_puts("Boot successful! \n"); uart_puts("Started execution at 0x"); diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..f3895e8 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,70 @@ +#include "memory.h" + +#include "stdint.h" +#include "io.h" +#include "convert.h" + +struct FreeBlock +{ + struct FreeBlock* next; +}; + +#define HEAP_BASE ((void*)0x40000000) +#define BLOCK_SIZE 4096 +#define MAGIC_NUMBER 0xDEADBEEF + +static struct FreeBlock* head = (struct FreeBlock*)HEAP_BASE; + +void heap_init() +{ + head->next = NULL; +} + +void* malloc() +{ + void* block = (void*)head; + + if(head->next == NULL) + { + head += BLOCK_SIZE; + head->next = NULL; + } + else + { + head = head->next; + } + + *(uint32_t*)block = MAGIC_NUMBER; + return block + sizeof(uint32_t); +} + +void free(void* ptr) +{ + void* blockStart = ptr - sizeof(uint32_t); + if(*(uint32_t*)blockStart != MAGIC_NUMBER) + return; + + if((struct FreeBlock*)blockStart < head) + { + struct FreeBlock* nextBlock = head->next; + head = blockStart; + head->next = nextBlock; + return; + } + + struct FreeBlock* block = head; + + while(block) + { + if(block < (struct FreeBlock*)blockStart && (struct FreeBlock*)blockStart < block->next) + { + struct FreeBlock* newBlock = (struct FreeBlock*)blockStart; + newBlock->next = block->next; + block->next = newBlock; + + return; + } + + block = block->next; + } +} \ No newline at end of file