***********************************************************
* VER$: 1.1 wb_startup.i
*
* some startup code to make a Workbench execute look like the CLI
* based loosely on RKM Vol 1 page 4-36
*
* Include this at the front of your program after any other includes.
*
* This code originally came from the DevPac 2.08 disk.  It has been
* heavily modified, though, by the Dancing Fool of Epsilon.
*
* Revision history:
* 1.1:
*	Added the CALL macro for some optimization.
*
* 1.0:
*	Original release.
*
***********************************************************

		incdir	'include/'

		include	'exec/exec.i'
		include	'libraries/dosextens.i'

		incdir	''

***********************************************************
* Other Macros
***********************************************************

 IFND	CALLEXEC
CALLEXEC	MACRO
		movea.l	4.w,a6
		jsr	_LVO\1(a6)
		ENDM
 ENDIF

 IFND	CALL
CALL		MACRO
		jsr	_LVO\1(a6)
		ENDM
 ENDIF

***********************************************************
* Other Equates
***********************************************************

 IFND	_LVOFindTask
_LVOFindTask	EQU	-$126
 ENDIF

 IFND _LVOWaitPort
_LVOWaitPort	EQU	-$180
 ENDIF

 IFND _LVOGetMsg
_LVOGetMsg	EQU	-$174
 ENDIF

 IFND _LVOReplyMsg
_LVOReplyMsg	EQU	-$17a
 ENDIF

 IFND _LVOForbid
_LVOForbid	EQU	-$84
 ENDIF

***********************************************************

WBStartUp:	movem.l	d0/a0,-(sp)		; save initial values
		suba.l	a3,a3

		suba.l	a1,a1			; clear out
		CALLEXEC FindTask		; find us
		move.l	d0,a4			; save our pointer in a4

		tst.l	pr_CLI(a4)		; are we from the cli?
		beq.b	.fromWorkbench		; nope, do other stuff...

	; we were called from the CLI
		movem.l	(sp)+,d0/a0		; restore regs
		bra.b	.end_startup		; and run the user prog

	; we were called from the Workbench
.fromWorkbench:	lea	pr_MsgPort(a4),a0	; our port
		CALL	 WaitPort		; wait for a message
		lea	pr_MsgPort(a4),a0	; our port
		CALL	GetMsg			; then get it
		move.l	d0,a3			; save it for later reply
		movem.l	(sp)+,d0/a0		; restore
.end_startup:	move.l	a3,-(sp)		; save for later reply
		bsr.b	.main			; start the main program

	; returns to here with exit code in d0
		move.l	d0,-(sp)		; save it

		tst.l	4(sp)			; this is 0 if...
		beq.b	.exitToDOS		; I was from the CLI

		CALLEXEC Forbid
		movea.l	4(sp),a1
		CALL	 ReplyMsg

.exitToDOS:	move.l	(sp)+,d0		; exit code
		addq.w	#4,sp
		rts

	; the program starts here
		cnop	0,4			; long word align
.main:
