Compare commits

...

4 Commits

Author SHA1 Message Date
nub31
08bbe517c5 ... 2025-09-03 13:44:44 +02:00
nub31
204c747c43 ... 2025-09-03 13:32:40 +02:00
nub31
87d1a291f7 ... 2025-09-03 13:13:16 +02:00
nub31
c338e05648 structure project 2025-09-03 11:18:17 +02:00
34 changed files with 471 additions and 332 deletions

18
.clangd Normal file
View File

@@ -0,0 +1,18 @@
CompileFlags:
Add:
- "-target"
- "x86_64-unknown-none"
- "-ffreestanding"
- "-fno-builtin"
- "-fno-common"
- "-Wall"
- "-Wextra"
- "-Wshadow"
- "-fno-strict-aliasing"
- "-nostdinc"
- "-nostdlib"
- "-I"
- "/home/oliste/repos/nub-os/src/stdlib"
Style:
AngledHeaders: "src/stdlib/.*"

View File

@@ -2,16 +2,14 @@ CC = x86_64-elf-gcc
LD = x86_64-elf-ld
AS = nasm
CFLAGS = -m64 -ffreestanding -fno-builtin -Wall -Wextra -Wshadow -std=c11 -g
CFLAGS = -m64 -ffreestanding -nostdinc -nostdlib -fno-builtin -Wall -Wextra -Wshadow -std=c11 -I src/stdlib -g
LDFLAGS = -g
ASFLAGS = -f elf64 -g -F dwarf
SRC_C := src/kernel.c src/mem.c src/pmm.c src/vmm.c src/string.c src/vga.c src/interrupts.c src/keyboard.c
SRC_ASM := src/start.asm
SRC_C := $(shell find src -name '*.c')
SRC_ASM := $(shell find src -name '*.asm')
OBJ_C := $(SRC_C:src/%.c=.build/%.o)
OBJ_ASM := $(SRC_ASM:src/%.asm=.build/%.o)
OBJS := $(OBJ_C) $(OBJ_ASM)
OBJS := $(patsubst src/%.c, .build/%.o, $(SRC_C)) $(patsubst src/%.asm, .build/%.o, $(SRC_ASM))
iso: .build/nub-os.iso
@echo "ISO created at '.build/nub-os.iso'"
@@ -19,9 +17,6 @@ iso: .build/nub-os.iso
clean:
@rm -r .build 2>/dev/null || true
build-dir:
mkdir .build 2>/dev/null || true
.build/nub-os.iso: .build/kernel grub.cfg
mkdir -p .build/nub-os/boot/grub
cp grub.cfg .build/nub-os/boot/grub
@@ -31,8 +26,10 @@ build-dir:
.build/kernel: $(OBJS)
$(LD) $(LDFLAGS) -T linker.ld -o $@ $^
.build/%.o: src/%.c | build-dir
.build/%.o: src/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c -o $@ $<
.build/%.o: src/%.asm | build-dir
.build/%.o: src/%.asm
@mkdir -p $(dir $@)
$(AS) $(ASFLAGS) -o $@ $<

4
src/arch/arch.h Normal file
View File

@@ -0,0 +1,4 @@
#pragma once
void panic();
void put_char(char character);

18
src/arch/mmap.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
typedef struct
{
uint64_t base_address;
size_t length;
} memory_region_t;
typedef struct
{
memory_region_t* regions;
size_t num_regions;
} memory_map_t;
extern memory_map_t memory_map;

View File

@@ -1,5 +1,7 @@
#include "interrupts.h"
#include "../arch.h"
#include "util.h"
#include <stdio.h>
#define PIC1_COMMAND 0x20
#define PIC1_DATA 0x21
@@ -55,7 +57,7 @@ void enable_apic()
uint64_t apic_base = rdmsr(0x1B);
apic_base |= (1 << 11);
wrmsr(0x1B, apic_base);
kprintf("APIC enabled\n");
printf("APIC enabled\n");
}
void remap_pic()
@@ -74,36 +76,36 @@ void remap_pic()
outb(PIC1_DATA, 0);
outb(PIC2_DATA, 0);
kprintf("PIC remapped\n");
printf("PIC remapped\n");
}
void disable_pic()
{
outb(PIC1_DATA, 0xFF);
outb(PIC2_DATA, 0xFF);
kprintf("PIC disabled\n");
printf("PIC disabled\n");
}
void handle_exception(const isr_frame_t* frame)
static void handle_exception(const isr_frame_t* frame)
{
kprintf("exception[%d]: %s, error code: %d\n", frame->int_no, exception_messages[frame->int_no], frame->err_code);
kpanic();
printf("exception[%d]: %s, error code: %d\n", frame->int_no, exception_messages[frame->int_no], frame->err_code);
panic();
}
void register_irq_handler(uint8_t irq, irq_handler_t handler)
{
if (irq >= 16)
{
kprintf("Cannot register irq %d is out of bounds\n", irq);
printf("Cannot register irq %d is out of bounds\n", irq);
}
else
{
irq_handlers[irq] = handler;
kprintf("Registered irq handler at vector %d\n", irq);
printf("Registered irq handler at vector %d\n", irq);
}
}
void handle_irq(const isr_frame_t* frame)
static void handle_irq(const isr_frame_t* frame)
{
uint8_t irq = frame->int_no - 32;
@@ -132,6 +134,6 @@ void handle_isr(const isr_frame_t* frame)
}
else
{
kprintf("interrupt[%d]: not implemented\n", frame->int_no);
printf("interrupt[%d]: not implemented\n", frame->int_no);
}
}

View File

@@ -1,6 +1,5 @@
#pragma once
#include "kernel.h"
#include <stdbool.h>
#include <stdint.h>
@@ -26,11 +25,9 @@ void register_irq_handler(uint8_t irq, irq_handler_t handler);
static inline void enable_interrupts()
{
__asm__ volatile("sti");
kprintf("Interrupts enabled\n");
}
static inline void disable_interrupts()
{
__asm__ volatile("cli");
kprintf("Interrupts disabled\n");
}

107
src/arch/x86_64/keyboard.c Normal file
View File

@@ -0,0 +1,107 @@
// #include "keyboard.h"
// #include "interrupts.h"
// #include "util.h"
// #include <stddef.h>
// #define SCANCODE_LEFT_SHIFT 42
// #define SCANCODE_RIGHT_SHIFT 54
// #define SCANCODE_CAPS_LOCK 58
// #define KEYBOARD_HANDLERS_LENGTH 32
// unsigned const char us_keymap[128] = {
// 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t', 'q', 'w',
// 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0, 'a', 's', 'd', 'f', 'g', 'h',
// 'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
// 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// 0, 0, '-', 0, 0, 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// };
// unsigned const char us_keymap_shift[128] = {
// 0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t', 'Q', 'W',
// 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0, 'A', 'S', 'D', 'F', 'G', 'H',
// 'J', 'K', 'L', ':', '"', '~', 0, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
// 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// 0, 0, '-', 0, 0, 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// };
// bool shift = false;
// bool caps_lock = false;
// // todo(nub): make dynamic when a memory allocator is implemented
// static keyboard_handler_t keyboard_handlers[KEYBOARD_HANDLERS_LENGTH];
// static size_t handler_index = 0;
// char scan_code_to_ascii(uint8_t scan_code)
// {
// if (scan_code >= 128)
// {
// return 0;
// }
// if ((!caps_lock && shift) || (caps_lock && !shift))
// {
// return us_keymap_shift[scan_code];
// }
// else
// {
// return us_keymap[scan_code];
// }
// }
// void handle_keyboard(const isr_frame_t* frame)
// {
// uint8_t code = inb(0x60);
// uint8_t scan_code = code & 0x7F;
// bool pressed = (code & 0x80) == 0;
// switch (scan_code)
// {
// case SCANCODE_LEFT_SHIFT:
// case SCANCODE_RIGHT_SHIFT:
// {
// shift = pressed;
// break;
// }
// case SCANCODE_CAPS_LOCK:
// {
// if (pressed)
// {
// caps_lock = !caps_lock;
// }
// break;
// }
// default:
// {
// keyboard_event_t event = {
// .scan_code = scan_code,
// .pressed = pressed,
// .caps_lock = caps_lock,
// .shift = shift,
// .ascii = scan_code_to_ascii(scan_code),
// };
// for (size_t i = 0; i < handler_index; i++)
// {
// keyboard_handlers[i](&event);
// }
// }
// }
// }
// void register_keypress_handler(const keyboard_handler_t handler)
// {
// // todo(nub31): remove when a memory allocator is implemented and
// // keyboard_handlers is a dynamic list
// if (handler_index >= KEYBOARD_HANDLERS_LENGTH)
// {
// }
// keyboard_handlers[handler_index] = handler;
// handler_index += 1;
// }
// void init_keyboard()
// {
// register_irq_handler(1, handle_keyboard);
// }

View File

@@ -0,0 +1,19 @@
// #pragma once
// #include <stdbool.h>
// #include <stdint.h>
// typedef struct
// {
// uint8_t scan_code;
// bool pressed;
// bool shift;
// bool caps_lock;
// char ascii;
// } keyboard_event_t;
// typedef void (*keyboard_handler_t)(const keyboard_event_t*);
// void init_keyboard();
// void register_keypress_handler(keyboard_handler_t handler);
// char scan_code_to_ascii(uint8_t scan_code);

44
src/arch/x86_64/mmap.c Normal file
View File

@@ -0,0 +1,44 @@
#include "mmap.h"
#include "../mmap.h"
#include <stddef.h>
#include <stdio.h>
#define USABLE_REGION_SIZE 32
memory_map_t memory_map;
static memory_region_t usable_regions[USABLE_REGION_SIZE];
void map_memory(multiboot_info_t* mbd)
{
if (!(mbd->flags & (1 << 6)))
{
printf("Invalid memory map given by bootloader\n");
panic();
}
size_t num_regions = 0;
uint64_t offset = 0;
while (offset < mbd->mmap_length)
{
multiboot_memory_map_t* mmmt = (multiboot_memory_map_t*)(mbd->mmap_addr + offset);
if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE && num_regions < USABLE_REGION_SIZE)
{
usable_regions[num_regions] = (memory_region_t){
.base_address = mmmt->addr,
.length = mmmt->len,
};
num_regions++;
}
offset += mmmt->size + sizeof(mmmt->size);
}
memory_map = (memory_map_t){
.num_regions = num_regions,
.regions = usable_regions,
};
}

6
src/arch/x86_64/mmap.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include "../arch.h"
#include "multiboot.h"
void map_memory(multiboot_info_t* mbd);

View File

@@ -1,5 +1,5 @@
global _start
extern kmain
extern entry
extern handle_isr
extern pml4
@@ -205,7 +205,7 @@ section .text
; Finally, we call in to c
mov rdi, [multiboot_info]
call kmain
call entry
.hang:
hlt
jmp .hang

View File

@@ -103,3 +103,11 @@ static inline void wrmsr(uint32_t msr, uint64_t value)
uint32_t hi = (uint32_t)(value >> 32);
__asm__ volatile("wrmsr" : : "c"(msr), "a"(lo), "d"(hi));
}
static inline void halt()
{
while (true)
{
__asm__ volatile("hlt");
}
}

36
src/arch/x86_64/x86_64.c Normal file
View File

@@ -0,0 +1,36 @@
#include "../../kernel.h"
#include "../arch.h"
#include "interrupts.h"
#include "mmap.h"
#include "multiboot.h"
#include "util.h"
#include "vga.h"
#include <stdio.h>
void entry(multiboot_info_t* mbd)
{
vga_clear();
map_memory(mbd);
remap_pic();
enable_interrupts();
main();
}
void arch_callback()
{
printf("Kernel panic!\n");
disable_interrupts();
halt();
}
void panic()
{
printf("Kernel panic!");
disable_interrupts();
halt();
}
void put_char(char character)
{
vga_put_char(character, VGA_DEFAULT_COLOR);
}

View File

@@ -1,105 +1,9 @@
#include "kernel.h"
#include "interrupts.h"
#include "multiboot.h"
#include "pmm.h"
#include "string.h"
#include "vga.h"
#include <stdarg.h>
#include <stdio.h>
void kmain(multiboot_info_t* mbd)
void main()
{
vga_clear();
init_pmm(mbd);
remap_pic();
enable_interrupts();
kprintf("Welcome to nub OS :)\n");
khalt();
}
void kpanic()
{
kprintf("Kernel panic!\n");
disable_interrupts();
khalt();
}
void kprintf(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
bool should_format = false;
for (size_t i = 0; fmt[i] != '\0'; i++)
{
if (should_format)
{
should_format = false;
if (fmt[i] == '%')
{
vga_put_char('%', VGA_DEFAULT_COLOR);
}
else if (fmt[i] == 's')
{
const char* str = va_arg(args, const char*);
for (size_t j = 0; str[j] != '\0'; j++)
{
vga_put_char(str[j], VGA_DEFAULT_COLOR);
}
}
else if (fmt[i] == 'c')
{
char character = (char)va_arg(args, int64_t);
vga_put_char(character, VGA_DEFAULT_COLOR);
}
else if (fmt[i] == 'd')
{
int64_t val = va_arg(args, int64_t);
char buf[21];
itoa64(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
vga_put_char(buf[j], VGA_DEFAULT_COLOR);
}
}
else if (fmt[i] == 'u')
{
uint64_t val = va_arg(args, uint64_t);
char buf[21];
uitoa64(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
vga_put_char(buf[j], VGA_DEFAULT_COLOR);
}
}
else if (fmt[i] == 'x')
{
uint64_t val = va_arg(args, uint64_t);
char buf[17];
uitoa64_hex(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
vga_put_char(buf[j], VGA_DEFAULT_COLOR);
}
}
else
{
vga_put_char(fmt[i], VGA_DEFAULT_COLOR);
}
}
else if (fmt[i] == '%')
{
should_format = true;
}
else
{
vga_put_char(fmt[i], VGA_DEFAULT_COLOR);
}
}
va_end(args);
init_pmm();
printf("Welcome to nub OS :)\n");
}

View File

@@ -1,15 +1,3 @@
#pragma once
#include <stdbool.h>
void kpanic();
void kprintf(const char* fmt, ...);
static inline void khalt()
{
while (true)
{
__asm__ volatile("hlt");
}
}
void main();

View File

@@ -1,110 +0,0 @@
#include "keyboard.h"
#include "interrupts.h"
#include "kernel.h"
#include "util.h"
#include <stddef.h>
#define SCANCODE_LEFT_SHIFT 42
#define SCANCODE_RIGHT_SHIFT 54
#define SCANCODE_CAPS_LOCK 58
#define KEYBOARD_HANDLERS_LENGTH 32
unsigned const char us_keymap[128] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t', 'q', 'w',
'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0, 'a', 's', 'd', 'f', 'g', 'h',
'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, '-', 0, 0, 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
unsigned const char us_keymap_shift[128] = {
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t', 'Q', 'W',
'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0, 'A', 'S', 'D', 'F', 'G', 'H',
'J', 'K', 'L', ':', '"', '~', 0, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, '-', 0, 0, 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
bool shift = false;
bool caps_lock = false;
// todo(nub): make dynamic when a memory allocator is implemented
static keyboard_handler_t keyboard_handlers[KEYBOARD_HANDLERS_LENGTH];
static size_t handler_index = 0;
char scan_code_to_ascii(uint8_t scan_code)
{
if (scan_code >= 128)
{
return 0;
}
if ((!caps_lock && shift) || (caps_lock && !shift))
{
return us_keymap_shift[scan_code];
}
else
{
return us_keymap[scan_code];
}
}
void handle_keyboard(const isr_frame_t* frame)
{
uint8_t code = inb(0x60);
uint8_t scan_code = code & 0x7F;
bool pressed = (code & 0x80) == 0;
switch (scan_code)
{
case SCANCODE_LEFT_SHIFT:
case SCANCODE_RIGHT_SHIFT:
{
shift = pressed;
break;
}
case SCANCODE_CAPS_LOCK:
{
if (pressed)
{
caps_lock = !caps_lock;
}
break;
}
default:
{
keyboard_event_t event = {
.scan_code = scan_code,
.pressed = pressed,
.caps_lock = caps_lock,
.shift = shift,
.ascii = scan_code_to_ascii(scan_code),
};
for (size_t i = 0; i < handler_index; i++)
{
keyboard_handlers[i](&event);
}
}
}
}
void register_keypress_handler(const keyboard_handler_t handler)
{
// todo(nub31): remove when a memory allocator is implemented and
// keyboard_handlers is a dynamic list
if (handler_index >= KEYBOARD_HANDLERS_LENGTH)
{
kprintf("Maximum keyboard handlers reached\n");
kpanic();
}
keyboard_handlers[handler_index] = handler;
handler_index += 1;
}
void init_keyboard()
{
register_irq_handler(1, handle_keyboard);
}

View File

@@ -1,19 +0,0 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
typedef struct
{
uint8_t scan_code;
bool pressed;
bool shift;
bool caps_lock;
char ascii;
} keyboard_event_t;
typedef void (*keyboard_handler_t)(const keyboard_event_t*);
void init_keyboard();
void register_keypress_handler(keyboard_handler_t handler);
char scan_code_to_ascii(uint8_t scan_code);

View File

@@ -1,10 +0,0 @@
#include "mem.h"
#include <stdint.h>
void kmemset(void* destination, uint8_t value, size_t length)
{
for (size_t i = 0; i < length; i++)
{
((uint8_t*)destination)[i] = value;
}
}

View File

@@ -1,6 +0,0 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
void kmemset(void* destination, uint8_t value, size_t length);

View File

@@ -1,58 +1,23 @@
#include "pmm.h"
#include "kernel.h"
#include "mem.h"
#include <stdbool.h>
#include "arch/mmap.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#define BITMAP_SIZE 32768 // Supports up to 1GB of RAM
#define USABLE_REGION_SIZE 32
typedef struct
{
uint64_t base_address;
uint64_t length;
} memory_region_t;
static uint8_t page_bitmap[BITMAP_SIZE];
static uint64_t total_pages = 0;
static uint64_t free_pages = 0;
void init_pmm(multiboot_info_t* mbd)
void init_pmm()
{
memory_region_t usable_regions[USABLE_REGION_SIZE];
size_t num_regions = 0;
memset(page_bitmap, 0xFF, BITMAP_SIZE);
if (!(mbd->flags & (1 << 6)))
for (size_t i = 0; i < memory_map.num_regions; i++)
{
kprintf("Invalid memory map given by bootloader\n");
kpanic();
}
uint64_t offset = 0;
while (offset < mbd->mmap_length)
{
multiboot_memory_map_t* mmmt = (multiboot_memory_map_t*)(mbd->mmap_addr + offset);
if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE && num_regions < USABLE_REGION_SIZE)
{
usable_regions[num_regions] = (memory_region_t){
.base_address = mmmt->addr,
.length = mmmt->len,
};
num_regions++;
kprintf("Available memory: 0x%x - 0x%x (%u KB)\n", mmmt->addr, mmmt->addr + mmmt->len, mmmt->len / 1024);
}
offset += mmmt->size + sizeof(mmmt->size);
}
kmemset(page_bitmap, 0xFF, BITMAP_SIZE);
for (size_t i = 0; i < num_regions; i++)
{
memory_region_t region = usable_regions[i];
memory_region_t region = memory_map.regions[i];
uint64_t start_page = region.base_address / PAGE_SIZE;
uint64_t num_pages = region.length / PAGE_SIZE;
@@ -67,7 +32,7 @@ void init_pmm(multiboot_info_t* mbd)
}
else
{
kprintf("System has more ram than the bitmap allows!\n");
printf("System has more ram than the bitmap allows!\n");
break;
}
}
@@ -86,7 +51,7 @@ void init_pmm(multiboot_info_t* mbd)
}
}
kprintf("PMM initialized\n");
printf("PMM initialized\n");
}
uint64_t pmm_alloc_page()

View File

@@ -1,10 +1,9 @@
#pragma once
#include "multiboot.h"
#include <stdint.h>
#define PAGE_SIZE 4096
void init_pmm(multiboot_info_t* mbd);
void init_pmm();
uint64_t pmm_alloc_page();
void pmm_free_page(uint64_t addr);

8
src/stdlib/stdarg.h Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
typedef __builtin_va_list va_list;
#define va_start(ap, last) __builtin_va_start(ap, last)
#define va_arg(ap, type) __builtin_va_arg(ap, type)
#define va_end(ap) __builtin_va_end(ap)
#define va_copy(dest, src) __builtin_va_copy(dest, src)

7
src/stdlib/stdbool.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1

12
src/stdlib/stddef.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#ifndef NULL
#define NULL ((void*)0)
#endif
typedef unsigned long size_t;
typedef long ptrdiff_t;
typedef long intptr_t;
typedef unsigned long uintptr_t;
#define offsetof(type, member) __builtin_offsetof(type, member)

50
src/stdlib/stdint.h Normal file
View File

@@ -0,0 +1,50 @@
#pragma once
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef signed long long int64_t;
typedef unsigned long long uint64_t;
typedef uint8_t u8;
typedef int8_t i8;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint64_t u64;
typedef int64_t i64;
#define INT8_MIN (-128)
#define INT8_MAX 127
#define UINT8_MAX 0xff
#define INT16_MIN (-32768)
#define INT16_MAX 32767
#define UINT16_MAX 0xffff
#define INT32_MIN (-2147483647 - 1)
#define INT32_MAX 2147483647
#define UINT32_MAX 0xffffffffU
#define INT64_MIN (-9223372036854775807LL - 1)
#define INT64_MAX 9223372036854775807LL
#define UINT64_MAX 0xffffffffffffffffULL
#define INT8_C(x) x
#define UINT8_C(x) x##U
#define INT16_C(x) x
#define UINT16_C(x) x##U
#define INT32_C(x) x
#define UINT32_C(x) x##U
#define INT64_C(x) x##LL
#define UINT64_C(x) x##ULL
#define INTMAX_C(x) x##LL
#define UINTMAX_C(x) x##ULL

84
src/stdlib/stdio.c Normal file
View File

@@ -0,0 +1,84 @@
#include "../arch/arch.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
void printf(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
bool should_format = false;
for (size_t i = 0; fmt[i] != '\0'; i++)
{
if (should_format)
{
should_format = false;
if (fmt[i] == '%')
{
put_char('%');
}
else if (fmt[i] == 's')
{
const char* str = va_arg(args, const char*);
for (size_t j = 0; str[j] != '\0'; j++)
{
put_char(str[j]);
}
}
else if (fmt[i] == 'c')
{
char character = (char)va_arg(args, int64_t);
put_char(character);
}
else if (fmt[i] == 'd')
{
int64_t val = va_arg(args, int64_t);
char buf[21];
itoa64(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
put_char(buf[j]);
}
}
else if (fmt[i] == 'u')
{
uint64_t val = va_arg(args, uint64_t);
char buf[21];
uitoa64(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
put_char(buf[j]);
}
}
else if (fmt[i] == 'x')
{
uint64_t val = va_arg(args, uint64_t);
char buf[17];
uitoa64_hex(val, buf);
for (size_t j = 0; buf[j] != '\0'; j++)
{
put_char(buf[j]);
}
}
else
{
put_char(fmt[i]);
}
}
else if (fmt[i] == '%')
{
should_format = true;
}
else
{
put_char(fmt[i]);
}
}
va_end(args);
}

3
src/stdlib/stdio.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
void printf(const char* fmt, ...);

View File

@@ -1,4 +1,12 @@
#include "string.h"
#include <string.h>
void memset(void* destination, uint8_t value, size_t length)
{
for (size_t i = 0; i < length; i++)
{
((uint8_t*)destination)[i] = value;
}
}
int strcmp(const char* a, const char* b)
{

View File

@@ -3,6 +3,7 @@
#include <stddef.h>
#include <stdint.h>
void memset(void* destination, uint8_t value, size_t length);
int strcmp(const char* a, const char* b);
void reverse(char* str, size_t length);
void uitoa64(uint64_t value, char* buffer);

View File

View File

@@ -1 +0,0 @@
#pragma once