-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.c
522 lines (498 loc) · 14.5 KB
/
console.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <stdio.h>
#include <string.h>
#include "bootpack.h"
void cons_newline(struct CONSOLE *cons)
{
int x, y;
struct SHEET *sheet = cons->sht;
if (cons->cur_y < 28 + 112)
{
cons->cur_y += 16;
}
else
{
for (y = 28; y < 28 + 112; y++)
for (x = 8; x < 8 + 240; x++)
sheet->buf[x + y * sheet->bxsize] = sheet->buf[x + (y + 16) * sheet->bxsize];
for (y = 28 + 112; y < 28 + 128; y++)
for (x = 8; x < 8 + 240; x++)
sheet->buf[x + y * sheet->bxsize] = COL8_000000;
sheet_refresh(sheet, 8, 28, 8 + 240, 28 + 128);
}
cons->cur_x = 8;
return;
}
void console_task(struct SHEET *sheet, unsigned int memtotal)
{
struct TASK *task = task_now();
struct MEMMAN *memman = (struct MEMMAN *)MEMMAN_ADDR;
struct CONSOLE cons;
int i, fifobuf[128], *fat = (int *)memman_alloc_4k(memman, 4 * 2880);
char cmdline[30];
cons.sht = sheet;
cons.cur_x = 8;
cons.cur_y = 28;
cons.cur_c = -1;
*((int *)0x0fec) = (int)&cons;
fifo32_init(&task->fifo, 128, fifobuf, task);
cons.timer = timer_alloc();
timer_init(cons.timer, &task->fifo, 1);
timer_settime(cons.timer, 50);
file_readfat(fat, (unsigned char *)(ADR_DISKING + 0x000200));
cons_putchar(&cons, '>', 1);
for (;;)
{
io_cli();
if (fifo32_status(&task->fifo) == 0)
{
task_sleep(task);
io_sti();
}
else
{
i = fifo32_get(&task->fifo);
io_sti();
if (i <= 1)
{
if (i != 0)
{
timer_init(cons.timer, &task->fifo, 0);
if (cons.cur_c >= 0)
cons.cur_c = COL8_FFFFFF;
}
else
{
timer_init(cons.timer, &task->fifo, 1);
if (cons.cur_c >= 0)
cons.cur_c = COL8_000000;
}
timer_settime(cons.timer, 50);
}
if (i == 2)
cons.cur_c = COL8_FFFFFF;
if (i == 3)
{
boxfill8(sheet->buf, sheet->bxsize, COL8_000000, cons.cur_x, cons.cur_y, cons.cur_x + 7, 43);
cons.cur_c = -1;
}
if (256 <= i && i <= 511)
{
if (i == 8 + 256)
{
if (cons.cur_x > 16)
{
cons_putchar(&cons, ' ', 0);
cons.cur_x -= 8;
}
}
// enter
else if (i == 10 + 256)
{
cons_putchar(&cons, ' ', 0);
cmdline[cons.cur_x / 8 - 2] = 0;
cons_newline(&cons);
cons_runcmd(cmdline, &cons, fat, memtotal);
cons_putchar(&cons, '>', 1);
}
else
{
if (cons.cur_x < 240)
{
cmdline[cons.cur_x / 8 - 2] = i - 256;
cons_putchar(&cons, i - 256, 1);
}
}
}
if (cons.cur_c >= 0)
boxfill8(sheet->buf, sheet->bxsize, cons.cur_c, cons.cur_x, cons.cur_y, cons.cur_x + 7, cons.cur_y + 15);
sheet_refresh(sheet, cons.cur_x, cons.cur_y, cons.cur_x + 8, cons.cur_y + 16);
}
}
}
void cons_putchar(struct CONSOLE *cons, int chr, char move)
{
char s[2];
s[0] = chr;
s[1] = 0;
if (s[0] == 0x09) // tab
{
for (;;)
{
putfonts8_asc_sht(cons->sht, cons->cur_x, cons->cur_y, COL8_FFFFFF, COL8_000000, " ", 1);
cons->cur_x += 8;
if (cons->cur_x == 8 + 240)
{
cons_newline(cons);
}
if (((cons->cur_x - 8) & 0x1f) == 0) // mod 32 = 0
break;
}
}
else if (s[0] == 0x0a) // cr
{
cons_newline(cons);
}
else if (s[0] == 0x0d) // lf
{
}
else
{
putfonts8_asc_sht(cons->sht, cons->cur_x, cons->cur_y, COL8_FFFFFF, COL8_000000, s, 1);
if (move != 0)
{
cons->cur_x += 8;
if (cons->cur_x == 8 + 240)
{
cons_newline(cons);
}
}
}
return;
}
void cons_runcmd(char *cmdline, struct CONSOLE *cons, int *fat, unsigned int memtotal)
{
if (strcmp(cmdline, "mem") == 0)
cmd_mem(cons, memtotal);
else if (strcmp(cmdline, "cls") == 0)
cmd_cls(cons);
else if (strcmp(cmdline, "dir") == 0)
cmd_dir(cons);
else if (strncmp(cmdline, "type ", 5) == 0)
cmd_type(cons, fat, cmdline);
else if (cmdline[0] != 0)
{
if (cmd_app(cons, fat, cmdline) == 0)
{
cons_putstr0(cons, "bad command.\n\n");
}
}
return;
}
void cmd_mem(struct CONSOLE *cons, unsigned int memtotal)
{
struct MEMMAN *memman = (struct MEMMAN *)MEMMAN_ADDR;
char s[60];
sprintf(s, "total %dMB\nfree %dKB\n\n", memtotal / (1024 * 1024), memman_total(memman) / 1024);
cons_putstr0(cons, s);
return;
}
void cmd_cls(struct CONSOLE *cons)
{
int x, y;
for (y = 28; y < 28 + 128; y++)
for (x = 8; x < 8 + 240; x++)
cons->sht->buf[x + y * cons->sht->bxsize] = COL8_000000;
sheet_refresh(cons->sht, 8, 28, 8 + 240, 29 + 128);
cons->cur_y = 28;
return;
}
void cmd_dir(struct CONSOLE *cons)
{
struct FILEINFO *finfo = (struct FILEINFO *)(ADR_DISKING + 0x002600);
int x, y;
char s[30];
for (x = 0; x < 224; x++)
{
if (finfo[x].name[0] == 0x00)
break;
if (finfo[x].name[0] != 0xe5)
{
if ((finfo[x].type & 0x18) == 0)
{
sprintf(s, "filename.ext %7d\n", finfo[x].size);
for (y = 0; y < 8; y++)
s[y] = finfo[x].name[y];
s[9] = finfo[x].ext[0];
s[10] = finfo[x].ext[1];
s[11] = finfo[x].ext[2];
cons_putstr0(cons, s);
}
}
}
cons_newline(cons);
return;
}
void cmd_type(struct CONSOLE *cons, int *fat, char *cmdline)
{
struct MEMMAN *memman = (struct MEMMAN *)MEMMAN_ADDR;
struct FILEINFO *finfo = file_search(cmdline + 5, (struct FILEINFO *)(ADR_DISKING + 0x002600), 224);
char *p;
if (finfo != 0)
{
p = (char *)memman_alloc_4k(memman, finfo->size);
file_loadfile(finfo->clustno, finfo->size, p, fat, (char *)(ADR_DISKING + 0x003e00));
cons_putstr1(cons, p, finfo->size);
memman_free_4k(memman, (int)p, finfo->size);
}
else
{
cons_putstr0(cons, "file not found.\n");
}
cons_newline(cons);
return;
}
int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
{
struct MEMMAN *memman = (struct MEMMAN *)MEMMAN_ADDR;
struct FILEINFO *finfo;
struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *)ADR_GDT;
char name[13], *p, *q;
struct TASK *task = task_now();
int i, segsiz, datsiz, esp, dathrb;
struct SHTCTL *shtctl;
struct SHEET *sht;
for (i = 0; i < 13; i++)
{
if (cmdline[i] <= ' ')
break;
name[i] = cmdline[i];
}
name[i] = 0;
finfo = file_search(name, (struct FILEINFO *)(ADR_DISKING + 0x002600), 224);
if (finfo == 0 && name[i - 1] != '.')
{
name[i] = '.';
name[i + 1] = 'H';
name[i + 2] = 'R';
name[i + 3] = 'B';
name[i + 4] = 0;
finfo = file_search(name, (struct FILEINFO *)(ADR_DISKING + 0x002600), 224);
}
if (finfo != 0)
{
p = (char *)memman_alloc_4k(memman, finfo->size);
file_loadfile(finfo->clustno, finfo->size, p, fat, (char *)(ADR_DISKING + 0x003e00));
if (finfo->size >= 36 && strncmp(p + 4, "Hari", 4) == 0 && *p == 0x00)
{
segsiz = *((int *)(p + 0x0000));
esp = *((int *)(p + 0x000c));
datsiz = *((int *)(p + 0x0010));
dathrb = *((int *)(p + 0x0014));
q = (char *)memman_alloc_4k(memman, segsiz);
*((int *)0xfe8) = (int)q;
set_segmdesc(gdt + 1003, finfo->size - 1, (int)p, AR_CODE32_ER + 0x60);
set_segmdesc(gdt + 1004, segsiz - 1, (int)q, AR_DATA32_RW + 0x60);
for (i = 0; i < datsiz; i++)
q[esp + i] = p[dathrb + i];
start_app(0x1b, 1003 * 8, esp, 1004 * 8, &(task->tss.esp0));
shtctl = (struct SHTCTL *)*((int *)0x0fe4);
for (i = 0; i < MAX_SHEETS; i++)
{
sht = &(shtctl->sheetsO[i]);
if ((sht->flags & 0x11) == 0x11 && sht->task == task)
{
sheet_free(sht);
}
}
timer_cancelall(&task->fifo);
memman_free_4k(memman, (int)q, segsiz);
}
else
cons_putstr0(cons, ".hrb file format eroor\n");
memman_free_4k(memman, (int)p, finfo->size);
cons_newline(cons);
return 1;
}
return 0;
}
void cons_putstr0(struct CONSOLE *cons, char *s)
{
for (; *s != 0; s++)
cons_putchar(cons, *s, 1);
return;
}
void cons_putstr1(struct CONSOLE *cons, char *s, int l)
{
int i;
for (i = 0; i < l; i++)
cons_putchar(cons, s[i], 1);
return;
}
int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax)
{
int ds_base = *((int *)0xfe8), i;
struct TASK *task = task_now();
struct CONSOLE *cons = (struct CONSOLE *)*((int *)0x0fec);
struct SHTCTL *shtctl = (struct SHTCTL *)*((int *)0x0fe4);
struct SHEET *sht;
int *reg = &eax + 1;
if (edx == 1)
cons_putchar(cons, eax & 0xff, 1);
else if (edx == 2)
cons_putstr0(cons, (char *)ebx + ds_base);
else if (edx == 3)
cons_putstr1(cons, (char *)ebx + ds_base, ecx);
else if (edx == 4)
return &(task->tss.esp0);
else if (edx == 5)
{
sht = sheet_alloc(shtctl);
sht->task = task;
sht->flags |= 0x10;
sheet_setbuf(sht, (char *)ebx + ds_base, esi, edi, eax);
make_window8((char *)ebx + ds_base, esi, edi, (char *)ecx + ds_base, 0);
sheet_slide(sht, 100, 50);
sheet_updown(sht, 3);
reg[7] = (int)sht;
}
else if (edx == 6)
{
sht = (struct SHEET *)(ebx & 0xfffffffe);
putfonts8_asc(sht->buf, sht->bxsize, esi, edi, eax, (char *)ebp + ds_base);
if ((ebx & 1) == 0)
sheet_refresh(sht, esi, edi, esi + ecx * 8, edi + 16);
}
else if (edx == 7)
{
sht = (struct SHEET *)(ebx & 0xfffffffe);
boxfill8(sht->buf, sht->bxsize, ebp, eax, ecx, esi, edi);
if ((ebx & 1) == 0)
sheet_refresh(sht, eax, ecx, esi + 1, edi + 1);
}
else if (edx == 8)
{
memman_init((struct MEMMAN *)(ebx + ds_base));
ecx &= 0xfffffff0;
memman_free((struct MEMMAN *)(ebx + ds_base), eax, ecx);
}
else if (edx == 9)
{
ecx = (ecx + 0x0f) & 0xfffffff0;
reg[7] = memman_alloc((struct MEMMAN *)(ebx + ds_base), ecx);
}
else if (edx == 10)
{
ecx = (ecx + 0x0f) & 0xfffffff0;
memman_free((struct MEMMAN *)(ebx + ds_base), eax, ecx);
}
else if (edx == 11)
{
sht = (struct SHEET *)(ebx & 0xfffffffe);
sht->buf[sht->bxsize * edi + esi] = eax;
if ((ebx & 1) == 0)
sheet_refresh(sht, esi, edi, esi + 1, edi + 1);
}
else if (edx == 12)
{
sht = (struct SHEET *)ebx;
sheet_refresh(sht, eax, ecx, esi, edi);
}
else if (edx == 13)
{
sht = (struct SHEET *)(ebx & 0xfffffffe);
hrb_api_linewin(sht, eax, ecx, esi, edi, ebp);
if ((ebx & 1) == 0)
sheet_refresh(sht, eax, ecx, esi + 1, edi + 1);
}
else if (edx == 14)
{
sheet_free((struct SHEET *)ebx);
}
else if (edx == 15)
{
for (;;)
{
io_cli();
if (fifo32_status(&task->fifo) == 0)
{
if (eax != 0)
task_sleep(task);
else
{
io_sti();
reg[7] = -1;
return 0;
}
}
i = fifo32_get(&task->fifo);
io_sti();
if (i <= 1)
{
timer_init(cons->timer, &task->fifo, 1);
timer_settime(cons->timer, 50);
}
if (i == 2)
cons->cur_c = COL8_FFFFFF;
if (i == 3)
cons->cur_c = -1;
if (256 <= i)
{
reg[7] = i - 256;
return 0;
}
}
}
else if (edx == 16)
{
reg[7] = (int)timer_alloc();
((struct TIMER *)reg[7])->flags2 = 1;
}
else if (edx == 17)
timer_init((struct TIMER *)ebx, &task->fifo, eax + 256);
else if (edx == 18)
timer_settime((struct TIMER *)ebx, eax);
else if (edx == 19)
timer_free((struct TIMER *)ebx);
return 0;
}
int *inthandler0d(int *esp)
{
struct CONSOLE *cons = (struct CONSOLE *)*((int *)0x0fec);
struct TASK *task = task_now();
char s[30];
cons_putstr0(cons, "\nINT 0D :\n General Protected Exception.\n");
sprintf(s, "EIP = %08X\n", esp[11]);
cons_putstr0(cons, s);
return &(task->tss.esp0);
}
int *inthandler0c(int *esp)
{
struct CONSOLE *cons = (struct CONSOLE *)*((int *)0x0fec);
struct TASK *task = task_now();
char s[30];
cons_putstr0(cons, "\nINT 0D :\n Stack Ecxeption.\n");
sprintf(s, "EIP = %08X\n", esp[11]);
cons_putstr0(cons, s);
return &(task->tss.esp0);
}
void hrb_api_linewin(struct SHEET *sht, int x0, int y0, int x1, int y1, int col)
{
int i, x, y, len, dx, dy;
dx = x1 - x0;
dy = y1 - y0;
x = x0 << 10;
y = y0 << 10;
if (dx < 0)
dx = -dx;
if (dx >= dy)
{
len = dx + 1;
if (x1 < x0)
dx = -1025;
else
dx = 1024;
if (y0 <= y1)
dy = ((y1 - y0 + 1) << 10) / len;
else
dy = ((y1 - y0 - 1) << 10) / len;
}
else
{
len = dy + 1;
if (y1 < y0)
dy = -1025;
else
dy = 1024;
if (x0 <= x1)
dx = ((x1 - x0 + 1) << 10) / len;
else
dx = ((x1 - x0 - 1) << 10) / len;
}
for (i = 0; i < len; i++)
{
sht->buf[(y >> 10) * sht->bxsize + (x >> 10)] = col;
x += dx;
y += dy;
}
return;
}