;
; xapp.s
;
; SDLS stubs for applications
;
;  1993-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's C library stubs (xstubs).
;
; xstubs 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.
;
; xstubs 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 xstubs.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Standard stuff -------------------------------------------------------

		GET	libs:header
		GET	libs:swis

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

		IMPORT	|_kernel_init|
		IMPORT	|Image$$RO$$Base|
		IMPORT	|Image$$RW$$Limit|
		IMPORT	|RTSK$$Data$$Base|
		IMPORT	|RTSK$$Data$$Limit|
		IMPORT	|__root_stack_size|,WEAK

		IMPORT	xstub__ktable
		IMPORT	xstub__ektable
		IMPORT	xstub__ctable
		IMPORT	xstub__ectable
		IMPORT	xstub__kdata
		IMPORT	xstub__ekdata
		IMPORT	xstub__cdata
		IMPORT	xstub__ecdata

;----- Exports --------------------------------------------------------------

		EXPORT	|__main|

;----- Initialise the library -----------------------------------------------

		AREA	|Stub$$Code|,CODE,READONLY

xstub__stack	EQU	4096			;Standard stack size thing
xstub__error	EQU	&800E90			;Error number base
xstub__version	EQU	5			;Version number of stubs

		ENTRY				;Start here :-)

|__main|	SWI	OS_GetEnv		;Get information about me
		MOV	R2,R1			;Keep memory limit thing
		LDR	R1,=|Image$$RW$$Limit|	;Point to end of program
		MOV	R3,#-1			;As required
		MOV	R4,#0			;As required
		MOV	R5,#-1			;As required
		ADR	R0,xstub__stubs		;Point to stub descriptions
		LDR	R6,=|__root_stack_size|	;Load root stack size
		CMP	R6,#0			;Has it been specified?
		MOVEQ	R6,#xstub__stack	;No -- substitute a default
		LDRNE	R6,[R6]			;Otherwise, read the size
		MOV	R6,R6,ASR #10		;Convert to kilobytes
		MOV	R6,R6,LSL #16		;And shift to top halfword
		SWI	XSharedCLibrary_LibInitAPCS_R ;Init C library
		ADRVS	R0,xstub__noCLib	;If failed, claim CLib gone
		SWIVS   OS_GenerateError        ;And generate the error
		CMP	R6,#xstub__version	;Check returned version
		MOVGE	R4,R0			;Point to workspae limit
		ADRGE	R0,xstub__kernel	;Point to _kernel_init block
		MOVGE	R3,#0			;Say we're an application
		BGE	|_kernel_init|		;Start up the kernel!
		ADR	R0,xstub__oldCLib	;Point to error message
		SWI     OS_GenerateError        ;Complain about old CLib

xstub__noCLib	DCD	xstub__error+&00	;Error number (official)
		DCB	"Couldn't find Shared C Library",0
		ALIGN

xstub__oldCLib	DCD	xstub__error+&01	;Error number (also official)
		DCB	"Shared C Library is out of date",0
		ALIGN

;----- Stub description block -----------------------------------------------

xstub__stubs

		; --- Magic numbers ---

xstub__krnlID	EQU	1			;Kernel's chunk ID
xstub__clibID	EQU	2			;C library's chunk ID

		; --- Kernel section ---

		DCD	xstub__krnlID
		DCD	xstub__ktable
		DCD	xstub__ektable
		DCD	xstub__kdata
		DCD	xstub__ekdata

		; --- C library section ---

		DCD	xstub__clibID
		DCD	xstub__ctable
		DCD	xstub__ectable
		DCD	xstub__cdata
		DCD	xstub__ecdata

		DCD	-1			;Terminate the table

;----- Kernel description block ---------------------------------------------

xstub__kernel

		DCD	|Image$$RO$$Base|
		DCD	|RTSK$$Data$$Base|
		DCD	|RTSK$$Data$$Limit|

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

		END
