;*; Updated on 26-Dec-86 at 12:22 PM by Cary Fitch; edit time: 0:03:57
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;       ATTACH - AlphaBASIC subroutine to attach job to another terminal
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;Commissioned by: Cary Fitch, NTSC
;
;Written by: David Pallmann, UltraSoft
;
;Edit History:
;1.0  23-Dec-86  created. /DFP
;1.01 23-Dec-86  clear T.JLK on old terminal [1-CF]
;                       (disconnects previous terminal from job)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;       Calling format:
;
;               XCALL ATTACH, address
;
;       Where:
;
;               address - is a 4-byte binary value that contains the
;                         address of the TCB to attach to.  Typically, this
;                         is the value returned by a prior TRMADR XCALL.
;
;       Example:
;
;               MAP1 TRMLOC, B, 4
;                       ...
;               XCALL TRMADR, "TRM1", TRMLOC
;               IF TRMLOC=0 THEN GOTO TERM'ERROR
;                       ...
;               XCALL ATTACH, TRMLOC
;
;       Uses:   Allows printing to another terminal or port using "print
;               using" and other convenient means from within a BASIC program.
;
;               Allows input from another terminal in a BASIC program using
;               normal INPUT or INPUT LINE statement.
;
;               Allows use of XCALL INFLD from another terminal, with use of
;               "time out feature" of INFLD, control can return to original
;               terminal at predetermined time.
;
;               Useful for getting input from devices such as card readers
;               etc. in control applications.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

       VMAJOR=1
       VMINOR=1
       VSUB=0

       OBJNAM  .SBR

       SEARCH  SYS
       SEARCH  SYSSYM
       SEARCH  TRM
       COPY    XCALL

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

;check argument count

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

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

CHKBIN: CMPW    TYPE1(A3),#BINARY       ; 1st argument a binary?
       JNE     BINERR                  ;  no - error
       CMP     SIZE1(A3),#4            ; 1st argument 4 bytes long
       JNE     BINERR                  ;  no - error

; detach terminal from job              [1-CF]

DETACH: JOBIDX                          ; get our job           [1-CF]
       MOV     JOBTRM(A6),A6           ; index the term        [1-CF]
       CLR     T.JLK(A6)               ; clear trm link to job [1-CF]

;if terminal in question is already attached, detach it

       MOV     ADDR1(A3),A0            ; point to 1st argument
       MOV     @A0,A5                  ; A5 is new TCB address
       TST     T.JLK(A5)               ; is new terminal attached?
       BEQ     ATTACH                  ;  no
       MOV     T.JLK(A5),A0            ;  yes - get JCB addres in A0
       CLR     JOBTRM(A0)              ; clear JCB link to TCB
;[1-CF] CLR     T.JLK(A5)               ; clear TCB link to JCB

;attach terminal to our job

ATTACH: JOBIDX  A1                      ; point to own JCB
       MOV     A5,JOBTRM(A1)           ; set JCB link to TCB
       MOV     A1,T.JLK(A5)            ; set TCB link to JCB
       RTN                             ; return to program

;incorrect number of arguments in XCALL

CNTERR: TYPESP  ?Incorrect number of arguments
       BR      ERROR

;1st argument not a 4-byte binary in XCALL

BINERR: TYPESP  ?1st argument not a 4-byte binary

;general error reporting

ERROR:  TYPECR  in ATTACH.SBR
       EXIT

       END