;
; sapphRes.s
;
; Sapphire shared resources (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 ------------------------------------------------

		IMPORT	|Image$$RO$$Base|

		IMPORT	rsc_sprites
		IMPORT	rsc_msgBase
		IMPORT	rsc_msgLimit
		IMPORT	rsc_tplBase
		IMPORT	rsc_tplLimit

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

		AREA	|Main$$Code|,CODE,READONLY

; --- sapphRes_find ---
;
; On entry:	R0 == resource type code
;		R1 == pointer to resource name (only required for
;			srType_template)
;
; On exit:	CS if found, and
;		  R0 == address of resource
;		  R1 == limit (for messages)
;		else CC and
;		  Registers preserved
;
; Use:		Looks up the address of a resource in the shared resource
;		DLL.

		EXPORT	sapphRes_find
sapphRes_find	ROUT

		CMP	R0,#srType_max		;Is the type in range?
		ADDCC	PC,PC,R0,LSL #2		;Yes -- dispatch it then
		BICS	PC,R14,#C_flag		;Otherwise couldn't find it

		B	sr__findSprite		;Find a sprite resource
		B	sr__findMessages	;Find a messages resource
		B	sr__findTemplate	;Find a template resource

		LTORG

; --- sr__findSprite ---
;
; On entry:	--
;
; On exit:	R0 == address of a sprite area
;
; Use:		Locates the sprite resource within the DLL.

sr__findSprite	ROUT

		LDR	R0,=rsc_sprites		;Find the sprite area
		ORRS	PC,R14,#C_flag		;And return to caller

		LTORG

; --- sr__findMessages ---
;
; On entry:	--
;
; On exit:	R0 == address of messages base
;		R1 == address of messages limit
;
; Use:		Locates the messages resource within the DLL.

sr__findMessages ROUT

		ADR	R0,sr__msgs		;Point to messages base/limit
		LDMIA	R0,{R0,R1}		;Load them out
		ORRS	PC,R14,#C_flag		;And return to caller

sr__msgs	DCD	rsc_msgBase
		DCD	rsc_msgLimit

		LTORG

; --- sr__findTemplate ---
;
; On entry:	R1 == pointer to template name
;
; On exit:	CS and R0 == pointer to embedded template block, or CC
;
; Use:		Locates a named window defintion.

sr__findTemplate ROUT

		STMFD	R13!,{R2-R6,R14}	;Save some registers
		ADR	R14,sr__tpls		;Point to base and limit
		LDMIA	R14,{R2,R3}		;Load them out

		; --- Search the template table ---

10		CMP	R2,R3			;Have we finished yet?
		BCS	%90sr__findTemplate	;Yes -- then we failed

		; --- Check for a name match ---

		MOV	R4,R1
00		LDRB	R5,[R4],#1		;Load first char of name
		SUB	R14,R5,#'A'		;Convert to a letter index
		CMP	R14,#26			;Is it in range?
		ORRCC	R5,R5,#&20		;Yes -- convert to lowercase
		CMP	R5,#&20			;Is the byte a control char?
		MOVCC	R5,#0			;Yes -- say it's zero

		LDRB	R6,[R2],#1		;Load first char of name
		SUB	R14,R6,#'A'		;Convert to a letter index
		CMP	R14,#26			;Is it in range?
		ORRCC	R6,R6,#&20		;Yes -- convert to lowercase
		CMP	R6,#&20			;Is the byte a control char?
		MOVCC	R6,#0			;Yes -- say it's zero

		; --- Compare the characters

		CMP	R5,R6			;Do the chars match?
		BNE	%50sr__findTemplate	;No -- move on to next one
		CMP	R5,#0			;Is it the end?
		BNE	%b00			;No -- keep looking

		; --- Found a match ---

		ADD	R2,R2,#3		;Word align the output
		BIC	R2,R2,#3		;To make sure
		LDR	R0,[R2,#0]		;Load the template base
		LDMFD	R13!,{R2-R6,R14}	;Restore registers
		ORRS	PC,R14,#C_flag		;And return success

		; --- Failed -- move on to next entry ---

50		CMP	R6,#&20			;Reached the end yet?
		LDRCSB	R6,[R2],#1		;No -- load next byte
		BCS	%b50			;And keep on looping
		ADD	R2,R2,#4+3		;Word align the output
		BIC	R2,R2,#3		;To make sure
		B	%10sr__findTemplate	;And rejoin the main loop

		; --- Failed entirely ---

90		LDMFD	R13!,{R2-R6,R14}	;Restore registers
		BICS	PC,R14,#C_flag		;And return to caller

sr__tpls	DCD	rsc_tplBase
		DCD	rsc_tplLimit

		LTORG

;----- Resource types -------------------------------------------------------

		^	0
srType_sprites	#	1			;Sprite resource, returned as
						;address of sprite area

srType_messages	#	1			;Messages resource, returned
						;as address of base and limit
						;pair

srType_template	#	1			;Template (window) resource,
						;returned as address of
						;embedded template def.

srType_max	#	0

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

		END
