-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
374 lines (359 loc) · 12 KB
/
Copy pathkernel.c
File metadata and controls
374 lines (359 loc) · 12 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
// .c
// OS Kernel Entry Point
// by Kyle Furey
#include "hlos.h"
/** Each selection. */
typedef enum selection {
SELECTION_NONE,
SELECTION_FILES,
SELECTION_TEXTEDIT,
SELECTION_SHUTDOWN,
SELECTION_FILES_OPEN,
SELECTION_FILES_SAVE,
SELECTION_FILES_DELETE,
SELECTION_FILES_FORMAT,
SELECTION_FILES_QUIT,
} selection_t;
/** Opening sequence. */
void opening();
/** Selection sequence. */
selection_t selection();
/** Coroutine for the clock. */
void clock(void *args);
/** Text-editing sequence. */
string_t textedit(string_t text);
/** Requests a directory from the user. */
string_t directory(string_t prompt, string_t operation);
/** Tests pixel rendering with noise. */
void noisetest();
/** The entry point of the HLOS kernel. */
void kernel_main() {
init();
noisetest(); // Noop when disabled
sleep(1000);
opening();
clock(NULL);
char_t text[VGA_SIZE] = {0};
strcopy(text, "int main() {\n\treturn 0;\n}\n");
char_t dir[VGA_WIDTH - 2] = {0};
uint_t i;
while (true) {
switch (selection()) {
default:
break;
case SELECTION_FILES_OPEN:
strcopy(dir, directory("file", "OPEN"));
i = strlast(dir, '.');
if (i != NOT_FOUND) {
if (strcompare(strlower(dir + i + 1), "txt") == EQUAL_TO) {
uint_t size;
if (filesize(dir, &size) && fileread(dir, 0, ALL_BYTES, text)) {
text[size] = '\0';
copy(text, textedit(text), size);
} else {
color(VGA_COLOR_RED, VGA_COLOR_BLACK);
print("\n\nFile not found!");
sleep(1000);
}
} else if (strcompare(dir + i + 1, "exe") == EQUAL_TO) {
if (fileread(dir, 0, ALL_BYTES, text)) {
call(text);
} else {
color(VGA_COLOR_RED, VGA_COLOR_BLACK);
print("\n\nFile not found!");
sleep(1000);
}
} else {
color(VGA_COLOR_RED, VGA_COLOR_BLACK);
print("\n\nUnknown file type!");
sleep(1000);
}
} else {
color(VGA_COLOR_RED, VGA_COLOR_BLACK);
print("\n\nFile not found!");
sleep(1000);
}
break;
case SELECTION_FILES_SAVE:
strcopy(dir, directory("filename", "SAVE"));
if (strlen(dir) > 0) {
filewrite(dir, strlen(text), text);
} else {
color(VGA_COLOR_RED, VGA_COLOR_BLACK);
print("\n\nInvalid file name!");
sleep(1000);
}
break;
case SELECTION_FILES_DELETE:
strcopy(dir, directory("filename", "DELETE"));
if (!filedelete(dir)) {
color(VGA_COLOR_RED, VGA_COLOR_BLACK);
print("\n\nFile not found!");
sleep(1000);
}
break;
case SELECTION_FILES_FORMAT:
color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK);
print("\n\nFormatting drive to FAT32 . . .");
format(
ATA_PRIMARY_PORT,
ATA_MASTER_DRIVE,
FAT32_START,
1,
FAT32_SIZE,
true
);
color(VGA_COLOR_GREEN, VGA_COLOR_BLACK);
print(" Formatting successful!\n\n");
color(VGA_COLOR_YELLOW, VGA_COLOR_BLACK);
print("Rebooting . . .");
reboot(1000);
break;
case SELECTION_TEXTEDIT:
strcopy(text, textedit(text));
break;
case SELECTION_SHUTDOWN:
color(VGA_COLOR_GREEN, VGA_COLOR_BLACK);
print("\n\nGoodbye!");
shutdown(1000);
break;
}
}
}
/** Opening sequence. */
void opening() {
clear();
beep();
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print("\n\t\t");
string_t welcome = "Welcome to the\n\n";
sleep(1000);
if (keyboard.index != 0) return;
while (*welcome != '\0') {
printchar(*welcome++);
sleep(75);
if (keyboard.index != 0) return;
}
color(VGA_COLOR_BROWN, VGA_COLOR_BLACK);
sleep(300);
if (keyboard.index != 0) return;
print(" High");
sleep(300);
if (keyboard.index != 0) return;
print("-Level ");
sleep(300);
if (keyboard.index != 0) return;
print("Operating ");
sleep(300);
if (keyboard.index != 0) return;
print("System!");
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
welcome = " High-Level Operating System!";
uint_t length = strlen(welcome);
VGA_color_t rainbow[] = {
VGA_COLOR_LIGHT_RED,
VGA_COLOR_LIGHT_BLUE,
VGA_COLOR_LIGHT_CYAN,
VGA_COLOR_LIGHT_GREEN,
VGA_COLOR_LIGHT_MAGENTA,
VGA_COLOR_YELLOW,
VGA_COLOR_BROWN,
VGA_COLOR_RED,
};
uint_t size = ARRAY_SIZE(rainbow);
uint_t index = (uint_t) -1;
sleep(750);
if (keyboard.index != 0) return;
print("\n\n\t\t\tYaay!");
VGA.row -= 2;
uint_t t = time() + 2000;
while (time() < t) {
for (uint_t i = 0; i < length; ++i) {
char_t c = pos(i, VGA.row);
color(rainbow[(index + i) % size], VGA_COLOR_BLACK);
printchar(c);
sleep(1);
if (keyboard.index != 0) return;
}
--index;
}
}
/** Selection sequence. */
selection_t selection() {
restart:
while (true) {
clear();
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print(datestr(date(), false));
color(VGA_COLOR_BROWN, VGA_COLOR_BLACK);
print("\n\nHLOS\n\n");
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print("1. FILES\n");
print("2. TEXTEDIT\n");
print("3. SHUTDOWN\n");
print("\n> ");
color(VGA_COLOR_YELLOW, VGA_COLOR_BLACK);
char_t *select = (char_t *) read(9, false, NULL);
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
strlower(select);
if (strcompare(select, "files") == EQUAL_TO) {
files:
while (true) {
clear();
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print(datestr(date(), false));
color(VGA_COLOR_BROWN, VGA_COLOR_BLACK);
print("\n\nHLOS/FILES\n\n");
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print("1. OPEN\n");
print("2. SAVE\n");
print("3. DELETE\n");
print("4. FORMAT\n");
print("5. QUIT\n");
print("\n> ");
color(VGA_COLOR_YELLOW, VGA_COLOR_BLACK);
select = (char_t *) read(7, false, NULL);
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
strlower(select);
if (strcompare(select, "open") == EQUAL_TO) {
return SELECTION_FILES_OPEN;
} else if (strcompare(select, "save") == EQUAL_TO) {
return SELECTION_FILES_SAVE;
} else if (strcompare(select, "delete") == EQUAL_TO) {
return SELECTION_FILES_DELETE;
} else if (strcompare(select, "format") == EQUAL_TO) {
return SELECTION_FILES_FORMAT;
} else if (strcompare(select, "quit") == EQUAL_TO) {
goto restart;
} else {
int_t index;
if (strint(select, &index)) {
switch (index + SELECTION_FILES_OPEN - 1) {
default:
break;
case SELECTION_FILES_OPEN:
return SELECTION_FILES_OPEN;
case SELECTION_FILES_SAVE:
return SELECTION_FILES_SAVE;
case SELECTION_FILES_DELETE:
return SELECTION_FILES_DELETE;
case SELECTION_FILES_FORMAT:
return SELECTION_FILES_FORMAT;
case SELECTION_FILES_QUIT:
goto restart;
}
}
}
}
} else if (strcompare(select, "textedit") == EQUAL_TO) {
return SELECTION_TEXTEDIT;
} else if (strcompare(select, "shutdown") == EQUAL_TO) {
return SELECTION_SHUTDOWN;
} else {
int_t index;
if (strint(select, &index)) {
switch (index) {
default:
break;
case SELECTION_FILES:
goto files;
case SELECTION_TEXTEDIT:
return SELECTION_TEXTEDIT;
case SELECTION_SHUTDOWN:
return SELECTION_SHUTDOWN;
}
}
}
}
}
/** Coroutine for the clock. */
void clock(void *args) {
coro(1000, clock, NULL);
byte_t col = VGA.column;
byte_t row = VGA.row;
byte_t color = VGA.color;
VGA.column = 0;
VGA.row = 0;
VGA.color = VGA_COLOR(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print(datestr(date(), false));
VGA.column = col;
VGA.row = row;
VGA.color = color;
}
/** Text-editing sequence. */
string_t textedit(string_t text) {
clear();
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print(datestr(date(), false));
color(VGA_COLOR_BROWN, VGA_COLOR_BLACK);
print("\n\nHLOS/TEXTEDIT\n");
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print("Ctrl+Enter to exit\n\n");
uint_t row = VGA.row;
color(VGA_COLOR_GREEN, VGA_COLOR_BLACK); // Matrix style
while (true) {
VGA.column = 0;
VGA.row = row;
text = (char_t *) read(VGA_SIZE, true, text);
if ((keyboard.queue[keyboard.index].flags & KEY_FLAGS_CTRL) != 0) {
return text;
}
}
}
/** Requests a directory from the user. */
string_t directory(string_t prompt, string_t operation) {
clear();
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print(datestr(date(), false));
color(VGA_COLOR_BROWN, VGA_COLOR_BLACK);
char_t list[FILE_NAME_LEN * 5] = {0};
uint_t size = filelist("", 5, list);
print("\n\nHLOS/FILES/");
print(operation);
print("\n\n");
color(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_BLACK);
for (uint_t i = 0; i < 5; ++i) {
print("- ");
if (i < size) {
print(list + (FILE_NAME_LEN * i));
}
printchar('\n');
}
color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
print("\nPlease enter a ");
print(prompt);
print(".\n\n> ");
color(VGA_COLOR_YELLOW, VGA_COLOR_BLACK);
return read(VGA_WIDTH - 2, false, "/");
}
/** Tests pixel rendering with noise. */
void noisetest() {
#if PIXEL_RENDERING
uint_t size = (PBA_HEIGHT / 3);
for (uint_t i = 0, j = 4; rng() != 0; ++i) {
i %= size;
if (i == 0) {
++j;
j %= 5;
}
for (uint_t i = 0; i < PBA_SIZE; ++i) {
PBA.next[i] = (j == 4 ? rng() : rngrange(0, ticks)) % 256;
}
if (j != 4) {
byte_t color = i * (255 / size);
square(
POINT(
(PBA_WIDTH / 2) - i,
(PBA_HEIGHT / 2) - i
),
POINT(
(PBA_WIDTH / 2) + i,
(PBA_HEIGHT / 2) + i
),
j != 3 ? COLOR(j == 0 ? color : 0, j == 1 ? color : 0, j == 2 ? color : 0) : COLOR(color, color, color)
);
}
render();
}
#endif
}