2 ** Program-chaining function for Commodore platforms.
4 ** 2013-08-24, Greg King
6 ** This function exploits the program-chaining feature in CBM BASIC's ROM.
7 ** It puts the desired program's name and unit number into a LOAD statement.
8 ** Then, it points BASIC to that statement, so that the ROM will run that
9 ** statement after this program quits. The ROM will load the next program,
10 ** and will execute it (because the LOAD will be seen in a running program).
19 #if defined(__CBM610__)
21 #elif defined(__CBM510__)
26 #pragma data-name(push, "LOWCODE")
28 const char end_of_line;
29 const struct line *const next;
30 const unsigned line_num;
31 const char load_token, quotes[2], add_token, quote;
36 '\0', &basic + 1, /* high byte must be non-zero */
39 /* This string operation copies the name to high BASIC RAM.
40 ** So, it won't be overwritten when the next program is loaded.
43 "\" ", /* format: "123:1234567890123456\"" */
46 #pragma data-name(pop)
48 /* These values are platform-specific. */
49 extern const struct line *txtptr;
50 #pragma zpsym("txtptr")
51 extern char basbuf[]; /* BASIC's input buffer */
52 extern void basbuf_len[];
53 #pragma zpsym("basbuf_len")
56 int __fastcall__ exec (const char* progname, const char* cmdline)
59 static unsigned char dv, n = 0;
61 /* Exclude devices that can't load files. */
62 dv = getcurrentdevice ();
63 if (dv < 8 && dv != 1 || dv > 30) {
64 return _mappederrno (9); /* illegal device number */
66 utoa (dv, basic.unit, 10);
68 /* Don't try to run a program that can't be found. */
69 fd = open (progname, O_RDONLY);
76 if ((basic.name[n] = progname[n]) == '\0') {
79 } while (++n < 20); /* truncate long names */
82 /* Build the next program's argument list. */
83 basbuf[0] = 0x8f; /* REM token */
85 if (cmdline != NULL) {
86 strncat (basbuf, cmdline, (size_t)basbuf_len - 2);
89 #if defined(__CBM510__) || defined(__CBM610__)
90 pokewsys ((unsigned)&txtptr, (unsigned)&basic);
95 /* (The return code, in ST, will be destroyed by LOAD.
96 ** So, don't bother to set it here.)