OS now allocs memory for called files. Slightly improved screen.h

This commit is contained in:
2024-04-26 11:50:37 +02:00
parent 2b7426b88f
commit c3d2ccd90d
11 changed files with 63 additions and 42 deletions
+2 -3
View File
@@ -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
+2 -1
View File
@@ -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)
+4
View File
@@ -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) {
+1
View File
@@ -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
+4 -12
View File
@@ -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
+6 -6
View File
@@ -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
+4 -8
View File
@@ -1,15 +1,11 @@
#include "stdint.h"
#include "screen.h"
__attribute__ ((section ("entry")))
void main() {
uint32_t wait = 0xfffffff;
uint32_t wait = 0xffffffff;
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;
}
}
putc('a');
while (wait--);
+2 -1
View File
@@ -1,5 +1,6 @@
SECTIONS {
.text 0x10000 : {
.text : {
*(entry);
*(.text*);
}
.data : {
+1
View File
@@ -11,6 +11,7 @@
_start:
pop ax
mov [driveNum], al
call main
; Exit QEMU
+35 -10
View File
@@ -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;
}
+1
View File
@@ -1,5 +1,6 @@
SECTIONS {
.text 0x1000 : {
*(entry);
*(.text*);
}
.data : {