* Startup Code for HCC by TetiSoft
*
* TetiSoft = Detlef Wuerkner, Werrastr. 7, D-6300 Giessen, GERMANY
*
* I think there could be a little more features.
* Perhaps someone would like to write a stack checking routine
* or some other features and send it to me. I will join the best routines
* and send the result to Fred Fish.

; This Startup is a mixture of Commodore's 1.2 and 1.3 startups,
; Robert W. Albrecht's _main.c, and self-made enhancements.
; I hope nobody will find here a copyright violation or miss the famous
; Alert when dos.library could not be opened (Have you ever seen this Alert?
; Who has loaded our program via LoadSeg() if dos.library isn't there?)

; This Startup is NOT for resident programs (global variables etc.),
;                 -------------------------
; but I think with HCC and CClib.library it would be difficult
; to write resident code.

; 13-01-91 TetiSoft Added Alert if CClib.library not found.

; 14-01-91 TetiSoft Added CurrentDir() calls.

; 14-01-91 TetiSoft Added _WBenchMsg in argv[] for compatibility to Commodore

; 20-01-91 TetiSoft Removed XDEF _IntuitionBase for compatibility
;		    to Aztec, Lattice and Commodore Startup.

; If you miss it - CClib.library replies the _WBenchMsg, so we should not!


	XREF	_SetupSTDIO
	XREF	_GetSTDIO
	XREF	_ClearSTDIO
	XREF	_main
	XREF	_blocksize

	XDEF	_WBenchMsg
	XDEF	_exit_fcn
	XDEF	_CCLibBase
	XDEF	_SysBase
	XDEF	_DOSBase
	XDEF	_stdin
	XDEF	_stdout
	XDEF	_stderr
	XDEF	_errno
	XDEF	_type
	XDEF	_exit
	XDEF	__exit
	XDEF	__MathBaseClose
	XDEF	__MathTransClose

	XDEF	_FindTask
	XDEF	_WaitPort
	XDEF	_GetMsg
	XDEF	_CloseLibrary
	XDEF	_OpenLibrary
	XDEF	_DisplayAlert
	XDEF	_CurrentDir

	CODE

	move.l	sp,InitialSP	; save Stackpointer
	move.l	4,_SysBase	; 4 = ExecBase
	move.l	d0,_alen	; Length of Command Line
	move.l	a0,_aptr	; Ptr to Command Line
	clr.l	-(sp)
	jsr	_FindTask	; find ourself
	addq.w	#4,sp
	move.l	d0,a4		; a4 = Ptr to Process

; Open the DOS library
	clr.l	-(sp)
	move.l	#DOSName,-(sp)
	jsr	_OpenLibrary
	addq.w	#8,sp
	move.l	d0,_DOSBase
	beq	abort

	tst.l	$AC(a4)		; pr_CLI
	bne	fromCLI

; Get the Workbench Message
	pea	$5C(a4)		; pr_MsgPort
	jsr	_WaitPort
	addq.w	#4,sp
	pea	$5C(a4)		; pr_MsgPort
	jsr	_GetMsg
	addq.w	#4,sp
	move.l	d0,_WBenchMsg
	move.l	d0,a0
	move.l	$24(a0),d0	; sm_ArgList
	beq	OpenIntuition	; no Arguments
	move.l	d0,a0
	move.l	(a0),d0		; wa_Lock
	beq	OpenIntuition	; no Lock
	move.l	d0,-(sp)
	jsr	_CurrentDir	; CurrentDir to the directory we are in.
	addq.w	#4,sp
	move.l	d0,_OldDir	; Save initial Currentdir Lock

; both CLI and Workbench

fromCLI:
OpenIntuition:
; Open the Intuition library
	clr.l	-(sp)
	move.l	#INTUITIONName,-(sp)
	jsr	_OpenLibrary
	addq.w	#8,sp
	move.l	d0,_IntuitionBase
	beq	abort

; Open the C library
	pea	3	;Version
	move.l	#CCLIBName,-(sp)
	jsr	_OpenLibrary
	addq.w	#8,sp
	move.l	d0,_CCLibBase
	bne	CClib_ok
	pea.l	60		; Height
	pea.l	AlertString
	pea.l	0		; RECOVERY_ALERT
	jsr	_DisplayAlert
	lea	12(sp),sp
abort:
	move.l	#20,-(sp)
	jsr	_exit

; Open the IEEE math library. This is commented out here since compiler
; uses MOTOROLA Fast Floating Point and CClib.library uses 64 bit IEEE.
;	XREF	__math
;	tst.w	__math
;	beq	L9
;	clr.l	-(sp)
;	move.l	#MATHIEEEDOUBBASName,-(sp)
;	jsr	_OpenLibrary
;	addq.w	#8,sp
;	move.l	d0,_MathIeeeDoubBasBase
;	beq	abort
;L9:

; Do some further initialization of the task specific structures.
; This will:

; 1) Initialize the task_UserData structure.
; 2) Parse the command line arguments or the Workbench message.
; 3) Open standard stream IO for the application.
; 4) Set up pointers to errno, and blocksize.
; 5) Give the library a pointer to the Ieee math library if opened above.
; 6) Set the standard abort function.

CClib_ok:
	move.l	#abort,-(sp)	; When using _exit, we would return with
				; the signal mask as returncode.
				; Notice that ^D, ^E and ^F will also
				; terminate us, since CClib.library will
				; call abort. (Is this a bug or not?)
				; But if we would check the signal mask in
				; abort for ^C only and would continue (rts)
				; if we got a ^D, the batch file that should
				; be terminated with ^D would continue
				; executing since the ^D is lost when
				; CClib.library receives it. So it's better
				; to stop the batch file with the return code
				; of 20 (EXIT_FAILURE).
	move.l	_WBenchMsg,-(sp)
	move.l	_aptr,-(sp)
	move.l	_alen,-(sp)
	move.l	_MathIeeeDoubBasBase,-(sp)
	move.l	#_type,-(sp)
	move.l	#_blocksize,-(sp)
	move.l	#_errno,-(sp)
	move.l	#_stderr,-(sp)
	move.l	#_stdout,-(sp)
	move.l	#_stdin,-(sp)
	jsr	_SetupSTDIO
	lea	44(sp),sp
	tst.l	d0
	beq	abort

; get a pointer to this task's user data structure
	jsr	_GetSTDIO
	move.l	d0,a3
	tst.l	_WBenchMsg
	beq	passargv
	move.l	_WBenchMsg,-(sp) ; argv contains WBenchMsg, argc is zero
	bra	domain
passargv
	move.l	100(a3),-(sp)	; ud->_argv
domain
	move.w	96(a3),d0
	ext.l	d0
	move.l	d0,-(sp)	; (long)ud->_argc

; application entry point
	jsr	_main
	addq.w	#8,sp
	clr.l	-(sp)		; EXIT_SUCCESS
	jsr	_exit		; no return

_exit:
__exit:
	tst.l	_exit_fcn
	beq	noExitFunc
	move.l	_exit_fcn,a0
	jsr	(a0)		; Call atexit() function
noExitFunc:

	tst.l	__MathTransClose
	beq	noMathTransClose
	move.l	__MathTransClose,a0
	jsr	(a0)		; Call MathTransClose() in mathtrans.asm
noMathTransClose:

	tst.l	__MathBaseClose
	beq	noMathBaseClose
	move.l	__MathBaseClose,a0
	jsr	(a0)		; Call MathBaseClose() in mathbas.asm
noMathBaseClose:

	tst.l	_IntuitionBase
	beq	noIntuitionClose
	move.l	_IntuitionBase,-(sp)
	jsr	_CloseLibrary	; Close Intuition
	addq.w	#4,sp
noIntuitionClose:

	tst.l	_CCLibBase
	beq	noCClibClose
	jsr	_ClearSTDIO	; Close files, free memory, reply WBenchMsg
	move.l	_CCLibBase,-(sp)
	jsr	_CloseLibrary	; Close CClib
	addq.w	#4,sp
noCClibClose:

	tst.l	_OldDir
	beq	NoOldDir
	move.l	_OldDir,-(sp)
	jsr	_CurrentDir	; CurrentDir is initial dir
	addq.w	#4,sp		; (recommended by Commodore)
NoOldDir:

	tst.l	_DOSBase
	beq	noDOSClose
	move.l	_DOSBase,-(sp)
	jsr	_CloseLibrary	; Close DOS
	addq.w	#4,sp
noDOSClose:

;	tst.l	_MathIeeeDoubBasBase
;	beq	_L18
;	move.l	_MathIeeeDoubBasBase,-(sp)
;	jsr	_CloseLibrary
;	addq.w	#4,sp

	move.l	4(sp),d0	; ReturnCode
	move.l	InitialSP,sp	; restore Stackpointer
	rts			; quit

; The following stub codes are added here to decrease link time
; when stubs.lib is not needed. Feel free to remove them and XREF them
; instead if you like, but your code will not be shorter.

_DisplayAlert:
	move.l	_IntuitionBase,A6
	movem.l	4(sp),D0/A0
	movem.l	12(sp),D1
	jmp	-90(A6)

_FindTask:
	move.l	_SysBase,A6
	movem.l	4(sp),A1
	jmp	-294(A6)

_GetMsg:
	move.l	_SysBase,A6
	movem.l	4(sp),A0
	jmp	-372(A6)

_WaitPort:
	move.l	_SysBase,A6
	movem.l	4(sp),A0
	jmp	-384(A6)

_CloseLibrary:
	move.l	_SysBase,A6
	movem.l	4(sp),A1
	jmp	-414(A6)

_OpenLibrary:
	move.l	_SysBase,A6
	movem.l	4(sp),A1
	movem.l	8(sp),D0
	jmp	-552(A6)

_CurrentDir:
	move.l	_DOSBase,A6
	movem.l	4(sp),D1
	jmp	-126(A6)

	DATA

DOSName:
	DC.B	'dos.library',0
CCLIBName:
	DC.B	'CClib.library',0	; CASE SENSITIVE !!!
;MATHIEEEDOUBBASName:
;	DC.B	'mathieeedoubbas.library',0
INTUITIONName:
	DC.B	'intuition.library',0
	EVEN
AlertString:
	DC.W	60	; x-Offset
	DC.B	20	; y-Offset
	DC.B	'Could not open CClib.library version 3 or higher.',0
	DC.B	1	; another Text follows...
	DC.B	0,120	; x
	DC.B	40	; y
	DC.B	'Press a mouse button to exit.',0,0

	BSS

;	XDEF	_MathIeeeDoubBasBase
_MathIeeeDoubBasBase:
	DS.L	1
_WBenchMsg:
	DS.L	1
_OldDir:
	DS.L	1
_exit_fcn:
	DS.L	1
__MathBaseClose:
	DS.L	1
__MathTransClose:
	DS.L	1
_CCLibBase:
	DS.L	1
_SysBase:
	DS.L	1
_DOSBase:
	DS.L	1
_IntuitionBase:
	DS.L	1
_stdin:
	DS.L	1
_stdout:
	DS.L	1
_stderr:
	DS.L	1
_errno:
	DS.L	1
_type:
	DS.L	1

_alen:
	DS.L	1
_aptr:
	DS.L	1
InitialSP:
	DS.L	1

	END
