]> git.sur5r.net Git - cc65/blobdiff - samples/overlaydemo.c
Changes due to code review.
[cc65] / samples / overlaydemo.c
index 70a35f11d99fd13131895011417eebc4ab5d54d3..7553f3d0ead6c1aaf21a7210f8f0cc2a9de1179b 100644 (file)
@@ -1,16 +1,23 @@
 /*
- * 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 <stdio.h>
-#include <conio.h>
+#include <cc65.h>
+#ifndef __CBM__
 #include <fcntl.h>
 #include <unistd.h>
+#else
+#include <cbm.h>
+#include <device.h>
+#endif
 
 
 extern void _OVERLAY1_LOAD__[], _OVERLAY1_SIZE__[];
@@ -19,9 +26,9 @@ 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.
- */
+** 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);
@@ -29,18 +36,18 @@ void log (char *msg)
 
 
 /* In a real-world overlay program one would probably not use a #pragma but
- * rather place the all the code of certain source files into the overlay by
- * compiling them with --code-name OVERLAY1.
- */
+** 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.
-     */
+    ** 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");
 }
 
@@ -69,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;
 }
 
@@ -86,23 +105,23 @@ void main (void)
     log ("Calling overlay 1 from main");
 
     /* 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.
-     */
+    ** 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.
-         */
+        ** 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");
 
     /* Replacing one overlay with another one can only happen from the main
-     * program. This implies that an overlay can never load another overlay.
-     */
+    ** program. This implies that an overlay can never load another overlay.
+    */
     if (loadfile ("ovrldemo.2", _OVERLAY2_LOAD__, _OVERLAY2_SIZE__)) {
         bar ();
     }
@@ -112,5 +131,7 @@ void main (void)
         foobar ();
     }
 
-    cgetc ();
+    if (doesclrscrafterexit ()) {
+        getchar ();
+    }
 }