;TS3.MAC contains some globally used subroutines.
; See list of entry points for names.

	PAGE 70
	.Z80

	TITLE TS3

;CP/M specific constants
	EXTERNAL BDOS	;address of BDOS jump

	SUBTTL VARIOUS SUBROUTINES

	ENTRY FCTINI,STEST,ARGMT,SUCCA,CHECK

FCTINI:	;initializes a new function argument list
	EXTERNAL ALIST,$ALIST,ARGST,$ARGST,$STACK
	EXTERNAL STATUS
	LD HL,($STACK)
	INC HL
	INC HL
	INC HL
	LD (ALIST),HL
	LD ($ALIST),HL
	XOR A
	LD (HL),A
	INC HL
	LD (HL),A
	LD ($ARGST),HL
	INC HL
	LD (ARGST),HL
;CALL STATUS
;DB 'after FCTINI$'
	RET

STEST:	;checks for singular function symbols
	EXTERNAL STATE,CCF,$ARGST
	LD HL,(STATE)	;IF STATE>=0 GOTO STEST2
STEST1:	LD DE,0
	SCF
	CCF
	SBC HL,DE
	JP P,STEST2
	LD A,(CCF)	;(USE PSTFC(CCF,ARGST))
	LD DE,($ARGST)
	INC DE
	LD (DE),A
	LD ($ARGST),DE
	INC HL		;INCR STATE
	LD (STATE),HL
	JP STEST1
STEST2:	RET

ARGMT:	;P1: A  = no. of argument in ALIST
	;P2: IY = spec of string to contain argument
	LD HL,(ALIST)	;P2.=ALIST
ARGMT1:	CP 1		;IF P1=1 GOTO ARGMT2
	JP Z,ARGMT2
	DEC A		;DECR P1
	LD E,(HL)	;P2.=P2+c(P2)
	INC HL
	LD D,(HL)
	DEC HL
	ADD HL,DE
	EX DE,HL
	LD HL,($ALIST)	;IF P2#$ALIST GOTO ARGMT1
	SCF
	CCF
	SBC HL,DE
	EX DE,HL
	JR NZ,ARGMT1
	LD (IY+0),L
	LD (IY+1),H
	DEC HL		;$P2.=P2-1
	LD (IY+2),L
	LD (IY+3),H
	JP ARGMT3
ARGMT2:	LD E,(HL)
	INC HL
	LD D,(HL)
	INC HL
	LD (IY+0),L
	LD (IY+1),H
	DEC HL		;$P2.=P2+c(P2)
	DEC HL
	ADD HL,DE
	DEC HL		;DECR $P2
	LD (IY+2),L
	LD (IY+3),H
ARGMT3:	RET

SUCCA:	;P1: IX = string spec of argument in ALIST
	;P2: IY = string spec to contain successor argument
	LD L,(IX+2)	;P2.=$P1+1
	LD H,(IX+3)
	PUSH HL
	INC HL
	LD E,(HL)
	INC HL
	LD D,(HL)
	INC HL		;INCR P2
	LD (IY+0),L
	LD (IY+1),H
	POP HL
	ADD HL,DE	;$P2.=$P1+c(P1)
	LD (IY+2),L
	LD (IY+3),H
	RET

CHECK:	;HL = string length to be checked against space between
	;     $VALST and ACTST.
	;Note:	CHECK1 and CHECK2 serve as side entries when
	;	an overflow was detected. In this case, 
	;	an error message is issued,
	;	the current VALST will be emptied,
	;	and the subroutine exits directly to recovery 
	;	point XEQFC3 in the main algorithm.
	INCLUDE ERRMSG.INC
	EXTERNAL ACTST,VALST,$VALST,TRCFLG,STKPTR,XEQFC3
	ENTRY CHECK1,CHECK2
	PUSH HL
	PUSH DE
	LD DE,($VALST)
	ADD HL,DE
	LD DE,(ACTST)
	SCF
	CCF
	SBC HL,DE
	POP DE
	POP HL
	RET C
CHECK1:	ERRMSG 'A'
	LD HL,(VALST)
	DEC HL
	LD ($VALST),HL
CHECK2:	LD A,1
	LD (TRCFLG),A
	LD HL,(STKPTR)
	LD SP,HL
	JP XEQFC3

	END
