;  (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
;	13 Apr 1990	Changed AmigaDOS library interface	S.Hawtin
;
;        Macro definitions
;
	INCLUDE	clibdefs.i
;
;
;
	section ONE
;
;
;
	XREF	__main
	XREF	initmem
	XREF	clearmem
	XREF	fp_open
	XREF	fp_close
	XREF	__WBConsole
	XREF	__AmiLibArray
	XREF	finalLib

;
;        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 library.  The mathtrans library is only 
;       opened when it is required.
;
	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:
; This section has been removed because I cannot seem to find the
; command name.
;	; Get the command name here
;	move.l	PROC.CLI(a2),a0
;	add.l	a0,a0
;	add.l	a0,a0		; Now points to CLI struct
;	move.l	CLI.COMMAND(a0),a1
;	add.l	a0,a0
;	add.l	a0,a0		; Now points to command name
;	moveq.l	#0,d0
;	move.b	(a0)+,d0
;	move.l	d0,__cmndnlen
;	move.l	(a0),__cmndnam
	; Now get the input and output handles
	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
	; Find the directory the program is in
	move.l	d0,a2
	move.l	SM.ARGLIST(a2),d0
	beq	noDir
	move.l	d0,a0
	move.l	WA.LOCK(a0),d1
	call	dos,CurrentDir
noDir:
	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 before ReplyMsg
	move.l	WBmsg,a1		; or risk vanishing
	call	exec,ReplyMsg
exit2:
	; Close any libraries that have been left open
	move.l	#finalLib,a2
exit3:
	subq.l	#4,a2
	move.l	(a2),a1
	cmp.l	#0,a1
	beq	exit4
	call	exec,CloseLibrary
exit4:
	cmp.l	#__AmiLibArray,a2
	bne	exit3
	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	#100,a0			; 100 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,__cmndnam,__cmndnlen
__cmndnlen ds.l	  1     ;Command name length
__cmndnam ds.l	  1	;Command name
__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
