{$C Copyright (c) 1983 SAGE Computer Technology, All Rights Reserved}

{ Stand Alone Assembly Code Loader

  File:	    ASSEMLOAD.TEXT
  Date:	    12-Aug-83
  Version:  1
  
  
  COPYRIGHT (c) 1983 SAGE Computer Technology
  All Rights Reserved
  
  Development History:
  
  1    12-Aug-83  Initial Release.
 

  Description:

    This program prompts the user for a code file, a target load
    location, and a starting address.  Given the proper information
    the routine turns off the BIOS hooks to the interpreter and
    uses the BIOS function 9 to load and start the assembly code
    program.

    This program must be linked with the assembly routine LOADIT
    (in file ASSEMHOOK) which contains the low level BIOS calls.

}

PROGRAM ASSEM_LOADER;

USES {$U SAGETOOLS.CODE} FILE_INFO,SIO_Unit,
     {$U KERNEL.CODE} Kernel;

VAR
  Found:BOOLEAN;
  Device:INTEGER;
  Block:INTEGER;
  Size:INTEGER;
  Name,S,Result:STRING;
  AddrHigh,AddrLow:INTEGER;
  StartHigh,StartLow:INTEGER;
  Dummy:BOOLEAN;
  Cursor:INTEGER;
  CH:CHAR;
  F:FILE;

PROCEDURE LOADIT(Channel,Size,AddrLow,AddrHigh,Block,
		 StartLow,StartHigh:INTEGER);EXTERNAL;

BEGIN
  WRITELN;
  WRITELN('Loader for Stand Alone Assembly Language Program');
  Found:=FALSE;
  REPEAT
    WRITELN;
    WRITE('Name of assembly code file: ');
    READLN(Name);
    WRITELN;
    IF LENGTH(Name) = 0 THEN EXIT(PROGRAM);
    SIO_Suffix('.CODE',Name);
    {$I-}
    RESET(F,Name);
    {$I+}
    IF IORESULT <> 0 THEN
      WRITELN(CHR(7),'Could not open ',Name)
    ELSE
      Found:=TRUE;
  UNTIL Found;
  
  Device:=F_Unit_Number(F);
  Block:=F_Start(F);
  Size:=F_Length(F);
  { Take care of subsidiary volume possibility }
  WITH Syscom^.unitable^[Device] DO
    BEGIN
      Device:=uphysvol;
      Block:=Block+ublkoff;
    END;
  
  Found:=FALSE;
  REPEAT
    WRITE('Target location (in Hex): ');
    READLN(S);
    WRITELN;
    IF LENGTH(S) = 0 THEN
      BEGIN
	WRITELN('Defaulting to 400H');
	WRITELN;
	AddrLow:=1024;
	AddrHigh:=0;
	Found:=TRUE;
      END
    ELSE
      BEGIN
	Cursor:=1;
	Dummy:=SIO_ByDlim(Cursor,S,' ');
	IF SIO_HexRd(Cursor,S,AddrHigh,AddrLow) THEN Found:=TRUE
	ELSE
	  WRITE(CHR(7));
      END;
  UNTIL Found;
  
  Found:=FALSE;
  REPEAT
    WRITE('Code startup address (in Hex): ');
    READLN(S);
      WRITELN;
      IF LENGTH(S) = 0 THEN
	BEGIN
	  WRITELN('Defaulting to 400H');
	  WRITELN;
	  StartLow:=1024;
	  StartHigh:=0;
	  Found:=TRUE;
	END
      ELSE
	BEGIN
	  Cursor:=1;
	  Dummy:=SIO_ByDlim(Cursor,S,' ');
	  IF SIO_HexRd(Cursor,S,StartHigh,StartLow) THEN Found:=TRUE
	  ELSE
	    WRITE(CHR(7));
	END;
  UNTIL Found;
  
  WRITE('Ready to load: ');
  READ(CH);
  WRITELN;
  WRITELN;
  IF (CH='Y') OR (CH='y') THEN
    BEGIN
      WRITELN('Loading program');
      LOADIT(Device,Size,AddrLow,AddrHigh,Block,StartLow,StartHigh);
      { Should never return if successful }
      WRITELN;
      WRITELN('Load failed');
    END
  ELSE
    WRITELN('Program aborted');
END.

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       