2 ** Program-chaining function for Commodore platforms.
4 ** 2015-09-27, 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.
35 ** 0 CLR:LOAD""+"" ,01
36 ** After this function has written into the line, it might look like this:
37 ** 0 CLR: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 is 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 is 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; /* fake previous line */
53 const struct line* const next;
54 const unsigned line_num;
55 const char CLR_token, colon, LOAD_token, quotes[2], add_token, quote;
60 '\0', &basic + 1, /* high byte of link must be non-zero */
61 0, 0x9C, ':', 0x93, "\"\"", 0xAA, '\"',
62 "\" ", /* format: "123:1234567890123456\"" */
65 #pragma data-name(pop)
67 /* These values are platform-specific. */
68 extern const void* vartab; /* points to BASIC program variables */
69 #pragma zpsym("vartab")
70 extern const void* memsize; /* points to top of BASIC RAM */
71 #pragma zpsym("memsize")
72 extern const struct line* txtptr; /* points to BASIC code */
73 #pragma zpsym("txtptr")
74 extern char basbuf[]; /* BASIC's input buffer */
75 extern void basbuf_len[];
76 #pragma zpsym("basbuf_len")
79 int __fastcall__ exec (const char* progname, const char* cmdline)
82 static unsigned char dv, n;
84 /* Exclude devices that can't load files. */
85 /* (Use hand optimization, to make smaller code.) */
86 dv = getcurrentdevice ();
87 if (dv < 8 && __AX__ != 1 || __AX__ > 30) {
88 return _mappederrno (9); /* illegal device number */
90 utoa (dv, basic.unit, 10);
92 /* Tape files can be openned only once; skip this test for the Datasette. */
94 /* Don't try to run a program that can't be found. */
95 fd = open (progname, O_RDONLY);
104 if ((basic.name[n] = progname[n]) == '\0') {
107 } while (++n < 20); /* truncate long names */
108 basic.name[n] = '\"';
110 /* This next part isn't needed by machines that put
111 ** BASIC source and variables in different RAM banks.
113 #if !defined(__CBM510__) && !defined(__CBM610__) && !defined(__C128__)
114 /* cc65 program loads might extend beyond the end of the RAM that is allowed
115 ** for BASIC. Then, the LOAD statement would complain that it is "out of
116 ** memory". Some pointers that say where to put BASIC program variables
117 ** must be changed, so that we do not get that error. One pointer is
118 ** changed here; a BASIC CLR statement changes the others.
120 vartab = (char*)memsize - 256;
123 /* Build the next program's argument list. */
124 basbuf[0] = 0x8F; /* REM token */
126 if (cmdline != NULL) {
127 strncat (basbuf, cmdline, (size_t)basbuf_len - 2);
130 /* Tell the ROM where to find that BASIC program. */
131 #if defined(__CBM510__) || defined(__CBM610__)
132 pokewsys ((unsigned)&txtptr, (unsigned)&basic);
137 /* (The return code, in ST [status], will be destroyed by LOAD.
138 ** So, don't bother to set it here.)