;
; crout.s
;
; Some interfaces to standard Sapphire routines
;
;  1995 Straylight
;

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

		GET	libs:header
;		GET	libs:swis

		GET	libs:stream

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

		GET	sapphire:alloc
		GET	sapphire:flex
		GET	sapphire:msgs
		GET	sapphire:sapphire
		GET	sapphire:string

;----- Macros ---------------------------------------------------------------

		MACRO
$label		PREAMBLE
$label		STMFD	sp!,{R11,lr}
		MOV	R11,sl
		MEND

		MACRO
$label		POSTAMBLE
$label		LDMFD	sp!,{R11,pc}^
		MEND

		MACRO
$label		VENEER
		EXPORT	__sapph_$label
__sapph_$label	PREAMBLE
		BL	$label
		POSTAMBLE
		MEND

		MACRO
$label		FLEXVNR
		EXPORT	__sapph_$label
__sapph_$label	PREAMBLE
		BL	$label
		MOVCS	a1,#0
		LDRCC	a1,[a1,#0]
		POSTAMBLE
		MEND

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

		AREA	|Sapphire$$Code|,CODE,READONLY

; --- __sapph_scratchpad ---

		EXPORT	|__sapph_scratchpad|
|__sapph_scratchpad|
		MOV	a1,sl
		MOVS	pc,lr

; --- strsubst ---

		EXPORT	strsubst
strsubst	MOV	ip,sp
		STMFD	sp!,{R4-R11,lr}
		MOV	R11,sl
		LDMIA	ip,{R4-R10}
		BL	str_subst
		LDMFD	sp!,{R4-R11,pc}^

; --- strcmp ---
; --- stricmp ---

		EXPORT	strcmp
		EXPORT	stricmp

strcmp		MOV	ip,lr
		BL	str_cmp
		B	%10

stricmp		MOV	ip,lr
		BL	str_icmp

10		MOVLT	a1,#-1
		MOVEQ	a1,#0
		MOVGT	a1,#1
		MOVS	pc,ip

		LTORG

; --- strbuffer ---

		EXPORT	strbuffer
strbuffer	PREAMBLE
		BL	str_buffer
		MOV	a1,R1
		POSTAMBLE

; --- strerror ---

		EXPORT	strerror
strerror	MOV	ip,sp
		STMFD	sp!,{R4-R11,lr}
		MOV	R11,sl
		LDMIA	ip,{R4-R10}
		MOV	ip,a2
		BL	str_buffer
		STR	a1,[a2],#4
		MOV	a1,ip
		BL	str_subst
		SUB	a1,a1,#4
		LDMFD	sp!,{R4-R11,pc}^

; --- msgs_lookup ---

msgs_lookup	VENEER

; --- msgs_error ---

		EXPORT	|__sapph_msgs_error|
|__sapph_msgs_error|
		MOV	ip,sp
		STMFD	sp!,{R4-R11,lr}
		MOV	R11,sl
		LDMIA	ip,{R4-R10}
		MOV	ip,a2
		BL	str_buffer
		STR	a1,[a2],#4
		MOV	a1,ip
		BL	msgs_lookup
		BL	str_subst
		SUB	a1,a1,#4
		LDMFD	sp!,{R4-R11,pc}^

; --- malloc ---

		EXPORT	malloc
malloc		CMP	a1,#0
		MOVEQS	pc,lr
		PREAMBLE
		BL	alloc
		MOVCS	a1,#0
		POSTAMBLE

; --- free ---

		EXPORT	|__sapph_free|
|__sapph_free|	CMP	a1,#0
		MOVEQS	pc,lr
		PREAMBLE
		BL	free
		POSTAMBLE

; --- alloc_error ---

alloc_error	VENEER

; --- flex ---

flex_alloc	FLEXVNR
flex_free	VENEER
flex_size	VENEER
flex_extend	FLEXVNR
flex_midExtend	FLEXVNR

		EXPORT	|__sapph_flex_save|
|__sapph_flex_save|
		PREAMBLE
		FSAVE	R0
		POSTAMBLE

		EXPORT	|__sapph_flex_load|
|__sapph_flex_load|
		PREAMBLE
		FLOAD	R0
		POSTAMBLE

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

		END
