;TDV - change terminal driver (loads from terminal definition or DSK0:[1,6])
;David F. Pallmann, 10-Jul-84
;usage: .TDV {driver}

       SEARCH  SYS
       SEARCH  SYSSYM
       SEARCH  TRM

       .OFINI
       .OFDEF  DRIVER,4                ;driver name
       .OFDEF  FILE,D.DDB              ;DDB to FETCH with
       .OFSIZ  MEMSIZ

START:  PHDR    -1,0,PH$REE!PH$REU      ;program header
       GETIMP  MEMSIZ,A5               ;allocate local memory

GETNAM: BYP                             ;bypass leading white space
       LEA     A1,DRIVER(A5)           ;point to storage
       PACK                            ;store driver
       PACK                            ; name RAD50

;SCAN TERMINAL DRIVER CHAIN

CHKTDC: MOV     TRMTDC,D0               ;D0 := current terminal driver
10$:    MOV     D0,A4                   ;A4 := current terminal driver
       CMM     4(A4),DRIVER(A5)        ;is this the specified driver?
       BEQ     CHGTDC                  ; yes - branch
       MOV     @A4,D0                  ;D0 := link to next driver
       BNE     10$                     ;branch unless end-of-table
       BR      CHKDSK                  ;branch - load driver from disk

CHGTDC: JOBIDX  A0                      ;A0 := job control block
       MOV     JOBTRM(A0),A1           ;A1 := terminal definition area
       ADD     #10,A4                  ;A4 := driver entry point
       MOV     A4,T.TDV(A1)            ;set new driver index
       JMP     EXIT                    ;done

;SCAN USER MEMORY, SYSTEM MEMORY, AND DISK (DSK0:[1,6])

CHKDSK: LEA     A2,TDVFIL
       FSPEC   FILE(A5),TDV
       MOV     DRIVER(A5),FILE+D.FIL(A5)
       FETCH   FILE(A5),A4
       BEQ     CHGDSK
       TYPECR  <? terminal driver not found in memory or on DSK0:[1,6]>
       CRLF
       BR      EXIT

CHGDSK: JLOCK                           ;lock out other jobs
       MOV     -14(A4),D5              ;D5 := size of terminal driver module
       MOV     -6(A4),-4(A4)           ;move driver name
       SUB     #6,D5                   ;D5 := size of terminal driver code
       SUB     #6,A4                   ;A4 := pointer to terminal driver code
       JOBIDX  A3                      ;A3 := job control block
       MOV     JOBBAS(A3),A2           ;A2 := base of partition
       ADD     JOBSIZ(A3),A2           ;A2 := end of partition
       SUB     D5,JOBSIZ(A3)           ;correct job size
       SUB     D5,A2                   ;A2 := address to copy driver to
       MOV     A2,A0                   ;A0 := address to copy driver to
10$:    MOVB    (A4)+,(A0)+             ;copy byte
       SOB     D5,10$                  ;loop till done

       JOBIDX  A0                      ;A0 := job control block
       MOV     JOBTRM(A0),A1           ;A1 := terminal definition area
       ADD     #6,A2                   ;A2 := driver entry point
       MOV     A2,T.TDV(A1)            ;set new driver index
       JUNLOK                          ;revive other jobs

EXIT:   EXIT                            ;exit

TDVFIL: ASCIZ   /DSK0:X.TDV[1,6]/       ;filespec for DVR:
       EVEN

       END