-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.cpp
More file actions
60 lines (49 loc) · 1.26 KB
/
kernel.cpp
File metadata and controls
60 lines (49 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// kernel.cpp - Main kernel code
// VGA text mode buffer
volatile unsigned short* vga_buffer = (unsigned short*)0xB8000;
const int VGA_WIDTH = 80;
const int VGA_HEIGHT = 25;
int cursor_x = 0;
int cursor_y = 0;
void clear_screen() {
for (int i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++) {
vga_buffer[i] = 0x0F00 | ' '; // White on black
}
cursor_x = 0;
cursor_y = 0;
}
void print_char(char c) {
if (c == '\n') {
cursor_x = 0;
cursor_y++;
return;
}
int index = cursor_y * VGA_WIDTH + cursor_x;
vga_buffer[index] = 0x0F00 | c; // White on black
cursor_x++;
if (cursor_x >= VGA_WIDTH) {
cursor_x = 0;
cursor_y++;
}
}
void print(const char* str) {
for (int i = 0; str[i] != '\0'; i++) {
print_char(str[i]);
}
}
// Kernel entry point
extern "C" void kernel_main(unsigned int magic, unsigned int addr) {
clear_screen();
print("Welcome to MyOS!\n");
print("Kernel is running...\n");
if (magic != 0x2BADB002) {
print("ERROR: Invalid Multiboot magic number!\n");
} else {
print("Multiboot successful!\n");
}
// Kernel main loop
while (1) {
// Halt CPU until next interrupt
asm volatile("hlt");
}
}