]> git.sur5r.net Git - cc65/blob - libsrc/cbm/exec.c
Merge remote-tracking branch 'upstream/master'
[cc65] / libsrc / cbm / exec.c
1 /*
2 ** Program-chaining function for Commodore platforms.
3 **
4 ** 2013-09-04, Greg King
5 **
6 ** This function exploits the program-chaining feature in CBM BASIC's ROM.
7 **
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.
12 **
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).
17 */
18
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <device.h>
25 #if   defined(__CBM610__)
26 #  include <cbm610.h>
27 #elif defined(__CBM510__)
28 #  include <cbm510.h>
29 #endif
30
31
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.
34 ** The line is:
35 **  0 LOAD""+""                    ,01
36 ** After this function has written into the line, it might look like this:
37 **  0 LOAD""+"program name"        ,08
38 **
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.
49 */
50 #pragma data-name(push, "LOWCODE")
51 static struct line {
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;
56     char name[21];
57     const char comma;
58     char unit[3];
59 } basic = {
60     '\0', &basic + 1,           /* high byte of link must be non-zero */
61     0, 0x93, "\"\"", 0xaa, '\"',
62     "\"                    ",   /* format: "123:1234567890123456\"" */
63     ',', "01"
64 };
65 #pragma data-name(pop)
66
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")
73
74
75 int __fastcall__ exec (const char* progname, const char* cmdline)
76 {
77     static int fd;
78     static unsigned char dv, n = 0;
79
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 */
84     }
85     utoa (dv, basic.unit, 10);
86
87     /* Don't try to run a program that can't be found. */
88     fd = open (progname, O_RDONLY);
89     if (fd < 0) {
90         return fd;
91     }
92     close (fd);
93
94     do {
95         if ((basic.name[n] = progname[n]) == '\0') {
96             break;
97         }
98     } while (++n < 20);         /* truncate long names */
99     basic.name[n] = '\"';
100
101     /* Build the next program's argument list. */
102     basbuf[0] = 0x8f;           /* REM token */
103     basbuf[1] = '\0';
104     if (cmdline != NULL) {
105         strncat (basbuf, cmdline, (size_t)basbuf_len - 2);
106     }
107
108 #if defined(__CBM510__) || defined(__CBM610__)
109     pokewsys ((unsigned)&txtptr, (unsigned)&basic);
110 #else
111     txtptr = &basic;
112 #endif
113
114     /* (The return code, in ST, will be destroyed by LOAD.
115     ** So, don't bother to set it here.)
116     */
117     exit (__AX__);
118 }