(* Epoc C header for use with PLIB include file *)
(* Release 1.10F NSM *)
(* Copyright (C) Psion PLC 1991-93 *)

include "epocdefs.inc"

Priority         =       2
ProcVar          =       3
InitSize	 =	 5
PRMAX            =       32

module iplib

section

segment _TEXT(CODE,68H)

segment STACK(STACK,74H)
public  __stack_start:

segment _MAGIC(MAGIC,68H)
public _DatWordDead: dw 0
public _DatHandNext: dw 0
public _DatHandPrev: dw 0
public _DatCountrySeg: dw 0
public _DatClassHandle: dw 0
public _DatClassPtr: dw 0
public _DatEClassHandle: dw 0
public _DatEClassPtr: dw 0
public _DatEnterFramePtr: dw 0
public _w_ws: dw 0
public _w_am: dw 0
public _wClientData: dw 0
public _wserv_channel: dw 0
public _T: dw 0
public _r: dw 0
public _DatOsFramePtr: dw 0
public _DatATFlag: db 0
public _DatHeapLocked: db 0
public _DatProcessNamePtr: dw 0
public _DatCommandPtr: dw 0
public _DatTest: dw 0
public _DatApp1: dw 0
public _DatApp2: dw 0
public _DatApp3: dw 0
public _DatApp4: dw 0
public _DatApp5: dw 0
public _DatApp6: dw 0
public _DatApp7: dw 0
public _DatDialogPtr: dw 0
public _DatGate: dw 0
public _DatLocked: dw 0
public _DatStatusNamePtr: dw 0
public _DatUsedPathNamePtr: dw 0
org	StackSize-40H

segment _INIT(DATA,48H)
public __init_begin:
        dw      0
public __init_start:

segment _CONST(DATA,48H)
public __const_start:
	dw	0

segment _DATA(DATA,68H)
public __data_start:
public CoreSig@CnsHandler :	dw	0

segment _BSS(BSS,68H)
public __bss_start:
public __SH_sword:
	org	2

segment BSS_END(BSS,68H)
public __bss_end:

select _DATA
extrn @InitPointer
extrn @InitPriority

select _BSS
extrn @InitCount
extrn @InitEnd
extrn @InitDone
extrn @ExitCode
extrn @StackContextSP
extrn @StackContextBP

group DGROUP(_MAGIC,_INIT,_CONST,_DATA,_BSS,BSS_END)

select _TEXT
public __init0:

extrn _main
extrn __FloatInit
extrn __FloatFinish
extrn __FloatMathExcepDos
extrn __FatalErrorPos
extrn __FatalError
extrn _ClassTable
extrn _ExtCatTable
extrn __InitSetup
extrn __InitLoop

	call	__begin
	ret	far 0

	dw	__bss_start
	dw	_ExtCatTable
	dw	_ClassTable
	dw	_DatWordDead
	dw	__bss_end

public _p_leave:
	int	LibLeave
	int	LibSendExit

        dw      __init_start
        dw      __const_start

public __begin:
	sub	al, al
	mov	ah, NmFilConnect
	int	FilManager
	jc	_p_abort
	call	__FloatInit
	call	__InitSetup
	call	__InitLoop
doExit:
	call	__FloatFinish
	mov	ah, NmProcId
	int	ProcManager
	mov	bx, ax
	mov	ax, [@ExitCode]
	mov	ah, NmProcKill
	int	ProcManager

public _p_abort:
	mov	al, FailedToStartErr
	mov	[@ExitCode], ax
	jmp	doExit

public __fp_0:
	ret	0

public __fp_1:
	ret	0

public __sigFp:
	jmp	__FloatMathExcepDos

public __math_excep:
	call	__FatalErrorPos
	mov	bx, FatalMathException
	jmp	__FatalError

public __InitSetup :
	mov	ax, __const_start
	sub	ax, __init_start
	mov	bl, InitSize
	div	bl
	sub	ah, ah
	mov	[@InitCount], ax
	mul	bl
	add	ax, __init_start
	mov	[@InitEnd], ax
	and	word [@InitDone], 0
	ret	0

public __InitLoop :
	push	di
	push	si
	mov	bx, [@InitPointer]
	jmp	traverseLoop
nextLoop:
	add	bx, InitSize
chainLoop:
traverseLoop:
	cmp	bx, [@InitEnd]
	jae	endOfArray
	mov	al, [@InitPriority]
	cmp	al, [bx][Priority]
	jne	nextLoop
	lea	ax, [bx][InitSize]
	mov	[@InitPointer], ax
	dec	word [@InitCount]
	call	[bx][ProcVar]
	mov	bx, [@InitPointer]
	cmp	word [@InitDone], 0
	je	traverseLoop
initLoopRet:
	pop	si
	pop	di
	ret	0

endOfArray:
	cmp	word [@InitCount], 0
	je	stageComplete

endOfList:
	dec	byte [@InitPriority]
	js	stageComplete
	mov	bx, __init_start
	jmp	chainLoop

stageComplete:
	inc	word [@InitDone]
	mov	[@StackContextBP], bp
	mov	[@StackContextSP], sp
	call	_main
	mov	[@ExitCode], ax
	jmp	initLoopRet

public _p_exit:
	mov	sp, [@StackContextSP]
	mov	bp, [@StackContextBP]
	mov	[@ExitCode], bx
	jmp	initLoopRet

select _DATA
public @InitPointer :		dw __init_start
public @InitPriority :		db PRMAX

select _BSS
public @InitCount :   org 2
public @InitEnd :     org 2
public @InitDone :    org 2
public @ExitCode :    org 2
public @StackContextSP : org 2
public @StackContextBP : org 2

end *
