*
*
*	ROUTINE TO EXECUTE SYSCALL IN USERSPACE
*
*	X REG POINTS TO SYSCALL BLOCK IN USER SPACE
*
*
	WITH	NDMP,WI=132
	TABS	16,26,42,50,58
	INCLUDE	SDOSIOPKDEFS.ASM
USER0	EQU	1
SYSTEM	EQU	0
BANK	EQU	$FAE6
SYSSCALLBLOCK	EQU	$100
*
*	STORAGE DEFS
*
	ORG	$FE00	AS GOOD AS ANY TIL I FIND OUT WHERE
	JMP	SCALLPASS	JUMP AROUND THE STORAGE
TRANSFERCOUNT	RMB	2
SOURCEPOINT	RMB	2
DESTPOINT	RMB	2
USERSYSCALPOINT	RMB	2
USERSTACK	RMB	2
	RMB	30	SAVE ROOM FO OUR STACK
OURSTACK	EQU	*-1
SCALLPASS	EQU	*
	STS	USERSTACK	SAVE THE USER STACKPOINTER
	LDS	#OURSTACK	USE THIS STACK WHILE WE ARE HERE
	STX	USERSYSCALPOINT	SAVE THE POINTER TO SYSCALL BLOCK
	LDAA	SCBLK:OPCODE,X	GET THE SYSCALL OP CODE
	LDX	#SYSCALLTABLE
	JSR	WHICH
	BEQ	SP1	BRANCH IF THERE IS NO SIDE EFFECT
	JMP	0,X	GO FAKE THE SIDE EFFECT
SP1	LDX	USERSYSCALPOINT	GET THIS BACK
	LDAA	SCBLK:WLEN,X
	STAA	TRANSFERCOUNT+1
	CLR	TRANSFERCOUNT	GET SET TO MOVE THE SYSCALL
	STX	SOURCEPOINT
	LDX	#SYSSCALLBLOCK	STX	DESTPOINT
	JSR	TRANSFERUSER0TOSYS
	LDX	USERSYSCALPOINT	GET THIS AGAIN
	LDX	SCBLK:WRLEN,X	GET THE LENGTH OF THE WRITE BUFFER
	CPX	#0	IS THERE A WRITE BUFFER?
	BEQ	SP2	BRANCH IF THERE IS NOT
	STX	TRANSFERCOUNT
	LDX	USERSYSCALPOINT
	LDX	SCBLK:WRBUF,X
	STX	SOURCEPOINT
*	DESTPOINT ALREADY POINTS TO THE NEXT AVAIL BYTE IN SYS SPACE
*
	JSR	TRANSFERUSER0TOSYS
*
SP2	LDX	#SYSSCALLBLOCK	CHANGE THE REPLY POINTER IN SCBLK
	LDAA	DESTPOINT
	STAA	SCBLK:RDBUF,X
	LDAA	DESTPOINT+1
	STAA	SCBLK:RDBUF+1,X
*
*
*
	JSR	SWITCHTOSYS	BRING IN SDOS
	LDX	#SYSSCALLBLOCK	GET POINTER TO THE COPIED SYSCALL
	JSR	SYSCALL$	DO THE SYSCALL
	TPA		SAVE THE CONDITION CODE
	PSHA
	LDX	#SYSSCALLBLOCK
	LDX	SCBLK:RPLEN,X
	CPX	#0
	BEQ	SP3	BRANCH IF THERE IS NO REPLY
	STX	TRANSFERCOUNT	SET UP TO MOVE THE REPLY
	LDX	SYSSCALLBLOCK
	LDX	SCBLK:RDBUF,X
	STX	SOURCEPOINT
	JSR	SWITCHTOUSER0
	LDX	USERSYSCALPOINT
	LDX	SCBLK:RDBUF,X
	STX	DESTPOINT
	JSR	TRANSFERSYSTOUSER0
	JSR	SWITCHTOSYS
SP3	LDX	#SYSSCALLBLOCK
	*
*
*	FIX CHANGING REPLY LENGTH!!!!!
	*
	PULA
	TAP
	RTS		WITH THE ERROR FLAG SET RIGHT
	PAGE
SYSCALLTABLE	FCB	SYSCALL:LOAD
	FDB	LOAD
	FCB	SYSCALL:CHAIN
	FDB	CHAIN
	FCB	0
	PAGE
*
*	WHICH ROUTINE
*		LOOKS UP THE CONTENS OF THE A REG 
*		IN A TABLE POINTED TO BY TH X REG
*		IF A MATCH IS FOUND IT RETURNS A VALUE IN X
*		ELSE RETURNS WITH THE CC SET TO NOT EQUAL
*
*
WHICH	LDAB	0,X
	BEQ	WHICHRTS
	CBA
	BEQ	GOTWHICH
	INX
	INX
	INX
	BRA	WHICH
GOTWHICH	LDX	1,X
WHICHRTS	RTS
	PAGE
LOAD	EQU	*
	PAGE
CHAIN	EQU	*
	PAGE
TRANSFERUSER0TOSYS	EQU	*
	LDAB	#USER0
	STAB	BANK	POINT AT USER BANK
	LDX	SOURCEPOINT
	LDAA	0,X
	INX
	STX	SOURCEPOINT
	LDAB	#SYSTEM
	STAB	BANK	POINT AT SYSTEM BANK
	LDX	DESTPOINT
	STAA	0,X
	INX
	STX	DESTPOINT
	LDX	TRANSFERCOUNT
	DEX
	STX	TRANSFERCOUNT	DONE?
	BNE	TRANSFERUSER0TOSYS	NO,GET NEXT BYTE
	RTS				YES,RETURN
*
*
*
TRANSFERSYSTOUSER0	LDAB	#SYSTEM
	STAB	BANK	POINT AT SYSTEM BANK
	LDX	SOURCEPOINT
	LDAA	0,X
	INX
	STX	SOURCEPOINT
	LDAB	#USER0
	STAB	BANK	POINT AT USER BANK
	LDX	DESTPOINT
	STAA	0,X
	INX
	STX	DESTPOINT
	LDX	TRANSFERCOUNT
	DEX
	STX	TRANSFERCOUNT
	BNE	TRANSFERSYSTOUSER0
	RTS
		PAGE
SWITCHTOUSER0	EQU	*
SWITCHTOSYS	EQU	*
	END
	