-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
390 lines (332 loc) · 6.96 KB
/
Copy pathkernel.c
File metadata and controls
390 lines (332 loc) · 6.96 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include"kernel.h"
#include "utils.h"
#include "char.h"
#define CALC_SLEEP 4
uint16 cursor_pos = 0, cursor_next_line_index = 1;
static int Y_INDEX = 1;
UINT16 MEM_SIZE = 0;
static UINT16 VGA_DefaultEntry(unsigned char to_print) {
return (UINT16) to_print | (UINT16)VGA_COLOR_WHITE << 8;
}
static UINT16 VGA_ColoredEntry(unsigned char to_print, UINT8 color) {
return (UINT16) to_print | (UINT16)color << 8;
}
void Clear_VGA_Buffer(UINT16 **buffer)
{
for(int i=0;i<BUFSIZE;i++){
(*buffer)[i] = '\0';
}
Y_INDEX = 1;
VGA_INDEX = 0;
}
void clear_screen()
{
Clear_VGA_Buffer(&TERMINAL_BUFFER);
cursor_pos = 0;
cursor_next_line_index = 1;
}
void InitTerminal()
{
TERMINAL_BUFFER = (UINT16*) VGA_ADDRESS;
Clear_VGA_Buffer(&TERMINAL_BUFFER);
}
void strcat(char *str_1, char *str_2)
{
int index_1 = strlen(str_1);
int index_2 = 0;
while(str_2[index_2]){
str_1[index_1] = str_2[index_2];
index_1++;
index_2++;
}
str_1[index_1] = '\0';
}
void memcpy(char *str_dest, char *str_src)
{
int index = 0;
while(str_src[index]){
str_dest[index] = str_src[index];
index++;
}
str_dest[index] = '\0';
}
int digitCount(int num)
{
int count = 0;
if(num == 0)
return 1;
while(num > 0){
count++;
num = num/10;
}
return count;
}
uint8 inb(uint16 port)
{
uint8 data;
asm volatile("inb %1, %0" : "=a"(data) : "Nd"(port));
return data;
}
void outb(uint16 port, uint8 data)
{
asm volatile("outb %0, %1" : : "a"(data), "Nd"(port));
}
char getInputCode() {
char ch = 0;
while((ch = inb(KEYBOARD_PORT)) != 0){
if(ch > 0)
return ch;
}
return ch;
}
void printNewLine()
{
if(Y_INDEX >= 55){
Y_INDEX = 0;
Clear_VGA_Buffer(&TERMINAL_BUFFER);
}
VGA_INDEX = 80*Y_INDEX;
Y_INDEX++;
}
void printN_NewLine(int n)
{
for(int i=0;i<n;i++)
printNewLine();
}
void printString(char *str)
{
int index = 0;
while(str[index]){
TERMINAL_BUFFER[VGA_INDEX] = VGA_DefaultEntry(str[index]);
index++;
VGA_INDEX++;
}
}
void printInt(int num)
{
char str_num[digitCount(num)+1];
itoa(num, str_num);
printString(str_num);
}
void printColoredString(char *str, UINT8 color)
{
int index = 0;
while(str[index]){
TERMINAL_BUFFER[VGA_INDEX] = VGA_ColoredEntry(str[index], color);
index++;
VGA_INDEX++;
}
}
void printCharN(char ch, int n)
{
int i = 0;
while(i <= n){
TERMINAL_BUFFER[VGA_INDEX] = VGA_DefaultEntry(ch);
i++;
VGA_INDEX++;
}
}
void printChar(char ch)
{
TERMINAL_BUFFER[VGA_INDEX] = VGA_DefaultEntry(ch);
VGA_INDEX++;
}
void print_int(int num)
{
char str_num[digit_count(num)+1];
itoa(num, str_num);
printString(str_num);
}
void printColoredCharN(char ch, int n, UINT8 color)
{
int i = 0;
while(i <= n){
TERMINAL_BUFFER[VGA_INDEX] = VGA_ColoredEntry(ch, color);
i++;
VGA_INDEX++;
}
}
void printColored_WCharN(UINT16 ch, int n, UINT8 color)
{
int i = 0;
while(i <= n){
TERMINAL_BUFFER[VGA_INDEX] = VGA_ColoredEntry(ch, color);
i++;
VGA_INDEX++;
}
}
//Operational part
void wait_for_io(uint32 timer_count)
{
while(1){
asm volatile("nop");
timer_count--;
if(timer_count <= 0)
break;
}
}
void sleep(uint32 timer_count)
{
wait_for_io(timer_count*0x02FFFFFF);
}
int read_int()
{
char ch = 0;
char keycode = 0;
char data[32];
int index = 0;
do{
keycode = getInputCode();
if(keycode == KEY_ENTER){
data[index] = '\0';
printNewLine();
break;
}else{
ch = get_ascii_char(keycode);
printChar(ch);
data[index] = ch;
index++;
}
sleep(CALC_SLEEP);
}while(ch > 0);
return atoi(data);
}
char getchar()
{
char keycode = 0;
sleep(CALC_SLEEP);
keycode = getInputCode();
sleep(CALC_SLEEP);
return get_ascii_char(keycode);
}
void DisplayIntro()
{
printColored_WCharN(2481,79,VGA_COLOR_RED);
printNewLine();
printColored_WCharN(2483,79,VGA_COLOR_LIGHT_GREEN);
printN_NewLine(4);
VGA_INDEX += 30;
printColoredString("Flyke Operating System", VGA_COLOR_YELLOW);
printN_NewLine(6);
VGA_INDEX += 28;
printColoredString("Kernel in C - 16/32 Bit", VGA_COLOR_WHITE);
printN_NewLine(6);
VGA_INDEX += 25;
printColoredString("! Press any key to move next !", VGA_COLOR_BROWN);
printN_NewLine(6);
printColored_WCharN(2483,79, VGA_COLOR_LIGHT_GREEN);
printNewLine();
printColored_WCharN(2481,79, VGA_COLOR_RED);
getInputCode();
getInputCode();
Clear_VGA_Buffer(&TERMINAL_BUFFER);
}
void display_menu()
{
VGA_INDEX += 25;
printString("! Flyke Operating System !");
printN_NewLine(2);
printString("***** Basic Operations *****");
printN_NewLine(2);
printString("!--- Menu ---!");
printN_NewLine(2);
printString("1. Addition");
printNewLine();
printString("2. Substraction");
printNewLine();
printString("3. Multiplication");
printNewLine();
printString("4. Division");
printNewLine();
printString("5. Modulus");
printNewLine();
printString("6. Logical AND");
printNewLine();
printString("7. Logical OR");
printNewLine();
printString("8. Exit");
}
void read_two_numbers(int* num1, int *num2)
{
printString("Enter first number : ");
sleep(CALC_SLEEP);
*num1 = read_int();
printString("Enter second number : ");
sleep(CALC_SLEEP);
*num2 = read_int();
}
void Operation()
{
int choice, num1, num2;
while(1){
display_menu();
printN_NewLine(2);
printString("Enter your choice : ");
choice = read_int();
switch(choice){
case 1:
read_two_numbers(&num1, &num2);
printString("Addition : ");
print_int(num1 + num2);
break;
case 2:
read_two_numbers(&num1, &num2);
printString("Substraction : ");
print_int(num1 - num2);
break;
case 3:
read_two_numbers(&num1, &num2);
printString("Multiplication : ");
print_int(num1 * num2);
break;
case 4:
read_two_numbers(&num1, &num2);
if(num2 == 0){
printString("Error: Divide by 0");
}else{
printString("Division : ");
print_int(num1 / num2);
}
break;
case 5:
read_two_numbers(&num1, &num2);
printString("Modulus : ");
print_int(num1 % num2);
break;
case 6:
read_two_numbers(&num1, &num2);
printString("LogicalAND : ");
print_int(num1 & num2);
break;
case 7:
read_two_numbers(&num1, &num2);
printString("Logical OR : ");
print_int(num1 | num2);
break;
case 8:
printNewLine();
printString("Exiting...");
sleep(CALC_SLEEP*3);
clear_screen();
KERNEL_MAIN();
printString("Exited...");
return;
default:
printNewLine();
printString("Invalid choice...!");
break;
}
printN_NewLine(2);
printString("Press any key to reload screen...");
getchar();
clear_screen();
}
}
void KERNEL_MAIN()
{
InitTerminal();
sleep(CALC_SLEEP);
clear_screen();
DisplayIntro();
Operation();
}