Repository navigation
Expand file tree
/
Copy pathkernel.cpp
More file actions
91 lines (70 loc) · 2.61 KB
/
Copy pathkernel.cpp
File metadata and controls
91 lines (70 loc) · 2.61 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "types.h"
#include "gdt.h"
#include "interrupts.h"
#include "keyboard.h"
#include "shell.h"
void clearScreen() noexcept(true);
static uint8_t x = 0, y = 0;
void printf(char* str) noexcept(true) {
static uint16_t* VideoMemory = reinterpret_cast<uint16_t*>(0xb8000);
for (int i = 0; str[i]!='\0';i++) {
if (str[i]=='\n') {
x = 0;
y++;
continue;
}
if (str[i]=='\b') {
if (x>0) {
x--;
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | ' ';
}
continue;
}
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | str[i];
x++;
if (x >= 80) {
y++;
x = 0;
}
if (y>=25) {
clearScreen();
x = 0;
y = 0;
}
}
}
void clearScreen() noexcept(true) {
uint16_t* VideoMemory = reinterpret_cast<uint16_t*>(0xb8000);
for (int i = 0; i< 80 * 25; i++) {
VideoMemory[i] = (0x0F<<8) | ' ';
}
x = 0;
y = 0;
}
// C++ global objects with constructors (e.g. static instances) need their constructors
// called manually in a freestanding kernel — there is no C runtime to do it for us.
// The linker script places pointers to all global constructor functions between the
// symbols start_ctors and end_ctors, so we iterate that range and call each one.
typedef void (*constructor)();
extern "C" constructor start_ctors;
extern "C" constructor end_ctors;
extern "C" void callConstructors() {
for(constructor* i = &start_ctors; i != &end_ctors; i++) {
(*i)();
}
}
// Entry point jumped to by the bootloader after it hands off control to the kernel.
// multiboot_structure: pointer to the Multiboot info block provided by the bootloader
// (contains memory map, boot device, command line, etc.)
// magicnumber: should equal 0x2BADB002, confirming the bootloader is Multiboot-compliant.
extern "C" void kernelMain(void* multiboot_structure, uint32_t magicnumber) {
clearScreen();
printf("Look it works!\n");
printf("Hello World!\n");
GlobalDescriptorTable gdt;
InterruptManager interrupts(&gdt);
KeyboardDriver keyboard(&interrupts);
interrupts.Activate();
Shell shell;
while (1);
}