X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=samples%2Foverlaydemo.c;h=7553f3d0ead6c1aaf21a7210f8f0cc2a9de1179b;hb=HEAD;hp=02451a28a08958a175481a6d0752da12cf66d8c3;hpb=3cb0cce00d689818657a1c2e4d058be7b3f2dc42;p=cc65 diff --git a/samples/overlaydemo.c b/samples/overlaydemo.c index 02451a28a..7553f3d0e 100644 --- a/samples/overlaydemo.c +++ b/samples/overlaydemo.c @@ -1,32 +1,53 @@ /* - * Minimalistic overlay demo program. - * - * 2009-10-02, Oliver Schmidt (ol.sc@web.de) - * - */ +** Minimalistic overlay demo program. +** +** Shows how to load overlay files from disk. +** +** 2009-10-02, Oliver Schmidt (ol.sc@web.de) +** +*/ #include +#include +#ifndef __CBM__ #include #include +#else +#include +#include +#endif -extern void _OVERLAY1_LOAD__, _OVERLAY1_SIZE__; -extern void _OVERLAY2_LOAD__, _OVERLAY2_SIZE__; -extern void _OVERLAY3_LOAD__, _OVERLAY3_SIZE__; +extern void _OVERLAY1_LOAD__[], _OVERLAY1_SIZE__[]; +extern void _OVERLAY2_LOAD__[], _OVERLAY2_SIZE__[]; +extern void _OVERLAY3_LOAD__[], _OVERLAY3_SIZE__[]; +/* Functions resident in an overlay can call back functions resident in the +** main program at any time without any precautions. The function log() is +** an example for such a function resident in the main program. +*/ void log (char *msg) { printf ("Log: %s\n", msg); } +/* In a real-world overlay program one would probably not use a #pragma but +** rather place all the code of certain source files into the overlay by +** compiling them with --code-name OVERLAY1. +*/ #pragma code-name (push, "OVERLAY1"); void foo (void) { + /* Functions resident in an overlay can access all program variables and + ** constants at any time without any precautions because those are never + ** placed in overlays. The string constant below is an example for such + ** a constant resident in the main program. + */ log ("Calling main from overlay 1"); } @@ -55,14 +76,26 @@ void foobar (void) unsigned char loadfile (char *name, void *addr, void *size) { +#ifndef __CBM__ + int file = open (name, O_RDONLY); if (file == -1) { log ("Opening overlay file failed"); return 0; } - read (file, addr, (unsigned) size); close (file); + +#else + + /* Avoid compiler warnings about unused parameters. */ + (void) addr; (void) size; + if (cbm_load (name, getcurrentdevice (), NULL) == 0) { + log ("Loading overlay file failed"); + return 0; + } + +#endif return 1; } @@ -70,17 +103,35 @@ unsigned char loadfile (char *name, void *addr, void *size) void main (void) { log ("Calling overlay 1 from main"); - if (loadfile ("overlaydemo.1", &_OVERLAY1_LOAD__, &_OVERLAY1_SIZE__)) { + + /* The symbols _OVERLAY1_LOAD__ and _OVERLAY1_SIZE__ were generated by the + ** linker. They contain the overlay area address and size specific to a + ** certain program. + */ + if (loadfile ("ovrldemo.1", _OVERLAY1_LOAD__, _OVERLAY1_SIZE__)) { + + /* The linker makes sure that the call to foo() ends up at the right mem + ** addr. However it's up to user to make sure that the - right - overlay + ** is actually loaded before making the the call. + */ foo (); } log ("Calling overlay 2 from main"); - if (loadfile ("overlaydemo.2", &_OVERLAY2_LOAD__, &_OVERLAY2_SIZE__)) { + + /* Replacing one overlay with another one can only happen from the main + ** program. This implies that an overlay can never load another overlay. + */ + if (loadfile ("ovrldemo.2", _OVERLAY2_LOAD__, _OVERLAY2_SIZE__)) { bar (); } log ("Calling overlay 3 from main"); - if (loadfile ("overlaydemo.3", &_OVERLAY3_LOAD__, &_OVERLAY3_SIZE__)) { + if (loadfile ("ovrldemo.3", _OVERLAY3_LOAD__, _OVERLAY3_SIZE__)) { foobar (); } + + if (doesclrscrafterexit ()) { + getchar (); + } }