2 * Extended memory overlay demo program.
4 * 2012-17-07, Oliver Schmidt (ol.sc@web.de)
19 /* The symbols _OVERLAY?_LOAD__ and _OVERLAY?_SIZE__ were generated by the
20 * linker. They contain the overlay area address and size specific to a
23 extern void _OVERLAY1_LOAD__, _OVERLAY1_SIZE__;
24 extern void _OVERLAY2_LOAD__, _OVERLAY2_SIZE__;
25 extern void _OVERLAY3_LOAD__, _OVERLAY3_SIZE__;
26 extern void _OVERLAY4_LOAD__, _OVERLAY4_SIZE__;
34 {{"multdemo.1", -1, &_OVERLAY1_LOAD__, (unsigned)&_OVERLAY1_SIZE__},
35 {"multdemo.2", -1, &_OVERLAY2_LOAD__, (unsigned)&_OVERLAY2_SIZE__},
36 {"multdemo.3", -1, &_OVERLAY3_LOAD__, (unsigned)&_OVERLAY3_SIZE__},
37 {"multdemo.4", -1, &_OVERLAY4_LOAD__, (unsigned)&_OVERLAY4_SIZE__}};
39 /* Copy overlays into extended memory up to overlay 3. Overlay 4 is known to
40 * to be loaded only once for onetime initialization purposes so there's no
41 * use in allocating extended memory for it.
43 #define MAX_EM_OVERLAY 3
45 /* Search for up to 10 extended memory drivers.
47 #define MAX_EM_DRIVER 10
51 /* Functions resident in an overlay can call back functions resident in the
52 * main program at any time without any precautions. The function log() is
53 * an example for such a function resident in the main program.
57 /* Functions resident in an overlay can access all program variables and
58 * constants at any time without any precautions because those are never
59 * placed in overlays. The string constant below is an example for such
60 * a constant resident in the main program.
62 printf ("Log: %s\n", msg);
66 /* In a real-world overlay program one would probably not use a #pragma but
67 * rather place the all the code of certain source files into the overlay by
68 * compiling them with --code-name OVERLAY1.
70 #pragma code-name (push, "OVERLAY1");
74 log ("Calling main from overlay 1");
77 #pragma code-name (pop);
80 #pragma code-name (push, "OVERLAY2");
84 log ("Calling main from overlay 2");
87 #pragma code-name (pop);
90 #pragma code-name (push, "OVERLAY3");
94 log ("Calling main from overlay 3");
97 #pragma code-name(pop);
100 #pragma code-name (push, "OVERLAY4");
102 unsigned char loademdriver (void)
104 static char emd[MAX_EM_DRIVER][FILENAME_MAX];
107 unsigned char max = 0;
110 printf ("Dbg: Searching for emdrivers\n");
113 log ("Opening directory failed");
117 while (ent = readdir (dir)) {
120 if (!_DE_ISREG (ent->d_type)) {
124 ext = strrchr (ent->d_name, '.');
125 if (!ext || strcasecmp (ext, ".emd")) {
126 printf ("Dbg: Skipping file %s\n", ent->d_name);
130 printf ("Dbg: Memorizing file %s\n", ent->d_name);
131 strcpy (emd[max], ent->d_name);
132 if (++max == MAX_EM_DRIVER) {
138 for (num = 0; num < max; ++num) {
139 printf ("Dbg: Trying emdriver %s\n", emd[num]);
140 if (em_load_driver (emd[num]) == EM_ERR_OK) {
141 printf ("Dbg: Loaded emdriver %s\n", emd[num]);
145 printf ("Dbg: Emdriver %s failed\n", emd[num]);
150 void copyoverlays (void)
155 for (num = 0; num < MAX_EM_OVERLAY; ++num) {
159 if ((overlay[num].size + EM_PAGE_SIZE - 1) / EM_PAGE_SIZE >
160 em_pagecount () - page) {
161 printf ("Dbg: Not enough memory for overlay %u\n", num + 1);
165 printf ("Dbg: Reading overlay file %s\n", overlay[num].name);
166 file = open (overlay[num].name, O_RDONLY);
168 log ("Opening overlay file failed");
172 overlay[num].page = page;
173 size = overlay[num].size;
177 /* In general one could as well use em_copyto() to copy a fully
178 * loaded overlay into extended memory in one step. However the
179 * "streaming" of an overlay from disk to extended memory shown
180 * here has two advantages:
181 * - It can be done from another overlay (like done here).
182 * - It avoids unnecessary double buffering with emdrivers that
183 * provide a hardware memory window.
185 buf = em_use (page++);
186 size -= read (file, buf, EM_PAGE_SIZE);
190 printf ("Dbg: Stored overlay %u in pages %u-%u\n",
191 num + 1, overlay[num].page, page - 1);
197 #pragma code-name(pop);
200 unsigned char loadoverlay (unsigned char num)
202 if (overlay[num - 1].page < 0) {
205 printf ("Dbg: Loading overlay %u from file\n", num);
206 file = open (overlay[num - 1].name, O_RDONLY);
208 log ("Opening overlay file failed");
211 read (file, overlay[num - 1].addr,
212 overlay[num - 1].size);
216 struct em_copy copyinfo;
218 printf ("Dbg: Loading overlay %u from memory\n", num);
220 copyinfo.page = overlay[num - 1].page;
221 copyinfo.buf = overlay[num - 1].addr;
222 copyinfo.count = overlay[num - 1].size;
223 em_copyfrom (©info);
230 if (loadoverlay (4)) {
231 log ("Loading extended memory driver");
232 if (loademdriver ()) {
233 log ("Copying overlays into ext. memory");
236 log ("No extended memory driver found");
240 log ("Press any key...");
243 if (loadoverlay (1)) {
244 log ("Calling overlay 1 from main");
246 /* The linker makes sure that the call to foo() ends up at the right mem
247 * addr. However it's up to user to make sure that the - right - overlay
248 * is actually loaded before making the the call.
253 /* Replacing one overlay with another one can only happen from the main
254 * program. This implies that an overlay can never load another overlay.
256 if (loadoverlay (2)) {
257 log ("Calling overlay 2 from main");
261 if (loadoverlay (3)) {
262 log ("Calling overlay 3 from main");
266 log ("Press any key...");