*--------------------------------------------------------------------*
*	Library startup module, not as funky as Jimm's example
*	Aztec code, but (hopefully) okay for Aztec 5.0.
*
*	Note:	Register usage has been rearranged to allow
*		library init call with parameters in registers.
*
*	Based on LibStart.A68 (with some of the bugs taken out),
*	Copyright (C) 1986, 1987 by Manx Software Systems, Inc.
*--------------------------------------------------------------------*

	INCLUDE	'exec/types.i'
	INCLUDE	'exec/nodes.i'
	INCLUDE	'exec/resident.i'

*--------------------------------------------------------------------*
*	Here we start the code generation.
*--------------------------------------------------------------------*

	SECTION	RomTag,CODE

*--------------------------------------------------------------------*
*	Not very executable (also does long word alignment).
*--------------------------------------------------------------------*

	MOVEQ	#-1,D0			; Fail if run from anywhere
	RTS

*--------------------------------------------------------------------*
*	Library version, this should match the lib ID string.
*--------------------------------------------------------------------*

LIBVERSION	EQU	34

*--------------------------------------------------------------------*
*	Global symbols (rather in a bunch than sprenkled).
*--------------------------------------------------------------------*

	PUBLIC	_LibRomTag,_LibName,_LibId,_LibInitTab,_LibInit
	PUBLIC	_LibMain,.begin,_geta4,__H0_org

*--------------------------------------------------------------------*
*	The RomTag structure, must be in first hunk.
*--------------------------------------------------------------------*

_LibRomTag:

	DC.W	RTC_MATCHWORD		; Tag ID (680x0 ILLEGAL)

	DC.L	_LibRomTag		; Points to beginning of tag
	DC.L	_LibEndTag		; Points to end of tag

	DC.B	RTF_AUTOINIT		; Auto init library
	DC.B	LIBVERSION		; Library version
	DC.B	NT_LIBRARY		; This is a library, not a device
	DC.B	0			; Priority (leave it zero)

	DC.L	_LibName		; Pointer to lib name (e.g. 'any.library')
	DC.L	_LibId			; Pointer to lib ID (e.g. 'any.library (31 Feb 1103)')
	DC.L	_LibInitTab		; Pointer to library init table

_LibEndTag:

*--------------------------------------------------------------------*
*	This is supposed to be the lib entry point.
*--------------------------------------------------------------------*

.begin					; First hunk

*--------------------------------------------------------------------*
*	Sets up the lib environment and calls the main init routine.
*
*	Register usage:	D0 - APTR to library base
*			A0 - BPTR to libray segment list
*
*	Main init routine is expected to return the library base
*	in D0 if library is ready to be used.
*--------------------------------------------------------------------*

_LibInit:

	MOVEM.L	D2-D7/A2-A6,-(SP)	; Save registers
	BSR	_geta4			; Get A4
	LEA	__H1_end,A1
	LEA	__H2_org,A2
	CMP.L	A1,A2			; Check if BSS and DATA together
	BNE	Start			; No, don't have to clear
	MOVE.W	#((__H2_end-__H2_org)/4)-1,D1
	BMI	Start			; Skip if no bss

	MOVEQ	#0,D2
Loop	MOVE.L	D2,(A1)+		; Clear out memory
	DBRA	D1,Loop

Start	MOVE.L	A6,_SysBase		; Put it where we can get it
	JSR	_LibMain

	MOVEM.L	(SP)+,D2-D7/A2-A6	; Restore registers
	RTS				; And return

*--------------------------------------------------------------------*
*	Sets A4 up to point into the middle of the data segment.
*--------------------------------------------------------------------*

_geta4:	FAR	DATA
	LEA	__H1_org+32766,A4
	RTS

*--------------------------------------------------------------------*
*	Data segment (globals/hunks).
*--------------------------------------------------------------------*

	SECTION	LibData,DATA

	PUBLIC	_SysBase,__H1_org,__H1_end,__H2_org,__H2_end
	BSS	_SysBase,4

	END
