]> git.sur5r.net Git - cc65/blob - samples/overlaydemo.c
Retrofitted comments from GEOS overlay demo to regular overlay demo.
[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 #include <fcntl.h>
13 #include <unistd.h>
14
15
16 extern void _OVERLAY1_LOAD__, _OVERLAY1_SIZE__;
17 extern void _OVERLAY2_LOAD__, _OVERLAY2_SIZE__;
18 extern void _OVERLAY3_LOAD__, _OVERLAY3_SIZE__;
19
20
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.
24  */
25 void log (char *msg)
26 {
27     printf ("Log: %s\n", msg);
28 }
29
30
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.
34  */
35 #pragma code-name (push, "OVERLAY1");
36
37 void foo (void)
38 {
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.
43      */
44     log ("Calling main from overlay 1");
45 }
46
47 #pragma code-name (pop);
48
49
50 #pragma code-name (push, "OVERLAY2");
51
52 void bar (void)
53 {
54     log ("Calling main from overlay 2");
55 }
56
57 #pragma code-name (pop);
58
59
60 #pragma code-name (push, "OVERLAY3");
61
62 void foobar (void)
63 {
64     log ("Calling main from overlay 3");
65 }
66
67 #pragma code-name(pop);
68
69
70 unsigned char loadfile (char *name, void *addr, void *size)
71 {
72     int file = open (name, O_RDONLY);
73     if (file == -1) {
74         log ("Opening overlay file failed");
75         return 0;
76     }
77
78     read (file, addr, (unsigned) size);
79     close (file);
80     return 1;
81 }
82
83
84 void main (void)
85 {
86     log ("Calling overlay 1 from main");
87
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
90      * certain program.
91      */
92     if (loadfile ("ovrldemo.1", &_OVERLAY1_LOAD__, &_OVERLAY1_SIZE__)) {
93
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.
97          */
98         foo ();
99     }
100
101     log ("Calling overlay 2 from main");
102
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.
105      */
106     if (loadfile ("ovrldemo.2", &_OVERLAY2_LOAD__, &_OVERLAY2_SIZE__)) {
107         bar ();
108     }
109
110     log ("Calling overlay 3 from main");
111     if (loadfile ("ovrldemo.3", &_OVERLAY3_LOAD__, &_OVERLAY3_SIZE__)) {
112         foobar ();
113     }
114
115     cgetc ();
116 }