Files

234 lines
5.7 KiB
C

/* loader.c - DOS-loaded tool that loads a program in CoCo memory and executes it.
By Pierre Sarrazin <http://sarrazip.com/>
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.
*/
#include "runDOSLoader.h" /* decbfile */
enum
{
VIDRAM = 0x0400, // address of 32x16 screen
SCREEN_SIZE_IN_BYTES = 512,
RAM_END = 0xFEEE, // first byte NOT part of usable RAM (interrupt vectors on CoCo 3)
};
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+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+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('+');
}
int
main()
{
width(32); // uses Color Basic routine
cls(255); // uses Color Basic routine
rgb();
printStr("ACCESSING DRIVE 0...\n");
byte errorCode = 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);
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;
}