forked from gifburst/charlex-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.c
289 lines (234 loc) · 6.31 KB
/
kernel.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "includes/kernel.h"
#include "includes/utils.h"
#include "includes/char.h"
#include "includes/reqs.h"
uint32 vga_index;
static uint32 next_line_index = 1;
uint8 g_fore_color = WHITE, g_back_color = BLACK;
int digit_ascii_codes[10] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
/*
this is same as we did in our assembly code for vga_print_char
vga_print_char:
mov di, word[VGA_INDEX]
mov al, byte[VGA_CHAR]
mov ah, byte[VGA_BACK_COLOR]
sal ah, 4
or ah, byte[VGA_FORE_COLOR]
mov [es:di], ax
ret
*/
uint16 vga_entry(unsigned char ch, uint8 fore_color, uint8 back_color){
uint16 ax = 0;
uint8 ah = 0, al = 0;
ah = back_color;
ah <<= 4;
ah |= fore_color;
ax = ah;
ax <<= 8;
al = ch;
ax |= al;
return ax;
}
void clear_vga_buffer(uint16 **buffer, uint8 fore_color, uint8 back_color){
uint32 i;
for(i = 0; i < BUFSIZE; i++){
(*buffer)[i] = vga_entry(NULL, fore_color, back_color);
}
next_line_index = 1;
vga_index = 0;
}
// set terminal color
void init_vga(uint8 fore_color, uint8 back_color){
vga_buffer = (uint16*)VGA_ADDRESS;
clear_vga_buffer(&vga_buffer, fore_color, back_color);
g_fore_color = fore_color;
g_back_color = back_color;
}
//this function print a new line on your termninal like '\n'
void newline_on_terminal(){
if(next_line_index >= 55){
next_line_index = 0;
clear_vga_buffer(&vga_buffer, g_fore_color, g_back_color);
}
vga_index = 80*next_line_index;
next_line_index++;
}
void print_char(char ch){
vga_buffer[vga_index] = vga_entry(ch, g_fore_color, g_back_color);
vga_index++;
}
void print_on_terminal(char *str){
uint32 index =0;
while(str[index]){
print_char(str[index]);
index++;
}
}
void print_int(int num){
char str_num[digit_count(num)+1];
itoa(num, str_num);
print_on_terminal(str_num);
}
uint8 inb(uint16 port){
uint8 ret;
asm volatile("inb %1, %0" : "=a"(ret) : "d"(port));
return ret;
}
void outb(uint16 port, uint8 data)
{
asm volatile("outb %0, %1" : "=a"(data) : "d"(port));
}
// get keys from your keyboard
//and read
char get_input_prompt(){
char ch = 0;
while((ch = inb(KEYBOARD_PORT)) != 0){
if(ch > 0)
return ch;
}
return ch;
}
/*
keep the cpu busy for doing nothing(nop)
so that io port will not be processed by cpu
here timer can also be used, but lets do this in looping counter
*/
void wait_for_io(uint32 timer_count){
while(1){
asm volatile("nop" /*do noting*/ );
timer_count--;
if(timer_count <= 0)
break;
}
}
//sleep function
void sleep(uint32 timer_count)
{
wait_for_io(timer_count);
}
// get and read the keys and kernel reaction
void input()
{
char ch = 0;
char keycode = 0;
do{
int enter_cuonter=1;
keycode = get_input_prompt();
// if client press enter (KEY)
if(keycode == KEY_ENTER){
newline_on_terminal();
print_on_terminal("# ");
enter_cuonter++;
}
/*
* if client press backspace for delete a single
*/
else if(keycode == KEY_BACKSPACE){
if (vga_index > 0){
vga_index = vga_index-1;
print_on_terminal(" ");
vga_index = vga_index-1;
}
else{
get_input_prompt();
}
}
//if client press arrow up (KEY)
else if(keycode == KEY_UP){
newline_on_terminal();
print_on_terminal("KEY : UP ARROW | STATUS: PRESSED ; ");
newline_on_terminal();
print_on_terminal("this key not work in this terminal !");
newline_on_terminal();
newline_on_terminal();
print_on_terminal("# ");
enter_cuonter++;
get_input_prompt();
}
//if client press arrow down (KEY)
else if(keycode == KEY_DOWN){
newline_on_terminal();
print_on_terminal("KEY : DOWN ARROW | STATUS : PRESSED; ");
newline_on_terminal();
print_on_terminal("this key not work in this terminal !");
newline_on_terminal();
newline_on_terminal();
print_on_terminal("# ");
enter_cuonter++;
get_input_prompt();
}
//if client press TAB (KEY)
else if(keycode == KEY_TAB){
newline_on_terminal();
print_on_terminal("KEY : TAB | STATUS : PRESSED ; ");
newline_on_terminal();
print_on_terminal("[ESC (key)] : exit, [BACKSPACE (key)] : delete a charecter ");
newline_on_terminal();
newline_on_terminal();
print_on_terminal("# ");
enter_cuonter++;
get_input_prompt();
}
//if client press ESC (KEY)
else if(keycode == KEY_ESC){
init_vga(RED,BLACK);
newline_on_terminal();
print_on_terminal("EXIT ! : ");
newline_on_terminal();
print_on_terminal("if you want use CHARLEX-OS => please REBOOT your machine !");
newline_on_terminal();
newline_on_terminal();
break;
enter_cuonter++;
newline_on_terminal();
}
else{
ch = get_ascii_char(keycode);
print_char(ch);
}
/*
* NO SCROLL TERMINAL (static prompt )
*/
if(enter_cuonter<5){
enter_cuonter = enter_cuonter *2;
next_line_index = next_line_index - enter_cuonter;
}
else{
next_line_index = next_line_index - enter_cuonter;
}
for (int i = 0; i < 1; i++){
sleep(0x4CFFFFFF);/* keyboard type speed for (((real machine))) */
}
}while(ch > 0);
}
// kernel entery point !!
void kernel_up(){
// color of terminal(you can change it if you want :D )
init_vga(WHITE, BLACK);
newline_on_terminal();
logo();
newline_on_terminal();
newline_on_terminal();
print_on_terminal("|------------------|");
newline_on_terminal();
print_on_terminal("!Welcome to charleX!");
newline_on_terminal();
print_on_terminal("! version 0.1 !");
newline_on_terminal();
print_on_terminal("|------------------|");
for (int i = 0; i < 40; i++){
sleep(0x02FAFFFFF); // sleep for logo to load ;;
}
//color set to green and black
init_vga(WHITE, BLACK);
newline_on_terminal();
// input() for infinity loop (terminal loop infinity)
print_on_terminal("# ");
while (1){
char keycode = 0;
keycode = get_input_prompt();
if (keycode == KEY_ESC){break;}
if (keycode == KEY_ENTER){next_line_index =next_line_index-1;}
input();
}
}