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 SeparateDefinitionBlocks: Always
BreakBeforeBraces: Allman BreakBeforeBraces: Allman
Cpp11BracedListStyle: false

View File

@@ -1,28 +1,27 @@
[org 0x7c00] [org 0x7c00]
[bits 16] [bits 16]
KERNEL_LOCATION equ 0x1000 KERNEL_INIT equ 0x1000
mov [BOOT_DISK], dl mov [BOOT_DISK], dl
xor ax, ax xor ax, ax
mov es, ax mov es, ax
mov ds, ax mov ds, ax
mov bp, 0x8000 mov bp, 0x8000
mov sp, bp mov sp, bp
mov bx, KERNEL_LOCATION mov bx, KERNEL_INIT
mov dh, 2 mov dh, 2
mov ah, 0x02 mov ah, 0x02
mov al, dh mov al, dh
mov ch, 0x00 mov ch, 0x00
mov dh, 0x00 mov dh, 0x00
mov cl, 0x02 mov cl, 0x02
mov dl, [BOOT_DISK] mov dl, [BOOT_DISK]
int 0x13 int 0x13
mov ah, 0x0 mov ah, 0x0
mov al, 0x3 mov al, 0x3
int 0x10 int 0x10
@@ -41,9 +40,8 @@ jmp $
BOOT_DISK: db 0 BOOT_DISK: db 0
gdt_start: gdt_start:
gdt_null: dd 0x0
dd 0x0 dd 0x0
dd 0x0
gdt_code: gdt_code:
dw 0xffff dw 0xffff
@@ -75,13 +73,12 @@ start_protected_mode:
mov es, ax mov es, ax
mov fs, ax mov fs, ax
mov gs, ax mov gs, ax
mov ebp, 0x90000 mov ebp, 0x90000
mov esp, ebp mov esp, ebp
jmp KERNEL_LOCATION jmp KERNEL_INIT
jmp $ hlt
times 510-($-$$) db 0 times 510-($-$$) db 0
dw 0xaa55 dw 0xaa55

View File

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

View File

@@ -1,7 +1,52 @@
#include "print.h" #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(); // check that the base if valid
println("Starting nub-os"); 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 CC = i386-elf-gcc -ffreestanding -m32
kernel.bin: entry.o kernel.o mem.o print.o kernel.bin: entry.o kernel.o linker.ld mem.o print.o
i386-elf-ld -o kernel.bin -Ttext 0x1000 entry.o kernel.o mem.o print.o --oformat binary i386-elf-ld -o kernel.bin -T linker.ld entry.o kernel.o mem.o print.o --oformat binary
entry.o: entry.asm
nasm -f elf -o entry.o entry.asm
kernel.o: kernel.c kernel.o: kernel.c
$(CC) -c -o kernel.o kernel.c $(CC) -c -o kernel.o kernel.c
@@ -15,5 +12,8 @@ mem.o: mem.c
print.o: print.c print.o: print.c
$(CC) -c -o print.o print.c $(CC) -c -o print.o print.c
entry.o: entry.asm
nasm -f elf entry.asm
clean: clean:
@rm *.o *.bin 2>/dev/null || true @rm *.o *.bin 2>/dev/null || true

View File

@@ -1,41 +1,60 @@
#include "print.h" #include "print.h"
#include <stdint.h>
#include <string.h>
#define VGA_WIDTH 80 #define ROWS 25
#define VGA_HEIGHT 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++) for (int i = 0; string[i]; i++)
{ {
*vga_text_pos++ = string[i]; // todo(nub): scroll if overflowing
*vga_text_pos++ = (bg_color << 4) | fg_color;
*print_buf++ = (vga_char){ string[i], FG_WHITE | BG_BLACK << 4 };
} }
} }
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();
}
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 void kernel_print(const char* string);
{
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);

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