;  (c) 1990 S.Hawtin.
;  Permission is granted to copy this file provided
;   1) It is not used for commercial gain
;   2) This notice is included in all copies
;   3) Altered copies are marked as such
;
;  No liability is accepted for the contents of the file.
;
;
; C startup file for the Amiga.
;
; This file must be linked as the first object module in any C program.
; it loads all the needed libraries, then calls __main to set up the
; command line parameters and the default FILE * structures.
;
;  CHANGES
;
;	12 Dec 1989	Created as simple startup		S.Hawtin
;	17 Feb 1990	Added workbench drivers			S.Hawtin
;
;        Macro definitions
;
	INCLUDE	clibdefs.i
;
;
;
	section ONE
;
;
;
	XREF	__main
	XREF	initmem
	XREF	clearmem
	XREF	fp_open
	XREF	fp_close
	XREF	__WBConsole
;
;        Define entry point
;
   	xdef	start
start:
	move.l	sp,StkPtr	; Save stack pointer for EXIT
	move.l	d0,_cmndlen	; Save length of command
	move.l	a0,_cmndstr	; Save command buffer
;
;        Open DOS library
;
	move.l	#dosname,a1
	clr   	d0
	call	exec,OpenLibrary
	move.l	d0,dosLib
	tst.l	d0
	beq	error_exit
;
;	Set up malloc lists
;
	jsr	initmem
	tst.l	d0
	beq	error_exit
;
;	Open the floating point libraries.  If the maths library is not
;	linked this will have no effect.
;
	jsr	fp_open
;
;	Were we started from the workbench ?
;
	move.l	#0,a1		; Find this tasks structure
	call	exec,FindTask
	move.l	d0,Task
	move.l	d0,a2
	tst.l	PROC.CLI(a2)	; Is it a CLI task?
	beq.s	WBtask
;
;        Find standard file handles
;
CLItask:
	move.l	#0,__fromWB	; Not from workbench
	call	dos,Input
	move.l	d0,__stdin
	beq	error_exit
	call	dos,Output
	move.l	d0,__stdout
	beq	error_exit
	bra	NowRun
;
;	Called from the workbench
;
WBtask:
	move.l	#1,__fromWB	; from Workbench
	lea	PROC.MSGPORT(a2),a0
	call	exec,WaitPort	; wait for the workbench message
	lea	PROC.MSGPORT(a2),a0
	call	exec,GetMsg
	move.l	d0,WBmsg
	move.l	__WBConsole,d1	; Default console
	move.l	#MODE_NEWFILE,d2
	call	dos,Open
	move.l	d0,__stdin	; Console becomes default
	move.l	d0,__stdout
	beq	error_exit
	; Convert open console into 68000 address
	lsl.l	#2,d0
	move.l	d0,a0
	move.l	Task,a2		; Link the console to the task
	move.l	FH.TYPE(a0),PROC.CONSOLE(a2)
;
;	Call main C entry point via Cstartup
;
NowRun:
	bsr	__main
;
;	If _main returns fall through to a normal (0) exit
;
	move.l	#0,a0

a0_exit:
	move.l	a0,-(sp)		; Hide the return code
	jsr	clearmem		; Tidy up memory

	tst.l	__fromWB
	beq	exit1
	move.l	__stdout,d1		; If stdout is still open, close it
	beq	exit1
	call	dos,Close
exit1:
	tst.l	WBmsg
	beq.s	exit2
	call	exec,Forbid		; Must do this arround ReplyMsg
	move.l	WBmsg,a1
	call	exec,ReplyMsg
	call	exec,Permit
exit2:
	jsr	fp_close
	move.l	dosLib,a1		; Close libraries
	call	exec,CloseLibrary
	move.l	(sp)+,d0		; Return code stored in d0
	rts
;
; 	Close with an error, we have no clue as to which one
;
error_exit:
	move.l	#1,a0			; 1 seems like a nice error code
	bra	a0_exit

;
;	void _exit(long num);
;
	xdef	__exit
__exit:
	move.l	4(sp),a0	; Put the arg into a0
	move.l	StkPtr,sp	; Restore the stack
	bra	a0_exit		; and exit

;
	section  TWO,data
;
;	DOS Library name
;
dosname dc.b	'dos.library',0
;
     	section	THREE,bss
;
;
;
CharBuff ds.b	  1
	 ds.b     1
;
;
;
StkPtr	 ds.l	  1 
;
;
;
	xdef	_cmndlen,_cmndstr
_cmndlen ds.l	  1	;Command line length
_cmndstr ds.l	  1	;Command line buffer

	xdef	__stdin,__stdout
__stdin  ds.l     1     ;input file handle
__stdout ds.l     1     ;output file handle
;
;
	xdef	__fromWB
__fromWB ds.l	1	; Zero if called from CLI
Task	ds.l	1	; This tasks structure
WBmsg	ds.l	1	; message from WB
	end
