2 ** Extended memory overlay demo program.
4 ** Shows how to combine multiple cc65 features
5 ** incl. overlays and extended memory drivers.
7 ** 2012-17-07, Oliver Schmidt (ol.sc@web.de)
28 /* The symbols _OVERLAY?_LOAD__ and _OVERLAY?_SIZE__ were generated by the
29 ** linker. They contain the overlay area address and size specific to a
32 extern void _OVERLAY1_LOAD__[], _OVERLAY1_SIZE__[];
33 extern void _OVERLAY2_LOAD__[], _OVERLAY2_SIZE__[];
34 extern void _OVERLAY3_LOAD__[], _OVERLAY3_SIZE__[];
42 {{"multdemo.1", -1, _OVERLAY1_LOAD__, (unsigned)_OVERLAY1_SIZE__},
43 {"multdemo.2", -1, _OVERLAY2_LOAD__, (unsigned)_OVERLAY2_SIZE__},
44 {"multdemo.3", -1, _OVERLAY3_LOAD__, (unsigned)_OVERLAY3_SIZE__}};
48 /* Functions resident in an overlay can call back functions resident in the
49 ** main program at any time without any precautions. The function log() is
50 ** an example for such a function resident in the main program.
54 /* Functions resident in an overlay can access all program variables and
55 ** constants at any time without any precautions because those are never
56 ** placed in overlays. The string constant below is an example for such
57 ** a constant resident in the main program.
59 printf ("Log: %s\n", msg);
63 /* In a real-world overlay program one would probably not use a #pragma but
64 ** rather place all the code of certain source files into the overlay by
65 ** compiling them with --code-name OVERLAY1.
67 #pragma code-name (push, "OVERLAY1");
71 log ("Calling main from overlay 1");
74 #pragma code-name (pop);
77 #pragma code-name (push, "OVERLAY2");
81 log ("Calling main from overlay 2");
84 #pragma code-name (pop);
87 #pragma code-name (push, "OVERLAY3");
91 log ("Calling main from overlay 3");
94 #pragma code-name(pop);
97 unsigned char loademdriver (void)
102 unsigned char max = 0;
105 printf ("Dbg: Searching for emdrivers\n");
108 log ("Opening directory failed");
112 while (ent = readdir (dir)) {
115 if (!_DE_ISREG (ent->d_type)) {
119 ext = strrchr (ent->d_name, '.');
120 if (!ext || strcasecmp (ext, ".emd")) {
121 printf ("Dbg: Skipping file %s\n", ent->d_name);
125 printf ("Dbg: Memorizing file %s\n", ent->d_name);
126 emd = realloc (emd, FILENAME_MAX * (max + 1));
127 strcpy (emd + FILENAME_MAX * max++, ent->d_name);
131 for (num = 0; num < max; ++num) {
134 drv = emd + FILENAME_MAX * num;
135 printf ("Dbg: Trying emdriver %s\n", drv);
136 if (em_load_driver (drv) == EM_ERR_OK) {
137 printf ("Dbg: Loaded emdriver %s\n", drv);
142 printf ("Dbg: Emdriver %s failed\n", drv);
150 unsigned char loadoverlay (unsigned char num)
152 if (overlay[num - 1].page < 0) {
157 printf ("Dbg: Loading overlay %u from file\n", num);
158 file = open (overlay[num - 1].name, O_RDONLY);
160 log ("Opening overlay file failed");
163 read (file, overlay[num - 1].addr,
164 overlay[num - 1].size);
169 if (cbm_load (overlay[num - 1].name, getcurrentdevice (), NULL) == 0) {
170 log ("Loading overlay file failed");
177 struct em_copy copyinfo;
179 printf ("Dbg: Loading overlay %u from memory\n", num);
181 copyinfo.page = overlay[num - 1].page;
182 copyinfo.buf = overlay[num - 1].addr;
183 copyinfo.count = overlay[num - 1].size;
184 em_copyfrom (©info);
190 void copyoverlays (void)
195 for (num = 0; num < sizeof (overlay) / sizeof (overlay[0]); ++num) {
196 struct em_copy copyinfo;
197 unsigned size = (overlay[num].size + EM_PAGE_SIZE - 1) / EM_PAGE_SIZE;
199 if (size > em_pagecount () - page) {
200 printf ("Dbg: Not enough memory for overlay %u\n", num + 1);
204 if (loadoverlay (num + 1) == 0)
208 copyinfo.page = page;
209 copyinfo.buf = overlay[num].addr;
210 copyinfo.count = overlay[num].size;
211 em_copyto (©info);
213 overlay[num].page = page;
216 printf ("Dbg: Stored overlay %u in pages %u-%u\n",
217 num + 1, overlay[num].page, page - 1);
224 log ("Loading extended memory driver");
225 if (loademdriver ()) {
226 log ("Copying overlays into ext. memory");
229 log ("No extended memory driver found");
232 log ("Press any key...");
235 if (loadoverlay (1)) {
236 log ("Calling overlay 1 from main");
238 /* The linker makes sure that the call to foo() ends up at the right mem
239 ** addr. However it's up to user to make sure that the - right - overlay
240 ** is actually loaded before making the the call.
245 /* Replacing one overlay with another one can only happen from the main
246 ** program. This implies that an overlay can never load another overlay.
248 if (loadoverlay (2)) {
249 log ("Calling overlay 2 from main");
253 if (loadoverlay (3)) {
254 log ("Calling overlay 3 from main");
258 if (doesclrscrafterexit ()) {
259 log ("Press any key...");