]> git.sur5r.net Git - cc65/blobdiff - samples/multidemo.c
Reorder CF_xxx flags so that they can be used as table index.
[cc65] / samples / multidemo.c
index bdf669cb049d3fa71215abb6514f80c410933406..e44a34b969fe2d968ca131a817690373387b5de5 100644 (file)
@@ -12,6 +12,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <stdlib.h>
 #include <em.h>
 #include <conio.h>
 
  * linker. They contain the overlay area address and size specific to a
  * certain program.
  */
-extern void _OVERLAY1_LOAD__, _OVERLAY1_SIZE__;
-extern void _OVERLAY2_LOAD__, _OVERLAY2_SIZE__;
-extern void _OVERLAY3_LOAD__, _OVERLAY3_SIZE__;
-extern void _OVERLAY4_LOAD__, _OVERLAY4_SIZE__;
+extern void _OVERLAY1_LOAD__[], _OVERLAY1_SIZE__[];
+extern void _OVERLAY2_LOAD__[], _OVERLAY2_SIZE__[];
+extern void _OVERLAY3_LOAD__[], _OVERLAY3_SIZE__[];
+extern void _OVERLAY4_LOAD__[], _OVERLAY4_SIZE__[];
 
 struct {
     char     *name;
@@ -31,10 +32,10 @@ struct {
     void     *addr;
     unsigned size;
 } overlay[] =
-    {{"multdemo.1", -1, &_OVERLAY1_LOAD__, (unsigned)&_OVERLAY1_SIZE__},
-     {"multdemo.2", -1, &_OVERLAY2_LOAD__, (unsigned)&_OVERLAY2_SIZE__},
-     {"multdemo.3", -1, &_OVERLAY3_LOAD__, (unsigned)&_OVERLAY3_SIZE__},
-     {"multdemo.4", -1, &_OVERLAY4_LOAD__, (unsigned)&_OVERLAY4_SIZE__}};
+    {{"multdemo.1", -1, _OVERLAY1_LOAD__, (unsigned)_OVERLAY1_SIZE__},
+     {"multdemo.2", -1, _OVERLAY2_LOAD__, (unsigned)_OVERLAY2_SIZE__},
+     {"multdemo.3", -1, _OVERLAY3_LOAD__, (unsigned)_OVERLAY3_SIZE__},
+     {"multdemo.4", -1, _OVERLAY4_LOAD__, (unsigned)_OVERLAY4_SIZE__}};
 
 /* Copy overlays into extended memory up to overlay 3. Overlay 4 is known to
  * to be loaded only once for onetime initialization purposes so there's no
@@ -42,10 +43,6 @@ struct {
  */
 #define MAX_EM_OVERLAY 3
 
-/* Search for up to 10 extended memory drivers.
- */
-#define MAX_EM_DRIVER 10
-
 
 
 /* Functions resident in an overlay can call back functions resident in the
@@ -101,9 +98,9 @@ void foobar (void)
 
 unsigned char loademdriver (void)
 {
-    static char emd[MAX_EM_DRIVER][FILENAME_MAX];
-    DIR* dir;
-    struct dirent* ent;
+    DIR *dir;
+    struct dirent *ent;
+    char *emd = NULL;
     unsigned char max = 0;
     unsigned char num;
 
@@ -128,22 +125,26 @@ unsigned char loademdriver (void)
         }
 
         printf ("Dbg: Memorizing file %s\n", ent->d_name);
-        strcpy (emd[max], ent->d_name);
-        if (++max == MAX_EM_DRIVER) {
-            break;
-        }
+        emd = realloc (emd, FILENAME_MAX * (max + 1));
+        strcpy (emd + FILENAME_MAX * max++, ent->d_name);
     }
     closedir (dir);
 
     for (num = 0; num < max; ++num) {
-        printf ("Dbg: Trying emdriver %s\n", emd[num]);
-        if (em_load_driver (emd[num]) == EM_ERR_OK) {
-            printf ("Dbg: Loaded emdriver %s\n", emd[num]);
+        char *drv;
+
+        drv = emd + FILENAME_MAX * num;
+        printf ("Dbg: Trying emdriver %s\n", drv);
+        if (em_load_driver (drv) == EM_ERR_OK) {
+            printf ("Dbg: Loaded emdriver %s\n", drv);
+            free (emd);
             return 1;
         }
-        printf ("Dbg: Emdriver %s failed\n", emd[num]);
+
+        printf ("Dbg: Emdriver %s failed\n", drv);
     }
+
+    free (emd);
     return 0;
 }