-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernel.c
More file actions
344 lines (256 loc) · 7.19 KB
/
kernel.c
File metadata and controls
344 lines (256 loc) · 7.19 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
/*
* Prototypes Definition
*/
void printString(char*);
void readString(char*);
int MOD(int, int);
int DIV(int, int);
void readSector(char*, int);
void writeSector(char*, int);
void readFile(char*, char*);
void writeFile(char* name, char* buffer, int secNum);
void deleteFile(char* name);
void executeProgram(char*, int);
void terminate();
void handleInterrupt21(int, int, int, int);
/*
* Main KERNEL program;
*/
main(){
makeInterrupt21();
interrupt(0x21, 4, "shell\0", 0x2000, 0); // Run the shell
while(1);
}
void printString(char* text){
int i = 0;
char c;
while( *(text+i) != 0x0 ){
c = *(text+i);
if( c == 0xA ) { interrupt(0x10, 0xE*256+(0xD), 0, 0, 0); }
interrupt(0x10, 0xE*256+(c), 0, 0, 0);
if( c == 0xA ) { interrupt(0x10, 0xE*256+(0x8), 0, 0, 0); }
i++;
}
}
void readString(char* text){
int size;
char c;
char* ptr;
size = 80;
ptr = text;
while( size && c != 0xD ){
c = interrupt(0x16, 0x0, 0, 0, 0);
if( c != 0x8 ){
interrupt(0x10, 0xE*256+c, 0, 0, 0);
*ptr++ = c;
size--;
} else {
if( ptr != text ){
interrupt(0x10, 0xE*256+c, 0, 0, 0);
interrupt(0x10, 0xE*256+0x20, 0, 0, 0);
interrupt(0x10, 0xE*256+0x8, 0, 0, 0);
ptr--;
size++;
}
}
}
if( c == 0xD ){
*ptr = 0xA;
} else {
interrupt(0x10, 0xE*256+0xD, 0, 0, 0);
*ptr = 0xD;
*++ptr = 0xA;
}
interrupt(0x10, 0xE*256+(*ptr), 0, 0, 0);
*++ptr=0x0;
}
int MOD(int x, int y){
if(x<y) return x;
return MOD(x-y, y);
}
int DIV(int x, int y){
if(x<y) return 0;
return (1+DIV(x-y,y));
}
void readSector(char* buffer, int sector){
int rvSector;
int head;
int track;
rvSector = (MOD(sector, 18) + 1);
head = MOD(DIV(sector, 18), 2);
track = DIV(sector, 36);
interrupt(0x13, 0x2*256+(0x1), buffer, (track)*256+rvSector, (head)*256+0x0);
}
void writeSector(char* buffer, int sector){
int rvSector;
int head;
int track;
rvSector = (MOD(sector, 18) + 1);
head = MOD(DIV(sector, 18), 2);
track = DIV(sector, 36);
interrupt(0x13, 0x3*256+(0x1), buffer, (track)*256+rvSector, (head)*256+0x0);
}
void readFile(char* fileName, char* buffer){
int iDir;
int i;
int j;
int zPos;
char dirBuffer[512];
readSector(dirBuffer, 2);
iDir=0;
if( *fileName==0 ) { interrupt(0x21, 0, "ERROR 0x05: No entered File Name.\0", 0, 0); return; }
zPos = 0;
while( *(fileName+zPos) != 0x0 ) zPos++;
if( zPos > 6 ) iDir = 512; // Since there doesNOT exist any file whose name > 6 characters. No need for search.
while(iDir<512){
i=0;
while( i<zPos ){
if(*(fileName+i)!=*(dirBuffer+iDir)) break;
iDir++;
i++;
}
if( i==zPos ){
while( i<6 ) {
if( *(dirBuffer+iDir) != 0x0 ) break;
iDir++;
i++;
}
if( i!=6 ) { iDir=(iDir-i) + 32; continue; }
} else { iDir=(iDir-i) + 32; continue; }
for(i=0; i<26; i++){
if(*(dirBuffer+iDir+i) != 0) {
readSector(buffer, *(dirBuffer+iDir+i));
buffer += 512;
} else break;
}
while( i<26 ){
for(j=0; j<512; j++) *(buffer+j) = 0x0;
buffer += 512;
i++;
}
return;
}
if( iDir==512 ){ interrupt(0x21, 0, "ERROR 0x04: Cannot Find a file with the name specified to read.\0", 0, 0); return; }
}
void writeFile(char* name, char* buffer, int secNum){
char mapSector[512];
char dirSector[512];
char emptySectors[26];
int iMap;
int iFile;
int iPerFile;
int iSec;
readSector(mapSector, 1);
readSector(dirSector, 2);
if( secNum > 26 ) { interrupt(0x21, 0, "ERROR 0x00: FileSystem Cannot Support >26 Sectors Files.\0", 0, 0); return; }
/* This will try to find a number of empty sectors equivalent to "secNum" and
* stores their addresses in an array called "emptySectors"
*/
iSec = 0; iMap = 0;
while( iSec<secNum ){
// Try to find an empty Sector
while( iMap<512 && *(mapSector+iMap)!=0x00 )
iMap++;
// If not able to find one and still it's needed; print an error and return
if( iMap==512 ) { interrupt(0x21, 0, "ERROR 0x01: No Disk Space for your file.\0", 0, 0); return; }
else { *(emptySectors + iSec) = (iMap); iSec++; iMap++; }
}
/* This will make "j" point to the first empty Directory entry in
* the dirSector; if none exists it'll stop at 512
*/
iFile = 0;
while( iFile<512 && *(dirSector + iFile)!=0x00 )
iFile += 32;
if( iFile >= 512 ) { interrupt(0x21, 0, "ERROR 0x02: Current FileSystem Cannot support more files (>13).\0", 0, 0); return; }
// This will write the filename to the directory entry
iPerFile = 0;
while ( iPerFile<6 && *(name+iPerFile)!='\0')
*((dirSector+iFile) + iPerFile++) = *(name + iPerFile);
while ( iPerFile<6 )
*((dirSector+iFile) + iPerFile++) = 0x00;
// This will write the File sectors to the disk, update the Directory entry and mapSectors
iSec = 0;
while( (iPerFile-6) < secNum ) {
*((dirSector + iFile) + iPerFile) = *(emptySectors + iSec);
*(mapSector + ((*(emptySectors + iSec)) - 1)) = 0xFF;
writeSector(buffer, *(emptySectors + iSec));
iPerFile++;
iSec++;
}
// Write back the updated Directory and Map sectors
writeSector(mapSector, 1);
writeSector(dirSector, 2);
}
void deleteFile(char* name){
char mapSector [512];
char dirSector [512];
int iDir;
int i;
int zPos;
iDir=0;
if(*name=='s' && *(name+1)=='h' && *(name+2)=='e' && *(name+3)=='l' && *(name+4)=='l' && *(name+5)=='\0')
return;
if(*name=='K' && *(name+1)=='E' && *(name+2)=='R' && *(name+3)=='N' && *(name+4)=='E' && *(name+5)=='L' && *(name+6)=='\0')
return;
readSector(mapSector, 1);
readSector(dirSector, 2);
if( *name==0 ) { interrupt(0x21, 0, "ERROR 0x05: No entered File Name.\0", 0, 0); return; }
zPos = 0;
while( *(name+zPos) != 0x0 ) zPos++;
if( zPos > 6 ) iDir = 512; // Since there doesNOT exist any file whose name > 6 characters. No need for search.
while( iDir<512 ){
i=0;
while( i<zPos ){
if(*(name+i)!=*(dirSector+iDir+i)) break;
i++;
}
if( i==zPos ){
while( i<6 ) {
if( *(dirSector+iDir+i) != 0x0 ) break;
i++;
}
if( i!=6 ) { iDir += 32; continue; }
} else { iDir += 32; continue; }
*(dirSector+iDir) = 0x0;
for(i=0; i<26; i++){
if( *(dirSector+iDir+6+i) != 0x00 ) { *( mapSector + *(dirSector+iDir+6+i) ) = 0x00; *(dirSector+iDir+6+i) = 0x0; }
else break;
}
break;
}
if( iDir==512 ){ interrupt(0x21, 0, "ERROR 0x03: Cannot Find a file with the name specified to delete.\0", 0, 0); return; }
writeSector(mapSector, 1);
writeSector(dirSector, 2);
}
void executeProgram(char* name, int segment){
char progBuffer[13312];
int ptr = 0;
for(ptr=0; ptr<13312; ptr++) progBuffer[ptr] = 0x0;
ptr = 0;
readFile(name, progBuffer);
if( *progBuffer==0x0 ) { return; }
while( ptr<13312 ) putInMemory(segment, ptr++, *(progBuffer+ptr));
launchProgram(segment);
}
void terminate(){
char shell[6];
shell[0] = 's';
shell[1] = 'h';
shell[2] = 'e';
shell[3] = 'l';
shell[4] = 'l';
shell[5] = '\0';
interrupt(0x21, 4, shell, 0x2000, 0);
}
void handleInterrupt21(int ax, int bx, int cx, int dx){
if(ax==0) printString(bx);
else if(ax==1) readString(bx);
else if(ax==2) readSector(bx,cx);
else if(ax==3) readFile(bx,cx);
else if(ax==4) executeProgram(bx,cx);
else if(ax==5) terminate();
else if(ax==6) writeSector(bx,cx);
else if(ax==7) deleteFile(bx);
else if(ax==8) writeFile(bx,cx,dx);
else printString("ERROR 0xFF: Undefined Interrupt !!\0");
}