 * $Revision Header *** Header built automatically - do not edit! ***********
 *
 *	(C) Copyright 1991 by Torsten Jürgeleit
 *
 *	Name .....: libstartup.asm
 *	Created ..: Sunday 22-Dec-91 20:35:08
 *	Revision .: 0
 *
 *	Date        Author                 Comment
 *	=========   ====================   ====================
 *	22-Dec-91   Torsten Jürgeleit      Created this file!
 *
 ****************************************************************************
 *
 *	Startup code for IntuiSup library
 *
 * $Revision Header ********************************************************

;---------------------------------------------------------------------------
; Includes
;---------------------------------------------------------------------------

	NOLIST
	INCLUDE	<exec/types.i>
	INCLUDE <exec/libraries.i>
	INCLUDE <exec/initializers.i>
	INCLUDE <exec/resident.i>
	INCLUDE	"libdata.i"
	LIST

;---------------------------------------------------------------------------
; External definitions
;---------------------------------------------------------------------------

	XDEF	.begin
	XDEF	_LibOpen
	XDEF	_LibClose
	XDEF	_LibExpunge
	XDEF	_LibNull
	XDEF	_geta4
	XDEF	_SysBase
	XDEF	_GfxBase
	XDEF	_IntuitionBase
	XDEF	_DiskfontBase
	XDEF	_LayersBase

;---------------------------------------------------------------------------
; External references
;---------------------------------------------------------------------------

	XREF	__H1_org
	XREF	_FuncTable

;---------------------------------------------------------------------------
; Support macros
;---------------------------------------------------------------------------

CALL	MACRO
	XREF	\1
	jsr	\1
	ENDM

CALLSYS	MACRO
	XREF	_LVO\1
	jsr	_LVO\1(a6)
	ENDM

PUSH	MACRO
	movem.l	\1,-(sp)
	ENDM

PULL	MACRO
	movem.l	(sp)+,\1
	ENDM

;---------------------------------------------------------------------------
; First executable location -> do nothing
;---------------------------------------------------------------------------

	SECTION IntuitionSupport,CODE
.begin:
	moveq	#0,d0
	rts

;---------------------------------------------------------------------------
; RomTag structure
;---------------------------------------------------------------------------

RomTag:				; STRUCTURE RT,0
	dc.w	RTC_MATCHWORD	;    UWORD RT_MATCHWORD
	dc.l	RomTag		;    APTR  RT_MATCHTAG
	dc.l	EndCode		;    APTR  RT_ENDSKIP
	dc.b	RTF_AUTOINIT	;    UBYTE RT_FLAGS
	dc.b	VERSION		;    UBYTE RT_VERSION
	dc.b	NT_LIBRARY	;    UBYTE RT_TYPE
	dc.b	PRIORITY	;    BYTE  RT_PRI
	dc.l	LibName		;    APTR  RT_NAME
	dc.l	LibID		;    APTR  RT_IDSTRING
	dc.l	InitTable	;    APTR  RT_INIT

;---------------------------------------------------------------------------
; Library strings
;---------------------------------------------------------------------------

LibName:	LIB_NAME
LibTag:		dc.b	0,"$VER:"
LibID:		LIB_ID
		INCLUDE <ram:compile_date.i>
GfxName:	dc.b	"graphics.library",0
IntuitionName:	dc.b	"intuition.library",0
DiskfontName:	dc.b	"diskfont.library",0
LayersName:	dc.b	"layers.library",0
	EVEN

;---------------------------------------------------------------------------
; Library tables
;---------------------------------------------------------------------------

InitTable:
	dc.l	LIB_SIZEOF	; size of library base data space
	dc.l	_FuncTable	; pointer to function initializers
	dc.l	DataTable	; pointer to data initializers
	dc.l	InitRoutine	; routine to run

DataTable:
	INITBYTE	LN_TYPE,NT_LIBRARY
	INITLONG	LN_NAME,LibName
	INITBYTE	LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
	INITWORD	LIB_VERSION,VERSION
	INITWORD	LIB_REVISION,REVISION
	INITLONG	LIB_IDSTRING,LibID
	dc.l		0

;---------------------------------------------------------------------------
; Library init routine
;
; Input: d0 = library ptr       Return: d0 = library ptr or NULL if error
;	 a0 = seglist ptr
;	 a6 = SysBase
;---------------------------------------------------------------------------

InitRoutine:
	PUSH	d2/a2-a4

	; --- save library ptr
	move.l	d0,a2

	; --- init important ptrs
	bsr	_geta4			; a4 := data segment ptr
	lea	SegList,a3		; a3 := start of ptr buffer
	move.l	a0,(a3)+		; init SegList
	move.l	a6,(a3)+		; init _SysBase

	; --- open libraries
	lea	GfxName(pc),a1
	moveq	#0,d0
	CALLSYS	OpenLibrary
	move.l	d0,(a3)+
	beq	InitRoutine_Error

	lea	IntuitionName(pc),a1
	moveq	#0,d0
	CALLSYS	OpenLibrary
	move.l	d0,(a3)+
	beq	InitRoutine_CloseGfxLib

	lea	DiskfontName(pc),a1
	moveq	#0,d0
	CALLSYS	OpenLibrary
	move.l	d0,(a3)+
	beq	InitRoutine_CloseIntuitionLib

	lea	LayersName(pc),a1
	moveq	#0,d0
	CALLSYS	OpenLibrary
	move.l	d0,(a3)+
	beq	InitRoutine_CloseDiskfontLib

	; --- call library specific init routine
	PUSH	a2-a4/a6
	CALL	_LibInit
	PULL	a2-a4/a6
	tst.w	d0			; check return code (BOOL)
	beq	InitRoutine_CloseLayersLib

	move.l	a2,d0			; set return code -> ok

InitRoutine_Exit:
	PULL	d2/a2-a4
	rts

InitRoutine_CloseLayersLib:
	moveq	#3,d2
	bra	InitRoutine_CloseLibs

InitRoutine_CloseDiskfontLib:
	moveq	#2,d2
	bra	InitRoutine_CloseLibs

InitRoutine_CloseIntuitionLib:
	moveq	#1,d2
	bra	InitRoutine_CloseLibs

InitRoutine_CloseGfxLib:
	moveq	#0,d2
	bra	InitRoutine_CloseLibs

InitRoutine_CloseLibs:
	move.l	-(a3),a1
	CALLSYS	CloseLibrary
	dbra	d2,InitRoutine_CloseLibs

InitRoutine_Error:
	moveq	#0,d0			; set return code -> error
	bra	InitRoutine_Exit

;---------------------------------------------------------------------------
; Library open routine
;
; Input: d0 = version		Return: d0 = library ptr or NULL if error
;	 a6 = library ptr
;---------------------------------------------------------------------------

_LibOpen:
	; --- mark us as having another opener
	addq.w	#1,LIB_OPENCNT(a6)

	; --- prevent delayed expunges
	bclr	#LIBB_DELEXP,LIB_FLAGS(a6)

	move.l	a6,d0			; set return code -> ok
	rts

;---------------------------------------------------------------------------
; Library close routine
;
; Input: a6 = library ptr	Return: d0 = NULL or seglist ptr if expunge
;---------------------------------------------------------------------------

_LibClose:
	; --- set the return value for no expunge
	moveq	#0,d0

	; --- mark us as having one fewer openers
	subq.w	#1,LIB_OPENCNT(a6)

	; --- see if there is anyone left with us open
	bne	LibClose_Exit

	; --- see if we have a delayed expunge pending
	btst	#LIBB_DELEXP,LIB_FLAGS(a6)
	beq	LibClose_Exit

	; --- do the expunge
	bsr	_LibExpunge

LibClose_Exit:
	rts

;---------------------------------------------------------------------------
; Library open routine
;
; Input: a6 = library ptr	Return: d0 = NULL or seglist ptr if expunge
;---------------------------------------------------------------------------

_LibExpunge:
	PUSH	d2/a4-a6

	; --- init some regs
	bsr	_geta4			; a4 := data segment ptr
	move.l	a6,a5			; a5 := library ptr
	move.l	_SysBase,a6		; a6 := SysBase
   
	; --- see if anyone has us open
	tst.w	LIB_OPENCNT(a5)
	beq	LibExpunge_DoExpunge

	; --- it is still open.  set the delayed expunge flag
	bset	#LIBB_DELEXP,LIB_FLAGS(a5)
	moveq	#0,d0			; set reurn code -> no expunge
	bra	LibExpunge_Exit

LibExpunge_DoExpunge:
	; --- unlink from library list
	move.l	a5,a1
	CALLSYS	Remove
   
	; --- library specific expunge routine
	PUSH	a4-a6
	CALL	_LibFree
	PULL	a4-a6

	; --- free our memory
	moveq	#0,d0
	move.l	a5,a1
	move.w	LIB_NEGSIZE(a5),d0
	sub.l	d0,a1
	add.w	LIB_POSSIZE(a5),d0
	CALLSYS	FreeMem

	; --- close libraries
	lea	_GfxBase,a5
	moveq	#3,d2

LibExpunge_CloseLoop:
	move.l	(a5)+,a1
	CALLSYS	CloseLibrary
	dbra	d2,LibExpunge_CloseLoop

	; --- set up our return value
	move.l	SegList,d0

LibExpunge_Exit:
	PULL	d2/a4-a6
	rts

;---------------------------------------------------------------------------
; Library null routine
;
; Input: a6 = library ptr
;---------------------------------------------------------------------------

_LibNull:
	moveq	#0,d0
	rts

;---------------------------------------------------------------------------
; Init data segment ptr a4 - needed for small data memory model
;---------------------------------------------------------------------------

_geta4:
	FAR	CODE
	FAR	DATA

	lea	__H1_org+32766,a4

	NEAR	DATA
	NEAR	CODE
	rts

;---------------------------------------------------------------------------
; Skip label for RomTag
;---------------------------------------------------------------------------

EndCode:

	SECTION IntuitionSupport,DATA

;---------------------------------------------------------------------------
; Important ptrs -> init by Init
;---------------------------------------------------------------------------
	
SegList:
	dc.l	0
_SysBase:
	dc.l	0
_GfxBase:
	dc.l	0
_IntuitionBase:
	dc.l	0
_DiskfontBase:
	dc.l	0
_LayersBase:
	dc.l	0

	END
