]> git.sur5r.net Git - cc65/blob - samples/overlaydemo.c
Fixed comment.
[cc65] / samples / overlaydemo.c
1 /*
2  * Minimalistic overlay demo program.
3  *
4  * 2009-10-02, Oliver Schmidt (ol.sc@web.de)
5  *
6  */
7
8
9
10 #include <stdio.h>
11 #include <conio.h>
12 #ifndef __CBM__
13 #include <fcntl.h>
14 #include <unistd.h>
15 #else
16 #include <device.h>
17 #endif
18
19
20 extern void _OVERLAY1_LOAD__[], _OVERLAY1_SIZE__[];
21 extern void _OVERLAY2_LOAD__[], _OVERLAY2_SIZE__[];
22 extern void _OVERLAY3_LOAD__[], _OVERLAY3_SIZE__[];
23
24
25 /* Functions resident in an overlay can call back functions resident in the
26  * main program at any time without any precautions. The function log() is
27  * an example for such a function resident in the main program.
28  */
29 void log (char *msg)
30 {
31     printf ("Log: %s\n", msg);
32 }
33
34
35 /* In a real-world overlay program one would probably not use a #pragma but
36  * rather place all the code of certain source files into the overlay by
37  * compiling them with --code-name OVERLAY1.
38  */
39 #pragma code-name (push, "OVERLAY1");
40
41 void foo (void)
42 {
43     /* Functions resident in an overlay can access all program variables and
44      * constants at any time without any precautions because those are never
45      * placed in overlays. The string constant below is an example for such 
46      * a constant resident in the main program.
47      */
48     log ("Calling main from overlay 1");
49 }
50
51 #pragma code-name (pop);
52
53
54 #pragma code-name (push, "OVERLAY2");
55
56 void bar (void)
57 {
58     log ("Calling main from overlay 2");
59 }
60
61 #pragma code-name (pop);
62
63
64 #pragma code-name (push, "OVERLAY3");
65
66 void foobar (void)
67 {
68     log ("Calling main from overlay 3");
69 }
70
71 #pragma code-name(pop);
72
73
74 unsigned char loadfile (char *name, void *addr, void *size)
75 {
76 #ifndef __CBM__
77
78     int file = open (name, O_RDONLY);
79     if (file == -1) {
80         log ("Opening overlay file failed");
81         return 0;
82     }
83     read (file, addr, (unsigned) size);
84     close (file);
85
86 #else
87
88     /* Avoid compiler warnings about unused parameters. */
89     (void) addr; (void) size;
90     if (cbm_load (name, getcurrentdevice (), NULL) == 0) {
91         log ("Loading overlay file failed");
92         return 0;
93     }
94
95 #endif
96     return 1;
97 }
98
99
100 void main (void)
101 {
102     log ("Calling overlay 1 from main");
103
104     /* The symbols _OVERLAY1_LOAD__ and _OVERLAY1_SIZE__ were generated by the
105      * linker. They contain the overlay area address and size specific to a
106      * certain program.
107      */
108     if (loadfile ("ovrldemo.1", _OVERLAY1_LOAD__, _OVERLAY1_SIZE__)) {
109
110         /* The linker makes sure that the call to foo() ends up at the right mem
111          * addr. However it's up to user to make sure that the - right - overlay
112          * is actually loaded before making the the call.
113          */
114         foo ();
115     }
116
117     log ("Calling overlay 2 from main");
118
119     /* Replacing one overlay with another one can only happen from the main
120      * program. This implies that an overlay can never load another overlay.
121      */
122     if (loadfile ("ovrldemo.2", _OVERLAY2_LOAD__, _OVERLAY2_SIZE__)) {
123         bar ();
124     }
125
126     log ("Calling overlay 3 from main");
127     if (loadfile ("ovrldemo.3", _OVERLAY3_LOAD__, _OVERLAY3_SIZE__)) {
128         foobar ();
129     }
130
131     cgetc ();
132 }