;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;	TRMADR - AlphaBASIC subroutine to return Term Ctl Blk address
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;Commissioned by: Cary Fitch, NTSC
;
;Written by: David Pallmann, UltraSoft
;
;Edit History:
;1.0  23-Dec-86  created. /DFP
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;	Calling format:
;
;		XCALL TRMADR, term$, address
;
;	Where:
;
;		term$ - is a string variable or constant containing the
;			name of the terminal/modem/printer in question
;
;		address - is a 4-byte binary variable that receives the
;			  address of the TCB in question.  A zero is returned
;			  if a non-existent TCB name is supplied in term$.
;
;	Example:
;
;		MAP1 TRMLOC, B, 4
;			...
;		XCALL TRMADR, "TRM1", TRMLOC
;		IF TRMLOC=0 THEN GOTO TERM'ERROR
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	VMAJOR=1
	VMINOR=0
	VSUB=0

	OBJNAM	.SBR

	SEARCH	SYS
	SEARCH	SYSSYM
	COPY	XCALL

	PHDR	-1,,PH$REE!PH$REU

;check argument count

CHKCNT:	CMPW	COUNT(A3),#2		; 2 arguments in XCALL?
	JNE	CNTERR			;  no - error

;check that first argument is of type string

CHKSTR:	CMPW	TYPE1(A3),#STRING	; 1st argument a string?
	JNE	STRERR			;  no - error

;check that second argument is of type binary and is 4 bytes long

CHKBIN:	CMPW	TYPE2(A3),#BINARY	; 2nd argument a binary?
	JNE	BINERR			;  no - error
	CMP	SIZE2(A3),#4		; 2nd argument 4 bytes long
	JNE	BINERR			;  no - error

;convert ASCII terminal name to RAD50 equivalent

CONVRT:	MOV	A4,A1			; set up A1 for PACK call
	MOV	ADDR1(A3),A2		; set up A2 for PACK call
	PACK				; convert ASCII term name
	PACK				;  to RAD50

;scan the terminal definition chain for the specified terminal

SCAN:	MOV	TRMDFC,D7		; point to base of table
10$:	MOV	D7,A5			; copy index to addr reg
	CTRLC	NOTFND			; ^C check
	CMM	4(A5),@A4		; name match?
	BEQ	FOUND			;  yes - branch
	MOV	@A5,D7			;  no - point to next link
	BNE	10$			; branch unless end-of-chain

;terminal not found - return zero

NOTFND:	MOV	ADDR2(A3),A0		; point to 2nd argument address
	CLR	@A0			; return zero
	RTN				; return to program

;terminal found - return address

FOUND:	ADD	#10,A5			; bypass link anb name
	MOV	ADDR2(A3),A0		; point to 2nd argument
	MOV	A5,@A0			; set address
	RTN				; return

;incorrect number of arguments in XCALL

CNTERR:	TYPESP	?Incorrect number of arguments
	BR	ERROR

;1st argument not a string in XCALL

STRERR:	TYPESP	?1st argument not a string
	BR	ERROR

;2nd argument not a 4-byte binary in XCALL

BINERR:	TYPESP	?2nd argument not a 4-byte binary

;general error reporting

ERROR:	TYPECR	in TRMADR.SBR
	EXIT

	END