]> git.sur5r.net Git - cc65/blob - libsrc/cbm/exec.c
Code review changes and build fix.
[cc65] / libsrc / cbm / exec.c
1 /*
2 ** Program-chaining function for Commodore platforms.
3 **
4 ** 2015-09-27, 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 CLR:LOAD""+""                    ,01
36 ** After this function has written into the line, it might look like this:
37 **  0 CLR: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 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.
49 */
50 #pragma data-name(push, "LOWCODE")
51 static struct line {
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;
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, 0x9C, ':', 0x93, "\"\"", 0xAA, '\"',
62     "\"                    ",           /* format: "123:1234567890123456\"" */
63     ',', "01"
64 };
65 #pragma data-name(pop)
66
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")
77
78
79 int __fastcall__ exec (const char* progname, const char* cmdline)
80 {
81     static int fd;
82     static unsigned char dv, n;
83
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 */
89     }
90     utoa (dv, basic.unit, 10);
91
92     /* Tape files can be openned only once; skip this test for the Datasette. */
93     if (dv != 1) {
94         /* Don't try to run a program that can't be found. */
95         fd = open (progname, O_RDONLY);
96         if (fd < 0) {
97             return -1;
98         }
99         close (fd);
100     }
101
102     n = 0;
103     do {
104         if ((basic.name[n] = progname[n]) == '\0') {
105             break;
106         }
107     } while (++n < 20);                 /* truncate long names */
108     basic.name[n] = '\"';
109
110 /* This next part isn't needed by machines that put
111 ** BASIC source and variables in different RAM banks.
112 */
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.
119     */
120     vartab = (char*)memsize - 256;
121 #endif
122
123     /* Build the next program's argument list. */
124     basbuf[0] = 0x8F;                   /* REM token */
125     basbuf[1] = '\0';
126     if (cmdline != NULL) {
127         strncat (basbuf, cmdline, (size_t)basbuf_len - 2);
128     }
129
130     /* Tell the ROM where to find that BASIC program. */
131 #if defined(__CBM510__) || defined(__CBM610__)
132     pokewsys ((unsigned)&txtptr, (unsigned)&basic);
133 #else
134     txtptr = &basic;
135 #endif
136
137     /* (The return code, in ST [status], will be destroyed by LOAD.
138     ** So, don't bother to set it here.)
139     */
140     exit (__AX__);
141 }