2012-06-03 16:16:11 +00:00
|
|
|
/*
|
|
|
|
* Minimalistic overlay demo program.
|
|
|
|
*
|
2014-04-28 19:49:38 +00:00
|
|
|
* Shows how to load overlay files from disk.
|
|
|
|
*
|
2012-06-03 16:16:11 +00:00
|
|
|
* 2009-10-02, Oliver Schmidt (ol.sc@web.de)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-06-24 21:15:39 +00:00
|
|
|
#include <conio.h>
|
2013-05-28 23:12:24 +00:00
|
|
|
#ifndef __CBM__
|
2012-06-03 16:16:11 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2013-05-28 23:12:24 +00:00
|
|
|
#else
|
|
|
|
#include <device.h>
|
|
|
|
#endif
|
2012-06-03 16:16:11 +00:00
|
|
|
|
|
|
|
|
2012-11-04 20:23:50 +00:00
|
|
|
extern void _OVERLAY1_LOAD__[], _OVERLAY1_SIZE__[];
|
|
|
|
extern void _OVERLAY2_LOAD__[], _OVERLAY2_SIZE__[];
|
|
|
|
extern void _OVERLAY3_LOAD__[], _OVERLAY3_SIZE__[];
|
2012-06-03 16:16:11 +00:00
|
|
|
|
|
|
|
|
2012-07-19 12:25:04 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2012-06-03 16:16:11 +00:00
|
|
|
void log (char *msg)
|
|
|
|
{
|
|
|
|
printf ("Log: %s\n", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-19 12:25:04 +00:00
|
|
|
/* In a real-world overlay program one would probably not use a #pragma but
|
2013-06-21 10:36:37 +00:00
|
|
|
* rather place all the code of certain source files into the overlay by
|
2012-07-19 12:25:04 +00:00
|
|
|
* compiling them with --code-name OVERLAY1.
|
|
|
|
*/
|
2012-06-03 16:16:11 +00:00
|
|
|
#pragma code-name (push, "OVERLAY1");
|
|
|
|
|
|
|
|
void foo (void)
|
|
|
|
{
|
2012-07-19 12:25:04 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2012-06-03 16:16:11 +00:00
|
|
|
log ("Calling main from overlay 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma code-name (pop);
|
|
|
|
|
|
|
|
|
|
|
|
#pragma code-name (push, "OVERLAY2");
|
|
|
|
|
|
|
|
void bar (void)
|
|
|
|
{
|
|
|
|
log ("Calling main from overlay 2");
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma code-name (pop);
|
|
|
|
|
|
|
|
|
|
|
|
#pragma code-name (push, "OVERLAY3");
|
|
|
|
|
|
|
|
void foobar (void)
|
|
|
|
{
|
|
|
|
log ("Calling main from overlay 3");
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma code-name(pop);
|
|
|
|
|
|
|
|
|
|
|
|
unsigned char loadfile (char *name, void *addr, void *size)
|
|
|
|
{
|
2013-05-28 23:12:24 +00:00
|
|
|
#ifndef __CBM__
|
|
|
|
|
2012-06-03 16:16:11 +00:00
|
|
|
int file = open (name, O_RDONLY);
|
|
|
|
if (file == -1) {
|
|
|
|
log ("Opening overlay file failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
read (file, addr, (unsigned) size);
|
|
|
|
close (file);
|
2013-05-28 23:12:24 +00:00
|
|
|
|
|
|
|
#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
|
2012-06-03 16:16:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void main (void)
|
|
|
|
{
|
|
|
|
log ("Calling overlay 1 from main");
|
2012-07-19 12:25:04 +00:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2012-11-04 20:23:50 +00:00
|
|
|
if (loadfile ("ovrldemo.1", _OVERLAY1_LOAD__, _OVERLAY1_SIZE__)) {
|
2012-07-19 12:25:04 +00:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2012-06-03 16:16:11 +00:00
|
|
|
foo ();
|
|
|
|
}
|
|
|
|
|
|
|
|
log ("Calling overlay 2 from main");
|
2012-07-19 12:25:04 +00:00
|
|
|
|
|
|
|
/* Replacing one overlay with another one can only happen from the main
|
|
|
|
* program. This implies that an overlay can never load another overlay.
|
|
|
|
*/
|
2012-11-04 20:23:50 +00:00
|
|
|
if (loadfile ("ovrldemo.2", _OVERLAY2_LOAD__, _OVERLAY2_SIZE__)) {
|
2012-06-03 16:16:11 +00:00
|
|
|
bar ();
|
|
|
|
}
|
|
|
|
|
|
|
|
log ("Calling overlay 3 from main");
|
2012-11-04 20:23:50 +00:00
|
|
|
if (loadfile ("ovrldemo.3", _OVERLAY3_LOAD__, _OVERLAY3_SIZE__)) {
|
2012-06-03 16:16:11 +00:00
|
|
|
foobar ();
|
|
|
|
}
|
2012-06-24 21:15:39 +00:00
|
|
|
|
|
|
|
cgetc ();
|
2012-06-03 16:16:11 +00:00
|
|
|
}
|