Support VCC Plus builds with verified disk and MCP screenshots

This commit is contained in:
Brian Holman committed 2026-11-09 23:19:55 -07:00
1 parent 5a725e2a5b
commit 42bf10fb87
12 files changed
+788 -19

No files matched your search

+235
View File
@@ -0,0 +1,235 @@
/* runDOSLoader.c - Moves and executes a loader program.
By Pierre Sarrazin <http://sarrazip.com/>
This file is in the public domain.
THERE IS NO WARRANTY AS TO THE RELIABILITY OF THIS LIBRARY.
Users are advised to make BACKUPS of any file that might come into
contact with this library.
*/
#include "runDOSLoader.h"
#include <dskcon-standalone.h>
enum
{
NUM_GRANULES = 68, // assumes a 35-track floppy disk
SECTORS_PER_GRANULE = 9, // as per Disk Basic file system
LAST_DIR_SECTOR = 18, // as per Disk Basic file system
};
// Global structure used to pass parameters to postMoveLoaderPhase().
// Parameters cannot be pushed on the stack to postMoveLoaderPhase(),
// since the stack gets moved to high memory in the process of moving
// the loader to lower memory.
//
struct PostMoveLoaderParams
{
char executableFilename[11]; // 11-character padded filename, e.g., "FOOBAR APP"
const void *clientProgramStart; // where to load the actual application
void *clientProgramStackBottom; // where to reset the stack bottom just before jumping to client program
void (*loadStartCallback)(size_t numSteps); // called once when the client program is about to get loaded; may be null
void (*progressCallback)(void); // called at each "step" while the client program is getting loaded; may be null
} postMoveLoaderParams;
static BOOL
readSector(byte track, byte sector, void *dest)
{
DCOPC = 2; // read
DCDRV = 0;
DCTRK = track;
DCSEC = sector;
DCBPT = (byte *) dest;
dskcon_processSector();
return DCSTA == 0;
}
// Returns number of sectors.
//
static word
computeFileLength(const byte fat[NUM_GRANULES], byte firstGranuleNo)
{
word numSectors = 0;
do
{
firstGranuleNo = fat[firstGranuleNo];
numSectors += (firstGranuleNo >= 0xC0 ? (firstGranuleNo & 0x0F) : SECTORS_PER_GRANULE);
} while (firstGranuleNo < 0xC0); // while the current granule has a successor
return numSectors;
}
// Uses postMoveLoaderParams.
// granuleNo: First granule of the file to be loaded (0..67).
// fatSectorBuffer: Buffer in which to load the FAT sector.
// Can be discarded after this call.
//
static BOOL
loadFile(byte granuleNo, byte fatSectorBuffer[256])
{
if (!readSector(17, 2, fatSectorBuffer))
return FALSE;
word numSectors = computeFileLength(fatSectorBuffer, granuleNo);
// Print a char for each sector to load. Will serve as a progress bar.
if (postMoveLoaderParams.loadStartCallback)
(*postMoveLoaderParams.loadStartCallback)(numSectors);
for (byte *dest = (byte *) postMoveLoaderParams.clientProgramStart; ; )
{
byte g = fatSectorBuffer[granuleNo];
byte numSectorsToLoad = (g >= 0xC0 ? (g & 0x0F) : SECTORS_PER_GRANULE);
// Tracks 0..16 contain granules 0..33.
// Tracks 18- contain granules 34-.
byte track = (granuleNo >> 1) + (granuleNo <= 33 ? 0 : 1);
byte sec = ((granuleNo & 1) ? 10 : 1); // even granules in sectors 1..9, odd ones in 10..18
for (byte i = 1; i <= numSectorsToLoad; ++i, ++sec, dest += 256)
{
if (!readSector(track, sec, dest))
return FALSE;
if (postMoveLoaderParams.progressCallback)
(*postMoveLoaderParams.progressCallback)();
}
if (g >= 0xC0) // if last granule
break;
granuleNo = g; // loop to read next granule
}
return TRUE;
}
// Must be called upon IRQ during disk operations.
//
static interrupt asm void
irqISR(void)
{
asm
{
tst $FF03
bpl @done
; 60 Hz interrupt.
ldb $FF02 ; Reset PIA0, port B interrupt flag.
lbsr dskcon_irqService
@done
}
}
// Function that gets called to continue the loading phase once the loader
// has been moved to lower memory.
// Interrupts are assumed to be masked upon entry to this function.
// Uses postMoveLoaderParams.
// Installs irqISR() as the IRQ handler.
// Installs dskcon_nmiService() as the NMI handler.
//
static byte
postMoveLoaderPhase(void)
{
asm // Point IRQ to new addres of irqISR(), while interrupts are masked.
{
leax irqISR ; PCR mode used here, so this loads the post-move address of irqISR()
tfr x,d
ldx $FFF8 ; IRQ vector
std 1,x ; store address argument of JMP
ldb #$7E ; JMP
stb ,x
}
dskcon_init(dskcon_nmiService);
enableInterrupts();
// Scan dir sectors and load postMoveLoaderParams.executableFilename.
byte sectorBuffer[256];
for (byte sec = 3; sec <= LAST_DIR_SECTOR; ++sec)
{
if (!readSector(17, sec, sectorBuffer))
return 1; // program not found
for (byte i = 0; i < 8; ++i) // for each entry of the sector
{
const byte *entry = sectorBuffer + ((word) i << 5);
if (entry[0] == 0xFF) // if end of dir
{
sec = LAST_DIR_SECTOR; // stop scanning
break;
}
if (entry[0] != 0x00 && memcmp(entry, postMoveLoaderParams.executableFilename, 11) == 0)
{
// Load the file. Pass the file's 1st granule no.
// Pass the sector buffer so that it can be reused to load the FAT sector into it.
//
if (!loadFile(entry[13], sectorBuffer))
return 2; // failed to load client program
disableInterrupts();
asm
{
; Reset stack to reuse space used by this function.
lds :postMoveLoaderParams.clientProgramStackBottom
jmp [:postMoveLoaderParams.clientProgramStart]
}
}
}
}
return 1; // program not found
}
byte
decbfile_runDOSLoader(void *loaderDestination,
const void *loaderSource,
void *newStackBottom,
const char *executableFilename,
void *clientProgramStart,
void (*loadStartCallback)(size_t numSteps),
void (*progressCallback)(void))
{
// Pass parameters to postMoveLoaderPhase() (before moving the code and globals
// to lower/higher memory).
memcpy(postMoveLoaderParams.executableFilename, executableFilename, 11);
postMoveLoaderParams.clientProgramStart = clientProgramStart;
postMoveLoaderParams.clientProgramStackBottom = newStackBottom;
// Compute the new address of postMoveLoaderPhase().
word delta = (word) loaderDestination - (word) loaderSource;
byte (*postMoveFunction)(void) = postMoveLoaderPhase + delta;
// Compute and pass the new addresses of the callback functions.
postMoveLoaderParams.loadStartCallback = loadStartCallback + delta;
postMoveLoaderParams.progressCallback = progressCallback + delta;
// Mask the interrupts now because the following memcpy() may put code
// in a region where Color Basic's IRQ routine may write (e.g., $0985,
// which is Disk Basic's RDYTMR variable, i.e., the motor turn-off timer).
disableInterrupts();
// Copy track 34 from (typically) the address where the DOS command loaded it
// to loaderDestination.
// N.B.: postMoveLoaderParams must have been fully filled at this point.
memcpy(loaderDestination, loaderSource, 18 * 256);
byte errorCode;
asm
{
ldx :postMoveFunction ; new address of postMoveLoaderPhase(); this instruction relies on U as stack frame ptr
clr $FFDF ; put CoCo in all RAM mode (already the case on a CoCo 3)
lds :newStackBottom ; stack space now at end of RAM; this instruction also relies on U
jsr ,x ; execute postMoveLoaderPhase() at its new address, with interrupts still masked
stb :errorCode ; JSR returns upon error, so store error code [relies on U to reach 'errorCode']
}
return errorCode;
}
+61
View File
@@ -0,0 +1,61 @@
/* runDOSLoader.h - Moves and executes a loader program.
By Pierre Sarrazin <http://sarrazip.com/>
This file is in the public domain.
THERE IS NO WARRANTY AS TO THE RELIABILITY OF THIS LIBRARY.
Users are advised to make BACKUPS of any file that might come into
contact with this library.
*/
#ifndef _H_RUNDOSLOADER
#define _H_RUNDOSLOADER
#include <coco.h>
// Moves and executes a loader program that has just been loaded, typically by DECB's DOS command.
//
// loaderDestination: Address where the loader program must be moved, then executed.
// There must be 4608 bytes free at that location.
// That region must not overlap the 4608-byte region at 'loaderSource',
// which is where the track is moved from.
// Typically, a suitable location in low memory is 0x0600..0x17FF.
// loaderSource: Address where the loader program currently is. (Typically 0x2600.)
// This region is assumed to be 4608 bytes long.
// newStackBottom: Address where to put the bottom of the stack during the loading phase
// and when jumping to the client program. Typically 0xFEEE on a 64k CoCo.
// executableFilename: 11-character filename (NUL-terminated), e.g., "FOOBAR APP".
// Must be in drive 0.
// Must contain an executable in raw format, NOT in DECB's BIN format.
// Normally, that executable should have been compiled with CMOC's --raw option.
// clientProgramStart: Address where the client program (the actual app) must be loaded.
// The buffer where the app is loaded must not overlap the 4608-byte region
// at 'loaderSource'. A typical location is 0x1800.
// N.B.: The stack gets relocated just below $FEEE before jumping to this app.
// loadStartCallback: Function that is called when 'executableFilename' has been found in the directory.
// It receives the number of "steps" in the loading phase.
// This information can be used to display a progress indicator.
// Allowed to be null.
// progressCallback: Function that is called at each "step" of the loading phase.
// Will be called a number of times equal to the number of steps passed to loadStartCallback.
// Allowed to be null.
//
// Does not return upon success; instead jumps to clientProgramStart, with interrupts masked.
//
// Upon error, returns an error code, with interrupts unmasked:
// 1: The program specified by 'executableFilename' was not found (in drive 0).
// 2: The program was found but an error happened when trying to load it.
// At this point, the IRQ service routine will be private to this function.
// If the caller wants to use the IRQ in some way, it must install its own IRQ routine.
//
byte decbfile_runDOSLoader(void *loaderDestination,
const void *loaderSource,
void *newStackBottom,
const char *executableFilename,
void *clientProgramStart,
void (*loadStartCallback)(size_t numSteps),
void (*progressCallback)(void));
#endif /* _H_RUNDOSLOADER */