/
kernel.cpp
171 lines (126 loc) · 3.83 KB
/
kernel.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <common/types.h>
#include <gdt.h>
#include <hardwarecommunication/interrupts.h>
#include <hardwarecommunication/pci.h>
#include <drivers/driver.h>
#include <drivers/keyboard.h>
#include <drivers/mouse.h>
#include <drivers/vga.h>
using namespace myos;
using namespace myos::common;
using namespace myos::drivers;
using namespace myos::hardwarecommunication;
void printf(char* str)
{
static uint16_t* VideoMemory = (uint16_t*)0xb8000;
static uint8_t x=0,y=0;
for(int i = 0; str[i] != '\0'; ++i)
{
switch(str[i])
{
case '\n':
x = 0;
y++;
break;
default:
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | str[i];
x++;
break;
}
if(x >= 80)
{
x = 0;
y++;
}
if(y >= 25)
{
for(y = 0; y < 25; y++)
for(x = 0; x < 80; x++)
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | ' ';
x = 0;
y = 0;
}
}
}
void printfHex(uint8_t key)
{
char* foo = "00";
char* hex = "0123456789ABCDEF";
foo[0] = hex[(key >> 4) & 0xF];
foo[1] = hex[key & 0xF];
printf(foo);
}
class PrintfKeyboardEventHandler : public KeyboardEventHandler
{
public:
void OnKeyDown(char c)
{
char* foo = " ";
foo[0] = c;
printf(foo);
}
};
class MouseToConsole : public MouseEventHandler
{
int8_t x, y;
public:
MouseToConsole()
{
uint16_t* VideoMemory = (uint16_t*)0xb8000;
x = 40;
y = 12;
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0x0F00) << 4
| (VideoMemory[80*y+x] & 0xF000) >> 4
| (VideoMemory[80*y+x] & 0x00FF);
}
virtual void OnMouseMove(int xoffset, int yoffset)
{
static uint16_t* VideoMemory = (uint16_t*)0xb8000;
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0x0F00) << 4
| (VideoMemory[80*y+x] & 0xF000) >> 4
| (VideoMemory[80*y+x] & 0x00FF);
x += xoffset;
if(x >= 80) x = 79;
if(x < 0) x = 0;
y += yoffset;
if(y >= 25) y = 24;
if(y < 0) y = 0;
VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0x0F00) << 4
| (VideoMemory[80*y+x] & 0xF000) >> 4
| (VideoMemory[80*y+x] & 0x00FF);
}
};
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)();
}
extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot_magic*/)
{
printf("Hello World! --- http://www.AlgorithMan.de\n");
GlobalDescriptorTable gdt;
InterruptManager interrupts(0x20, &gdt);
printf("Initializing Hardware, Stage 1\n");
DriverManager drvManager;
PrintfKeyboardEventHandler kbhandler;
KeyboardDriver keyboard(&interrupts, &kbhandler);
drvManager.AddDriver(&keyboard);
MouseToConsole mousehandler;
MouseDriver mouse(&interrupts, &mousehandler);
drvManager.AddDriver(&mouse);
PeripheralComponentInterconnectController PCIController;
PCIController.SelectDrivers(&drvManager, &interrupts);
VideoGraphicsArray vga;
printf("Initializing Hardware, Stage 2\n");
drvManager.ActivateAll();
printf("Initializing Hardware, Stage 3\n");
interrupts.Activate();
vga.SetMode(320,200,8);
for(int32_t y = 0; y < 200; y++)
for(int32_t x = 0; x < 320; x++)
vga.PutPixel(x, y, 0x00, 0x00, 0xA8);
while(1);
}