*--------------------------------------------------------------------*
*	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	LibCode,code

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

	moveq	#-1,d0			; Fail if run from Shell
	rts

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

LIBVERSION	equ	36

*--------------------------------------------------------------------*
*	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
	dc.l	_LibId			; Pointer to lib ID
	dc.l	_LibInitTab		; Pointer to library init table

_LibEndTag:

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

.begin					; First hunk

*--------------------------------------------------------------------*
*	Set up the lib environment and call the main init routine.
*
*	Register usage:	D0 - APTR to library base
*			A0 - BPTR to library 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
1$	move.l	d2,(a1)+		; Clear out memory
	dbra	d1,1$

Start	move.l	A6,_SysBase		; Put it where we can get it

	jsr	_LibMain		; Call 'C' init routine

	movem.l	(sp)+,d2-d7/a2-a6	; Restore registers
	rts				; And return

*--------------------------------------------------------------------*
*	Set up A4 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,_DOSBase,__H1_org,__H1_end,__H2_org,__H2_end

	end
