*
* C initial startup procedure under AmigaDOS
* Special stripped version !!
* 

	INCLUDE	"exec/types.i"
	INCLUDE	"exec/libraries.i"
	INCLUDE	"libraries/dos.i"
	INCLUDE	"libraries/dosextens.i"
	INCLUDE	"exec/funcdef.i"
	INCLUDE	"exec/exec_lib.i"
	INCLUDE	"libraries/dos_lib.i"

AbsExecBase	EQU	4

	xdef	_XCEXIT		exit(code) is standard way to leave C.

	xref	_LinkerDB	linker defined base value
	xref	__BSSBAS	linker defined base of BSS
	xref	__BSSLEN	linker defined length of BSS

	csect	text,0,0,1,2	any xref's after this are 16-bit reloc

	xref	__main		Name of C program to start with.

start:
	movem.l d1-d6/a0-a6,-(a7)
REGSIZE	EQU	(6+7)*4
	lea	REGSIZE(a7),A5		determine old stack pointer
	move.l	a0,a2			save command pointer
	move.l	d0,d2			and command length
	lea	_LinkerDB,a4		load base register

	lea	__BSSBAS,a0		get base of BSS
	moveq	#0,d1
	move.l	#__BSSLEN,d0		get length of BSS in longwords
	bra.s	clr_lp			and clear for length given
clr_bss move.l	d1,(a0)+
clr_lp	dbf	d0,clr_bss

	move.l	AbsExecBase.W,a6
	move.l	a6,_SysBase(A4)
	move.l	a7,__StackPtr(A4)	Save stack ptr

*------ get the address of our task
	suba.l	a1,a1
	jsr	_LVOFindTask(a6)
	move.l	d0,a3

*------ are we running as a son of Workbench?
	move.l	pr_CurrentDir(A3),_curdir(A4)
	tst.l	pr_CLI(A3)
	beq	exit4			Not supported

	move.l	a5,D0			Get top of stack
	sub.l	4(a5),D0		compute bottom 
	move.l	D0,__base(A4)		save for stack checking

*------ attempt to open DOS library:
	lea	DOSName(pc),A1
	moveq.l	#0,D0
	jsr	_LVOOpenLibrary(a6)
	move.l	D0,_DOSBase(A4)
	bne.s	DOSok
	moveq.l	#100,d0
	bra.s	exit2
DOSok
*------ find command name:
	move.l	pr_CLI(a3),a0
	add.l	a0,a0			bcpl pointer conversion
	add.l	a0,a0
	move.l	cli_CommandName(a0),a1
	add.l	a1,a1			bcpl pointer conversion
	add.l	a1,a1

*------ collect parameters:
	move.l	d2,d0			get command line length
	moveq.l	#0,d1
	move.b	(a1)+,d1
	move.l	a1,__ProgramName(A4)
	add.l	d1,d0			add length of command name
	addq.l	#1,d0			allow for space after command 

	clr.w	-(A7)			set null terminator for command line
	addq.l	#1,D0			force to even number of bytes
	andi.w	#$fffe,D0		*(round up)
	sub.l	D0,A7			make room on stack for command line
	subq.l	#2,D0
	clr.w	0(A7,D0)

*------ copy command line onto stack
	move.l	d2,d0			get command line length
	subq.l	#1,d0
	add.l	d1,d2

copy_line:
	move.b	0(A2,D0.W),0(A7,D2.W)	copy command line to stack
	subq.l	#1,d2
	dbf	d0,copy_line
	move.b	#' ',0(a7,d2.w)		add space between command and parms
	subq.l	#1,d2

copy_cmd:
	move.b	0(a1,d2.w),0(a7,d2.w)	copy command name to stack
	dbf	d2,copy_cmd
	move.l	A7,A1
	move.l	A1,-(A7)		push command line address

	lea.l	Asterix(pc),a0
	move.l	a0,d1
	move.l	#MODE_OLDFILE,d2
	move.l	_DOSBase(A4),a6
	jsr	_LVOOpen(a6)
	move.l	d0,_ConOut(a4)
	beq.s	exit4

main	jsr	__main(PC)		call C entrypoint
*	moveq.l #0,d0			set successful status
	bra.s	exit2


_XCEXIT:
	move.l	4(SP),d0		extract return code
exit2:
	move.l	d0,-(a7)
	move.l	__ONEXIT(A4),d0		exit trap function?
	beq.s	exit3
	move.l	d0,a0
	jsr	(a0)

exit3	move.l	_ConOut(a4),d1
	move.l	_DOSBase(A4),a6
	jsr	_LVOClose(a6)

exit4	move.l	AbsExecBase.W,a6
	move.l	_DOSBase(A4),a1
	jsr	_LVOCloseLibrary(a6)	close Dos library

exitToDOS:
	move.l	(a7)+,d0
	movea.l	__StackPtr(a4),SP	restore stack ptr
	movem.l	(a7)+,d1-d6/a0-a6
	rts

DOSName	dc.b	'dos.library',0
Asterix	dc.b	'*',0

*-----------------------------------------------------------------------

	csect	__MERGED,1,,2,2

	xref	_DOSBase

	xdef	_SysBase,_LoadAddress
	xdef	_curdir,__mbase,__mnext,__msize,__tsize
	xdef	__oserr,__OSERR,__FPERR,__SIGFPE,__ONERR,__ONEXIT,__ONBREAK
	xdef	__SIGINT,__ECS
	xdef	__ProgramName,__StackPtr,__base
	xdef	_ConOut

_ConOut		ds.l	1
__base		ds.l	1		base of stack
__mbase		ds.l	1		base of memory pool
__mnext		ds.l	1		next available memory location
__msize		ds.l	1		size of memory pool
__tsize		ds.l	1		total size?
__oserr		equ	*
__OSERR		ds.l	1
__FPERR		ds.l	1
__SIGFPE	ds.l	1
__SIGINT	ds.l	1
__ONERR		ds.l	1
__ONEXIT	ds.l	1
__ONBREAK	ds.l	1
__ECS		ds.l	1		extended character set flag
_curdir		ds.l	1
_console_dev	ds.l	1
_SysBase	ds.l	1
_LoadAddress	ds.l	1		program load address
__StackPtr	ds.l	1
__ProgramName	ds.l	1

	END
	
