;
; dheader.s
;
; Module header for DLL manager
;
;  1994-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's Dynamic Linking System (SDLS)
;
; SDLS 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.
;
; SDLS 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 SDLS.  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 ------------------------------------------------

		GET	sh.wSpace

		GET	sh.misc
		GET     sh.dll
		GET     sh.app
		GET	sh.suballoc
		GET	sh.messages

		IMPORT	hdr_help

;----- Magic numbers --------------------------------------------------------

hdr_swiChunk	EQU	&4A300			;SWI chunk number

;----- Yer actual module header ---------------------------------------------

		AREA	|!!!Module$$Header|,CODE,READONLY

        	DCD	hdr_enter		;Module application code
		DCD	hdr_init		;Module initialisation code
		DCD	hdr_final		;Module finalisation code
		DCD	hdr_service		;Module service call code
		DCD	hdr_title		;Module title
		DCD	hdr_help		;Module version string
		DCD	hdr_commands		;Module *command table
		DCD	hdr_swiChunk		;Module SWI chunk number
		DCD	hdr_swiHandler		;Module SWI handler code
		DCD	hdr_swiTable		;Module SWI name table
		DCD	hdr_swiCode		;Module SWI name code

;----- Some text strings ----------------------------------------------------

hdr_title	DCB	"DLLManager",0

;----- Initialisation and finalisation --------------------------------------

hdr_init	ROUT

		STMFD	R13!,{R14}		;Stack anything relevant

		; --- Now we allocate some memory ---

		MOV	R0,#6			;Better allocate some memory
		LDR	R3,=dm__wSize		;Get the length of the data
		SWI	XOS_Module		;Try to allocate the memory
		LDMVSFD	R13!,{PC}		;If no memory, quit now

		; --- Store workspace address in private word ---

		STR	R2,[R12]		;Store in private word
		MOV	R12,R2			;Keep relocation in R12 now

		; --- Initialise the workspace ---
		;
		; Just zero-init the whole lot.

		MOV	R0,R2			;Point to base of area
		ADD	R1,R2,R3		;Find add length to get limit
		BL	misc_zinit		;Zero-init the whole lot

		; --- Set up application section ---

		BL	app_init		;Allow app to store PW

		; --- Tha's it ---

		LDMFD	R13!,{PC}		;We are initialised

		LTORG

hdr_final	ROUT

		STMFD	R13!,{R12,R14}		;Keep hold of the link
		LDR	R12,[R12]		;Get relocation offset
		BL	app_checkQuit		;Make sure we can quit
		LDMVSFD	R13!,{R12,PC}		;If not, return the error
		BL	dll_freeAll		;Remove DLLs in memory
		BL	app_killAll		;Remove application blocks
		BL	sub_die			;Remove link blocks
		MOV	R2,R12			;Point to workspace
		MOV	R0,#7			;Free the workspace
		SWI	XOS_Module		;Do it!
		LDMFD	R13!,{R12}		;Get back private word ptr
		MOV	R0,#0			;Now zero out private word
		STR	R0,[R12]		;Right, now that's done
		LDMFD	R13!,{PC}		;Return to OS

		LTORG

;----- Application entry ----------------------------------------------------

hdr_enter	EQU	0			;No module application code

;----- *commands and things -------------------------------------------------

; --- The commands table ---

hdr_commands

		DCB	"DLLs",0
		ALIGN
		DCD	hdr_dlls_c
		DCB	0,0,1,0
		DCD	synt_DLLs
		DCD	help_DLLs

		DCB	"DLLApps",0
		ALIGN
		DCD	hdr_dll_apps_c
		DCB	0,0,1,0
		DCD	synt_DLLApps
		DCD	help_DLLApps

		DCB	"DLLInfo",0
		ALIGN
		DCD	hdr_dll_info_c
		DCB	1,0,2,0
		DCD	synt_DLLInfo
		DCD	help_DLLInfo

		DCB	"DLLKillApp",0
		ALIGN
		DCD	hdr_dll_killapp_c
		DCB	1,0,1,0
		DCD	synt_DLLKillApp
		DCD	help_DLLKillApp

		DCB	"DLLReset",0
		ALIGN
		DCD	hdr_dll_reset_c
		DCB	0,0,0,0
		DCD	synt_DLLReset
		DCD	help_DLLReset

		DCB	"DLLEnsure",0
		ALIGN
		DCD	hdr_dll_ensure_c
		DCB	2,0,2,0
		DCD	synt_DLLEnsure
		DCD	help_DLLEnsure

		DCD	0

; --- *DLLs ---

hdr_dlls_c	LDR	R12,[R12]		;Find relocation offset
		CMP	R1,#0			;Did he give arguments?
		BEQ	dll_list		;Display a list of DLLs
		B	app_listDLLs

; --- *DLLApps ---

hdr_dll_apps_c	LDR	R12,[R12]		;Load data relocation
		CMP	R1,#0			;Did he give arguments?
		BEQ	app_list		;Display the list
		B	app_listUsing

; --- *DLLInfo ---

hdr_dll_info_c	LDR	R12,[R12]		;Find relocation to data
		B	dll_showInfo		;Go to a routine that can

; --- *DLLKillApp ---

hdr_dll_killapp_c
		LDR	R12,[R12]		;Find relocation to data
		STMFD	R13!,{R14}		;Keep return address safe
		BL	app_findNamed		;Find the application named
		LDMFD	R13!,{R14}		;Get return address back
		BVC	app_kill		;If no error, kill the app
		ORRS	PC,R14,#V_flag		;Return to caller with error

; --- *DLLReset ---

hdr_dll_reset_c LDR	R12,[R12]
		STMFD	R13!,{R14}
		BL	sub_die
		BL	dll_freeAll
		LDMFD	R13!,{R14}
		B	app_killAll

; --- *DLLEnsure ---

hdr_dll_ensure_c
		LDR	R12,[R12]
		B	dll_check

;----- SWI stuff ------------------------------------------------------------

; --- SWI name-number translation table ---

hdr_swiTable	DCB	"DLL",0			;SWI name prefix

		DCB	"Find",0
		DCB	"FindFromTable",0
		DCB	"Load",0
		DCB	"Lose",0
		DCB	"AppDying",0
		DCB	"GiveCLibData",0
		DCB	"FindCLibData",0
		DCB	"InstanceVars",0
		DCB	"SetInstanceVars",0
		DCB	"AppData",0
		DCB	"Prologue",0
		DCB	"ReadStackPtr",0
		DCB	"SetStackPtr",0
		DCB	"NameApp",0
		DCB	"Info",0
		DCB	"FindEntry",0
		DCB	"SaveHandle",0
		DCB	"RestoreHandle",0
		DCB	"FindInstanceVars",0
		DCB	"RegisterAppEntryTable",0
		DCB	"FindAppEntry",0
		DCB	"SetExtensionTable",0

		DCB	0			;End of list
		ALIGN

; --- Main SWI entry point ---

hdr_swiHandler	ROUT

		LDR	R12,[R12]		;Get workspace relocation
		CMP	R11,#(%01hdr_swiHandler-%00hdr_swiHandler)/4
		ADDCC	PC,PC,R11,LSL #2	;Branch to correct handler
		B	%01hdr_swiHandler	;If SWI out of range, error

00hdr_swiHandler
		B	app_findDLL		;DLL_Find
		B	app_fromtable		;DLL_FindFromTable
		B	dll_load		;DLL_Load
		B	app_loseDLL		;DLL_Lose
		B	app_dying		;DLL_AppDying
		B	app_giveclib		;DLL_GiveCLibData
		B	app_findclib		;DLL_FindCLibData
		B	app_instvars		;DLL_InstanceVars
		B	dll_instvars		;DLL_SetInstanceVars
		B	app_appdata		;DLL_AppData
		B	app_prologue		;DLL_Prologue
		B	app_readstkptr		;DLL_ReadStackPtr
		B	app_setstkptr		;DLL_SetStackPtr
		B	app_setname		;DLL_NameApp
		B	dll_info		;DLL_Info
		B	dll_findEntry		;DLL_FindEntry
		B	app_getHandle		;DLL_SaveHandle
		B	app_restoreHandle	;DLL_RestoreHandle
		B	dll_findWorkspace	;DLL_FindInstanceVars
		B	app_setBtable		;DLL_RegisterAppEntryTable
		B	app_appEntry		;DLL_FindAppEntry
		B	app_fixExtension	;DLL_SetExtensionTable

01hdr_swiHandler
		ADRL	R0,msg_errBadSWI	;Point to an error message
		ORRS	PC,R14,#V_flag		;Return an error to OS

		LTORG

; --- Name-number translation code ---

hdr_swiCode	EQU	0			;No name-number code

;----- Service call handling ------------------------------------------------

hdr_service	EQU	0			;Not interested in these

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

		END
