-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.cpp
More file actions
120 lines (105 loc) · 2.76 KB
/
kernel.cpp
File metadata and controls
120 lines (105 loc) · 2.76 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "kernel.hpp"
// Implementação de strcmp para substituir libc
extern "C" int strcmp(const char* s1, const char* s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(unsigned char*)s1 - *(unsigned char*)s2;
}
TerminalRootOS::TerminalRootOS() {
cursor_x = 0;
cursor_y = 0;
clear_screen();
}
void TerminalRootOS::move_cursor(int x, int y) {
uint16_t pos = y * VGA_WIDTH + x;
outb(0x3D4, 14);
outb(0x3D5, (pos >> 8) & 0xFF);
outb(0x3D4, 15);
outb(0x3D5, pos & 0xFF);
}
void TerminalRootOS::advance_cursor() {
cursor_x++;
if (cursor_x >= VGA_WIDTH) {
cursor_x = 0;
cursor_y++;
if (cursor_y >= VGA_HEIGHT) {
cursor_y = 0;
}
}
move_cursor(cursor_x, cursor_y);
}
void TerminalRootOS::put_char(char c) {
volatile char* video = (volatile char*)VGA_MEMORY;
if (c == '\n') {
cursor_x = 0;
cursor_y++;
} else if (c == '\b') {
if (cursor_x > 0) {
cursor_x--;
video[(cursor_y * VGA_WIDTH + cursor_x) * 2] = ' ';
}
} else {
video[(cursor_y * VGA_WIDTH + cursor_x) * 2] = c;
video[(cursor_y * VGA_WIDTH + cursor_x) * 2 + 1] = 0x07;
cursor_x++;
if (cursor_x >= VGA_WIDTH) {
cursor_x = 0;
cursor_y++;
}
}
if (cursor_y >= VGA_HEIGHT) {
cursor_y = 0;
}
move_cursor(cursor_x, cursor_y);
}
void TerminalRootOS::clear_screen() {
volatile char* video = (volatile char*)VGA_MEMORY;
for (int y = 0; y < VGA_HEIGHT; y++) {
for (int x = 0; x < VGA_WIDTH; x++) {
int index = (y * VGA_WIDTH + x) * 2;
video[index] = ' ';
video[index + 1] = 0x07;
}
}
cursor_x = 0;
cursor_y = 0;
move_cursor(cursor_x, cursor_y);
}
void TerminalRootOS::printf(const char* str) {
for (int i = 0; str[i] != '\0'; i++) {
put_char(str[i]);
}
}
uint8_t TerminalRootOS::read_key_scancode() {
while ((inb(KEYBOARD_STATUS_PORT) & 1) == 0) {}
return inb(KEYBOARD_DATA_PORT);
}
char TerminalRootOS::translate_scancode(uint8_t scancode) {
const char scancode_map[] = {
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
};
if (scancode >= sizeof(scancode_map)) return 0;
return scancode_map[scancode];
}
void TerminalRootOS::reboot_system() {
uint8_t good = 0x02;
while (good & 0x02) {
good = inb(0x64);
}
outb(0x64, 0xFE);
__builtin_unreachable();
}
inline uint8_t TerminalRootOS::inb(uint16_t port) {
uint8_t ret;
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
inline void TerminalRootOS::outb(uint16_t port, uint8_t value) {
asm volatile("outb %0, %1" : : "a"(value), "Nd"(port));
}