;***************************************************************************;
;                                                                           ;
;                    Hazeltine Esprit II Terminal Driver                    ;
;                                                                           ;
;                         Author: David F. Pallmann                         ;
;            Copyright (C) 1984 by LMS Technical Services, Inc.             ;
;                                                                           ;
;***************************************************************************;

OBJNAM  ESPRIT.TDV

;universals
SEARCH  SYS
SEARCH  SYSSYM
SEARCH  TRM

LI=233                                  ;lead-in code (escape)

;ASCII chars
BS=10
LF=12
CR=15

;Terminal driver communications area

ESPRIT: WORD    TD$NEW                  ;terminal attributes
       BR      JMPINP                  ;input routine
       RTN                             ;output routine
       BR      ECHO                    ;echo routine
       BR      JMPCRT                  ;crt control
       RTN                             ;no init routine yet
       WORD    0                       ;no impure area yet
       BYTE    24.                     ;number of rows
       BYTE    80.                     ;number of columns
       LWORD   TD$LID!TD$DIM!TD$EOL!TD$EOS

JMPINP: JMP     INP                     ;go handle input characters
JMPCRT: JMP     CRT                     ;go handle tcrt codes

;ECHO
;Special echo processing is performed here.
;DEL will backspace and erase the previous character.
;^U will erase the entire line by backspacing and erasing.

ECHO:   CMPB    D1,#'U-'@               ;^U
       BEQ     CTRLU
       CMPB    D1,#177                 ;DEL
       BNE     ECHX

;DELs are handled by the old backspace-and-erase game.
;Special handling must be performed if we are deleting a <TAB>.
;D6 contains the character being deleted.

RUBOUT: CMPB    D6,#'I-'@               ;was it a tab?
       BEQ     RBTB                    ;  yes

;DEL was of a printable character - queue up the backspace sequence
KRTG:   MOV     #3,D3                   ;set character count
       LEA     A6,ERUB                 ;set buffer address
       MOV     A6,D1                   ;  into d1
       TRMBFQ                          ;queue the backspace sequence
       RTN
ERUB:   BYTE    10,40,10,0

;DEL was of a <TAB> - calculate how big the <TAB> was and backup over it.
RBTB:   CLR     D3                      ;preclear D3
       MOVW    T.POB(A5),D3            ;set beginning position count
       MOV     T.ICC(A5),D2            ;set input character count
       MOV     T.IBF(A5),A6            ;set input buffer base
KRTS:   DEC     D2                      ;done with scan?
       BMI     KRTQ                    ;  yes
       MOVB    (A6)+,D1                ;scan forward calculating position
       CMPB    D1,#11                  ;<TAB>
       BEQ     KRTT
       CMPB    D1,#15                  ;<CR>
       BEQ     KRTC
       CMPB    D1,#33                  ;<ESC>
       BEQ     KRTI
       CMPB    D1,#40                  ;control character
       BLO     KRTS
       CMPB    D1,#172
       BHI     KRTS
KRTI:   INC     D3                      ;increment position for one character
       BR      KRTS
KRTT:   ADD     #10,D3                  ;adjust position for <TAB>
       AND     #^C7,D3
       BR      KRTS
KRTC:   CLR     D3                      ;clear position for <CR>
       BR      KRTS
KRTQ:   COM     D3                      ;calculate necessary backspaces
       AND     #7,D3
       INC     D3
       MOV     #10,D1                  ;set immediate backspace character
       TRMBFQ                          ;queue the backspaces
ECHX:   RTN

;Echo a control-U by erasing the entire line.
CTRLU:  TST     D6                      ;no action if nothing to erase
       BEQ     CTUX
       CLR     D3                      ;preclear D3
       MOVW    T.POO(A5),D3            ;calculate backspace number to erase the line
       SUBW    T.POB(A5),D3
       BEQ     ECHX
       CMP     D3,T.ILS(A5)            ;insure not greater than terminal width
       BLOS    CLUA
       MOV     T.ILS(A5),D3
CLUA:   MOV     #10,D1                  ;queue up backspaces
       TRMBFQ
       ASL     D1,#2                   ;queue up spaces
       TRMBFQ
       MOV     #10,D1                  ;queue up backspaces
       TRMBFQ
CTUX:   RTN

;INP
;Input character processing subroutine.
;Return a negative flag to indicate possible multi-byte key codes.
;Detect a negative flag which indicates the multi-byte processing return.
INP:    AND     #177,D1                 ;strip char to 7 bits
       LCC     #0
       RTN

PAGE
;CRT
;Special crt control processing.
;D1 contains the control code for X,Y positioning or special commands.
;If D1 is positive we have screen positioning (row in hi byte, col in lo).
;If D1 is negative we have the special command in the low byte.

CRT:    TSTW    D1                      ;is it cursor position?
       BMI     CRTS                    ;  no

;Cursor positioning - D1 contains X,Y coordinates
       TTYI                            ;send position command
       BYTE    LI,17.,0,0
       ADD     #<95._8.>-1,D1
       CMPB    D1,#31.
       BHIS    100$
       ADDB    #96.,D1
100$:   TTY                             ;send column first
       ROR     D1,#8.
       TTY                             ;send row second
       RTN

;Special commands - D1 contains the command code in the low byte
CRTS:   AND     #377,D1                 ;strip the high byte
       BNE     CRTU                    ;  and branch unless clear screen
       TTYI                            ;special case for clear screen
       BYTE    LI,28.,0
       EVEN

;output some <NUL>s after screen-oriented functions
CRTZ:   MOV     #45.,D0
       CLR     D1
10$:    TTY
       SOB     D0,10$
       RTN

;Command processing per director tables
CRTU:   PUSH    A2                      ;save A2
       ASL     D1                      ; times 2 (word offset).
       CMP     D1,#CRCB-CRCA           ;check for valid code
       BHI     CRTX                    ;  and bypass if bad
       LEA     A2,CRCA-2               ;index the table
       ADD     D1,A2                   ;add command code
       MOVW    @A2,D1                  ;pick up data field offset
       ADD     D1,A2                   ;make absolute data address
       TTYL    @A2                     ;print the data field
       CMPB    @A2,#36                 ;sleep if home command
       BEQ     CRTXZ
       CMPB    1(A2),#100              ;sleep if erase command
       BHI     CRTXZ
CRTX:   POP     A2                      ;restore A2
       RTN

CRTXZ:  POP     A2                      ;restore A2
       BR      CRTZ                    ;  and go output nulls

;Byte offset and data tables follow for all commands
CRCA:   WORD    C1-.,C2-.,C3-.,C4-.,C5-.,C6-.,C7-.,C8-.
       WORD    C9-.,C10-.,C11-.,C12-.,C13-.,C14-.,C15-.,C16-.
CRCB:
PAGE

DEFINE  CRT     CODE1,CODE2,CODE3
       IF      NB,CODE1,BYTE CODE1
       IF      NB,CODE2,BYTE CODE2
       IF      NB,CODE3,BYTE CODE3
       BYTE    0
       ENDM

C0:     CRT                             ;clear screen
C1:     CRT     LI,22                   ;cursor home
C2:     CRT     CR                      ;cursor return
C3:     CRT     LI,14                   ;cursor up
C4:     CRT     LF                      ;cursor down
C5:     CRT     BS                      ;cursor left
C6:     CRT     20                      ;cursor right
C7:     CRT     LI,25                   ;lock keyboard
C8:     CRT     LI,6                    ;unlock keyboard
C9:     CRT     LI,17                   ;erase to end of line
C10:    CRT     LI,'W-'@                ;erase to end of screen (background only)
C11:    CRT     LI,31                   ;low intensity
C12:    CRT     LI,37                   ;high intensity
C13:    CRT     LI,37                   ;enable prot. fields
C14:    CRT     LI,31                   ;disable prot. fields
C15:    CRT     LI,19.                  ;delete line
C16:    CRT     LI,26.                  ;insert line
       EVEN

       END