;----------------------------------------
;              Startup.i
;----------------------------------------
; for DevPac2    by Bruce Abbott  19-1-91
;

 include "amiga.i"      ; all wb1.3 includes

NULL  =  0
FALSE =  0
NO    =  0

TRUE  = -1
YES   = -1

JAM1 = RP_JAM1
JAM2 = RP_JAM2

CALL MACRO Offset
     jsr     _LVO\1(A6)
     ENDM

EXEC MACRO Offset
     move.l  ExecBase(pc),A6
     jsr     _LVO\1(A6)
     ENDM

GFX  MACRO Offset
     move.l  GfxBase(PC),A6
     jsr     _LVO\1(A6)
     ENDM

INT  MACRO Offset
     move.l  IntuiBase(PC),A6
     jsr     _LVO\1(A6)
     ENDM

DOS  MACRO Offset
     move.l  DosBase(PC),A6
     jsr     _LVO\1(A6)
     ENDM

CLOSELIB: MACRO libbaseptr
    move.l   \1(PC),D0
    beq.s    .closed\@
    move.l   D0,A1
    move.l   4,A6
    jsr      _LVOCloselibrary(A6)
.closed\@:
    ENDM



CLOSES MACRO screenptr
   lea     \1,A1
   tst.l   (A1)
   beq.s   .done\@
   move.l  (A1),A0
   clr.l   (A1)
   INT     closescreen
.done\@:
   ENDM

CLOSEW MACRO windowptr
   lea     \1,A1
   tst.l   (A1)
   beq.s   .done\@
   move.l  (A1),A0
   clr.l   (A1)
   INT     CloseWindow
.done\@:
   ENDM


; --- IntuiText Macro ---
ITEXT:  MACRO  x,y,apen,bpen,"text"
 dc.b   \3,\4
 dc.b   JAM2,0
 dc.w   \1,\2
 dc.l   myfont
 dc.l   .text
 dc.l   0
.text:
 dc.b   \5,0
 EVEN
 ENDM




;===============================
;        STARTUP  CODE
;===============================
;
; - start from WorkBench or CLI
;
; - opens Exec,Gfx,Intuition,Dos
;
; - currentdir for CLI and WorkBench
;
; - ErrorCode returned to DOS
;
; - all code PC relative
;
; - start your program with "_main:" , "jmp _exit" to end
;
;

_startup:
 lea     DosBase(PC),A5              ; A5 -> local variables
 move.l  A0,DOSCmdLine-Dosbase(a5)
 move.l  D0,DOSCmdLen-DosBase(a5)   ; keep command line params
 move.l  4.w,A6
 move.l  a6,ExecBase-DosBase(a5)    ; stash execbase in FastRAM
 lea     DosName(PC),A1
 moveq   #0,d0
 CALL    OpenLibrary                ; open DOS lib
 move.l  D0,(A5)
 suba.l  A1,A1                        ; find our task
 CALL    FindTask
 move.l  D0,OurTask-DosBase(A5)
 move.l  D0,A4
 move.l  LN_NAME(A4),TaskName-DosBase(A5)
 tst.l   PR_CLI(A4)                   ; process running from CLI ?
 bne.s   _openlibs
 lea     PR_MsgPort(A4),A0            ; WorkBench startup
 CALL    WaitPort
 lea     PR_MsgPort(A4),A0
 CALL    GetMsg
 move.l  D0,WBenchMsg-DosBase(A5)   ; get Workbench startup msg
 move.l  D0,A0                        ; (BEFORE using DOS functions)
 tst.l   sm_NumArgs(A0)
 beq.s   _openlibs
 move.l  sm_ArgList(A0),A0
 move.l  wa_Lock(A0),D1
 DOS     CurrentDir                   ; cd to workbench drawer
_openlibs:
 lea     GfxName(PC),A1
 moveq   #0,d0
 EXEC    OpenLibrary
 move.l  D0,GfxBase-DosBase(A5)     ; open GFX,Intuition libs
 lea     IntuiName(PC),A1
 moveq   #0,d0
 CALL    OpenLibrary
 move.l  D0,IntuiBase-DosBase(A5)
 move.l  DOSCmdLine(pc),a0           ; get command line params
 move.l  DOSCmdLen(pc),d0
 move.l  A7,Initial_SP-DosBase(A5)  ; remember stack base

 bsr     _main                        ; *** execute our program! ***

_exit:
 move.l  Initial_SP(PC),A7            ; clean up stack
 move.l  D0,-(SP)                     ; push error code
 move.l  ExecBase(pc),A6
 move.l  IntuiBase(PC),A1
 CALL    CloseLibrary
 move.l  GfxBase(PC),A1
 CALL    CloseLibrary                 ; close libraries
 move.l  DosBase(PC),A1
 CALL    CloseLibrary
 move.l  WBenchMsg(PC),D0            ; started from workbench ?
 beq.s   .done
 CALL    Forbid                       ; don't let workbench unload us yet
 move.l  WBenchMsg(PC),A1
 CALL    ReplyMsg           ; workbench will get reply after we're gone
.done:
 move.l  (SP)+,D0                     ; pop error code
 RTS                                  ; we're gone...

DosName    dc.b "dos.library",0
GfxName    dc.b "graphics.library",0
IntuiName  dc.b "intuition.library",0
           even

;- PC relative Vars here! -

ExecBase    dc.l 0
DosBase:    dc.l 0
GfxBase:    dc.l 0
IntuiBase:  dc.l 0
Initial_SP: dc.l 0
WBenchMsg:  dc.l 0    ; message with tooltypes data
OurTask:    dc.l 0
TaskName:   dc.l 0    ; name of our task

DOSCmdLine  dc.l 0    ; CLI command line
DOSCmdLen   dc.l 0

