diff --git a/Makefile b/Makefile index eaa401a..a33bbe3 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,6 @@ CFLAGS = -ffreestanding -m32 -Iinclude -Wall -Wextra SRC = ./src BUILD = ./build C_FILES = calc -# C_FILES = $(shell find $(SRC) -name "*.c" -exec basename -s ".c" {} \;) .PHONY: os clean run @@ -45,13 +44,13 @@ kernel: $(SRC)/kernel.c $(SRC)/entry.asm @$(AS) -felf32 -o $(BUILD)/entry.o $(SRC)/entry.asm @echo "Compiling $(SRC)/kernel.c..." @$(CC) $(CFLAGS) -o $(BUILD)/kernel.o -c $(SRC)/kernel.c - @$(LD) -T $(SRC)/kernel.ld --oformat binary -o $(BUILD)/kernel.bin $(BUILD)/*.o + @$(LD) -T$(SRC)/kernel.ld --oformat binary -o $(BUILD)/kernel.bin $(BUILD)/*.o @rm $(BUILD)/*.o $(C_FILES): @echo "Compiling $(SRC)/$@.c..." @$(CC) $(CFLAGS) -o $(BUILD)/$@.o -c $(SRC)/$@.c - @$(LD) -T $(SRC)/$@.ld --oformat binary -o $(BUILD)/$@.bin $(BUILD)/$@.o + @$(LD) -T$(SRC)/$@.ld --oformat binary -o $(BUILD)/$@.bin $(BUILD)/$@.o @rm $(BUILD)/$@.o # Run QEMU diff --git a/README.md b/README.md index a555079..d19b917 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Since there's always more to do, here are the [todos](todo.txt). - Disk I/O - Font support (funky format) - 1920x1080 Resolution, 32bpp +- Physical memory manager ## Building & Running If you want to try kern for some reason, you can. @@ -52,7 +53,7 @@ Disk R/W: \ _These images may be outdated, Idk_ ## Known issues -- Idk, they're probably just unknown ;) +- If you run kernel.bin from shell, it will clear the memory map so when you load anything it will override the kernel code ## [Old Version](https://github.com/Tom-on64/old-kern) diff --git a/include/graphics.h b/include/graphics.h index d6cbddc..442352b 100644 --- a/include/graphics.h +++ b/include/graphics.h @@ -18,6 +18,8 @@ #define rgb(r,g,b) ( ((r) << 16 | (g) << 8 | (b)) & 0x00ffffff ) void drawPixel(int32_t x, int32_t y, uint32_t color) { + uint32_t* vidmem = *(uint32_t**)(MODE_INFO_BLOCK + 40); + uint32_t offset = (y * WIDTH) + x; vidmem[offset] = color; } @@ -94,6 +96,8 @@ void drawCircle(int32_t x, int32_t y, uint16_t radius, uint32_t color) { // Recursive (may need large stack) void boundaryFill(int32_t x, int32_t y, uint32_t fillColor, uint32_t boundaryColor) { + uint32_t* vidmem = *(uint32_t**)(MODE_INFO_BLOCK + 40); + uint32_t offset = y * WIDTH + x; if (vidmem[offset] != fillColor && vidmem[offset] != boundaryColor) { diff --git a/include/physicalMemManager.h b/include/physicalMemManager.h index 398fb80..5341e1a 100644 --- a/include/physicalMemManager.h +++ b/include/physicalMemManager.h @@ -2,6 +2,7 @@ #define PHYSICAL_MEM_MANAGER_H #include "string.h" +#include "stdint.h" #define BLOCK_SIZE 4096 // 4kB #define BLOCKS_PER_BYTE 8 diff --git a/include/screen.h b/include/screen.h index a3d96a0..227ca38 100644 --- a/include/screen.h +++ b/include/screen.h @@ -14,19 +14,13 @@ struct cursor_s { uint32_t y; }; -static char* font; -uint32_t* vidmem; -uint16_t bytesPerScanline; +static char* font = (char*)0x6000; static struct cursor_s cursor = { 0, 0 }; -void setupScreen() { - vidmem = *(uint32_t**)(MODE_INFO_BLOCK + 40); // Vidmem pointer location - bytesPerScanline = *(uint16_t*)(MODE_INFO_BLOCK + 16); // Bytes per scanline location -} - // TODO: Probably make the font bigger in a better way, but this works for now void putcAt(unsigned char c, uint32_t x, uint32_t y) { + uint32_t* vidmem = *(uint32_t**)(MODE_INFO_BLOCK + 40); uint32_t offset = (WIDTH * y * 16 + x * 8) * 2; char* bitmap = &font[((c % 128) - 1) * 16]; @@ -73,6 +67,8 @@ void print(const char* str) { } void clear() { + uint32_t* vidmem = *(uint32_t**)(MODE_INFO_BLOCK + 40); + size_t i = 0; while (i < WIDTH*HEIGHT) { vidmem[i] = BG_COLOR; // #1f2937 @@ -83,8 +79,4 @@ void clear() { cursor.y = 0; } -void loadFont(char* address) { - font = address; -} - #endif diff --git a/notes.txt b/notes.txt index 6477ce0..97076d9 100644 --- a/notes.txt +++ b/notes.txt @@ -1,8 +1,8 @@ --- Memory Map --- -0x1000 - Kernel -0x5000 - VBE Mode Info Block -0x6000 - Font -0x7000 - Filetable (0x7c00 - Bootloader) -... -0x10000 - Loaded programs +0x1000 - Kernel +0x5000 - VBE Mode Info Block +0x6000 - Font +0x7000 - Filetable (0x7c00 - Bootloader) +0x8500 - ACPI Memory map +0x30000 - Memory manager memmap diff --git a/src/calc.c b/src/calc.c index d4d55d9..144334d 100644 --- a/src/calc.c +++ b/src/calc.c @@ -1,16 +1,12 @@ #include "stdint.h" +#include "screen.h" +__attribute__ ((section ("entry"))) void main() { - uint32_t wait = 0xfffffff; - - uint32_t* vidmem = *(uint32_t**)(0x5000 + 40); - - for (uint32_t x = 200; x < 400; x++) { - for (uint32_t y = 200; y < 400; y++) { - vidmem[y * 1920 + x] = 0x00ffffff; - } - } + uint32_t wait = 0xffffffff; + putc('a'); + while (wait--); return; diff --git a/src/calc.ld b/src/calc.ld index 732a574..fd8721d 100644 --- a/src/calc.ld +++ b/src/calc.ld @@ -1,5 +1,6 @@ SECTIONS { - .text 0x10000 : { + .text : { + *(entry); *(.text*); } .data : { diff --git a/src/entry.asm b/src/entry.asm index 15c290a..7e2e899 100644 --- a/src/entry.asm +++ b/src/entry.asm @@ -11,6 +11,7 @@ _start: pop ax mov [driveNum], al + call main ; Exit QEMU diff --git a/src/kernel.c b/src/kernel.c index 820c956..2de06de 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -19,6 +19,7 @@ uint32_t memmapAddr = 0x30000; void printFiletable(char* ft); void printPhysicalMemmap(); +__attribute__ ((section ("entry"))) // Make sure that main() is at the start of kernel.bin void main() { // Interrupts setupIdt(); @@ -49,8 +50,7 @@ void main() { // Screen setup // TODO: Find font in filetable diskRead(36, 4, (char*)0x6000); // Read font from disk - setupScreen(); - loadFont((char*)0x6000); + // loadFont((char*)0x6000); clear(); print("kern.\n\n"); @@ -229,27 +229,52 @@ void main() { uint8_t size = *ft++; found++; // Set found to 1 - char buf[size * 512]; - diskRead(sector, size, buf); - char* fileType = strchr(filename, '.') + 1; + uint32_t neededBlocks = (size * 512) / BLOCK_SIZE + 1; + + /* Debug printing + print("Allocating "); + print(itoa(neededBlocks, 10)); + print(" Block"); + if (neededBlocks > 1) putc('s'); + */ + + char* address = allocBlocks(neededBlocks); + + if (address == NULL) { + print("\nNot enough memory to allocate!\n"); + break; + } + + /* Debug printing + print("\nAllocated to address 0x"); + print(itoa((uint32_t)address, 16)); + print("!\n"); + */ + + diskRead(sector, size, address); + if (strcmp(fileType, "bin") == 0) { - memcopy(buf, (char*)0x10000, size * 512); clear(); - ((void(*)(void))0x10000)(); // Call program + ((void(*)(void))address)(); // Call program clear(); } else if (strcmp(fileType, "tab") == 0) { - printFiletable(buf); + printFiletable(address); } else { for (size_t i = 0; i < size * 512; i++) { - if (buf[i] != '\0') { - putc(buf[i]); + if (address[i] != '\0') { + putc(address[i]); } } putc('\n'); } + // Debug printing + // print("Freeing allocated memory...\n"); + freeBlocks(address, neededBlocks); + // print("Done!\n"); + break; } diff --git a/src/kernel.ld b/src/kernel.ld index b95856d..0a4ca21 100644 --- a/src/kernel.ld +++ b/src/kernel.ld @@ -1,5 +1,6 @@ SECTIONS { .text 0x1000 : { + *(entry); *(.text*); } .data : {