diff --git a/Makefile b/Makefile index 8e4a43d..a43b393 100644 --- a/Makefile +++ b/Makefile @@ -64,8 +64,8 @@ obj/stdlib.o: src/stdlib.c include/stdlib.h include/varargs.h \ $(CC) $(FLAGS) $(CCFLAGS) -o $@ -c src/stdlib.c # src/timer.c -obj/timer.o: src/timer.c include/timer.h include/interrupts.h \ - include/cpustate.h +obj/timer.o: src/timer.c include/timer.h include/kernel.h \ + include/interrupts.h include/cpustate.h $(CC) $(FLAGS) $(CCFLAGS) -o $@ -c src/timer.c # src/vmm.c diff --git a/include/console.h b/include/console.h index 5445dfa..3eb21b5 100644 --- a/include/console.h +++ b/include/console.h @@ -5,7 +5,7 @@ extern "C" { #endif #define CONSOLE_WIDTH 80 -#define CONSOLE_HEIGHT 25 +#define CONSOLE_HEIGHT 24 #define COLOR_BLACK 0x0 #define COLOR_BLUE 0x1 @@ -23,6 +23,12 @@ extern "C" { #define COLOR_YELLOW 0xE #define COLOR_WHITE 0xF +/** + * @brief Sets the console status bar + * @param text + */ +void console_setstate(const char *text); + /** * @brief Sets the cursor position. * @param x Distance of the cursor from the left border. diff --git a/include/timer.h b/include/timer.h index 18446f1..8a1a8e0 100644 --- a/include/timer.h +++ b/include/timer.h @@ -25,6 +25,13 @@ time_t __cdecl timer_get(); */ void __cdecl timer_set(time_t time); +/** + * @brief Adds a callback to the timer. + * @param interval The period time when the callback is called. + * @param callback The callback that is called. + */ +void __cdecl timer_add_callback(time_t interval, void (*callback)(time_t)); + /** * @brief Waits until a certain time elapsed. * @param ticks The number of ticks to wait diff --git a/scripts/main.ts b/scripts/main.ts index e3abe7e..3a1447b 100644 --- a/scripts/main.ts +++ b/scripts/main.ts @@ -5,7 +5,10 @@ BEGIN 0 -> i; # Print numbers from 1 to 5 WHILE ((i + 1) -> i) <= 5 DO + BEGIN printInt(i); + sleep(20); + END END PUB fun() -> i : INT diff --git a/src/console.c b/src/console.c index a82dda3..1881544 100644 --- a/src/console.c +++ b/src/console.c @@ -28,6 +28,21 @@ static ConsoleState state = csDefault; static ConsoleChar *screen = (ConsoleChar*)0xB8000; +static ConsoleChar *statusbar = (ConsoleChar*)(0xB8000 + 0x02 * CONSOLE_WIDTH * CONSOLE_HEIGHT); + +void console_setstate(const char *text) +{ + for(size_t i = 0; i < CONSOLE_WIDTH; i++) { + statusbar[i].c = ' '; + statusbar[i].color = 0x70; + } + ConsoleChar *ptr = statusbar; + while(*text) { + (ptr++)->c = *text++; + } + +} + void ksetpos(int x, int y) { if((x >= 0) && (x < CONSOLE_WIDTH)) diff --git a/src/init.c b/src/init.c index b112475..e0f096b 100644 --- a/src/init.c +++ b/src/init.c @@ -18,6 +18,41 @@ void die(const char *msg) } } +extern size_t mallocCount; +extern size_t freeCount; +extern size_t allocatedMemory; + +void update_statusbar(time_t t) +{ + size_t a, b; + static char tmp[64]; + static char buffer[CONSOLE_WIDTH]; + memset(buffer, ' ', sizeof(char) * CONSOLE_WIDTH); + + memcpy(&buffer[0], "Malloc: ", 8); + + b = 8; + a = strlen(itoa(freeCount, tmp, 10)); + memcpy(&buffer[8], tmp, a); + b += a; + + buffer[b] = '/'; b++; + + a = strlen(itoa(mallocCount, tmp, 10)); + memcpy(&buffer[b], tmp, a); + b += a; + + b += 2; + + a = strlen(itoa(allocatedMemory, tmp, 10)); + memcpy(&buffer[b], tmp, a); + b += a; + + memcpy(&buffer[b], " Byte", 5); + + console_setstate(buffer); +} + static void debug_test() { char buffer[64]; @@ -31,12 +66,13 @@ static void debug_test() kprintf("This %s %c test line.\n", "is", 'a'); kprintf("Numbers: %d %i %x %b\n", 15, 15, 15, 15); - /* + ///* kputs("scroll-test:\n"); - for(int i = 0; i < 100; i++) + for(int i = 0; i < 10; i++) { kprintf("They see me scrolling, they hating! %i\n", i); - } + } + /* for(int i = 0; i < 272; i++) { kprintf("x"); @@ -136,7 +172,7 @@ void init(const MultibootStructure *mbHeader) putsuccess(); kputs("Prepare heap memory:"); - for(uintptr_t ptr = 0x400000; ptr < 0x800000; ptr += 4096) + for(uintptr_t ptr = 0x400000; ptr < 0x800000; ptr += 4096) { vmm_map(ptr, (uintptr_t)pmm_alloc(), VM_PROGRAM); } @@ -148,11 +184,11 @@ void init(const MultibootStructure *mbHeader) kputs("Initialize C++ objects: "); cpp_init(); - putsuccess(); + putsuccess(); - double f = 1.0; + timer_add_callback(1, update_statusbar); - int i = (int)f; + debug_test(); vm_start(); @@ -171,3 +207,8 @@ int main(int argc, char **argv) void __init_array_start() { } void __init_array_end() { } + + + + + diff --git a/src/interrupts.c b/src/interrupts.c index 038a14d..5aef37f 100644 --- a/src/interrupts.c +++ b/src/interrupts.c @@ -85,7 +85,11 @@ void intr_routine(CpuState *state) if(handler != nullptr) { handler(state); } else { - kprintf("\n\x12\x04Exception [%d] %s!\x12\0x7\n", state->intr, name); + kprintf("\n\x12\x04 Exception [%d] %s!\x12\0x07 \n", state->intr, name); + kprintf( + "EIP: %x" + "", + state->eip); while(1) { __asm__ volatile("cli; hlt"); diff --git a/src/malloc.c b/src/malloc.c index cfe75f4..b835d9f 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -41,6 +41,9 @@ struct __freelist { struct __freelist *nx; }; +size_t mallocCount = 0, freeCount = 0; +size_t allocatedMemory = 0; + size_t __malloc_margin = 32; char * const __malloc_heap_start = (char *)0x400000; char * const __malloc_heap_end = (char *)0x800000; @@ -50,6 +53,8 @@ struct __freelist *__flp; void *malloc(size_t len) { + allocatedMemory += len; + mallocCount++; struct __freelist *fp1, *fp2, *sfp1, *sfp2; char *cp; size_t s, avail; @@ -168,6 +173,7 @@ void *malloc(size_t len) void free(void *p) { + freeCount++; struct __freelist *fp1, *fp2, *fpnew; char *cp1, *cp2, *cpnew; diff --git a/src/timer.c b/src/timer.c index d93675b..1e9ce35 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,12 +1,31 @@ +#include +#include +#include +#include -#include "timer.h" -#include "interrupts.h" +#define CALLBACK_COUNT 64 static volatile time_t ticks = 0; +static struct { + time_t interval; + void (*callback)(time_t); +} callbacks[CALLBACK_COUNT]; + static void timer_irq() { ticks++; + + irq_disable(); + for(size_t i = 0; i < CALLBACK_COUNT; i++) { + if(callbacks[i].callback != nullptr) { + if((ticks % callbacks[i].interval) == 0) { + callbacks[i].callback(ticks); + } + return; + } + } + irq_enable(); } void timer_init() @@ -25,6 +44,18 @@ void timer_set(time_t time) ticks = time; } +void timer_add_callback(time_t interval, void (*callback)(time_t)) +{ + for(size_t i = 0; i < CALLBACK_COUNT; i++) { + if(callbacks[i].callback == nullptr) { + callbacks[i].interval = interval; + callbacks[i].callback = callback; + return; + } + } + die("no timer callbacks left."); +} + void sleep(time_t t) { uint64_t end = timer_get() + t;