formatting and make events const

This commit is contained in:
nub31
2025-08-29 15:30:30 +02:00
parent 9c1b4a2c94
commit 827d27e3e3
7 changed files with 67 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
IndentWidth: 4 IndentWidth: 4
ColumnLimit: 120 ColumnLimit: 100
# Pointer formatting # Pointer formatting
DerivePointerAlignment: false DerivePointerAlignment: false
@@ -16,5 +16,4 @@ AllowShortLoopsOnASingleLine: false
SeparateDefinitionBlocks: Always SeparateDefinitionBlocks: Always
BreakBeforeBraces: Allman BreakBeforeBraces: Allman
Cpp11BracedListStyle: true
Cpp11BracedListStyle: false

View File

@@ -29,40 +29,42 @@ typedef struct
} __attribute__((packed)) idtr_t; } __attribute__((packed)) idtr_t;
extern void* isr_stub_table[]; extern void* isr_stub_table[];
static irq_handler_t irq_handlers[16] = { 0 }; static irq_handler_t irq_handlers[16] = {0};
static const char* exception_messages[32] = { "divide by zero", static const char* exception_messages[32] = {
"debug", "divide by zero",
"non maskable interrupt", "debug",
"breakpoint", "non maskable interrupt",
"overflow", "breakpoint",
"bound range exceeded", "overflow",
"invalid opcode", "bound range exceeded",
"device not available", "invalid opcode",
"double fault", "device not available",
"coprocessor segment overrun", "double fault",
"invalid tss", "coprocessor segment overrun",
"segment not present", "invalid tss",
"stack-segment fault", "segment not present",
"general protection fault", "stack-segment fault",
"page fault", "general protection fault",
"reserved", "page fault",
"x87 floating point exception", "reserved",
"alignment check", "x87 floating point exception",
"machine check", "alignment check",
"simd floating point exception", "machine check",
"virtualization exception", "simd floating point exception",
"control protection exception", "virtualization exception",
"reserved", "control protection exception",
"reserved", "reserved",
"reserved", "reserved",
"reserved", "reserved",
"reserved", "reserved",
"reserved", "reserved",
"hypervisor injection exception", "reserved",
"vmm communication exception", "hypervisor injection exception",
"security exception", "vmm communication exception",
"reserved" }; "security exception",
"reserved",
};
static interrupt_descriptor idt[IDT_SIZE]; static interrupt_descriptor idt[IDT_SIZE];
@@ -126,7 +128,10 @@ void init_idt(void)
idt_set_descriptor(i, isr_stub_table[i], 0); idt_set_descriptor(i, isr_stub_table[i], 0);
} }
idtr_t idtr = { .base = (uintptr_t)&idt[0], .limit = (uint16_t)sizeof(interrupt_descriptor) * IDT_SIZE - 1 }; idtr_t idtr = {
.base = (uintptr_t)&idt[0],
.limit = (uint16_t)sizeof(interrupt_descriptor) * IDT_SIZE - 1,
};
__asm__ volatile("lidt %0" : : "m"(idtr)); __asm__ volatile("lidt %0" : : "m"(idtr));
__asm__ volatile("sti"); __asm__ volatile("sti");

View File

@@ -11,7 +11,7 @@ typedef struct
uint64_t rip, cs, rflags, rsp, ss; uint64_t rip, cs, rflags, rsp, ss;
} __attribute__((packed)) isr_frame_t; } __attribute__((packed)) isr_frame_t;
typedef void (*irq_handler_t)(isr_frame_t*); typedef void (*irq_handler_t)(const isr_frame_t*);
void init_idt(void); void init_idt(void);

View File

@@ -4,7 +4,7 @@
#include "vga.h" #include "vga.h"
#include <stdbool.h> #include <stdbool.h>
void keyboard_handler(keyboard_event_t* event) void keyboard_handler(const keyboard_event_t* event)
{ {
if (event->pressed) if (event->pressed)
{ {

View File

@@ -18,7 +18,7 @@ bool caps_lock = false;
static keyboard_handler_t keyboard_handlers[KEYBOARD_HANDLERS_LENGTH]; static keyboard_handler_t keyboard_handlers[KEYBOARD_HANDLERS_LENGTH];
static int handler_index = 0; static int handler_index = 0;
void handle_keyboard(isr_frame_t* frame) void handle_keyboard(const isr_frame_t* frame)
{ {
uint8_t code = inb(0x60); uint8_t code = inb(0x60);
uint8_t scan_code = code & 0x7F; uint8_t scan_code = code & 0x7F;
@@ -42,7 +42,12 @@ void handle_keyboard(isr_frame_t* frame)
} }
default: default:
{ {
keyboard_event_t event = { .scan_code = scan_code, .pressed = pressed, .caps_lock = caps_lock, .shift = shift }; keyboard_event_t event = {
.scan_code = scan_code,
.pressed = pressed,
.caps_lock = caps_lock,
.shift = shift,
};
for (int i = 0; i < handler_index; i++) for (int i = 0; i < handler_index; i++)
{ {
@@ -52,9 +57,10 @@ void handle_keyboard(isr_frame_t* frame)
} }
} }
void register_keypress_handler(keyboard_handler_t handler) 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 // todo(nub31): remove when a memory allocator is implemented and
// keyboard_handlers is a dynamic list
if (handler_index >= KEYBOARD_HANDLERS_LENGTH) if (handler_index >= KEYBOARD_HANDLERS_LENGTH)
{ {
vga_print_error(); vga_print_error();

View File

@@ -9,9 +9,10 @@ typedef struct
bool pressed; bool pressed;
bool shift; bool shift;
bool caps_lock; bool caps_lock;
char ascii;
} keyboard_event_t; } keyboard_event_t;
typedef void (*keyboard_handler_t)(keyboard_event_t*); typedef void (*keyboard_handler_t)(const keyboard_event_t*);
void init_keyboard(); void init_keyboard();
void register_keypress_handler(keyboard_handler_t handler); void register_keypress_handler(keyboard_handler_t handler);

View File

@@ -33,7 +33,10 @@ void vga_clear(void)
{ {
for (uint8_t col = 0; col < COLUMNS; col++) for (uint8_t col = 0; col < COLUMNS; col++)
{ {
vga_char character = { .character = ' ', .color = vga_default_color() }; vga_char character = {
.character = ' ',
.color = vga_default_color(),
};
vga_set_char(row, col, character); vga_set_char(row, col, character);
} }
} }
@@ -68,7 +71,10 @@ void vga_print_char_colored(char character, vga_color_t color)
} }
default: default:
{ {
vga_char c = { .character = character, .color = color }; vga_char c = {
.character = character,
.color = color,
};
vga_set_char(cursor_row, cursor_col, c); vga_set_char(cursor_row, cursor_col, c);
cursor_col += 1; cursor_col += 1;
break; break;
@@ -93,7 +99,10 @@ void vga_print_char_colored(char character, vga_color_t color)
for (uint8_t col = 0; col < COLUMNS; col++) for (uint8_t col = 0; col < COLUMNS; col++)
{ {
vga_char c = { .character = ' ', .color = vga_default_color() }; vga_char c = {
.character = ' ',
.color = vga_default_color(),
};
vga_set_char(ROWS - 1, col, c); vga_set_char(ROWS - 1, col, c);
}; };