;
; embTemp.s
;
; Handle embedded templates
;
;  1995-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's core libraries (corelib).
;
; Corelib 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.
;
; Corelib 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 Corelib.  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

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

		AREA	|Resources$$Code|,CODE,READONLY

; --- embTemp_extract ---
;
; On entry:	R0 == pointer to template block
;		R1 == pointer to window definition block
;		R2 == pointer to indirection space
;		R3 == pointer to end of indirection space
;		R4 == pointer to window sprite area
;		R5 == pointer to icon sprite area
;
; On exit:	R2 == updated
;
; Use:		Loads an embedded template (generated by templAOF) into a
;		block.

		EXPORT	embTemp_extract
embTemp_extract	ROUT

		STMFD	R13!,{R0,R1,R3-R12,R14}	;Save some registers
		MOV	R12,R0			;Look after this pointer
		LDMIA	R0!,{R9-R11}		;Load offsets from block
		ADD	R9,R9,R12		;Relocate window offset
		ADD	R10,R10,R12		;And the indirected offset
		ADD	R11,R11,R12		;And the limit offset

		; --- Copy the window block over --

		MOV	R0,R1			;Point to destination block
00		LDMIA	R9!,{R6-R8,R14}		;Load 16 words
		STMIA	R0!,{R6-R8,R14}		;And blat them out again
		CMP	R9,R10			;Have we finished yet?
		BCC	%b00			;No -- keep on going then

		; --- Now copy the indirected data ---

		SUB	R14,R3,R2		;Find how much room we have
		SUB	R9,R11,R10		;And how much there is
		CMP	R14,R9			;Do we have enough?
		ADRCC	R0,embTemp_noRoom	;No -- complain then
		SWICC	OS_GenerateError	;Really make a go of it
		MOV	R0,R2			;Get a copy of output pointer

00		SUBS	R9,R9,#16		;Knock another 16 bytes off
		LDMCSIA	R10!,{R6-R8,R14}	;If OK, load lots of data
		STMCSIA	R2!,{R6-R8,R14}		;And store it out again
		BCS	%b00			;And loop back for more

		ADD	R9,R9,#16		;Now we overshot a bit
00		SUBS	R9,R9,#1		;Knock one byte off
		LDRCSB	R14,[R10],#1		;Load a byte out
		STRCSB	R14,[R2],#1		;Store it out again
		BCS	%b00			;And loop back for more
		ADD	R2,R2,#3		;Word align output pointer
		BIC	R2,R2,#3		;To make next call nice

		; --- Now do the relocation ---

		ADD	R10,R12,#12		;Point to relocation table
		LDR	R11,[R12,#0]		;Find end of relocations
		ADD	R11,R11,R12		;Relocate that nicely

00		CMP	R10,R11			;Reached the end yet?
		LDMCSFD	R13!,{R0,R1,R3-R12,PC}^	;Yes -- that's it then
		LDR	R14,[R10],#4		;Load next directive
		BIC	R3,R14,#&F0000000	;Just leave the offset
		ADD	PC,PC,R14,LSR #26	;Jump table time
		DCB	"MDW!"
		B	%10embTemp_extract	;Handle indirect data reloc
		B	%20embTemp_extract	;Handle window sprite reloc
		B	%30embTemp_extract	;Handle icon sprite reloc

10		LDR	R14,[R1,R3]		;Load the word
		ADD	R14,R14,R0		;Relocate by indirected space
		STR	R14,[R1,R3]		;Store it back again
		B	%b00			;And loop

20		STR	R4,[R1,R3]		;Store window sprite area
		B	%b00			;And loop

30		STR	R5,[R1,R3]		;Store icon sprite area
		B	%b00			;And loop

embTemp_noRoom	DCD	1
		DCB	"No memory for indirected data",0

		LTORG

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

		END
