2 ** Program-chaining function for Commodore platforms.
4 ** 2013-09-04, Greg King
6 ** This function exploits the program-chaining feature in CBM BASIC's ROM.
8 ** CC65's CBM programs have a BASIC program stub. We start those programs by
9 ** RUNning that stub; it SYSes to the Machine Language code. Normally, after
10 ** the ML code exits, the BASIC ROM continues running the stub. But, it has
11 ** no more statements; so, the program stops.
13 ** This function puts the desired program's name and device number into a LOAD
14 ** statement. Then, it points BASIC to that statement, so that the ROM will run
15 ** that statement after this program quits. The ROM will load the next program,
16 ** and will execute it (because the LOAD will be seen in a running program).
25 #if defined(__CBM610__)
27 #elif defined(__CBM510__)
32 /* The struct below is a line of BASIC code. It sits in the LOWCODE segment
33 ** to make sure that it won't be hidden by a ROM when BASIC is re-enabled.
36 ** After this function has written into the line, it might look like this:
37 ** 0 LOAD""+"program name" ,08
39 ** When BASIC's LOAD command asks the Kernal to load a file, it gives the
40 ** Kernal a pointer to a file-name string. CC65's CBM programs use that
41 ** pointer to give a copy of the program's name to main()'s argv[0] parameter.
42 ** But, when BASIC uses a string literal that's in a program, it points
43 ** directly to that literal -- in the models that don't use banked RAM
44 ** (Pet/CBM, VIC-20, and 64). The literal is overwritten by the next program
45 ** that's loaded. So, argv[0] would point to machine code. String operations
46 ** create a new result string -- even when that operation changes nothing. The
47 ** result is put in the string space at the top of BASIC's memory. So, the ""+
48 ** in this BASIC line guarantees that argv[0] will get a name from a safe place.
50 #pragma data-name(push, "LOWCODE")
52 const char end_of_line;
53 const struct line *const next;
54 const unsigned line_num;
55 const char load_token, quotes[2], add_token, quote;
60 '\0', &basic + 1, /* high byte of link must be non-zero */
61 0, 0x93, "\"\"", 0xaa, '\"',
62 "\" ", /* format: "123:1234567890123456\"" */
65 #pragma data-name(pop)
67 /* These values are platform-specific. */
68 extern const struct line *txtptr;
69 #pragma zpsym("txtptr")
70 extern char basbuf[]; /* BASIC's input buffer */
71 extern void basbuf_len[];
72 #pragma zpsym("basbuf_len")
75 int __fastcall__ exec (const char* progname, const char* cmdline)
78 static unsigned char dv, n = 0;
80 /* Exclude devices that can't load files. */
81 dv = getcurrentdevice ();
82 if (dv < 8 && dv != 1 || dv > 30) {
83 return _mappederrno (9); /* illegal device number */
85 utoa (dv, basic.unit, 10);
87 /* Don't try to run a program that can't be found. */
88 fd = open (progname, O_RDONLY);
95 if ((basic.name[n] = progname[n]) == '\0') {
98 } while (++n < 20); /* truncate long names */
101 /* Build the next program's argument list. */
102 basbuf[0] = 0x8f; /* REM token */
104 if (cmdline != NULL) {
105 strncat (basbuf, cmdline, (size_t)basbuf_len - 2);
108 #if defined(__CBM510__) || defined(__CBM610__)
109 pokewsys ((unsigned)&txtptr, (unsigned)&basic);
114 /* (The return code, in ST, will be destroyed by LOAD.
115 ** So, don't bother to set it here.)