	ifnd	PPCLIBEMU_I
PPCLIBEMU_I	set	1
**
** ppc.library emulation
** (c)1998-2001 Frank Wille <frank@phoenix.owl.de>
**
** Private assembler includes
**
** V0.8f (10.03.2001) phx
**       New flag: PPCLibF_IgnoreNoCacheM68k
** V0.8e (16.12.2000) phx
**       New flag: PPCLibF_NoFreeMemPatch, PPCLibF_NoLoadSegPatch.
** V0.8c (21.10.2000) phx
**       MEMMAGIC identifies memory blocks allocated by the WarpUp kernel.
** V0.7d (22.02.2000) phx (46.25)
**       New structure member ppc_HID1 for the PLL code.
** V0.7  (12.12.1999) phx (46.21)
**       Changed all dates to a 4-digit year string (just in time :).
** V0.6  (11.04.1999) phx (46.17)
**       This version of the emulation library requires powerpc.library V15!
**       0.5d was an intermediate release for V15 beta versions (V14.9/14.91).
**       MPSemaphore protects the LibBase from reserved0 on.
** V0.5d (10.04.1999) phx (46.12)
**       Security buffer at end of library base.
**       PortList.
**       MPSemaphore LibMPS replaces all M68k- and PPC-specific
**       semaphores. This will hopefully allow read/write access for
**       both CPUs on all data.
**       Pad-bytes for longword alignment after List structures.
** V0.5b (12.02.1999) phx (46.10)
**       Moved Ext_UserData into Task.
** V0.5a (26.01.1999) phx (46.9)
**       Ext_UserData, used by PPCSet/GetAttr PPCTASKTAG_EXTUSERDATA.
** V0.5  (24.01.1999) phx (46.8)
**       Internal ELFLoadSegPatch. Library memory pool.
** V0.4c (23.01.1999) phx (46.7)
**       ELF-library support.
**       TaskObjects contains a list of all PowerUp task, launched by
**       the 68k or the PPC. It should be modified from 68k only.
** V0.4b (05.01.1999) phx (46.6)
**       TimerObjects Server task. Support for TimerObjects and signals.
** V0.4a (19.12.1998) phx (46.5)
**       ppclib_Flags is an UWORD.
** V0.4  (27.11.1998) phx (46.4)
**       Infos about PowerPC will be determined on startup. New fields:
**       ppc_PVR, ppc_BusClock, ppc_CPUClock.
**       Included all kernel symbols of ppc.library V46.19,
**       except _KERNEL_xxxxx.
**       As a consequence bumped the library version from 45 to 46.
** V0.1  (15.11.1998) phx (45.1)
**       First partially working ppc.library emulation. Synchronous PPC
**       tasks, started by runelf, which only use the basic PowerUp kernel
**       functions for I/O, memory and context-switch seem to work fine.
**       Supported 68k functions:
**        PPCLoadObject,PPCUnloadObject,PPCRunObject,PPCAllocMem,PPCFreeMem,
**        PPCAllocVec,PPCFreeVec,PPCCreateTask(only synchronous),
**        PPCGetObjectAttrs,PPCLoadObjectTagList
**       Supported PowerUp kernel functions:
**        PPCAllocMem,PPCAllocPooled(dummy),PPCAllocVec,
**        PPCAllocVecPooled(dummy),PPCCallM68k,PPCCallOS,PPCClose,
**        PPCCreatePool(dummy),PPCDeletePool(dummy),PPCFreeMem,
**        PPCFreePooled(dummy),PPCFreeVec,PPCFreeVecPooled(dummy),
**        PPCOpen,PPCRead,PPCSeek,PPCWrite
**       Pooled memory functions currently use the normal Alloc/FreeVec
**       functions, because pooled memory support is still missing in WarpOS.
** V0.0  (31.10.1998) phx (45.0)
**       created
**

	ifnd	EXEC_TYPES_I
	include	"exec/types.i"
	endc
	ifnd	EXEC_LISTS_I
	include	"exec/lists.i"
	endc
	ifnd	EXEC_LIBRARIES_I
	include	"exec/libraries.i"
	endc
	ifnd	EXEC_SEMAPHORES_I
	include	"exec/semaphores.i"
	endc
	ifnd	DOS_DOS_I
	include	"dos/dos.i"
	endc


VERSION		equ	46	; compatibility with real ppc.library V46
REVISION	equ	36	; my revision counter
WARPOS_VER	equ	15	; required powerpc.library version for emul.


REVTXT	macro
	dc.b	"ppc.library 46.36 (20.04.2001)"
	dc.b	" Emulation v0.8h by Frank Wille <frank@phoenix.owl.de>"
	dc.b	13,10,0
	endm

CALLSYS	macro
	xref	_LVO\1
	jsr	_LVO\1(a6)
	endm

LIBFUNC	macro
	xdef	\1
	cnop	0,4
\1:
	endm

CALLWOS	macro
	move.l	a6,-(sp)
	move.l	ppclib_PowerPCBase(a6),a6
	jsr	_LVO\1(a6)
	move.l	(sp)+,a6
	endm


	STRUCTURE PPCLibBase,LIB_SIZE
	UWORD	ppclib_Flags
	BPTR	ppclib_SegList
	APTR	ppclib_WOSLinkerDB	; TOC-pointer for r2 under WarpOS
	ULONG	ppclib_ppc_PVR		; copy of PVR
	ULONG	ppclib_ppc_BusClock	; PPC bus clock
	ULONG	ppclib_ppc_CPUClock	; PPC CPU clock
	APTR	ppclib_SysBase
	APTR	ppclib_PowerPCBase	; Offset 60!
	APTR	ppclib_DOSBase
	APTR	ppclib_UtilityBase
	APTR	ppclib_IntuitionBase
	ULONG	ppclib_ppc_HID1		; copy of HID1
	APTR	ppclib_LibPool
	STRUCT	ppclib_reserved0,32	; Data below is prot. by MPSemaphore
	STRUCT	ppclib_ElfObjects,LH_SIZE
	STRUCT	ppclib_reserved1,2
	STRUCT	ppclib_TaskObjects,LH_SIZE
	STRUCT	ppclib_reserved2,34
	STRUCT	ppclib_LibMPS,20	; CPU LibBase access arbitration
	; PowerPC-only data below!
	STRUCT	ppclib_reserved3,32
	APTR	ppclib_TimerObjSrv	; PPC TimerObject Server Task
	STRUCT	ppclib_ElfLibraries,LH_SIZE
	STRUCT	ppclib_reserved4,2
	STRUCT	ppclib_PortList,LH_SIZE	; global MsgPort list
	STRUCT	ppclib_reserved5,34
	LABEL	ppclib_SIZE

	BITDEF	PPCLib,AllErrors,0	; show requesters for all errors
	BITDEF	PPCLib,NoFreeMemPatch,1	; don't patch FreeMem/FreeVec
	BITDEF	PPCLib,NoLoadSegPatch,2	; don't patch LoadSeg
	BITDEF	PPCLib,IgnoreNoCacheM68k,3 ; ignore NOCACHE attr. for 68k
	BITDEF	PPCLib,Debug,15		; debugging mode (serial port)


MEMMAGIC	equ	$7070634d	; magic id for PPC memory allocations


	ifd	DEBUGMODE
DPRINT	macro	; PPCLibBaseReg,FmtString[,ArgRegs,...]
	save
	section	debugstrings,data
dbtxt\@:
	dc.b	\2,0
	restore
	movem.l	a0-a1/d0-d1/a6,-(sp)
	move.l	ppclib_PowerPCBase(\1),a6
	lea	dbtxt\@,a0
	ifgt	NARG-2
CARG	set	NARG
	rept	NARG-2
	move.l	\-,-(sp)
	endr
	move.l	sp,a1
	endc
	jsr	-66(a6)			; SPrintF68k (powerpc.library)
	ifgt	NARG-2
	add.w	#4*(NARG-2),sp
	endc
	movem.l	(sp)+,a0-a1/d0-d1/a6
	endm

	else
DPRINT	macro
;
	endm
	endc


	endc	; PPCLIBEMU_I
