-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
301 lines (248 loc) · 5.47 KB
/
main.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
#include "extra.h"
#define PIC1_C 0x20
#define PIC1_D 0x21
#define PIC2_C 0xa0
#define PIC2_D 0xa1
#define ICW1_DEF 0x10
#define ICW1_ICW4 0x01
#define ICW4_x86 0x01
void cls();
void setMonitorColor(char);
void printString(char*);
void printChar(char);
void scroll();
void printColorString(char* , char);
void printColorChar(char , char);
void getDecAscii(int);
void initIDT();
// "extern" allows us to jumps to IDT.asm
extern void loadIdt();
extern void isr1_Handler();
void handleKeypress(int);
void pressed(char);
void picRemap();
unsigned char inportb(unsigned short);
void outportb(unsigned short , unsigned char);
char NumberAscii[10];
int CELL;
char COMMAND[21];
int i = 0;
struct IDT_ENTRY{
unsigned short base_Lower;
unsigned short selector;
unsigned char zero;
unsigned char flags;
unsigned short base_Higher;
};
// * There are a total of
// * 256 possible idt entries.
// * So we created 256 of them with struct
// * IDT_ENTRY idt[256];
struct IDT_ENTRY idt[256]; // used to define the idt
/*
* Says to the compiler to implement compilation
* so that it knows that there will be a section named
* isr1 in an outside source file. We could find this
* section in the IDT.asm file.
*/
extern unsigned int isr1;
unsigned int base;
// This function will from called from Kernel_Entry.asm
int start(){
TM_START = (char*) 0xb8000;
CELL = 0;
base = (unsigned int)&isr1;
cls();
setMonitorColor(0xa5);
char Welcome[] = "Welcome To BLOCKS OS : Copyright @2022\n";
char Welcome2[] = "Command Line Version 1.0.0.0 developed by Team Blocks\n\n";
char OSM[] = "user@admin > ";
printString(Welcome);
printString(Welcome2);
printColorString(OSM , 0xa8);
initIDT();
}
void cls(){
int i = 0;
CELL = 0;
while(i < (2 * 80 * 25)){
*(TM_START + i) = ' '; // Clear screen
i += 2;
}
}
void setMonitorColor(char Color){
int i = 1;
while(i < (2 * 80 * 25)){
*(TM_START + i) = Color;
i += 2;
}
}
void printString(char* cA){
int i = 0;
while(*(cA + i) != '\0'){
printChar(*(cA + i));
i++;
}
}
void printChar(char c){
if(CELL == 2 * 80 * 25)
scroll();
if(c == '\n'){
CELL = ((CELL + 160) - (CELL % 160));
return;
}
*(TM_START + CELL) = c;
CELL += 2;
}
void scroll(){
int i = 160 , y = 0;
while(i < 2 * 80 * 25){
*(TM_START + y) = *(TM_START + i);
i += 2;
y += 2;
}
CELL = 2 * 80 * 24;
i = 0;
while(i < 160){
*(TM_START + CELL + i) = ' ';
i += 2;
}
}
void printColorString(char* c , char co){
int i = 0;
while(*(c + i) != '\0'){
printColorChar(*(c + i) , co);
i++;
}
}
void printColorChar(char c , char co){
if(CELL == 2 * 80 * 25)
scroll();
if(c == '\n'){
CELL = ((CELL + 160) - (CELL % 160));
return;
}
*(TM_START + CELL) = c;
*(TM_START + CELL + 1) = co;
CELL += 2;
}
void getDecAscii(int num){
if(num == 0){
NumberAscii[0] = '0';
return;
}
char NUM[10];
int i = 0 , j = 0;
while(num > 0){
NUM[i] = num % 10;
num /= 10;
i++;
}
i--;
while(i >= 0){
NumberAscii[j] = NUM[i];
i--;
j++;
}
NumberAscii[j] = 'J';
j = 0;
while(NumberAscii[j] != 'J'){
NumberAscii[j] = '0' + NumberAscii[j];
j++;
}
NumberAscii[j] = 0;
}
/*
* The job of this function is to set the
* Interrupt Descriptor Table so that when a
* key is pressed, It will call the isr1_Handler()
* function.
*/
// Mapped the #1 interrupt(Keyboard interrupt(IRQ 1))
void initIDT(){
idt[1].base_Lower = (base & 0xFFFF);
idt[1].base_Higher = (base >> 16) & 0xFFFF;
idt[1].selector = 0x08;
idt[1].zero = 0;
idt[1].flags = 0x8e;
picRemap();
outportb(0x21 , 0xfd);
outportb(0xa1 , 0xff);
loadIdt();
}
/*
* THE inportb AND outportb FUNCTIONS ARE FUNCTIONS THAT HELP US
* COMMUNICATE WITH EXTERNAL DEVICES. in COMMAND ACCEPTS INPUT
* FROM EXTERNAL DEVICES AND out COMMAND OUTPUTS COMMANDS AND
* DATA TO EXTERNAL DEVICES.
*/
// helps to obtain the first scan code of key being pressed
unsigned char inportb(unsigned short _port){
unsigned char rv;
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}
void outportb(unsigned short _port, unsigned char _data){
__asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}
/*
* When ever a key is pressed, the fuction isr1_Handler() will be called
* isr1_Handler mentioned in IDT.asm
*/
extern void isr1_Handler(){
handleKeypress(inportb(0x60));
outportb(0x20 , 0x20);
outportb(0xa0 , 0x20);
}
/*
* The arrangement of values in Scancode[] gives us the ascii
* representation of the scan code.
*/
void handleKeypress(int code){
char OSM[] = "\nuser@admin > ";
char Scancode[] = {
0 , 0 , '1' , '2' ,
'3' , '4' , '5' , '6' ,
'7' , '8' , '9' , '0' ,
'-' , '=' , 0 , 0 , 'Q' ,
'W' , 'E' , 'R' , 'T' , 'Y' ,
'U' , 'I' , 'O' , 'P' , '[' , ']' ,
0 , 0 , 'A' , 'S' , 'D' , 'F' , 'G' ,
'H' , 'J' , 'K' , 'L' , ';' , '\'' , '`' ,
0 , '\\' , 'Z' , 'X' , 'C' , 'V' , 'B' , 'N' , 'M' ,
',' , '.' , '/' , 0 , '*' , 0 , ' '
};
if(code == 0x1c) {
COMMAND[i] = '\0';
i = 0;
strEval(COMMAND);
printString(OSM);
}
else if(code < 0x3a)
pressed(Scancode[code]);
}
void pressed(char key){
if(i != 20){
COMMAND[i] = key;
i++;
printChar(key);
}
else{
blink();
}
}
void picRemap(){
unsigned char a , b;
a = inportb(PIC1_D);
b = inportb(PIC2_D);
outportb(PIC1_C , ICW1_DEF | ICW1_ICW4);
outportb(PIC2_C , ICW1_DEF | ICW1_ICW4);
outportb(PIC1_D , 0);
outportb(PIC2_D , 8);
outportb(PIC1_D , 4);
outportb(PIC2_D , 2);
outportb(PIC1_D , ICW4_x86);
outportb(PIC2_D , ICW4_x86);
outportb(PIC1_D , a);
outportb(PIC2_D , b);
}