2012-08-08 21:23:18 +00:00
|
|
|
/*
|
2014-06-30 09:10:35 +00:00
|
|
|
** Extended memory overlay demo program.
|
|
|
|
**
|
|
|
|
** Shows how to combine multiple cc65 features
|
|
|
|
** incl. overlays and extended memory drivers.
|
|
|
|
**
|
|
|
|
** 2012-17-07, Oliver Schmidt (ol.sc@web.de)
|
|
|
|
**
|
|
|
|
*/
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2012-10-23 19:24:27 +00:00
|
|
|
#include <stdlib.h>
|
2013-05-28 23:12:24 +00:00
|
|
|
#include <dirent.h>
|
2012-08-08 21:23:18 +00:00
|
|
|
#include <em.h>
|
2016-06-18 21:35:57 +00:00
|
|
|
#include <cc65.h>
|
2013-05-28 23:12:24 +00:00
|
|
|
#ifndef __CBM__
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
2016-06-19 21:38:37 +00:00
|
|
|
#include <cbm.h>
|
2013-05-28 23:12:24 +00:00
|
|
|
#include <device.h>
|
|
|
|
#endif
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* The symbols _OVERLAY?_LOAD__ and _OVERLAY?_SIZE__ were generated by the
|
2014-06-30 09:10:35 +00:00
|
|
|
** linker. They contain the overlay area address and size specific to a
|
|
|
|
** certain program.
|
|
|
|
*/
|
2012-11-04 20:31:23 +00:00
|
|
|
extern void _OVERLAY1_LOAD__[], _OVERLAY1_SIZE__[];
|
|
|
|
extern void _OVERLAY2_LOAD__[], _OVERLAY2_SIZE__[];
|
|
|
|
extern void _OVERLAY3_LOAD__[], _OVERLAY3_SIZE__[];
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
char *name;
|
|
|
|
int page;
|
|
|
|
void *addr;
|
|
|
|
unsigned size;
|
|
|
|
} overlay[] =
|
2012-11-04 20:31:23 +00:00
|
|
|
{{"multdemo.1", -1, _OVERLAY1_LOAD__, (unsigned)_OVERLAY1_SIZE__},
|
|
|
|
{"multdemo.2", -1, _OVERLAY2_LOAD__, (unsigned)_OVERLAY2_SIZE__},
|
2013-05-28 23:12:24 +00:00
|
|
|
{"multdemo.3", -1, _OVERLAY3_LOAD__, (unsigned)_OVERLAY3_SIZE__}};
|
2012-08-08 21:23:18 +00:00
|
|
|
|
2012-09-30 14:58:31 +00:00
|
|
|
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
/* Functions resident in an overlay can call back functions resident in the
|
2014-06-30 09:10:35 +00:00
|
|
|
** main program at any time without any precautions. The function log() is
|
|
|
|
** an example for such a function resident in the main program.
|
|
|
|
*/
|
2012-08-08 21:23:18 +00:00
|
|
|
void log (char *msg)
|
|
|
|
{
|
|
|
|
/* Functions resident in an overlay can access all program variables and
|
2014-06-30 09:10:35 +00:00
|
|
|
** 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.
|
|
|
|
*/
|
2012-08-08 21:23:18 +00:00
|
|
|
printf ("Log: %s\n", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* In a real-world overlay program one would probably not use a #pragma but
|
2014-06-30 09:10:35 +00:00
|
|
|
** rather place all the code of certain source files into the overlay by
|
|
|
|
** compiling them with --code-name OVERLAY1.
|
|
|
|
*/
|
2024-12-08 23:44:44 +00:00
|
|
|
#pragma code-name (push, "OVERLAY1")
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
void foo (void)
|
|
|
|
{
|
|
|
|
log ("Calling main from overlay 1");
|
|
|
|
}
|
|
|
|
|
2024-12-08 23:44:44 +00:00
|
|
|
#pragma code-name (pop)
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
|
2024-12-08 23:44:44 +00:00
|
|
|
#pragma code-name (push, "OVERLAY2")
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
void bar (void)
|
|
|
|
{
|
|
|
|
log ("Calling main from overlay 2");
|
|
|
|
}
|
|
|
|
|
2024-12-08 23:44:44 +00:00
|
|
|
#pragma code-name (pop)
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
|
2024-12-08 23:44:44 +00:00
|
|
|
#pragma code-name (push, "OVERLAY3")
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
void foobar (void)
|
|
|
|
{
|
|
|
|
log ("Calling main from overlay 3");
|
|
|
|
}
|
|
|
|
|
2024-12-08 23:44:44 +00:00
|
|
|
#pragma code-name(pop)
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
unsigned char loademdriver (void)
|
|
|
|
{
|
2012-10-14 19:34:20 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
2012-10-23 19:24:27 +00:00
|
|
|
char *emd = NULL;
|
2012-09-30 14:58:31 +00:00
|
|
|
unsigned char max = 0;
|
|
|
|
unsigned char num;
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
printf ("Dbg: Searching for emdrivers\n");
|
|
|
|
dir = opendir (".");
|
|
|
|
if (!dir) {
|
|
|
|
log ("Opening directory failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ent = readdir (dir)) {
|
|
|
|
char *ext;
|
|
|
|
|
|
|
|
if (!_DE_ISREG (ent->d_type)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext = strrchr (ent->d_name, '.');
|
|
|
|
if (!ext || strcasecmp (ext, ".emd")) {
|
|
|
|
printf ("Dbg: Skipping file %s\n", ent->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-09-30 14:58:31 +00:00
|
|
|
printf ("Dbg: Memorizing file %s\n", ent->d_name);
|
2012-10-23 19:24:27 +00:00
|
|
|
emd = realloc (emd, FILENAME_MAX * (max + 1));
|
|
|
|
strcpy (emd + FILENAME_MAX * max++, ent->d_name);
|
2012-08-08 21:23:18 +00:00
|
|
|
}
|
|
|
|
closedir (dir);
|
2012-09-30 14:58:31 +00:00
|
|
|
|
|
|
|
for (num = 0; num < max; ++num) {
|
2012-10-23 19:24:27 +00:00
|
|
|
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);
|
2012-09-30 14:58:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2012-10-23 19:24:27 +00:00
|
|
|
|
|
|
|
printf ("Dbg: Emdriver %s failed\n", drv);
|
2012-09-30 14:58:31 +00:00
|
|
|
}
|
2012-10-23 19:24:27 +00:00
|
|
|
|
|
|
|
free (emd);
|
2012-09-30 14:58:31 +00:00
|
|
|
return 0;
|
2012-08-08 21:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned char loadoverlay (unsigned char num)
|
|
|
|
{
|
|
|
|
if (overlay[num - 1].page < 0) {
|
2013-05-28 23:12:24 +00:00
|
|
|
#ifndef __CBM__
|
|
|
|
|
2012-08-08 21:23:18 +00:00
|
|
|
int file;
|
|
|
|
|
|
|
|
printf ("Dbg: Loading overlay %u from file\n", num);
|
|
|
|
file = open (overlay[num - 1].name, O_RDONLY);
|
|
|
|
if (file == -1) {
|
|
|
|
log ("Opening overlay file failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
read (file, overlay[num - 1].addr,
|
|
|
|
overlay[num - 1].size);
|
|
|
|
close (file);
|
2013-05-28 23:12:24 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (cbm_load (overlay[num - 1].name, getcurrentdevice (), NULL) == 0) {
|
|
|
|
log ("Loading overlay file failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2012-08-08 21:23:18 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
struct em_copy copyinfo;
|
|
|
|
|
|
|
|
printf ("Dbg: Loading overlay %u from memory\n", num);
|
|
|
|
copyinfo.offs = 0;
|
|
|
|
copyinfo.page = overlay[num - 1].page;
|
|
|
|
copyinfo.buf = overlay[num - 1].addr;
|
|
|
|
copyinfo.count = overlay[num - 1].size;
|
|
|
|
em_copyfrom (©info);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 23:12:24 +00:00
|
|
|
|
|
|
|
void copyoverlays (void)
|
2012-08-08 21:23:18 +00:00
|
|
|
{
|
2013-05-28 23:12:24 +00:00
|
|
|
unsigned page = 0;
|
|
|
|
unsigned char num;
|
|
|
|
|
|
|
|
for (num = 0; num < sizeof (overlay) / sizeof (overlay[0]); ++num) {
|
|
|
|
struct em_copy copyinfo;
|
|
|
|
unsigned size = (overlay[num].size + EM_PAGE_SIZE - 1) / EM_PAGE_SIZE;
|
|
|
|
|
|
|
|
if (size > em_pagecount () - page) {
|
|
|
|
printf ("Dbg: Not enough memory for overlay %u\n", num + 1);
|
|
|
|
continue;
|
2012-08-08 21:23:18 +00:00
|
|
|
}
|
2013-05-28 23:12:24 +00:00
|
|
|
|
|
|
|
if (loadoverlay (num + 1) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
copyinfo.offs = 0;
|
|
|
|
copyinfo.page = page;
|
|
|
|
copyinfo.buf = overlay[num].addr;
|
|
|
|
copyinfo.count = overlay[num].size;
|
|
|
|
em_copyto (©info);
|
|
|
|
|
|
|
|
overlay[num].page = page;
|
|
|
|
page += size;
|
|
|
|
|
|
|
|
printf ("Dbg: Stored overlay %u in pages %u-%u\n",
|
|
|
|
num + 1, overlay[num].page, page - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void main (void)
|
|
|
|
{
|
|
|
|
log ("Loading extended memory driver");
|
|
|
|
if (loademdriver ()) {
|
|
|
|
log ("Copying overlays into ext. memory");
|
|
|
|
copyoverlays ();
|
|
|
|
} else {
|
|
|
|
log ("No extended memory driver found");
|
2012-08-08 21:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log ("Press any key...");
|
2016-06-18 21:35:57 +00:00
|
|
|
getchar ();
|
2012-08-08 21:23:18 +00:00
|
|
|
|
|
|
|
if (loadoverlay (1)) {
|
|
|
|
log ("Calling overlay 1 from main");
|
|
|
|
|
|
|
|
/* The linker makes sure that the call to foo() ends up at the right mem
|
2014-06-30 09:10:35 +00:00
|
|
|
** addr. However it's up to user to make sure that the - right - overlay
|
2022-02-21 20:44:31 +00:00
|
|
|
** is actually loaded before making the call.
|
2014-06-30 09:10:35 +00:00
|
|
|
*/
|
2012-08-08 21:23:18 +00:00
|
|
|
foo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replacing one overlay with another one can only happen from the main
|
2014-06-30 09:10:35 +00:00
|
|
|
** program. This implies that an overlay can never load another overlay.
|
|
|
|
*/
|
2012-08-08 21:23:18 +00:00
|
|
|
if (loadoverlay (2)) {
|
|
|
|
log ("Calling overlay 2 from main");
|
|
|
|
bar ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loadoverlay (3)) {
|
|
|
|
log ("Calling overlay 3 from main");
|
|
|
|
foobar ();
|
|
|
|
}
|
|
|
|
|
2016-06-18 21:35:57 +00:00
|
|
|
if (doesclrscrafterexit ()) {
|
|
|
|
log ("Press any key...");
|
|
|
|
getchar ();
|
|
|
|
}
|
2012-08-08 21:23:18 +00:00
|
|
|
}
|