@----------------------------------------------------------------------------
@
@ \Source /usr/local/cvsroot/gccsdk/unixlib/source/clib/unixlib/asm_dec.s,v $
@ \Date: 2003/06/23 20:3303 $
@ \Revision 1.13 $
@ \State Exp $
@ \Author joty $
@
@ Declare registers and SWIs we will be calling.
@
@----------------------------------------------------------------------------

@ Bits that control which bits are compiled into UnixLib. Note, this must be
@ kept in sync with <sys/syslib.h>, <signal.h> and <errno.h>.

@ Is an alloca() failure fatal? Nick says yes, Linux says no.
.equ	ALLOCA_FATAL, 1
@ Do paranoid checks ?
.equ	PARANOID, 0
@ Use dynamic areas for heap on RISC OS 3.5+
.equ	DYNAMIC_AREA, 1
@ Emulate the SWP instruction for ARM2 compatibility
.equ	__SWP_ARM2, 0

.equ	__INTEGRITY_CHECK, 1
.equ	__FEATURE_ITIMERS, 1
.equ	__FEATURE_SOCKET, 1
.equ	__FEATURE_PIPEDEV, 1
.equ	__FEATURE_PTHREADS, 1
.equ	USEFILEPATH, 0
@ For Internet 4 compatibility
.equ	COMPAT_INET4, 0

@ The offset of various members of the __pthread_thread structure
@ This should be kept in sync with pthread.h, lib1aof.s, and stubs.s
.equ	__PTHREAD_MAGIC_OFFSET, 0
.equ	__PTHREAD_CONTEXT_OFFSET, 4
.equ	__PTHREAD_ALLOCA_OFFSET, 8
.equ	__PTHREAD_ERRNO_OFFSET, 20
.equ	__PTHREAD_ERRBUF_OFFSET, 24

@ registers





.equ	USR_Mode, 0x0
.equ	FIQ_Mode, 0x1
.equ	IRQ_Mode, 0x2
.equ	SVC_Mode, 0x3
.equ	Mode_Bits, 0x3

.equ	NFlag, 0x80000000	@ Negative flag
.equ	ZFlag, 0x40000000	@ Zero flag
.equ	CFlag, 0x20000000	@ Carry flag
.equ	VFlag, 0x10000000	@ Overflow flag
.equ	IFlag, 0x08000000	@ IRQ disable
.equ	FFlag, 0x04000000	@ FIRQ disable

.equ	USR32_mode, 0b10000
.equ	FIQ32_mode, 0b10001
.equ	IRQ32_mode, 0b10010
.equ	SVC32_Mode, 0b10011
.equ	ABT32_Mode, 0b10111
.equ	UND32_Mode, 0b11011
.equ	SYS32_Mode, 0b11111

.equ	IFlag32, 0x00000080



	@ Macro for embedding function names in code, just before
	@ the function prologue.
	.macro 	NAME	name
@	.ascii	"\name",0
@	.align
@	.word	0xFF000000 + (:LEN "\name"+3+1) & 0xFFFFFFFC
	.endm

	.macro 	return	cond, dstreg, srcreg
	MOV\cond	\dstreg, \srcreg
	.endm

	.macro 	stackreturn	cond, regs, FD=FD
	LDM\cond\FD	sp!, {\regs}
	.endm

	@ Assembler equivalent of __set_errno in errno.h.
	@ #define __set_errno(val) (errno = (val), -1)
	@ Entry condition
	@   val = new error code
	@ Exit condition
	@   val = -1
	@   Rerrno destroyed

	.macro 	__set_errno	val,Rerrno
	LDR	\Rerrno,=errno
	STR	\val,[\Rerrno]
	MOV	\val,#-1
	.endm

	.macro 	__get_errno	val,Rerrno
	LDR	\Rerrno,=errno
	LDR	\val,[\Rerrno]
	.endm

	@ NetSWI macro to call a networking (tcp/ip) swi.
	@ We ensure that we are in SVC mode before calling the SWI because,
	@ according to Stewart Brodie, certain versions of Acorn's Internet
	@ stack require SWIs to be called in SVC mode.
	@
	@ If ModuleCode is "yes", then nothing needs to be done, since
	@ it is assumed we are already in SVC mode (possibly not always true,
	@ so beware).
	@ Note, UnixLib does not define ModuleCode.
	@
	@ ip is destroyed. lr must be saved if called in SVC mode. This
	@ macro is *NOT* safe for calling from IRQ or FIQ mode because
	@ SVC_r14 is corrupted.

	.macro 	NetSWI	swiname

        @ Not really required, and not 32-bit
@	[	"\ModuleCode" <> "yes"
@	MOV	ip, pc			@ PC+PSR
@	EOR	ip, ip, #SVC_Mode	@ current mode EOR SVC_Mode
@	SWI	XOS_EnterOS		@ enter SVC mode
@	]

	SWI	\swiname

	BLVS	__net_error		@ Call net error still in SVC
@	[	"\ModuleCode" <> "yes"
@	TEQP	ip, pc			@ restore mode, corrupting flags
@	MOV	a1, a1			@ no-op to prevent contention
@	]

	.endm

	.macro 	NetSWIsimple	swiname

	MOV	ip, lr			@ Save mode, return address
@	[	"\ModuleCode" <> "yes"
@	SWI	XOS_EnterOS		@ enter SVC mode
@	]

	SWI	\swiname

	MOVVC	pc, ip			@ return, restore mode, flags
	B	__net_error_simple_entry
	@ branch to error routine still in SVC mode, with return in ip

	.endm

	@ Macros for Socket SWIs which corrupt R0
	@ Ensure R0 is 0 for VC, net_error ensures 1 for VS
	.macro 	NetSWI0	swiname

@	[	"\ModuleCode" <> "yes"
@	MOV	ip, pc			@ PC+PSR
@	EOR	ip, ip, #SVC_Mode	@ current mode EOR SVC_Mode
@	SWI	XOS_EnterOS		@ enter SVC mode
@	]

	SWI	\swiname
	MOVVC	a1, #0

	BLVS	__net_error		@ Call net error still in SVC
@	[	"\ModuleCode" <> "yes"
@	TEQP	ip, pc			@ restore mode, corrupting flags
@	MOV	a1, a1			@ no-op to prevent contention
@	]

	.endm

	@ Macro for Socket SWIs which corrupt R0
	@ Ensure R0 is 0 for VC, net_error ensures 1 for VS
	.macro 	NetSWIsimple0	swiname

	MOV	ip, lr			@ Save mode, flags, return adrress
@	[	"\ModuleCode" <> "yes"
@	SWI	XOS_EnterOS		@ enter SVC mode
@	]

	SWI	\swiname
	MOVVC	a1, #0

	MOVVC	pc, ip			@ return, restore mode, flags
	B	__net_error_simple_entry
	@ branch to error routine still in SVC mode, with return in ip

	.endm

	@ Macro to implement SWP instruction
	@ srcreg and dstreg can be the same register, provided scratch is a
	@ different register.
	@ If srcreg and dstreg are different registers then scratch can be the
	@ same as dstreg
	.macro 	swp_arm2	dstreg, srcreg, addr, scratch
	.if __SWP_ARM2 == 0
	SWP	\dstreg, \srcreg, [\addr]
	.else
	STMFD	sp!, {lr}	@ Could be called in USR or SVC mode
	SWI	XOS_IntOff
	LDR	\scratch, [\addr]
	STR	\srcreg, [\addr]
	.if \dstreg <> \scratch
	MOV	\dstreg, \scratch
	.endif
	SWI	XOS_IntOn
	LDMFD	sp!, {lr}
	.endif

	.endm

	@ Macro to change processor modes and interrupt flags
	@ Works in 26bit or 32bit modes, on all architectures
	@ Use e.g. CHGMODE a1, SVC_Mode+IFlag
	.macro 	CHGMODE	scratch, mode
	TEQ	a1, a1	@ Set Z
	TEQ	pc, pc	@ EQ if 32-bit mode
	TEQNEP	pc, #\mode
	MRSEQ	\scratch, CPSR	@ Acts a NOP for TEQP
	BICEQ	\scratch, \scratch, #0xcf	@ Preserve 32bit mode bit
	.if \mode <> 0
	ORREQ	\scratch, \scratch, #((\mode) & 0xf) + ((\mode) >> 20)
	.endif
	MSREQ	CPSR_c, \scratch
	MOV	a1, a1	@ Avoid StrongARM MSR bug

	.endm

.equ	EXIT_SUCCESS, 0
.equ	EXIT_FAILURE, 1

.equ	TRUE, 1
.equ	FALSE, 0

.equ	SIGHUP, 1	@   hangup
.equ	SIGINT, 2	@   terminal interrupt
.equ	SIGQUIT, 3	@ * ^\ from terminal
.equ	SIGILL, 4	@ * illegal instruction
.equ	SIGTRAP, 5	@ * trace trap - unused
.equ	SIGIOT, 6	@   abort
.equ	SIGABRT, 6	@   abort
.equ	SIGEMT, 7	@ * EMT (h/ware error)
.equ	SIGFPE, 8	@ * FPE trap
.equ	SIGKILL, 9	@   kill signal
.equ	SIGBUS, 10	@ * bus error
.equ	SIGSEGV, 11	@ * segmentation violation
.equ	SIGSYS, 12	@ * bad system call
.equ	SIGPIPE, 13	@   broken pipe
.equ	SIGALRM, 14	@   alarm call
.equ	SIGTERM, 15	@   termination signal
.equ	SIGURG, 16	@   urgent or out-of-band data
.equ	SIGSTOP, 17	@   stop, unblockable
.equ	SIGTSTP, 18	@   keyboard stop
.equ	SIGCONT, 19	@   continue
.equ	SIGCHLD, 20	@   child process has terminated/stopped
.equ	SIGCLD, SIGCHLD
.equ	SIGTTIN, 21	@   background read from tty
.equ	SIGTTOU, 22	@   background write to tty
.equ	SIGIO, 23	@   file descriptor is ready to perform I/O
.equ	SIGPOLL, SIGIO
.equ	SIGXCPU, 24	@   CPU time limit exceeded
.equ	SIGXFSZ, 25	@   file size limit exceeded
.equ	SIGVTALRM, 26	@   alarm call
.equ	SIGPROF, 27	@   profiler alarm call
.equ	SIGWINCH, 28	@   window size change
.equ	SIGINFO, 29	@   information request
.equ	SIGUSR1, 30	@   user signal 1
.equ	SIGUSR2, 31	@   user signal 2
.equ	SIGLOST, 32	@   resource lost
.equ	SIGOSERROR, 33	@   RISC OS error

.equ	EOPSYS, 88	@ RISC OS error


.equ	OS_WriteS, 0x000001
.equ	OS_NewLine, 0x000003
.equ	OS_Exit, 0x000011
.equ	OS_EnterOS, 0x000016
.equ	OS_GenerateError, 0x00002B

.equ	X_Bit, 0x20000

.equ	XOS_WriteC, 0x000000 + X_Bit
.equ	XOS_WriteS, 0x000001 + X_Bit
.equ	XOS_Write0, 0x000002 + X_Bit
.equ	XOS_NewLine, 0x000003 + X_Bit
.equ	XOS_ReadC, 0x000004 + X_Bit
.equ	XOS_CLI, 0x000005 + X_Bit
.equ	XOS_Byte, 0x000006 + X_Bit
.equ	XOS_Word, 0x000007 + X_Bit
.equ	XOS_File, 0x000008 + X_Bit
.equ	XOS_Args, 0x000009 + X_Bit
.equ	XOS_BGet, 0x00000A + X_Bit
.equ	XOS_BPut, 0x00000B + X_Bit
.equ	XOS_GBPB, 0x00000C + X_Bit
.equ	XOS_Find, 0x00000D + X_Bit
.equ	XOS_ReadLine, 0x00000E + X_Bit
.equ	XOS_Control, 0x00000F + X_Bit
.equ	XOS_GetEnv, 0x000010 + X_Bit
.equ	XOS_Exit, 0x000011 + X_Bit
.equ	XOS_SetEnv, 0x000012 + X_Bit
.equ	XOS_IntOn, 0x000013 + X_Bit
.equ	XOS_IntOff, 0x000014 + X_Bit
.equ	XOS_CallBack, 0x000015 + X_Bit
.equ	XOS_EnterOS, 0x000016 + X_Bit
.equ	XOS_BreakPt, 0x000017 + X_Bit
.equ	XOS_BrealCtrl, 0x000018 + X_Bit

.equ	XOS_UpdateMEMC, 0x00001A + X_Bit
.equ	XOS_SetCallBack, 0x00001B + X_Bit
.equ	XOS_Mouse, 0x00001C + X_Bit
.equ	XOS_Heap, 0x00001D + X_Bit
.equ	XOS_Module, 0x00001E + X_Bit
.equ	XOS_ReadUnsigned, 0x000021 + X_Bit
.equ	XOS_ReadVarVal, 0x000023 + X_Bit
.equ	XOS_SetVarVal, 0x000024 + X_Bit
.equ	XOS_BinaryToDecimal, 0x000028 + X_Bit
.equ	XOS_FSControl, 0x000029 + X_Bit
.equ	XOS_ChangeDynamicArea, 0x00002A + X_Bit
.equ	XOS_ValidateAddress, 0x00003A + X_Bit
.equ	XOS_CallAfter, 0x00003B + X_Bit
.equ	XOS_CallEvery, 0x00003C + X_Bit
.equ	XOS_RemoveTickerEvent, 0x00003D + X_Bit
.equ	XOS_ChangeEnvironment, 0x000040 + X_Bit
.equ	XOS_ReadMonotonicTime, 0x000042 + X_Bit
.equ	XOS_WriteN, 0x000046 + X_Bit
.equ	XOS_ReadMemMapInfo, 0x000051 + X_Bit
.equ	XOS_AddCallBack, 0x000054 + X_Bit
.equ	XOS_SerialOp, 0x000057 + X_Bit
.equ	XOS_DynamicArea, 0x000066 + X_Bit
.equ	XOS_PlatformFeatures, 0x00006D + X_Bit
.equ	XOS_SynchroniseCodeAreas, 0x00006E + X_Bit
.equ	XOS_CallASWI, 0x00006F + X_Bit
.equ	XOS_AMBControl, 0x000070 + X_Bit
.equ	XOS_CallASWIR12, 0x000071 + X_Bit
.equ	XOS_ConvertHex8, 0x0000D4 + X_Bit
.equ	XOS_ConvertInteger1, 0x0000D9 + X_Bit
.equ	XOS_ConvertInteger2, 0x0000DA + X_Bit
.equ	XOS_ConvertInteger3, 0x0000DB + X_Bit
.equ	XOS_ConvertInteger4, 0x0000DC + X_Bit
.equ	XOS_WriteI, 0x000100 + X_Bit

.equ	XFPEmulator_Version, 0x040480 + X_Bit

.equ	Socket_Creat, 0x41200
.equ	Socket_Bind, 0x41201
.equ	Socket_Listen, 0x41202
.equ	Socket_Accept, 0x41203
.equ	Socket_Connect, 0x41204
.equ	Socket_Recv, 0x41205
.equ	Socket_Recvfrom, 0x41206
.equ	Socket_Recvmsg, 0x41207
.equ	Socket_Send, 0x41208
.equ	Socket_Sendto, 0x41209
.equ	Socket_Sendmsg, 0x4120A
.equ	Socket_Shutdown, 0x4120B
.equ	Socket_Setsockopt, 0x4120C
.equ	Socket_Getsockopt, 0x4120D
.equ	Socket_Getpeername, 0x4120E
.equ	Socket_Getsockname, 0x4120F
.equ	Socket_Close, 0x41210
.equ	Socket_Select, 0x41211
.equ	Socket_Ioctl, 0x41212
.equ	Socket_Read, 0x41213
.equ	Socket_Write, 0x41214
.equ	Socket_Stat, 0x41215
.equ	Socket_Readv, 0x41216
.equ	Socket_Writev, 0x41217
.equ	Socket_Gettsize, 0x41218
.equ	Socket_Sendtosm, 0x41219
.equ	Socket_Sysctl, 0x4121A
.equ	Socket_Accept_1, 0x4121B
.equ	Socket_Recvfrom_1, 0x4121C
.equ	Socket_Recvmsg_1, 0x4121D
.equ	Socket_Sendmsg_1, 0x4121E
.equ	Socket_Getpeername_1, 0x4121F
.equ	Socket_Getsockname_1, 0x41220
.equ	Socket_InternalLookup, 0x41221
.equ	Socket_Version, 0x41222

.equ	Internet_GetHostByName, 0x46000
.equ	Internet_GetHostByAddr, 0x46001

.equ	XSocket_Creat, Socket_Creat + X_Bit
.equ	XSocket_Bind, Socket_Bind + X_Bit
.equ	XSocket_Listen, Socket_Listen + X_Bit
.equ	XSocket_Accept, Socket_Accept + X_Bit
.equ	XSocket_Connect, Socket_Connect + X_Bit
.equ	XSocket_Recv, Socket_Recv + X_Bit
.equ	XSocket_Recvfrom, Socket_Recvfrom + X_Bit
.equ	XSocket_Recvmsg, Socket_Recvmsg + X_Bit
.equ	XSocket_Send, Socket_Send + X_Bit
.equ	XSocket_Sendto, Socket_Sendto + X_Bit
.equ	XSocket_Sendmsg, Socket_Sendmsg + X_Bit
.equ	XSocket_Shutdown, Socket_Shutdown + X_Bit
.equ	XSocket_Setsockopt, Socket_Setsockopt + X_Bit
.equ	XSocket_Getsockopt, Socket_Getsockopt + X_Bit
.equ	XSocket_Getpeername, Socket_Getpeername + X_Bit
.equ	XSocket_Getsockname, Socket_Getsockname + X_Bit
.equ	XSocket_Close, Socket_Close + X_Bit
.equ	XSocket_Select, Socket_Select + X_Bit
.equ	XSocket_Ioctl, Socket_Ioctl + X_Bit
.equ	XSocket_Read, Socket_Read + X_Bit
.equ	XSocket_Write, Socket_Write + X_Bit
.equ	XSocket_Stat, Socket_Stat + X_Bit
.equ	XSocket_Readv, Socket_Readv + X_Bit
.equ	XSocket_Writev, Socket_Writev + X_Bit
.equ	XSocket_Gettsize, Socket_Gettsize + X_Bit
.equ	XSocket_Sendtosm, Socket_Sendtosm + X_Bit
.equ	XSocket_Sysctl, Socket_Sysctl + X_Bit
.equ	XSocket_Accept_1, Socket_Accept_1 + X_Bit
.equ	XSocket_Recvfrom_1, Socket_Recvfrom_1 + X_Bit
.equ	XSocket_Recvmsg_1, Socket_Recvmsg_1 + X_Bit
.equ	XSocket_Sendmsg_1, Socket_Sendmsg_1 + X_Bit
.equ	XSocket_Getpeername_1, Socket_Getpeername_1 + X_Bit
.equ	XSocket_Getsockname_1, Socket_Getsockname_1 + X_Bit
.equ	XSocket_InternalLookup, Socket_InternalLookup + X_Bit
.equ	XSocket_Version, Socket_Version + X_Bit

.equ	XInternet_GetHostByName, Internet_GetHostByName + X_Bit
.equ	XInternet_GetHostByAddr, Internet_GetHostByAddr + X_Bit

.equ	XSharedCLibrary_LibInitAPCS_R, 0x080681 + X_Bit
.equ	XSharedCLibrary_LibInitModule, 0x080682 + X_Bit

.equ	XMessageTrans_ErrorLookup, 0x041506 + X_Bit

.equ	XTaskWindow_TaskInfo, 0x043380 + X_Bit
.equ	XWimp_ReadSysInfo, 0x0400F2 + X_Bit
.equ	XWimp_SlotSize, 0x0400EC + X_Bit
.equ	XDDEUtils_SetCLSize, 0x042581 + X_Bit

.equ	XSharedUnixLibrary_RegisterUpCall, 0x55c80 + X_Bit
.equ	XSharedUnixLibrary_DeRegisterUpCall, 0x55c81 + X_Bit
.equ	XSharedUnixLibrary_SetValue, 0x55c82 + X_Bit
.equ	XSharedUnixLibrary_Count, 0x55c83 + X_Bit

