/* loader.c - DOS-loaded tool that loads a program in CoCo memory and executes it.
By Pierre Sarrazin
This file is in the public domain.
This loader calls decbfile's decbfile_runDOSLoader() to load EXECUTABLE_FILENAME
from drive 0 to address CLIENT_PROGRAM_START, then jumps to that address.
See decbfile_runDOSLoader() for details.
*/
#ifdef _COCO_BASIC_
#include "runDOSLoader.h" /* decbfile */
#else
#include
#endif
#include "constants.h"
byte *textScreenCursor = VIDRAM;
// 32x16 character output routine.
// Called with a ASCII code in A. Can be used by printf(), putchar() and putstr().
// A newline is expected to be represented in A with character 13.
// Does not support other control characters, inclding backspace.
// Does not scroll the screen when the writing position reaches the end.
// Preserves U, X and B. Trashes A.
//
static asm __norts__ void
consoleOutHook(void)
{
asm
{
pshs x ; preserve registers used by this routine, except A
cmpa #13
beq @newline
; Convert A from ASCII to 6847.
cmpa #$20
blo @converted
cmpa #$40
blo @add0x40
cmpa #$60
blo @converted
cmpa #$80
bhs @converted ; graphics code
suba #$60 ; 0x60..0x7F: lower-case letters
bra @converted
@add0x40
adda #$40
@converted
sta [:textScreenCursor]
; Advance cursor.
ldx :textScreenCursor
cmpx #VIDRAM+TEXT_SCREEN_SIZE_IN_BYTES-1
bhs @noAdvance
leax 1,x
stx :textScreenCursor
@noAdvance
puls x,pc
;
@newline
pshs b
ldb :textScreenCursor[1] ; LSB of pointer
andb #$1F ; modulo 32
subb #32
negb ; number of spaces to write
beq @newlineDone
lda #$60 ; space char on 6847
ldx :textScreenCursor
@newlineLoop
sta ,x+
decb
bne @newlineLoop
cmpx #VIDRAM+TEXT_SCREEN_SIZE_IN_BYTES ; beyond end of screen?
blo @storeX
leax -32,x ; back to start of last row
@storeX
stx :textScreenCursor
@newlineDone
puls b,x,pc
}
}
asm __norts__ void
printChar(char c)
{
asm
{
lda 3,s ; c
lbra consoleOutHook
}
}
// str: C string.
//
void
printStr(const char *str)
{
asm
{
ldx :str
bra @cond
@loop
cmpa #10
bne @putchar
lda #13
@putchar
lbsr consoleOutHook
@cond
lda ,x+
bne @loop
}
}
// Calls printStr() to print an alternating sequence of strings and integers
// of type 'unsigned'.
// The first argument must be the number of integers passed (NOT the total
// number of arguments).
// The last argument must be a character pointer.
// The integers are printed in decimal.
// Example: printStrUInts(2, "GOT ", x, " APPLE(S) AND ", y, " ORANGE(S).");
//
void
printStrUInts(byte numIntegers, const char *firstString, ...)
{
va_list ap;
va_start(ap, firstString);
printStr(firstString);
for (byte i = numIntegers; i--; )
{
unsigned n = va_arg(ap, unsigned);
const char *s = va_arg(ap, const char *);
char temp[11];
utoa(n, temp, 10);
printStr(temp);
printStr(s);
}
va_end(ap);
}
void
printStrNoTrailingSpaces(const char *src, byte size)
{
while (size > 0 && src[size - 1] == ' ')
--size;
while (size--)
printChar(*src++);
}
#ifndef CLIENT_PROGRAM_START /* where this loader loads the .BIN file (see doc) */
#error Makefile must define CLIENT_PROGRAM_START
#endif
#ifndef EXECUTABLE_FILENAME
#error EXECUTABLE_FILENAME not defined
#endif
static void
printExecutableFilename(void)
{
printStrNoTrailingSpaces(EXECUTABLE_FILENAME, 8);
printStr(".");
printStrNoTrailingSpaces(EXECUTABLE_FILENAME + 8, 3);
}
static void
loadStartCallback(size_t numSteps)
{
printStr("LOADING ");
printExecutableFilename();
printStr("...\n");
// Print a char for each sector to load. Will serve as a progress indicator.
byte *cursorBeforeProgressBar = textScreenCursor;
for ( ; numSteps--; )
printChar('.');
textScreenCursor = cursorBeforeProgressBar; // back to start of progress bar
}
static void
progressCallback(void)
{
printChar('+');
}
enum
{
BLK = COCO3_FIRST_CODE_MMU_BLOCK, // abbreviation
};
// The stack is assumed to be at or below $03D7, the LINE INPUT buffer.
// On a CoCo 3, that allows this function to tune in alternate MMU blocks
// at $6000..$7FFF, where register S pointed into when main() was jumped into
// by the DOS command.
//
// Interrupts are assumed to be masked.
//
int
mainWithNewStackSpace(void)
{
width(32); // uses Color Basic routine
cls(255); // uses Color Basic routine
rgb();
// On a CoCo 3, tune in alternate MMU blocks from $4000 to $FFEF.
// (This does nothing on a CoCo 1 or 2.)
// decbfile_runDOSLoader() will load the .app file into blocks 48..53.
// Without this, the code would be loaded to blocks 58..63, and when
// the user will press Reset, SECB will copy itself from ROM to those
// pages, thereby overwriting and corrupting the .app code.
//
// Instead, we load the code to alternate pages, and the warm start
// routine in warmStartRoutine[] (see coco-planes.c) will tune in
// those pages when Reset is pressed.
//
* (word *) 0xFFA2 = ((BLK + 0) << 8) | (BLK + 1);
* (word *) 0xFFA4 = ((BLK + 2) << 8) | (BLK + 3);
* (word *) 0xFFA6 = ((BLK + 4) << 8) | (BLK + 5);
printStr("ACCESSING DRIVE 0...\n");
byte errorCode =
#ifdef _COCO_BASIC_
decbfile_runDOSLoader( // this call puts CoCo in all-RAM mode
(void *) 0x0600, // DECB work space, right after 32x16 screen
(void *) 0x2600, // track 34 loaded by DOS command
RAM_END, // new stack bottom
EXECUTABLE_FILENAME, // client program binary file to be loaded
CLIENT_PROGRAM_START, // client program entry point
loadStartCallback,
progressCallback);
#else
3;
#endif
switch (errorCode)
{
case 1:
printStr("\n""PROGRAM NOT FOUND:\n");
printExecutableFilename();
break;
case 2:
printStr("\n""FAILED TO LOAD PROGRAM:\n");
printExecutableFilename();
break;
default: // unexpected error code
printStr("\n???");
}
for (;;) // freeze
;
return 0;
}
int main()
{
disableInterrupts();
asm
{
lds #$03D7 ; use top of LINE INPUT buffer as temp stack
}
return mainWithNewStackSpace();
}