Contiki 3.x
exec.c
1 /*
2 ** Program-chaining function for Commodore platforms.
3 **
4 ** This copy of the cc65 system library function makes smaller code by using
5 ** Contiki's Personal File System (instead of POSIX) functions.
6 **
7 ** 2016-03-16, Greg King
8 **
9 ** This function exploits the program-chaining feature in CBM BASIC's ROM.
10 **
11 ** CC65's CBM programs have a BASIC program stub. We start those programs by
12 ** RUNning that stub; it SYSes to the Machine Language code. Normally, after
13 ** the ML code exits, the BASIC ROM continues running the stub. But, it has
14 ** no more statements; so, the program stops.
15 **
16 ** This function puts the desired program's name and device number into a LOAD
17 ** statement. Then, it points BASIC to that statement, so that the ROM will run
18 ** that statement after this program quits. The ROM will load the next program,
19 ** and will execute it (because the LOAD will be seen in a running program).
20 */
21 
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <device.h>
27 
28 #include "cfs.h"
29 
30 
31 /* The struct below is a line of BASIC code. It sits in the LOWCODE segment
32 ** to make sure that it won't be hidden by a ROM when BASIC is re-enabled.
33 ** The line is:
34 ** 0 CLR:LOAD""+"" ,01
35 ** After this function has written into the line, it might look like this:
36 ** 0 CLR:LOAD""+"program name" ,08
37 **
38 ** When BASIC's LOAD command asks the Kernal to load a file, it gives the
39 ** Kernal a pointer to a file-name string. CC65's CBM programs use that
40 ** pointer to give a copy of the program's name to main()'s argv[0] parameter.
41 ** But, when BASIC uses a string literal that is in a program, it points
42 ** directly to that literal -- in the models that don't use banked RAM
43 ** (Pet/CBM, VIC-20, and 64). The literal is overwritten by the next program
44 ** that is loaded. So, argv[0] would point to machine code. String operations
45 ** create a new result string -- even when that operation changes nothing. The
46 ** result is put in the string space at the top of BASIC's memory. So, the ""+
47 ** in this BASIC line guarantees that argv[0] will get a name from a safe place.
48 */
49 #pragma data-name(push, "LOWCODE")
50 static struct line {
51  const char end_of_line; /* fake previous line */
52  const struct line* const next;
53  const unsigned line_num;
54  const char CLR_token, colon, LOAD_token, quotes[2], add_token, quote;
55  char name[21];
56  const char comma;
57  char unit[3];
58 } basic = {
59  '\0', &basic + 1, /* high byte of link must be non-zero */
60  0, 0x9C, ':', 0x93, "\"\"", 0xAA, '\"',
61  "\" ", /* format: "123:1234567890123456\"" */
62  ',', "01"
63 };
64 #pragma data-name(pop)
65 
66 /* These values are platform-specific. */
67 extern const void* vartab; /* points to BASIC program variables */
68 #pragma zpsym("vartab")
69 extern const void* memsize; /* points to top of BASIC RAM */
70 #pragma zpsym("memsize")
71 extern const struct line* txtptr; /* points to BASIC code */
72 #pragma zpsym("txtptr")
73 extern char basbuf[]; /* BASIC's input buffer */
74 extern void basbuf_len[];
75 #pragma zpsym("basbuf_len")
76 
77 
78 int __fastcall__
79 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 = cfs_open(progname, CFS_READ);
96  if(fd < 0) {
97  return -1;
98  }
99  cfs_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(__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. Some space is
119  ** needed for the file-name string. Subtracting an entire RAM page allows
120  ** better optimization of this expression.
121  */
122  vartab = (char*)memsize - 0x0100;
123 #endif
124 
125  /* Build the next program's argument list. */
126  basbuf[0] = 0x8F; /* REM token */
127  basbuf[1] = '\0';
128  if(cmdline != NULL) {
129  strncat(basbuf, cmdline, (size_t)basbuf_len - 2);
130  }
131 
132  /* Tell the ROM where to find that BASIC program. */
133  txtptr = &basic;
134 
135  /* (The return code, in ST [status], will be destroyed by LOAD.
136  ** So, don't bother to set it here.)
137  */
138  exit(__AX__);
139 }
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:1011
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1047
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
#define NULL
The null pointer.
CFS header file.