-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.c
317 lines (248 loc) · 6.93 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
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
#include "includes/keyboard_map.h"
#include "includes/itoa.h"
#include "includes/terminal_fg_colors.h"
#define LINES 25
#define COLUMNS_IN_LINE 80
#define BYTES_FOR_EACH_ELEMENT 2
#define SCREENSIZE BYTES_FOR_EACH_ELEMENT * COLUMNS_IN_LINE * LINES
#define KEYBOARD_DATA_PORT 0x60
#define KEYBOARD_STATUS_PORT 0x64
#define IDT_SIZE 256
#define INTERRUPT_GATE 0x8e
#define KERNEL_CODE_SEGMENT_OFFSET 0x08
#define ENTER_KEY_CODE 0x1C
//#define BACKSPACE_KEY_CODE 0x0E
extern unsigned char keyboard_map[128];
extern void keyboard_handler(void);
extern char read_port(unsigned short port);
extern void write_port(unsigned short port, unsigned char data);
extern void load_idt(unsigned long *idt_ptr);
/* current cursor location */
unsigned int current_loc = 0;
int current_loc_y = 0;
int current_loc_x = 11;
char current_command[255] = {0};
int current_command_length = 0;
/* video memory begins at address 0xb8000 */
char *vidptr = (char*)0xb8000;
struct IDT_entry {
unsigned short int offset_lowerbits;
unsigned short int selector;
unsigned char zero;
unsigned char type_attr;
unsigned short int offset_higherbits;
};
struct IDT_entry IDT[IDT_SIZE];
void idt_init(void)
{
unsigned long keyboard_address;
unsigned long idt_address;
unsigned long idt_ptr[2];
keyboard_address = (unsigned long)keyboard_handler;
IDT[0x21].offset_lowerbits = keyboard_address & 0xffff;
IDT[0x21].selector = KERNEL_CODE_SEGMENT_OFFSET;
IDT[0x21].zero = 0;
IDT[0x21].type_attr = INTERRUPT_GATE;
IDT[0x21].offset_higherbits = (keyboard_address & 0xffff0000) >> 16;
/* Ports
* PIC1 PIC2
*Command 0x20 0xA0
*Data 0x21 0xA1
*/
write_port(0x20 , 0x11);
write_port(0xA0 , 0x11);
write_port(0x21 , 0x20);
write_port(0xA1 , 0x28);
write_port(0x21 , 0x00);
write_port(0xA1 , 0x00);
write_port(0x21 , 0x01);
write_port(0xA1 , 0x01);
write_port(0x21 , 0xff);
write_port(0xA1 , 0xff);
idt_address = (unsigned long)IDT ;
idt_ptr[0] = (sizeof (struct IDT_entry) * IDT_SIZE) + ((idt_address & 0xffff) << 16);
idt_ptr[1] = idt_address >> 16 ;
load_idt(idt_ptr);
}
void kb_init(void)
{
write_port(0x21 , 0xFD);
}
void kprint(const char *str,int color)
{
unsigned int i = 0;
while (str[i] != '\0') {
vidptr[current_loc++] = str[i++];
vidptr[current_loc++] = color;
}
}
void back_cursor_function_x(){
current_loc_x = current_loc_x - 1;
//current_loc_y = current_loc_y + 1;
update_cursor((int)current_loc_x,(int)current_loc_y);
}
void reset_cursor_function_x(){
current_loc_x = current_loc_x - current_loc_x + 11;
//current_loc_y = current_loc_y + 1;
update_cursor((int)current_loc_x,(int)current_loc_y);
}
void update_cursor_function_x(){
current_loc_x = current_loc_x + 1 ;
//current_loc_y = current_loc_y + 1;
update_cursor((int)current_loc_x,(int)current_loc_y);
}
void reset_cursor_function_y(){
//current_loc_x = current_loc_x + 1 ;
current_loc_y = current_loc_y - current_loc_y;
update_cursor((int)current_loc_x,(int)current_loc_y);
}
void update_cursor_function_y(){
//current_loc_x = current_loc_x + 1 ;
current_loc_y = current_loc_y + 1;
update_cursor((int)current_loc_x,(int)current_loc_y);
}
void kprint_newline(void)
{
unsigned int line_size = BYTES_FOR_EACH_ELEMENT * COLUMNS_IN_LINE;
current_loc = current_loc + (line_size - current_loc % (line_size));
update_cursor_function_y();
}
void clear_screen(void)
{
unsigned int i = 0;
while (i < SCREENSIZE) {
vidptr[i++] = ' ';
vidptr[i++] = 0x07;
}
}
void enable_cursor(int cursor_start, int cursor_end)
{
write_port(0x3D4, 0x0A);
write_port(0x3D5, (read_port(0x3D5) & 0xC0) | cursor_start);
write_port(0x3D4, 0x0B);
write_port(0x3D5, (read_port(0x3D5) & 0xE0) | cursor_end);
}
void update_cursor(int x, int y)
{
int pos = y * COLUMNS_IN_LINE + x;
write_port(0x3D4, 0x0F);
write_port(0x3D5, (int) (pos & 0xFF));
write_port(0x3D4, 0x0E);
write_port(0x3D5, (int) ((pos >> 8) & 0xFF));
}
int get_cursor_position(void)
{
int pos = 0;
write_port(0x3D4, 0x0F);
pos |= read_port(0x3D5);
write_port(0x3D4, 0x0E);
pos |= ((int)read_port(0x3D5)) << 8;
return pos;
}
void keyboard_handler_main(void)
{
unsigned char status;
char keycode;
write_port(0x20, 0x20);
status = read_port(KEYBOARD_STATUS_PORT);
if (status & 0x01) {
keycode = read_port(KEYBOARD_DATA_PORT);
if(keycode < 0)
return;
if(keycode == ENTER_KEY_CODE) {
kprint_newline();
reset_cursor_function_x();
current_command[current_command_length] = '\0';
unsigned int i = 0;
char command[255] = "clear\0";
int command_status = 1;
int loop_command_length = 0;
kprint("'",VGA_COLOR_GREEN);
while (current_command[i] != '\0') {
if (keyboard_map[(unsigned char) current_command[i]] == command[i]){
if (command_status == 0){}
else{
command_status =1;
}
}
else{
command_status = 0;
}
vidptr[current_loc++] = keyboard_map[(unsigned char) current_command[i]];
vidptr[current_loc++] = 0x07;
i++;
loop_command_length++;
}
if (loop_command_length == 0){kprint("Please enter a command",VGA_COLOR_GREEN);}
else{
kprint("'",VGA_COLOR_GREEN);
if (command_status==1){
clear_screen();
reset_cursor_function_y();
reset_cursor_function_x();
current_loc = 0;
}
else{
kprint(" is a Invalid Command",VGA_COLOR_GREEN);
}
}
i=0;
kprint_newline();
current_command_length = 0;
kprint((const char*)"TERMINAL>> ",VGA_COLOR_GREEN);
return;
}
if (keyboard_map[(unsigned char) keycode] == '\b'){
if (current_command_length <=0){
}
else{
current_loc = current_loc - 2;
vidptr[current_loc++] = ' ';
vidptr[current_loc++] = 0x07;
current_loc = current_loc - 2;
current_command[current_command_length - 1] = '0';
current_command_length = current_command_length - 1;
back_cursor_function_x();
}
return;
}
update_cursor_function_x();
/*
char buffer [33];
kprint(itoa( (int)current_loc, buffer, ITOA_DECIMAL_VALUE ),VGA_COLOR_GREEN);
*/
current_command[current_command_length] = (char)keycode ;
current_command_length++;
vidptr[current_loc++] = keyboard_map[(unsigned char) keycode];
vidptr[current_loc++] = 0x07;
}
}
void kmain(void)
{
const char *str = "Lisse Os 0.0.1 x86";
clear_screen(); // clear all the junk on the screen before we start
kprint(str,VGA_COLOR_CYAN);
kprint_newline();
kprint_newline();
idt_init();
kprint("[",VGA_COLOR_GREEN);
kprint("1",VGA_COLOR_LIGHT_BLUE);
kprint("]",VGA_COLOR_GREEN);
kprint(" Loaded IDT!",VGA_COLOR_GREEN);
kprint_newline();
kb_init();
kprint("[",VGA_COLOR_GREEN);
kprint("2",VGA_COLOR_LIGHT_BLUE);
kprint("]",VGA_COLOR_GREEN);
kprint(" Loaded keyboard support!",VGA_COLOR_GREEN);
kprint_newline();
enable_cursor(0,15);
kprint("[",VGA_COLOR_GREEN);
kprint("3",VGA_COLOR_LIGHT_BLUE);
kprint("]",VGA_COLOR_GREEN);
kprint(" Enabled Curser!",VGA_COLOR_GREEN);
kprint_newline();
kprint_newline();
kprint((const char*)"TERMINAL>> ",VGA_COLOR_GREEN);
while(1);
}