This commit is contained in:
nub31
2025-08-23 15:52:54 +02:00
parent 3b5e08951c
commit d11c1072f9
9 changed files with 143 additions and 99 deletions

View File

@@ -16,3 +16,5 @@ AllowShortLoopsOnASingleLine: false
SeparateDefinitionBlocks: Always
BreakBeforeBraces: Allman
Cpp11BracedListStyle: false

View File

@@ -1,7 +1,7 @@
[org 0x7c00]
[bits 16]
KERNEL_LOCATION equ 0x1000
KERNEL_INIT equ 0x1000
mov [BOOT_DISK], dl
@@ -11,7 +11,7 @@ mov ds, ax
mov bp, 0x8000
mov sp, bp
mov bx, KERNEL_LOCATION
mov bx, KERNEL_INIT
mov dh, 2
mov ah, 0x02
@@ -22,7 +22,6 @@ mov cl, 0x02
mov dl, [BOOT_DISK]
int 0x13
mov ah, 0x0
mov al, 0x3
int 0x10
@@ -41,7 +40,6 @@ jmp $
BOOT_DISK: db 0
gdt_start:
gdt_null:
dd 0x0
dd 0x0
@@ -79,9 +77,8 @@ start_protected_mode:
mov ebp, 0x90000
mov esp, ebp
jmp KERNEL_LOCATION
jmp $
jmp KERNEL_INIT
hlt
times 510-($-$$) db 0
dw 0xaa55

View File

@@ -1,5 +1,4 @@
section .text
[extern kernel_init]
[bits 32]
[extern init]
call init
jmp $
call kernel_init
hlt

View File

@@ -1,7 +1,52 @@
#include "print.h"
void init(void)
/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
char* itoa(int value, char* result, int base)
{
print_init();
println("Starting nub-os");
// check that the base if valid
if (base < 2 || base > 36)
{
*result = '\0';
return result;
}
char *ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do
{
tmp_value = value;
value /= base;
*ptr++ =
"zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
} while (value);
// Apply negative sign
if (tmp_value < 0)
*ptr++ = '-';
*ptr-- = '\0';
// Reverse the string
while (ptr1 < ptr)
{
tmp_char = *ptr;
*ptr-- = *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
void kernel_init(void)
{
for (int i = 0; i < 100; i++)
{
// char buf[10];
// itoa(i, buf, 10);
// kernel_print(buf);
kernel_print("Starting nub-osStarting");
}
}

22
src/kernel/linker.ld Normal file
View File

@@ -0,0 +1,22 @@
ENTRY(kernel_init)
SECTIONS {
. = 0x1000;
.text : {
*(.text)
}
.rodata : {
*(.rodata)
}
.data : {
*(.data)
}
.bss : {
*(.bss)
*(COMMON)
}
}

View File

@@ -1,10 +1,7 @@
CC = i386-elf-gcc -ffreestanding -m32
kernel.bin: entry.o kernel.o mem.o print.o
i386-elf-ld -o kernel.bin -Ttext 0x1000 entry.o kernel.o mem.o print.o --oformat binary
entry.o: entry.asm
nasm -f elf -o entry.o entry.asm
kernel.bin: entry.o kernel.o linker.ld mem.o print.o
i386-elf-ld -o kernel.bin -T linker.ld entry.o kernel.o mem.o print.o --oformat binary
kernel.o: kernel.c
$(CC) -c -o kernel.o kernel.c
@@ -15,5 +12,8 @@ mem.o: mem.c
print.o: print.c
$(CC) -c -o print.o print.c
entry.o: entry.asm
nasm -f elf entry.asm
clean:
@rm *.o *.bin 2>/dev/null || true

View File

@@ -1,41 +1,60 @@
#include "print.h"
#include <stdint.h>
#include <string.h>
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
#define ROWS 25
#define COLUMNS 80
#define BUF_SIZE ((ROWS * COLUMNS) * 2)
char* vga_text_pos;
#define BUF_START 0xb8000
#define BUF_END (BUF_START + BUF_SIZE)
void print_init(void)
enum FG_COLOR
{
vga_text_pos = (char*)0xb8000;
}
FG_BLACK = 0x0,
FG_BLUE = 0x1,
FG_GREEN = 0x2,
FG_CYAN = 0x3,
FG_RED = 0x4,
FG_MAGENTA = 0x5,
FG_BROWN = 0x6,
FG_LIGHT_GRAY = 0x7,
FG_DARK_GRAY = 0x8,
FG_LIGHT_BLUE = 0x9,
FG_LIGHT_GREEN = 0xA,
FG_LIGHT_CYAN = 0xB,
FG_LIGHT_RED = 0xC,
FG_LIGHT_MAGENTA = 0xD,
FG_YELLOW = 0xE,
FG_WHITE = 0xF,
};
void print_newline(void)
enum BG_COLOR
{
vga_text_pos += 160 - ((vga_text_pos - (char*)0xb8000) % 160);
}
BG_BLACK = 0x0,
BG_BLUE = 0x1,
BG_GREEN = 0x2,
BG_CYAN = 0x3,
BG_RED = 0x4,
BG_MAGENTA = 0x5,
BG_BROWN = 0x6,
BG_LIGHT_GRAY = 0x7,
};
void print_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color)
typedef struct
{
uint8_t character;
uint8_t color;
} vga_char;
static vga_char* print_buf = (vga_char*)BUF_START;
void kernel_print(const char* string)
{
for (int i = 0; string[i]; i++)
{
*vga_text_pos++ = string[i];
*vga_text_pos++ = (bg_color << 4) | fg_color;
}
}
// todo(nub): scroll if overflowing
void println_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color)
{
print_clr(string, fg_color, bg_color);
print_newline();
*print_buf++ = (vga_char){ string[i], FG_WHITE | BG_BLACK << 4 };
}
void print(const char* string)
{
print_clr(string, VGA_FG_WHITE, VGA_BG_BLACK);
}
void println(const char* string)
{
println_clr(string, VGA_FG_WHITE, VGA_BG_BLACK);
}

View File

@@ -1,41 +1 @@
enum VGA_FG_COLOR
{
VGA_FG_BLACK = 0x0,
VGA_FG_BLUE = 0x1,
VGA_FG_GREEN = 0x2,
VGA_FG_CYAN = 0x3,
VGA_FG_RED = 0x4,
VGA_FG_MAGENTA = 0x5,
VGA_FG_BROWN = 0x6,
VGA_FG_LIGHT_GRAY = 0x7,
VGA_FG_DARK_GRAY = 0x8,
VGA_FG_LIGHT_BLUE = 0x9,
VGA_FG_LIGHT_GREEN = 0xA,
VGA_FG_LIGHT_CYAN = 0xB,
VGA_FG_LIGHT_RED = 0xC,
VGA_FG_LIGHT_MAGENTA = 0xD,
VGA_FG_YELLOW = 0xE,
VGA_FG_WHITE = 0xF,
};
enum VGA_BG_COLOR
{
VGA_BG_BLACK = 0x0,
VGA_BG_BLUE = 0x1,
VGA_BG_GREEN = 0x2,
VGA_BG_CYAN = 0x3,
VGA_BG_RED = 0x4,
VGA_BG_MAGENTA = 0x5,
VGA_BG_BROWN = 0x6,
VGA_BG_LIGHT_GRAY = 0x7,
};
void print_init(void);
void print_newline(void);
void print(const char* string);
void println(const char* string);
void print_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color);
void println_clr(const char* string, enum VGA_FG_COLOR fg_color, enum VGA_BG_COLOR bg_color);
void kernel_print(const char* string);

View File

@@ -1,4 +1,7 @@
os.bin: boot/bootloader.bin kernel/kernel.bin zeroes.bin
.PHONY: kernel/kernel.bin boot/bootloader.bin
os.bin: boot/bootloader.bin kernel/kernel.bin
dd if=/dev/zero of=zeroes.bin bs=1 count=10240
cat boot/bootloader.bin kernel/kernel.bin zeroes.bin > os.bin
kernel/kernel.bin:
@@ -7,9 +10,6 @@ kernel/kernel.bin:
boot/bootloader.bin:
pushd boot; make bootloader.bin; popd
zeroes.bin:
dd if=/dev/zero of=zeroes.bin bs=1 count=10240
run: os.bin
qemu-system-x86_64 -drive file=os.bin,format=raw,index=0,media=disk