-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkernel.c
executable file
·820 lines (672 loc) · 21.8 KB
/
kernel.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
// This file implements read(2) and write(2) along with a minimal console
// driver for reads from stdin and writes to stdout -- enough to enable
// cc65's stdio functions. It really should be written in assembler for
// speed (mostly for scrolling), but this will at least give folks a start.
#include <stddef.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include "api.h"
#include "app.h" // need for FILE_MAX_PATHNAME_SIZE
#include "dirent.h" // Users are expected to "-I ." to get the local copy.
#include "general.h" // need for strnlen
#include "f256.h"
#include "keyboard.h" // need for F1 key values
#define VECTOR(member) (size_t) (&((struct call*) 0xff00)->member)
#define EVENT(member) (size_t) (&((struct events*) 0)->member)
#define CALL(fn) (unsigned char) ( \
asm("jsr %w", VECTOR(fn)), \
asm("stz %v", error), \
asm("ror %v", error), \
__A__)
#pragma bss-name (push, "KERNEL_ARGS")
struct call_args args; // in gadget's version of f256 lib, this is allocated and initialized with &args in crt0.
#pragma bss-name (pop)
#pragma bss-name (push, "ZEROPAGE")
struct event_t event; // in gadget's version of f256 lib, this is allocated and initialized with &event in crt0.
char error;
#pragma bss-name (pop)
#define MAX_DRIVES 8
// Just hard-coded for now.
#define MAX_ROW 60
#define MAX_COL 80
// MB definitions
#define MAX_PEXEC_APP_PATH_LEN 62 // for pexec loading only: based on 128 total chars, lose 3 for terminators, and 1 for '-' pexec
#define MAX_PEXEC_FILE_PATH_LEN 62 // for pexec loading only: based on 128 total chars, lose 3 for terminators, and 1 for '-' pexec
static char row = 0;
static char col = 0;
static char *line = (char*) 0xc000;
void
kernel_init(void)
{
args.events.event = &event;
}
static void
cls()
{
int i;
char *vram = (char*)0xc000;
asm("lda #$02");
asm("sta $01");
for (i = 0; i < 80*60; i++) {
*vram++ = 32;
}
row = col = 0;
line = (char*)0xc000;
asm("stz $1"); asm("lda #9"); asm("sta $d010");
(__A__ = row, asm("sta $d016"), asm("stz $d017"));
(__A__ = col, asm("sta $d014"), asm("stz $d015"));
asm("lda #'_'"); asm("sta $d012");
asm("stz $d011");
}
void
scroll()
{
int i;
char *vram = (char*)0xc000;
asm("lda #$02");
asm("sta $01");
for (i = 0; i < 80*59; i++) {
vram[i] = vram[i+80];
}
vram += i;
for (i = 0; i < 80; i++) {
*vram++ = 32;
}
}
static void
out(char c)
{
switch (c) {
case 12:
cls();
break;
default:
asm("lda #2");
asm("sta $01");
line[col] = c;
col++;
if (col != MAX_COL) {
break;
}
case 10:
case 13:
col = 0;
row++;
if (row == MAX_ROW) {
scroll();
row--;
break;
}
line += 80;
break;
}
asm("stz $01");
(__A__ = row, asm("sta $d016"));
(__A__ = col, asm("sta $d014"));
}
char
GETIN()
{
while (1) {
CALL(NextEvent);
if (error) {
asm("jsr %w", VECTOR(Yield));
continue;
}
if (event.type != EVENT(key.PRESSED)) {
continue;
}
if (event.key.flags) {
// if a function key, return raw code.
if (event.key.raw >= CH_F1 && event.key.raw <= CH_F8)
{
return event.key.raw;
}
continue; // Meta key.
}
return event.key.ascii;
}
}
// check for any kernel key press. return true if any key was pressed, otherwise false
// NOTE: the key press in question will be lost! only use when you want to check, but not wait for, a user key press
bool Kernal_AnyKeyEvent()
{
while (1) {
CALL(NextEvent);
if (error) {
asm("jsr %w", VECTOR(Yield));
return false;
}
if (event.type == EVENT(key.PRESSED)) {
return true;
}
return false;
}
}
static const char *
path_without_drive(const char *path, char *drive)
{
*drive = 0;
if (strlen(path) < 2) {
return path;
}
if (path[1] != ':') {
return path;
}
if ((*path >= '0') && (*path <= '7')) {
*drive = *path - '0';
}
return (path + 2);
}
int
open(const char *fname, int mode, ...)
{
int ret = 0;
char drive;
fname = path_without_drive(fname, &drive);
args.common.buf = (uint8_t*) fname;
args.common.buflen = strlen(fname);
args.file.open.drive = drive;
if (mode == 1) {
mode = 0;
} else {
mode = 1;
}
args.file.open.mode = mode;
ret = CALL(File.Open);
if (error) {
return -1;
}
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
switch (event.type) {
case EVENT(file.OPENED):
return ret;
case EVENT(file.NOT_FOUND):
case EVENT(file.ERROR):
return -1;
default:
continue;
}
}
}
static int
Kernel_Read(int fd, void *buf, uint16_t nbytes)
{
if (fd == 0) {
// stdin
*(char*)buf = GETIN();
return 1;
}
if (nbytes > 255) {
nbytes = 255;
}
args.file.read.stream = fd;
args.file.read.buflen = nbytes;
CALL(File.Read);
if (error) {
return -1;
}
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
switch (event.type) {
case EVENT(file.DATA):
args.common.buf = buf;
args.common.buflen = event.file.data.delivered;
asm("jsr %w", VECTOR(ReadData));
if (!event.file.data.delivered) {
return 256;
}
return event.file.data.delivered;
case EVENT(file.EOFx):
return 0;
case EVENT(file.ERROR):
return -1;
default:
continue;
}
}
}
int
read(int fd, void *buf, uint16_t nbytes)
{
char *data = buf;
int gathered = 0;
// fread should be doing this, but it isn't, so we're doing it.
while (gathered < nbytes) {
int returned = Kernel_Read(fd, data + gathered, nbytes - gathered);
if (returned <= 0) {
break;
}
gathered += returned;
}
return gathered;
}
static int
kernel_write(uint8_t fd, void *buf, uint8_t nbytes)
{
args.file.read.stream = fd;
args.common.buf = buf;
args.common.buflen = nbytes;
CALL(File.Write);
if (error) {
return -1;
}
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
if (event.type == EVENT(file.WROTE)) {
return event.file.data.delivered;
}
if (event.type == EVENT(file.ERROR)) {
return -1;
}
}
}
int
write(int fd, const void *buf, uint16_t nbytes)
{
uint8_t *data = buf;
int total = 0;
uint8_t writing;
int written;
if (fd == 1) {
int i;
char *text = (char*) buf;
for (i = 0; i < nbytes; i++) {
out(text[i]);
}
return i;
}
while (nbytes) {
if (nbytes > 254) {
writing = 254;
} else {
writing = nbytes;
}
written = kernel_write(fd, data+total, writing);
if (written <= 0) {
return -1;
}
total += written;
nbytes -= written;
}
return total;
}
int
close(int fd)
{
args.file.close.stream = fd;
asm("jsr %w", VECTOR(File.Close));
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
switch (event.type) {
case EVENT(file.CLOSED):
return 0;
case EVENT(file.ERROR):
return -1;
default: continue;
}
}
return 0;
}
////////////////////////////////////////
// dirent
static char dir_stream[MAX_DRIVES];
DIR* __fastcall__
Kernel_OpenDir(const char* name)
{
char drive, stream;
// out(name[0]);
// out(name[1]);
// out(name[2]);
name = path_without_drive(name, &drive);
//out(48+drive);
// out(48+(uint8_t)strlen(name));
if (dir_stream[drive]) {
//out(64);
return NULL; // Only one at a time.
}
args.directory.open.drive = drive;
args.common.buf = name;
args.common.buflen = strlen(name);
//out(48+(uint8_t)args.common.buflen);
stream = CALL(Directory.Open);
if (error) {
//out(66); // B
return NULL;
}
//out(67); // C
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
if (event.type == EVENT(directory.OPENED)) {
//out(68); // D
break;
}
if (event.type == EVENT(directory.ERROR)) {
//out(69); // E
return NULL;
}
}
dir_stream[drive] = stream;
//out(70); // F
return (DIR*) &dir_stream[drive];
}
struct dirent* __fastcall__
Kernel_ReadDir(DIR* dir)
{
static struct dirent dirent;
if (!dir) {
return NULL;
}
args.directory.read.stream = *(char*)dir;
CALL(Directory.Read);
if (error) {
return NULL;
}
for(;;) {
unsigned len;
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
switch (event.type) {
case EVENT(directory.VOLUME):
dirent.d_blocks = 0;
dirent.d_type = 2;
break;
case EVENT(directory.FILE):
// common.ext isn't returning expected values. i think it's not meant to be used for reading like this.
//args.common.ext = &dirent.d_blocks;
// args.common.extlen = sizeof(dirent.d_blocks) + 6; // 6 to pick up the 6 bytes of date info
// common.buf returns blocks, 2 bytes of 0s, then a filename, looks like maybe the last-read file's filename. probably just junk from previous event.
args.common.buf = &dirent.d_blocks;
args.common.buflen = sizeof(dirent.d_blocks) + 6; // 6 to pick up the 6 bytes of date info
CALL(ReadExt);
dirent.d_type = (dirent.d_blocks == 0);
break;
case EVENT(directory.FREE):
// dirent doesn't care about these types of records.
args.directory.read.stream = *(char*)dir;
CALL(Directory.Read);
if (!error) {
continue;
}
// Fall through.
case EVENT(directory.EOFx):
case EVENT(directory.ERROR):
return NULL;
default: continue;
}
// Copy the name.
len = event.directory.file.len;
if (len >= sizeof(dirent.d_name)) {
len = sizeof(dirent.d_name) - 1;
}
if (len > 0) {
args.common.buf = &dirent.d_name;
args.common.buflen = len;
CALL(ReadData);
}
dirent.d_name[len] = '\0';
return &dirent;
}
}
int __fastcall__
Kernel_CloseDir (DIR* dir)
{
if (!dir) {
return -1;
}
for(;;) {
if (*(char*)dir) {
args.directory.close.stream = *(char*)dir;
CALL(Directory.Close);
if (!error) {
*(char*)dir = 0;
}
}
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
if (event.type == EVENT(directory.CLOSED)) {
*(char*)dir = 0;
return 0;
}
}
}
// deletes the file at the specified path
// returns false in all error conditions
bool __fastcall__ Kernel_DeleteFile(const char* name)
{
char drive, stream;
name = path_without_drive(name, &drive);
args.file.delete.drive = drive;
args.common.buf = name;
args.common.buflen = strlen(name);
stream = CALL(File.Delete);
if (error) {
return false;
}
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
if (event.type == EVENT(file.DELETED)) {
break;
}
if (event.type == EVENT(file.ERROR)) {
return false;
}
}
return true;
}
// deletes the folder at the specified path
// returns false in all error conditions
bool __fastcall__ Kernel_DeleteFolder(const char* name)
{
char drive, stream;
name = path_without_drive(name, &drive);
args.file.delete.drive = drive;
args.common.buf = name;
args.common.buflen = strlen(name);
stream = CALL(Directory.RmDir);
if (error) {
return false;
}
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
if (event.type == EVENT(directory.DELETED)) {
break;
}
if (event.type == EVENT(directory.ERROR)) {
return false;
}
}
return true;
}
int __fastcall__
rename(const char* name, const char *to)
{
char drive, stream, dest;
name = path_without_drive(name, &drive);
to = path_without_drive(to, &dest);
if (dest != drive) {
// rename across drives is not supported.
return -1;
}
args.file.delete.drive = drive;
args.common.buf = name;
args.common.buflen = strlen(name);
args.common.ext = to;
args.common.extlen = strlen(to);
stream = CALL(File.Rename);
if (error) {
return -1;
}
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
if (event.type == EVENT(file.RENAMED)) {
break;
}
if (event.type == EVENT(file.ERROR)) {
return -1;
}
}
return 0;
}
// wrapper to mkfs
// pass the name you want for the formatted disk/SD card in name, and the drive number (0-2) in the drive param.
// do NOT prepend the path onto name.
// return negative number on any error
int __fastcall__
mkfs(const char* name, const char drive)
{
char stream;
args.file.delete.drive = drive;
args.common.buf = name;
args.common.buflen = strlen(name);
stream = CALL(FileSystem.MkFS);
if (error) {
return -2;
}
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
if (event.type == EVENT(fs.CREATED)) {
break;
}
if (event.type == EVENT(fs.ERROR)) {
return -3;
}
}
return 0;
}
// perform a MkDir on the specified device, with the specified path
// returns false on any error
bool Kernal_MkDir(char* the_path, uint8_t drive_num)
{
char stream;
the_path += 2; // get past 0:, 1:, 2:, etc.
args.directory.mkdir.drive = drive_num;
//args.directory.mkdir.path = the_path;
//args.directory.mkdir.path_len = General_Strnlen(the_path, FILE_MAX_PATHNAME_SIZE) + 1;
args.common.buf = the_path;
args.common.buflen = General_Strnlen(the_path, FILE_MAX_PATHNAME_SIZE) + 1;
//args.directory.mkdir.cookie = 126; // NOT HANDLING THIS CURRENTLY. FUTURE: PROVIDE A COOKIE AND USE IT TO TRACK COMPLETION?
stream = CALL(Directory.MkDir);
if (error)
{
return false;
}
for(;;) {
event.type = 0;
asm("jsr %w", VECTOR(NextEvent));
if (event.type == EVENT(directory.CREATED)) {
break;
}
if (event.type == EVENT(file.ERROR)) {
return false;
}
}
return true;
}
// Directory.MkDir
//
// Creates a sub-directory.
//
// Input
//
// kernel.args.directory.mkdir.drive contains the device id (0 = SD, 1 = IEC #8, 2 = IEC #9).
// kernel.args.directory.mkdir.path points to a buffer containing the path.
// kernel.args.directory.mkdir.path_len contains the length of the path above. May be zero for the root directory.
// kernel.args.directory.mkdir.cookie contains a user-supplied cookie for matching the completed event.
// Output
//
// Carry cleared on success.
// Carry set on error (device not found, kernel out of event or stream objects).
// Events
//
// On successful completion, the kernel will queue an event.directory.CREATED event.
// On error, the kernel will queue an event.directory.ERROR event.
// In either case, event.directory.cookie will contain the above cookie.
// runs a named program (a KUP, in other words)
// pass the KUP name and length
// returns error on error, and never returns on success (because SuperBASIC took over)
void Kernal_RunNamed(char* kup_name, uint8_t name_len)
{
char stream;
args.common.buf = kup_name;
args.common.buflen = name_len;
stream = CALL(RunNamed);
return; // just so cc65 is happy; but will not be hit in event of success as SuperBASIC will already be running.
}
// calls pexec, passing the path to an app to load, and optionally, the path to a file for that app to load
// returns error on error, and never returns on success (because pexec took over)
bool Kernal_LoadApp(char* the_app_path, char* the_file_path)
{
char stream;
uint8_t path_len;
// LOGIC:
// kernel.args.buf needs to have name of named app to run, which in this case is '-' (pexec's real name)
// we also need to prep a different buffer with a series of pointers (2), one of which points to a string for '-', one for the app (e.g, 'modojr.pgz', and optionally one for the file the called up app should load (e.g., 'mymodfile.mod')
// We have from $200 to $27f to use for the paths
// Because we only have 128 chars for all 3 paths (126 after pexec), the max len of either path is 62 (NULL terminators eat a space)
// The pointers to the path components start at $280.
// we set arg0 to pexec ('-'), arg1 to the path of the app to load, and arg2, if passed, to the path of the file to load
args.common.buf = (char*)0x0200; // tell Kernel which buffer to work with
args.common.buflen = 2;
args.common.ext = (char*)0x0280; // tell Kernel where the arg pointers start and how many there are
args.common.extlen = 6; // if no file for called app to load. will change if necessary.
// arg0: pexec "-"
*(uint8_t*)0x0200 = '-';
*(uint8_t*)0x0201 = 0;
*(uint8_t*)0x0280 = 0x00; // set pointer to arg0
*(uint8_t*)0x0281 = 0x02; // first arg (pexec '-') is at $0200
// arg1: path to file for pexec to load
path_len = General_Strnlen(the_app_path, MAX_PEXEC_APP_PATH_LEN) + 1;
General_Strlcpy((char*)0x0202, the_app_path, path_len);
*(uint8_t*)0x0282 = 0x02; // set pointer to arg1
*(uint8_t*)0x0283 = 0x02; // 2nd arg (the app path) is at $0202
*(uint8_t*)0x0284 = 0x00; // terminator (will be overwritten if there is a file to load)
// arg2: path to the file you want the called up app to load, if any
if (the_file_path != NULL)
{
path_len = General_Strnlen(the_file_path, MAX_PEXEC_FILE_PATH_LEN) + 1;
General_Strlcpy((char*)0x0242, the_file_path, path_len);
*(uint8_t*)0x0284 = 0x42; // set pointer to arg2
*(uint8_t*)0x0285 = 0x02; // 3rd arg (the file path) is at $0242
*(uint8_t*)0x0286 = 0x00; // terminator
args.common.extlen = 8; // let Kernel know we have 8 bytes / 4 pointers for it to look at.
}
stream = CALL(RunNamed);
if (error)
{
return false;
}
return true; // just so cc65 is happy; but will not be hit in event of success as pexec will already be running.
}
// Input
// • kernel.args.buf points to a buffer containing the name of the program to run.
// • kernel.args.buflen contains the length of the name.
// Output
// • On success, the call doesn’t return.
// • Carry set on error (a program with the provided name was not found).
// Notes
// • The name match is case-insensitive.
//https://github.com/FoenixRetro/Documentation/blob/main/f256/programming-developing.md
// Parameter Passing
//
// Although not part of the kernel specification, a standardized method of passing commandline arguments to programs exists.
//
// Both DOS and SuperBASIC are able to pass arguments to the program to run, and pexec is also able to pass any further arguments after the filename on to the program. As an example, /- program.pgz hello in SuperBASIC would start pexec with the parameters -, program.pgz, and hello. pexec would then load program.pgz, and start it with the parameters program.pgz and hello.
//
// Arguments are passed in the ext and extlen kernel arguments. This approach is suitable for passing arguments through the RunNamed and RunBlock kernel functions, and is also used by pexec when starting a PGX or PGZ program.
//
// ext will contain an array of pointers, one for each argument given on the commandline. The first pointer is the program name itself. The list is terminated with a null pointer. extlen contains the length in bytes of the array, less the null pointer. For instance, if two parameters are passed, extlen will be 4.
//
// pexec reserves $200-$2FF for parameters - programs distributed in the PGX and PGZ formats should therefore load themselves no lower than $0300, if they want to access commandline parameters. If they do not use the commandline parameters, they may load themselves as low as $0200.