;
; resources.s
;
; Access to shared resource DLL (MDW)
;
;  1995-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's Sapphire library.
;
; Sapphire is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2, or (at your option)
; any later version.
;
; Sapphire is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Sapphire.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Standard header ------------------------------------------------------

		GET	libs:header
		GET	libs:swis

		GET	libs:stream

;----- External dependencies ------------------------------------------------

		GET	sapphire:except
		GET	sapphire:sapphire

rsc__version	EQU	100

;----- Main code ------------------------------------------------------------

		AREA	|Sapphire$$Code|,CODE,READONLY

; --- resources_init ---
;
; On entry:	--
;
; On exit:	--
;
; Use:		Initialises the shared resource system, setting up the
;		link to the SapphRes DLL.  Note that this initialisation is
;		/not/ performed automatically.

		EXPORT	resources_init
resources_init	ROUT

		STMFD	R13!,{R0,R1,R12,R14}	;Save some registers
		WSPACE	rsc__wSpace		;Find my workspace
		LDR	R14,rsc__find		;Locate the function pointer
		CMP	R14,#0			;Have I set it up?
		LDMNEFD	R13!,{R0,R1,R12,PC}^	;I guess so then
		MOV	R14,#0			;A nice null pointer
		STR	R14,rsc__dllHandle	;Resource DLL not found yet

		; --- Save the application handle

		SWI	XDLL_SaveHandle		;Find my current app handle
		BVS	%90resources_init	;Failed -- return now then
		STR	R0,rsc__appHandle	;Save that away

		; --- Now do the dynamic linking ---

		ADR	R0,rsc__dllName		;Point to the DLL name
		LDR	R1,=rsc__version	;Load the resource version
		SWI	XDLL_Find		;Try to find the version
		STRVC	R0,rsc__dllHandle	;Store the DLL handle
		ADRVC	R1,rsc__findRsc		;Point to routine name
		SWIVC	XDLL_FindEntry		;Try to find the entry
		STRVC	R0,rsc__find		;Store this away nicely

		; --- Initialise things which we need ---

		BL	except_init		;We need to finalise properly

		; --- Set up the finalisation ---

		ADR	R0,rsc__closeDown	;Point to the exit handler
		MOV	R1,R12			;Pass it my workspace
		BL	except_atExit		;And register the handler

90		LDMFD	R13!,{R0,R1,R12,PC}^	;Return to caller when done

rsc__dllName	DCB	"[Sapphire.Resources]",0
rsc__findRsc	DCB	"sapphRes_find",0

		LTORG

; --- resources_find ---
;
; On entry:	R0 == resource code
;		R1 == pointer to name (only for rsType_template)
;
; On exit:	CS if found, and
;		  R0 == pointer to resource
;		  R1 == pointer to resource limit (only for rsType_message)
;		else CC and
;		  R0,R1 preserved
;
; Use:		Locates resources in the shared resource DLL.

		EXPORT	resources_find
resources_find	ROUT

		STMFD	R13!,{R12,R14}		;Save some registers
		WSPACE	rsc__wSpace		;Locate my workspace address
		LDR	R12,rsc__find		;Load the routine pointer
		CMP	R12,#0			;Have we found the DLL?
		BEQ	%90resources_find	;No -- skip
		MOV	R14,PC			;Set up return address
		MOV	PC,R12			;Call the routine
		LDMFD	R13!,{R12,R14}		;Restore caller's registers
		ORRCSS	PC,R14,#C_flag		;Set C if C must be set
		BICCCS	PC,R14,#C_flag		;Clear C if C must be clear

90		LDMFD	R13!,{R12,R14}		;Restore caller's registers
		BICS	PC,R14,#C_flag		;Couldn't find the resource

		LTORG

; --- rsc__closeDown ---
;
; On entry:	--
;
; On exit:	--
;
; Use:		Closes down the shared resource DLL.

rsc__closeDown	ROUT

		STMFD	R13!,{R0,R14}		;Save some registers
		LDR	R0,rsc__appHandle	;Load my application handle
		SWI	XDLL_RestoreHandle	;Set that up nicely
		LDR	R0,rsc__dllHandle	;Recover the DLL handle
		SWI	XDLL_Lose		;No longer need this
		LDMFD	R13!,{R0,PC}^		;And return to caller

		LTORG

rsc__wSpace	DCD	0

;----- Workspace ------------------------------------------------------------

		^	0,R12
rsc__wStart	#	0

rsc__find	#	4			;Address of sapphRes_find
rsc__appHandle	#	4			;Our application handle
rsc__dllHandle	#	4			;SapphRes's DLL handle

rsc__wSize	EQU	{VAR}-rsc__wStart

		AREA	|Sapphire$$LibData|,CODE,READONLY

		DCD	rsc__wSize
		DCD	rsc__wSpace
		DCD	0
		DCD	0

;----- That's all, folks ----------------------------------------------------

		END
