]> git.sur5r.net Git - cc65/blob - samples/multidemo.c
Adjusted according to the recently updated readdir() doc that now says:
[cc65] / samples / multidemo.c
1 /*
2  * Extended memory overlay demo program.
3  *
4  * 2012-17-07, Oliver Schmidt (ol.sc@web.de)
5  *
6  */
7
8
9
10 #include <string.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <dirent.h>
15 #include <em.h>
16 #include <conio.h>
17
18
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
21  * certain program.
22  */
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__;
27
28 struct {
29     char     *name;
30     int      page;
31     void     *addr;
32     unsigned size;
33 } overlay[] =
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__}};
38
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.
42  */
43 #define MAX_EM_OVERLAY 3
44
45 /* Search for up to 10 extended memory drivers.
46  */
47 #define MAX_EM_DRIVER 10
48
49
50
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.
54  */
55 void log (char *msg)
56 {
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.
61      */
62     printf ("Log: %s\n", msg);
63 }
64
65
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.
69  */
70 #pragma code-name (push, "OVERLAY1");
71
72 void foo (void)
73 {
74     log ("Calling main from overlay 1");
75 }
76
77 #pragma code-name (pop);
78
79
80 #pragma code-name (push, "OVERLAY2");
81
82 void bar (void)
83 {
84     log ("Calling main from overlay 2");
85 }
86
87 #pragma code-name (pop);
88
89
90 #pragma code-name (push, "OVERLAY3");
91
92 void foobar (void)
93 {
94     log ("Calling main from overlay 3");
95 }
96
97 #pragma code-name(pop);
98
99
100 #pragma code-name (push, "OVERLAY4");
101
102 unsigned char loademdriver (void)
103 {
104     static char emd[MAX_EM_DRIVER][FILENAME_MAX];
105     DIR* dir;
106     struct dirent* ent;
107     unsigned char max = 0;
108     unsigned char num;
109
110     printf ("Dbg: Searching for emdrivers\n");
111     dir = opendir (".");
112     if (!dir) {
113         log ("Opening directory failed");
114         return 0;
115     }
116
117     while (ent = readdir (dir)) {
118         char *ext;
119
120         if (!_DE_ISREG (ent->d_type)) {
121             continue;
122         }
123
124         ext = strrchr (ent->d_name, '.');
125         if (!ext || strcasecmp (ext, ".emd")) {
126             printf ("Dbg: Skipping file %s\n", ent->d_name);
127             continue;
128         }
129
130         printf ("Dbg: Memorizing file %s\n", ent->d_name);
131         strcpy (emd[max], ent->d_name);
132         if (++max == MAX_EM_DRIVER) {
133             break;
134         }
135     }
136     closedir (dir);
137
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]);
142             return 1;
143         }
144  
145         printf ("Dbg: Emdriver %s failed\n", emd[num]);
146     }
147     return 0;
148 }
149
150 void copyoverlays (void)
151 {
152     unsigned page = 0;
153     unsigned char num;
154
155     for (num = 0; num < MAX_EM_OVERLAY; ++num) {
156         int file;
157         int size;
158
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);
162             continue;
163         }
164
165         printf ("Dbg: Reading overlay file %s\n", overlay[num].name);
166         file = open (overlay[num].name, O_RDONLY);
167         if (file == -1) {
168             log ("Opening overlay file failed");
169             continue;
170         }
171
172         overlay[num].page = page;
173         size = overlay[num].size;
174         while (size) {
175             void *buf;
176
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.
184              */
185             buf = em_use (page++);
186             size -= read (file, buf, EM_PAGE_SIZE);
187             em_commit ();
188         }
189
190         printf ("Dbg: Stored overlay %u in pages %u-%u\n",
191                 num + 1, overlay[num].page, page - 1);
192
193         close (file);
194     }
195 }
196
197 #pragma code-name(pop);
198
199
200 unsigned char loadoverlay (unsigned char num)
201 {
202     if (overlay[num - 1].page < 0) {
203         int file;
204
205         printf ("Dbg: Loading overlay %u from file\n", num);
206         file = open (overlay[num - 1].name, O_RDONLY);
207         if (file == -1) {
208             log ("Opening overlay file failed");
209             return 0;
210         }
211         read (file, overlay[num - 1].addr,
212                     overlay[num - 1].size);
213         close (file);
214         return 1;
215     } else {
216         struct em_copy copyinfo;
217
218         printf ("Dbg: Loading overlay %u from memory\n", num);
219         copyinfo.offs  = 0;
220         copyinfo.page  = overlay[num - 1].page;
221         copyinfo.buf   = overlay[num - 1].addr;
222         copyinfo.count = overlay[num - 1].size;
223         em_copyfrom (&copyinfo);
224         return 1;
225     }
226 }
227
228 void main (void)
229 {
230     if (loadoverlay (4)) {
231         log ("Loading extended memory driver");
232         if (loademdriver ()) {
233             log ("Copying overlays into ext. memory");
234             copyoverlays ();
235         } else {
236             log ("No extended memory driver found");
237         }
238     }
239
240     log ("Press any key...");
241     cgetc ();
242
243     if (loadoverlay (1)) {
244         log ("Calling overlay 1 from main");
245
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.
249          */
250         foo ();
251     }
252
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.
255      */
256     if (loadoverlay (2)) {
257         log ("Calling overlay 2 from main");
258         bar ();
259     }
260
261     if (loadoverlay (3)) {
262         log ("Calling overlay 3 from main");
263         foobar ();
264     }
265
266     log ("Press any key...");
267     cgetc ();
268 }