2 * Minimalistic overlay demo program.
4 * 2009-10-02, Oliver Schmidt (ol.sc@web.de)
16 extern void _OVERLAY1_LOAD__, _OVERLAY1_SIZE__;
17 extern void _OVERLAY2_LOAD__, _OVERLAY2_SIZE__;
18 extern void _OVERLAY3_LOAD__, _OVERLAY3_SIZE__;
21 /* Functions resident in an overlay can call back functions resident in the
22 * main program at any time without any precautions. The function log() is
23 * an example for such a function resident in the main program.
27 printf ("Log: %s\n", msg);
31 /* In a real-world overlay program one would probably not use a #pragma but
32 * rather place the all the code of certain source files into the overlay by
33 * compiling them with --code-name OVERLAY1.
35 #pragma code-name (push, "OVERLAY1");
39 /* Functions resident in an overlay can access all program variables and
40 * constants at any time without any precautions because those are never
41 * placed in overlays. The string constant below is an example for such
42 * a constant resident in the main program.
44 log ("Calling main from overlay 1");
47 #pragma code-name (pop);
50 #pragma code-name (push, "OVERLAY2");
54 log ("Calling main from overlay 2");
57 #pragma code-name (pop);
60 #pragma code-name (push, "OVERLAY3");
64 log ("Calling main from overlay 3");
67 #pragma code-name(pop);
70 unsigned char loadfile (char *name, void *addr, void *size)
72 int file = open (name, O_RDONLY);
74 log ("Opening overlay file failed");
78 read (file, addr, (unsigned) size);
86 log ("Calling overlay 1 from main");
88 /* The symbols _OVERLAY1_LOAD__ and _OVERLAY1_SIZE__ were generated by the
89 * linker. They contain the overlay area address and size specific to a
92 if (loadfile ("ovrldemo.1", &_OVERLAY1_LOAD__, &_OVERLAY1_SIZE__)) {
94 /* The linker makes sure that the call to foo() ends up at the right mem
95 * addr. However it's up to user to make sure that the - right - overlay
96 * is actually loaded before making the the call.
101 log ("Calling overlay 2 from main");
103 /* Replacing one overlay with another one can only happen from the main
104 * program. This implies that an overlay can never load another overlay.
106 if (loadfile ("ovrldemo.2", &_OVERLAY2_LOAD__, &_OVERLAY2_SIZE__)) {
110 log ("Calling overlay 3 from main");
111 if (loadfile ("ovrldemo.3", &_OVERLAY3_LOAD__, &_OVERLAY3_SIZE__)) {