;***************************************************************************;
;                                                                           ;
;                         Ampex D81 Terminal Driver                         ;
;                                                                           ;
;                         Author: David F. Pallmann                         ;
;             Copyright (C) 1984 - LMS Technical Services, Inc.             ;
;                                                                           ;
;***************************************************************************;

       OBJNAM  AMPEX.TDV

       SEARCH  SYS
       SEARCH  SYSSYM
       SEARCH  TRM

       ESC=233                         ;<ESC> (lead-in code)

       ;Terminal driver communications area

AMPEX:  WORD    TD$NEW                  ;Terminal attributes
       BR      JMPINP                  ;Input routine
       RTN                             ;Output routine
       BR      ECHO                    ;Echo routine
       BR      JMPCRT                  ;CRT control
       RTN                             ;No init routine
       WORD    0                       ;No impure area
       BYTE    24.                     ;Row count
       BYTE    80.                     ;Column count
       LWORD   TD$LID!TD$CID!TD$DIM!TD$EOS!TD$EOL!TD$BLN!TD$UND!TD$RVA!TD$MLT

JMPINP: JMP     INPUT                   ;Jump to input routine
JMPCRT: JMP     CRT                     ;Jump to CRT routine

       ;**********
       ;*  ECHO  *
       ;**********

       ;Special echo processing is performed here
       ;<DEL>s 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

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

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

       ;Delete 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

       ;Deletion was of a <TAB> - we must 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-char
       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 ^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

       ;***********
       ;*  INPUT  *
       ;***********

       ;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

INPUT:  BMI     INMLT                   ;skip if multi-byte processing
       CMPB    D1,#1                   ;function code?
       BEQ     INPM                    ;  yes - could be multi-byte sequence
       CMPB    D1,#33                  ;escape?
       BEQ     INPM                    ;  yes - could be multi-byte sequence
       LCC     #0                      ;   no - normal processing
       RTN

INPM:   LCC     #PS.N                   ;possible multi-byte - return N flag
       RTN

       ;Multi-byte processing is done here
       ;This occurs when TRMSER has accumulated all bytes of a multi-byte keystroke
       ;D0 contains the character count and A0 indexes the data string
       ;A5 indexes the terminal definition block and must be preserved
       ;The translated character must be returned in D1 for storage
       ;This routine may destroy only A0,A3,A6,D0,D6,D7

INMLT:  MOVB    (A0)+,D1                ;get the first character
       DECB    D0                      ;no translation if single character
       BEQ     INMX
       CMPB    D1,#1                   ;function sequences start with SOH
       BEQ     INMF                    ;  function sequence -

       ;Escape sequences are translated directly by setting bit 7 on
       ;This will cause them to map to 240-377

       MOVB    (A0)+,D1                ;get the second character
INMG:   ORB     #200,D1                 ;set bit 7 on
       BIT     #T$XLT,@A5              ;are we doing translation?
       BEQ     INMNOT                  ;  no - check for another translation
INMX:   LCC     #0                      ;reset the flags
INMX2:  RTN

       ;Function codes require additional translation so that they become 200-237
       ;Unshifted function keys map to 200-212
       ;Shifted function keys map to 240-252

INMF:   MOVB    (A0)+,D1                ;get the second character
       SUBB    #'@,D1                  ;offset so that F1 becomes 0
       BR      INMG                    ;  and go finish up

       ;Come here if program is not doing translation and we must do our own

INMNOT: LEA     A6,XLTTBL               ;index the translation table
10$:    MOVB    (A6)+,D7                ;get character
       BEQ     INMX2                   ;  end of table - ignore the character
       CMPB    D1,D7                   ;is it in the table?
       BEQ     20$                     ;  yes -
       INC     A6                      ;  no - bypass translation
       BR      10$                     ;loop for more -

       ;Come here to translate the character

20$:    MOVB    @A6,D1                  ;translate the character
       BR      INMX

       ;Translation table

XLTTBL: BYTE    327,'D-'@               ;CHAR DEL -> ^D
       BYTE    321,'F-'@               ;CHAR INS -> ^F
       BYTE    305,'B-'@               ;LINE INS -> ^B
       BYTE    322,'Z-'@               ;LINE DEL -> ^Z
       BYTE    324,'Y-'@               ;LINE ERASE -> ^Y
       BYTE    311,212                 ;BACK TAB = T925 F11 = AM60 F16
       BYTE    000,000

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 byte)
       ;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    ESC,75,0,0
       ADD     #17437,D1               ;add position offsets
       ROR     D1,#8.                  ;send row first
       TTY
       ROL     D1,#8.                  ;send column second
       TTY
       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    ESC,'+,0
       EVEN
CRTZ:   TTYL    CRTNUL                  ; output some nulls
       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
CRTX:   POP     A2                      ;restore A2
       RTN

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

       ;Null characters for long commands

CRTNUL: BYTE    200,200,200,200,200,200,200,200,200,200,200,0
       EVEN

       ;Byte offset and data tables follow for all commands

CRCA:   WORD    C1-.,C2-.,C3-.,C4-.,C5-.,C6-.,C7-.,C8-.,C9-.,C10-.
       WORD    C11-.,C12-.,C13-.,C14-.,C15-.,C16-.,C17-.,C18-.,C19-.,C20-.
       WORD    C21-.,C22-.,C23-.,C24-.,C25-.,C26-.,C27-.,C28-.,C29-.,C30-.
       WORD    C31-.,C32-.,C33-.
CRCB:

PAGE
;Macro CRT defines CRT codes

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

C0:     CRT                     ;Clear screen
C1:     CRT     ESC,'=,40,40    ;Home cursor
C2:     CRT     215             ;Cursor return
C3:     CRT     13              ;Cursor up
C4:     CRT     12              ;Cursor down
C5:     CRT     10              ;Cursor left
C6:     CRT     20              ;Cursor right
C7:     CRT     ESC,'#          ;Lock keyboard
C8:     CRT     ESC,'"          ;Unlock keyboard
C9:     CRT     ESC,'t          ;Erase to end of line
C10:    CRT     ESC,'y          ;Erase to end of screen
C11:    CRT     ESC,')          ;Low intensity
C12:    CRT     ESC,'(          ;High intensity
C13:    CRT                     ;Enable protected  fields
C14:    CRT                     ;Disable protected fields
C15:    CRT     ESC,'R          ;Delete line
C16:    CRT     ESC,'E          ;Insert line
C17:    CRT     ESC,'W          ;Delete character
C18:    CRT     ESC,'Q          ;Insert character
C19:    CRT                     ;Read cursor address
C20:    CRT                     ;Read character at current cursor position
C21:    CRT     ESC,'n          ;Blink on
C22:    CRT     ESC,'o          ;Blink off
C23:    CRT                     ;Graphics on
C24:    CRT                     ;Graphics off
C25:    CRT                     ;Set horizontal pos
C26:    CRT                     ;Set vertical pos
C27:    CRT                     ;Set attributes
C28:    CRT                     ;Cursor on
C29:    CRT                     ;Cursor off
C30:    CRT     ESC,'l          ;Underline on
C31:    CRT     ESC,'m          ;Underline off
C32:    CRT     ESC,'j          ;Reverse on
C33:    CRT     ESC,'k          ;Reverse off

       EVEN

       END