*************************************************************************
*                                                                       *
*               ABSOFT CORPORATION FORTRAN 77 COMPILER                  *
*                                                                       *
*                       MAIN PROGRAM START UP CODE                      *
*                               FOR                                     *
*                       AMIGADOS BASED SYSTEMS                          *
*                                                                       *
*                       Copyright (C) 1986                              *
*               Absoft Corporation, Royal Oak, MI  48072                *
*                                                                       *
*                                                                       *
*       This code segment is the main program start up procedure. If    *
*       it is modified, it must be assembled and linked.  The           *
*       resulting linker output file must be named "init.sc". The       *
*       compiler will first search the local directory, then ":l/",     *
*       and finally the "sys:l/" directory for a copy of the file.      *
*                                                                       *
*       The file is also used in source form as an INCLUDE file when    *
*       assembly language source is being generated by the compiler     *
*       for a main program.                                             *
*                                                                       *
*       The routine performs the following functions:                   *
*                                                                       *
*               1. allocate a memory segment for the heap               *
*               2. open "dos.library"                                   *
*               3. locate the STDIN and STDOUT file handles             *
*               4. load f77.rl into the heap                            *
*                                                                       *
*       The routine passes the following registers to f77.rl:           *
*                                                                       *
*               D4 - heap size                                          *
*               D5 - command line length                                *
*               A0 - pointer to heap                                    *
*               A2 - pointer to main program                            *
*               A3 - command line pointer                               *
*               A4 - debug or profil command line pointer               *
*               A5 - pointer to f77.rl                                  *
*                                                                       *
*                                                                       *
*************************************************************************
*
* Edit history:
*
*  19 Feb 85    files created                                           PAJ
*

*
* - Selected AmigaDOS equates:
*

* - from "depsys"

AEXEC   EQU     -16                     * "exec_lib" pointer
DOS     EQU     AEXEC-4                 * "dos.library" pointer
STDIN   EQU     DOS-4                   * file handle for STDIN
STDOUT  EQU     STDIN-4                 * file handle for STDOUT
H.BASE  EQU     STDOUT-4                * base of heap
H.SIZE  EQU     H.BASE-4                * size of heap
AMIGA   EQU     -H.SIZE                 * size of Amiga global storage

F77.RL  EQU     16640                   * size of "f77.rl"


* - from "exec_lib"

.ALLOCMEM.      EQU     -198
.FREEMEM.       EQU     -210
.OPENLIB.       EQU     -552

EXEC.LIB        EQU     4               * pointer to "exec_lib"

* - from "dos.library"

.OPEN.          EQU     -30
.CLOSE.         EQU     -36
.READ.          EQU     -42
.WRITE.         EQU     -48
.INPUT.         EQU     -54
.OUTPUT.        EQU     -60
.EXIT.          EQU     -144

OLDFILE         EQU     1005


*
* - the following code is already in the source file:
*
*START: LEA     START(PC),A1            * load address of a handy label
*       MOVEA.L A1,A5
*       ADDA.L  #OFFSET,A1              * add offset to main program
*       ADDA.L  #OFFSET,A5              * add offset to data section
*       MOVE.L  #HEAP,D1                * load default heap size
*

        MOVE.L  D1,D4                   * copy for global use
        MOVEA.L A1,A2                   * copy for global use
        MOVEA.L A0,A3                   * copy command line pointer
        MOVE.L  D0,D5                   * copy command line length

*
* - allocate the heap segment and establish global addresses
*

        MOVE.L  D4,D0                   * module size
        MOVEQ   #0,D1                   * MEMF_FAST first, then MEMF_CHIP
        MOVE.W  #.ALLOCMEM.,D6
        BSR     ECALL
        TST.L   D0                      * did the allocation succeed?
        BEQ.S   ERROR                   *   no
        MOVEA.L D0,A0                   * set pointer to base of heap
        MOVE.L  A0,D3                   * save pointer to "f77.rl" for load
        ADDA.W  #F77.RL,A0              * allocate "f77.rl" area
        ADDA.W  #AMIGA,A0               * allocate global area
        MOVE.L  D0,H.BASE(A0)           * save base of heap for .FREEMEM.
        MOVE.L  D4,H.SIZE(A0)           * save size of heap for .FREEMEM.
        MOVE.L  EXEC.LIB,AEXEC(A0)      * set pointer to "exec_lib"

*
* - open the "dos.library" and locate STDIN and STDOUT
*

        LEA     DOSLIB(PC),A1           * "dos.library"
        MOVEQ   #0,D0                   * any version will do
        MOVE.W  #.OPENLIB.,D6
        BSR     ECALL                   * attempt to open the libary
        MOVE.L  D0,DOS(A0)              * save pointer, error?
        BNE.S   L1                      *   yes
ERROR:  RTS                             *   no, fail silently (UNIX style)

L1:     MOVEQ   #.INPUT.,D6
        BSR     DCALL
        MOVE.L  D0,STDIN(A0)
        MOVEQ   #.OUTPUT.,D6
        BSR     DCALL
        MOVE.L  D0,STDOUT(A0)

*
* - locate and load "f77.rl"
*

        LEA     F77LIB+6(PC),A6
        MOVE.L  A6,D1                   * "name"
        MOVE.L  #OLDFILE,D2             * "accessMode"
        MOVEQ   #.OPEN.,D6
        BSR     DCALL
        MOVE.L  D0,D7                   * found in local library?
        BNE.S   L2                      *   yes
        SUBQ.L  #3,D1
        BSR     DCALL
        MOVE.L  D0,D7                   * found in current volume library?
        BNE.S   L2                      *   yes
        SUBQ.L  #3,D1
        BSR     DCALL
        MOVE.L  D0,D7                   * found in system volume library?
        BEQ.S   L3                      *   no
L2:     MOVEA.L D3,A5
        MOVE.L  D7,D1                   * "file"
        MOVE.L  A5,D2                   * "buffer"
        MOVE.L  #F77.RL,D3              * "length"
        MOVEQ   #.READ.,D6
        BSR     DCALL                   * load the library
        TST.L   D0                      * error?
        BMI.S   L3                      *   yes
        MOVE.L  D7,D1                   * "file"
        MOVEQ   #.CLOSE.,D6
        BSR     DCALL                   * close the file
        JMP     (A5)                    * transfer control to "f77.rl"

*
* - "f77.rl" load failed: report error and quit
*

L3:     MOVE.L  STDOUT(A0),D1           * "file"
        LEA     F77LIB+6(PC),A6
        MOVE.L  A6,D2                   * "buffer"
        MOVEQ   #6,D3                   * "length"
        MOVEQ   #.WRITE.,D6
        BSR.S   DCALL
        MOVE.L  STDOUT(A0),D1           * "file"
        LEA     ERRMSG(PC),A6
        MOVE.L  A6,D2                   * "buffer"
        MOVEQ   #11,D3                  * "length"
        MOVEQ   #.WRITE.,D6
        BSR.S   DCALL
        MOVE.L  DOS(A0),-(A7)           * save pointer to "dos.library"
        MOVEA.L H.BASE(A0),A1           * pointer to module to delete
        MOVE.L  H.SIZE(A0),D0           * size of module to delete
        MOVE.W  #.FREEMEM.,D6
        BSR.S   ECALL
        MOVEA.L (A7)+,A6                * pointer to "dos.library"
        MOVEQ   #10,D1                  * "something wrong"
        JSR     .EXIT.(A6)


*
* - execute "exec_lib" call (offset passed in D6)
*

ECALL:  MOVEM.L D1-D7/A0/A2/A3/A5,-(A7)
        MOVEA.L EXEC.LIB,A6
        JSR     0(A6,D6.W)
        MOVEM.L (A7)+,D1-D7/A0/A2/A3/A5
        RTS

*
* - execute "dos.library" call (offset passed in D6)
*

DCALL:  MOVEM.L D1-D7/A0/A2/A3/A5,-(A7)
        MOVEA.L DOS(A0),A6
        JSR     0(A6,D6.W)
        MOVEM.L (A7)+,D1-D7/A0/A2/A3/A5
        RTS


DOSLIB: DC.B    'dos.library'
        DC.B    0

F77LIB: DC.B    'sys:l/f77.rl'
        DC.B    0

ERRMSG: DC.B    ' not found'
        DC.B    10
