-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
168 lines (144 loc) · 3.25 KB
/
kernel.c
File metadata and controls
168 lines (144 loc) · 3.25 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
int mod(int a, int b);
int div(int a, int b);
void printString(char* string);
void readString(char* line);
void readSector(char* buffer, int sector);
void handleInterrupt21(int ax, int bx, int cx, int dx);
void readFile(char* file_name);
void executeProgram(char* name, int segment);
void terminate();
void shell();
main() {
//char buffer[13312]; /*this is the maximum size of a file*/
//makeInterrupt21();
//interrupt(0x21, 3, "messag\0", buffer, 0); /*read the file into buffer*/
//interrupt(0x21, 0, buffer, 0, 0); /*print out the file*/
//while(1); /*hang up*/
//makeInterrupt21();
//interrupt(0x21, 4, "tstpr2\0", 0x2000, 0);
//while(1);
makeInterrupt21();
shell();
while (1);
}
void shell() {
char prog[6];
prog[0] = 's';
prog[1] = 'h';
prog[2] = 'e';
prog[3] = 'l';
prog[4] = 'l';
prog[5] = 0x0;
interrupt(0x21, 4, prog, 0x2000, 0);
}
void printString(char* string) {
while (*string != '\0') {
interrupt(0x10, 0xe * 256 + *string, 0, 0, 0);
string++;
}
}
void readString(char* line) {
char enter = 0xd;
char back = 0x8;
int i = 0;
while (1) {
char ch = interrupt(0x16, 0, 0, 0, 0);
if (ch == enter) {
line[i] = '\0';
return;
} else if (ch == back) {
if (i > 0) {
line[i] = 0x0;
--i;
interrupt(0x10, 0xe * 256 + ch, 0, 0, 0);
interrupt(0x10, 0xe * 256 + 0x0, 0, 0, 0);
interrupt(0x10, 0xe * 256 + ch, 0, 0, 0);
}
} else {
line[i] = ch;
interrupt(0x10, 0xe * 256 + ch, 0, 0, 0);
++i;
}
}
}
void executeProgram(char* name, int segment) {
char file_buffer[13312];
int i = 0;
readFile(name, file_buffer);
while (i < 13312) {
putInMemory(segment, i, file_buffer[i]);
++i;
}
launchProgram(segment);
}
void terminate() {
shell();
}
void readFile(char* file_name, char* buffer) {
int i = 0;
char dir_buffer[512];
readSector(dir_buffer, 2);
while (i < 16) {
if (dir_buffer[(i * 32)] != 0x0) {
int j = 0;
while (j < 6) {
if (dir_buffer[(i * 32) + j] == file_name[j]) ++j;
else break;
}
if (j == 6) {
int buffer_addr = 0;
while (j < 32) {
int sector = dir_buffer[(i * 32) + j];
if (sector == 0x0) break;
readSector(buffer + buffer_addr, sector);
buffer_addr += 512;
++j;
}
}
}
++i;
}
}
void readSector(char* buffer, int sectorNumber) {
int ah = 2; // tells BIOS to read
int al = 1; // number of sectors to read
int ax = ah * 256 + al;
int bx = buffer; // address where the data should be stored to
int ch = div(sectorNumber, 36); //0 // track number
int cl = mod(sectorNumber, 18) + 1; //13; // relative sector number
int cx = ch * 256 + cl;
int dh = mod(div(sectorNumber, 18), 2); //1; // head number
int dl = 0; // device number; 0=floppy
int dx = dh * 256 + dl;
interrupt(0x13, ax, bx, cx, dx);
}
int mod(int a, int b) {
while (a >= b)
a -= b;
return a;
}
int div(int a, int b) {
int q = 0;
while (a >= b) {
q++;
a -= b;
}
return q;
}
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 {
printString("Error ax must be <= 5 \0");
}
}