Use cbm_load() to load overlays on CBMs.

This commit is contained in:
Oliver Schmidt
2013-05-29 01:12:24 +02:00
parent 17776739e2
commit 7572834ebf
5 changed files with 161 additions and 73 deletions

View File

@@ -8,13 +8,17 @@
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <em.h>
#ifndef __CBM__
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <em.h>
#include <conio.h>
#else
#include <device.h>
#endif
/* The symbols _OVERLAY?_LOAD__ and _OVERLAY?_SIZE__ were generated by the
@@ -24,7 +28,6 @@
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;
@@ -34,14 +37,7 @@ struct {
} 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__}};
/* 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
* use in allocating extended memory for it.
*/
#define MAX_EM_OVERLAY 3
{"multdemo.3", -1, _OVERLAY3_LOAD__, (unsigned)_OVERLAY3_SIZE__}};
@@ -94,8 +90,6 @@ void foobar (void)
#pragma code-name(pop);
#pragma code-name (push, "OVERLAY4");
unsigned char loademdriver (void)
{
DIR *dir;
@@ -148,59 +142,12 @@ unsigned char loademdriver (void)
return 0;
}
void copyoverlays (void)
{
unsigned page = 0;
unsigned char num;
for (num = 0; num < MAX_EM_OVERLAY; ++num) {
int file;
int size;
if ((overlay[num].size + EM_PAGE_SIZE - 1) / EM_PAGE_SIZE >
em_pagecount () - page) {
printf ("Dbg: Not enough memory for overlay %u\n", num + 1);
continue;
}
printf ("Dbg: Reading overlay file %s\n", overlay[num].name);
file = open (overlay[num].name, O_RDONLY);
if (file == -1) {
log ("Opening overlay file failed");
continue;
}
overlay[num].page = page;
size = overlay[num].size;
while (size) {
void *buf;
/* In general one could as well use em_copyto() to copy a fully
* loaded overlay into extended memory in one step. However the
* "streaming" of an overlay from disk to extended memory shown
* here has two advantages:
* - It can be done from another overlay (like done here).
* - It avoids unnecessary double buffering with emdrivers that
* provide a hardware memory window.
*/
buf = em_use (page++);
size -= read (file, buf, EM_PAGE_SIZE);
em_commit ();
}
printf ("Dbg: Stored overlay %u in pages %u-%u\n",
num + 1, overlay[num].page, page - 1);
close (file);
}
}
#pragma code-name(pop);
unsigned char loadoverlay (unsigned char num)
{
if (overlay[num - 1].page < 0) {
#ifndef __CBM__
int file;
printf ("Dbg: Loading overlay %u from file\n", num);
@@ -212,6 +159,15 @@ unsigned char loadoverlay (unsigned char num)
read (file, overlay[num - 1].addr,
overlay[num - 1].size);
close (file);
#else
if (cbm_load (overlay[num - 1].name, getcurrentdevice (), NULL) == 0) {
log ("Loading overlay file failed");
return 0;
}
#endif
return 1;
} else {
struct em_copy copyinfo;
@@ -226,16 +182,47 @@ unsigned char loadoverlay (unsigned char num)
}
}
void copyoverlays (void)
{
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;
}
if (loadoverlay (num + 1) == 0)
continue;
copyinfo.offs = 0;
copyinfo.page = page;
copyinfo.buf = overlay[num].addr;
copyinfo.count = overlay[num].size;
em_copyto (&copyinfo);
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)
{
if (loadoverlay (4)) {
log ("Loading extended memory driver");
if (loademdriver ()) {
log ("Copying overlays into ext. memory");
copyoverlays ();
} else {
log ("No extended memory driver found");
}
log ("Loading extended memory driver");
if (loademdriver ()) {
log ("Copying overlays into ext. memory");
copyoverlays ();
} else {
log ("No extended memory driver found");
}
log ("Press any key...");