*-Revision Header-**********************************************************
*                                                                          *
*    Project: ECSLaunch                                _____       ____    *
*                                                     /    |\    /|    \   *
*    Version: 1.0                                    /     | \  / |     \  *
*                                                   /      |  \/  |     /  *
*       File: ECSLaunch.s                          /-------|      |-----   *
*                                                 /        |      |     \  *
*     Author: Alastair M. Robinson               /         |      |      \ *
*                                                                          *
****Revision: 0007                                                © - 1995 *
*                                                                          *
****************************************************************************
*            *                                                             *
*    Date    *                          Comment                            *
*            *                                                             *
****************************************************************************
*            *                                                             *
* 17.8.1995  * Tidied up a bit for Prospective P.D. release.               *
*            *                                                             *
*************************************************************-RevisionTail-*

        incdir  "text_include:"

        include "exec/exec_lib.i"
        include "graphics/graphics_lib.i"
        include "graphics/gfxbase.i"
        include "graphics/modeid.i"
        include "dos/dos_lib.i"
        include "intuition/intuition_lib.i"
        include "intuition/intuition.i"
        include "intuition/screens.i"
        include "hardware/custom.i"

main
        move.l  a0,argptr
        move.l  4,a6
        moveq   #37,d0
        lea     gfxname,a1
        jsr     _LVOOpenLibrary(a6)
        move.l  d0,gfxbase
        beq     error_exit

        moveq   #37,d0
        lea     dosname,a1
        jsr     _LVOOpenLibrary(a6)
        move.l  d0,dosbase
        beq     error_exit

        moveq   #37,d0
        lea     intuiname,a1
        jsr     _LVOOpenLibrary(a6)
        move.l  d0,intuitionbase
        beq     error_exit

        bsr     LosePointer             ; Intuition *currently* puts the pointer
                                        ; sprite back in ECS resolution when a
                                        ; custom image is attached to a window.
                                        ; Rather than poke an undocumented AGA
                                        ; register....
        move.l  gfxbase,a6

        jsr     _LVOWaitTOF(a6)         ; Give the pointer stuff a chance to
        jsr     _LVOWaitTOF(a6)         ; complete.

        move.l  gb_ActiView(a6),OldView
        suba.l  a1,a1
        jsr     _LVOLoadView(a6)        ; Get the Amiga OS out of the way.

        jsr     _LVOWaitTOF(a6)
        jsr     _LVOWaitTOF(a6)         ; Wait for the O.S. to sling its hook.

        move.l  #$dff000,a5
        move.w  #0,bpl1mod(a5)          ; The most common cause of skewed displays
        move.w  #0,bpl2mod(a5)          ; is these two not being initialised.

        move.w  #$38,ddfstrt(a5)        ; Any metal bashing software _Should_ set
        move.w  #$d0,ddfstop(a5)        ; these in its startup code.

        move.w  #$2c81,diwstrt(a5)      ; Added in an attempt to get AMOS 1.3
        move.w  #$2cc1,diwstop(a5)      ; and compiled programs running from
                                        ; HardDisk.  Unsuccessful.
        move.l  argptr,d1
        moveq   #0,d2
        moveq   #0,d3
        move.l  dosbase,a6
        jsr     _LVOExecute(a6)         ; Execute whatever was supplied on the
                                        ; command line.
        move.l  gfxbase,a6
        move.l  OldView,a1
        jsr     _LVOLoadView(a6)        ; Restore workbench display (if launched
                                        ; program returns!)
        bsr     CloseLPWindow

        bra     the_end

the_end
        tst.l   intuitionbase
        beq     .nointuition
        move.l  4,a6
        move.l  intuitionbase,a1
        jsr     _LVOCloseLibrary(a6)
.nointuition
        tst.l   dosbase
        beq     .nodos
        move.l  4,a6
        move.l  dosbase,a1
        jsr     _LVOCloseLibrary(a6)
.nodos
        tst.l   gfxbase
        beq     .nogfx
        move.l  4,a6
        move.l  gfxbase,a1
        jsr     _LVOCloseLibrary(a6)
.nogfx
        moveq   #0,d0
        rts

error_exit
        bsr     the_end
        moveq   #100,d0
        rts

dosbase
        dc.l    0
gfxbase
        dc.l    0
intuitionbase
        dc.l    0
OldView
        dc.l    0
argptr
        dc.l    0
gfxname
        dc.b    "graphics.library",0
dosname
        dc.b    "dos.library",0
intuiname
        dc.b    "intuition.library",0
        even
        include "LosePointer.s"

