2 * Extended memory overlay demo program.
4 * 2012-17-07, Oliver Schmidt (ol.sc@web.de)
24 /* The symbols _OVERLAY?_LOAD__ and _OVERLAY?_SIZE__ were generated by the
25 * linker. They contain the overlay area address and size specific to a
28 extern void _OVERLAY1_LOAD__[], _OVERLAY1_SIZE__[];
29 extern void _OVERLAY2_LOAD__[], _OVERLAY2_SIZE__[];
30 extern void _OVERLAY3_LOAD__[], _OVERLAY3_SIZE__[];
38 {{"multdemo.1", -1, _OVERLAY1_LOAD__, (unsigned)_OVERLAY1_SIZE__},
39 {"multdemo.2", -1, _OVERLAY2_LOAD__, (unsigned)_OVERLAY2_SIZE__},
40 {"multdemo.3", -1, _OVERLAY3_LOAD__, (unsigned)_OVERLAY3_SIZE__}};
44 /* Functions resident in an overlay can call back functions resident in the
45 * main program at any time without any precautions. The function log() is
46 * an example for such a function resident in the main program.
50 /* Functions resident in an overlay can access all program variables and
51 * constants at any time without any precautions because those are never
52 * placed in overlays. The string constant below is an example for such
53 * a constant resident in the main program.
55 printf ("Log: %s\n", msg);
59 /* In a real-world overlay program one would probably not use a #pragma but
60 * rather place all the code of certain source files into the overlay by
61 * compiling them with --code-name OVERLAY1.
63 #pragma code-name (push, "OVERLAY1");
67 log ("Calling main from overlay 1");
70 #pragma code-name (pop);
73 #pragma code-name (push, "OVERLAY2");
77 log ("Calling main from overlay 2");
80 #pragma code-name (pop);
83 #pragma code-name (push, "OVERLAY3");
87 log ("Calling main from overlay 3");
90 #pragma code-name(pop);
93 unsigned char loademdriver (void)
98 unsigned char max = 0;
101 printf ("Dbg: Searching for emdrivers\n");
104 log ("Opening directory failed");
108 while (ent = readdir (dir)) {
111 if (!_DE_ISREG (ent->d_type)) {
115 ext = strrchr (ent->d_name, '.');
116 if (!ext || strcasecmp (ext, ".emd")) {
117 printf ("Dbg: Skipping file %s\n", ent->d_name);
121 printf ("Dbg: Memorizing file %s\n", ent->d_name);
122 emd = realloc (emd, FILENAME_MAX * (max + 1));
123 strcpy (emd + FILENAME_MAX * max++, ent->d_name);
127 for (num = 0; num < max; ++num) {
130 drv = emd + FILENAME_MAX * num;
131 printf ("Dbg: Trying emdriver %s\n", drv);
132 if (em_load_driver (drv) == EM_ERR_OK) {
133 printf ("Dbg: Loaded emdriver %s\n", drv);
138 printf ("Dbg: Emdriver %s failed\n", drv);
146 unsigned char loadoverlay (unsigned char num)
148 if (overlay[num - 1].page < 0) {
153 printf ("Dbg: Loading overlay %u from file\n", num);
154 file = open (overlay[num - 1].name, O_RDONLY);
156 log ("Opening overlay file failed");
159 read (file, overlay[num - 1].addr,
160 overlay[num - 1].size);
165 if (cbm_load (overlay[num - 1].name, getcurrentdevice (), NULL) == 0) {
166 log ("Loading overlay file failed");
173 struct em_copy copyinfo;
175 printf ("Dbg: Loading overlay %u from memory\n", num);
177 copyinfo.page = overlay[num - 1].page;
178 copyinfo.buf = overlay[num - 1].addr;
179 copyinfo.count = overlay[num - 1].size;
180 em_copyfrom (©info);
186 void copyoverlays (void)
191 for (num = 0; num < sizeof (overlay) / sizeof (overlay[0]); ++num) {
192 struct em_copy copyinfo;
193 unsigned size = (overlay[num].size + EM_PAGE_SIZE - 1) / EM_PAGE_SIZE;
195 if (size > em_pagecount () - page) {
196 printf ("Dbg: Not enough memory for overlay %u\n", num + 1);
200 if (loadoverlay (num + 1) == 0)
204 copyinfo.page = page;
205 copyinfo.buf = overlay[num].addr;
206 copyinfo.count = overlay[num].size;
207 em_copyto (©info);
209 overlay[num].page = page;
212 printf ("Dbg: Stored overlay %u in pages %u-%u\n",
213 num + 1, overlay[num].page, page - 1);
220 log ("Loading extended memory driver");
221 if (loademdriver ()) {
222 log ("Copying overlays into ext. memory");
225 log ("No extended memory driver found");
228 log ("Press any key...");
231 if (loadoverlay (1)) {
232 log ("Calling overlay 1 from main");
234 /* The linker makes sure that the call to foo() ends up at the right mem
235 * addr. However it's up to user to make sure that the - right - overlay
236 * is actually loaded before making the the call.
241 /* Replacing one overlay with another one can only happen from the main
242 * program. This implies that an overlay can never load another overlay.
244 if (loadoverlay (2)) {
245 log ("Calling overlay 2 from main");
249 if (loadoverlay (3)) {
250 log ("Calling overlay 3 from main");
254 log ("Press any key...");