;***************************************************************************;
;                                                                           ;
;                           VT100 Terminal Driver                           ;
;                                                                           ;
;***************************************************************************;
;Copyright (C) 1986, 1987 UltraSoft Corporation.  All Rights Reserved.
;
;Written by: David Pallmann
;
;1.0 02-Oct-84 created. /DFP
;
; **** This modified version was donated by DD/AM. A new version with
;      edit comments will hopefully be sent in the future. ****
;                                                  AMUS Staff

       ASMMSG  "MACTRM Terminal Driver"

       SEARCH  SYS
       SEARCH  SYSSYM
       SEARCH  TRM

       OBJNAM  .TDV

       TFEATS=TD$LID!TD$CID!TD$EOS!TD$EOL!TD$MLT

       ESC=33

;**********
;   VT100
;**********
;Terminal driver communications area
VT100:  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   TFEATS                  ; terminal has:
                                       ;   insert/delete line
                                       ;   insert/delete character
                                       ;   dim video attribute
                                       ;   erase to end of screen
                                       ;   erase to end of line
                                       ;   multi-byte keystrokes

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

;********************
;*       ECHO       *
;********************
;Special echo processing is performed here
;Rubouts will backspace and erase the previous character
;Control-U will erase the entire line by backspacing and erasing
ECHO:   CMPB    D1,#25                  ; control-u
       BEQ     CTRLU
       CMPB    D1,#177                 ; rubout
       BNE     ECHX

;Rubouts are handled by the old backspace-and-erase game
;Special handling must be performed if we are rubbing out a tab
;D6 contains the character being rubbed out
RUBOUT: CMPB    D6,#11                  ; was it a tab?
       BEQ     RBTB                    ;   yes
;Rubout 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
;Rubout 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,#ESC                 ;  altmode
       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 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:    BMI     INMLT                   ; skip if multi-byte processing
       CMPB    D1,#ESC                 ; 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,#ESC
       BNE     INMX

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

ESCAPE: TSTB    (A0)+                   ; discard [
       MOVB    (A0)+,D1                ; get the second character
       LEA     A0,ESCTBL               ;point to translation table
10$:    TSTB    @A0                     ;end of table?
       BEQ     INMX                    ; yes - escape sequence is invalid
       CMMB    (A0)+,D1                ;match?
       BEQ     20$                     ; yes
       INC     A0                      ; no - increment index
       BR      10$                     ;      and try next table entry
20$:    MOVB    @A0,D1                  ;put translated char in D1
       BR      INMX                    ; and return to program

ESCTBL: BYTE    'A,'K-'@                ;UP ARROW     becomes  ^K
       BYTE    'D,'H-'@                ;LEFT ARROW   becomes  ^H
       BYTE    'C,'L-'@                ;RIGHT ARROW  becomes  ^L
       BYTE    'B,'J-'@                ;DOWN ARROW   becomes  ^J
       BYTE    'f,'^-'@                ;HOME         becomes  ^^
       BYTE    'M,'Z-'@                ;DEL LINE     becomes  ^Z
       BYTE    'P,'D-'@                ;DEL CHAR     becomes  ^D
       BYTE    'S,'T-'@                ;NEXT SCREEN  becomes  ^T
       BYTE    'L,'B-'@                ;INSERT LINE  becomes  ^B
       BYTE    '@,'F-'@                ;INSERT CHAR  becomes  ^F
       BYTE    'T,'R-'@                ;PREV SCREEN  becomes  ^R
       BYTE    0,0

INMX:   LCC     #0                      ; reset the flags
INMX2:  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 byte)
;If D1 is negative we have the special command in the low byte
CRT:    TSTW    D1                      ; is it cursor position?
       BMI     CRTU                    ;   no

;Cursor positioning - D1 contains X,Y coordinates

       TTYI
       BYTE    ESC,'[,0,0

       SAVE    D0

       MOV     D1,D0
       RORW    D1,#8.
       AND     #377,D1
       DCVT    0,OT$TRM

       TTYI
       BYTE    ';,0

       MOV     D0,D1
       AND     #377,D1
       DCVT    0,OT$TRM

       TTYI
       BYTE    'f,0

       REST    D0

       RTN

;Special commands - D1 contains the command code in the low byte
CRTU:   AND     #377,D1                 ; strip the high byte

;Command processing per director tables
       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                 ; 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

;Escape sequence translation table
;
EXTB:   BYTE    67,73,105,111,120,121,122,124,127,131,377,377
       EVEN

;Byte offset and data tables follow for all commands
;
CRCA:
WORD    C0-.,  C1-.,  C2-.,  C3-.,  C4-.,  C5-.,  C6-.,  C7-.,  C8-.,  C9-.
WORD    C10-., C11-., C12-., C13-., C14-., C15-., C16-., C17-., C18-., C19-.
WORD    C20-., C21-., C22-., C23-., C24-., C25-., C26-., C27-., C28-., C29-.
WORD    C30-., C31-., C32-., C33-., C34-., C35-., C36-., C37-., C38-., C39-.
WORD    C40-., C41-., C42-., C43-., C44-., C45-., C46-., C47-., C48-., C49-.
WORD    C50-., C51-., C52-., C53-., C54-., C55-., C56-., C57-., C58-., C59-.
WORD    C60-., C61-., C62-., C63-., C64-., C65-., C66-., C67-., C68-., C69-.
WORD    C70-., C71-., C72-., C73-., C74-., C75-., C76-., C77-., C78-., C79-.
WORD    C80-., C81-., C82-., C83-., C84-., C85-., C86-., C87-., C88-., C89-.
WORD    C90-., C91-., C92-., C93-., C94-., C95-., C96-., C97-., C98-., C99-.
WORD    C100-.,C101-.,C102-.,C103-.,C104-.,C105-.,C106-.,C107-.,C108-.,C109-.
WORD    C110-.,C111-.,C112-.,C113-.,C114-.,C115-.,C116-.,C117-.,C118-.,C119-.
WORD    C120-.,C121-.,C122-.,C123-.,C124-.,C125-.,C126-.,C127-.,C128-.,C129-.
WORD    C130-.,C131-.,C132-.,C133-.,C134-.,C135-.,C136-.,C137-.,C138-.,C139-.
WORD    C140-.,C141-.,C142-.,C143-.,C144-.,C145-.,C146-.,C147-.,C148-.,C149-.
WORD    C150-.,C151-.,C152-.,C153-.,C154-.,C155-.,C156-.,C157-.,C158-.,C159-.
WORD    C160-.,C161-.,C162-.,C163-.,C164-.,C165-.,C166-.,C167-.,C168-.,C169-.
WORD    C170-.,C171-.,C172-.,C173-.,C174-.,C175-.,C176-.,C177-.,C178-.,C179-.
WORD    C180-.,C181-.,C182-.,C183-.,C184-.,C185-.,C186-.,C187-.,C188-.,C189-.
WORD    C190-.,C191-.,C192-.,C193-.,C194-.,C195-.,C196-.,C197-.,C198-.,C199-.
WORD    C200-.,C201-.,C202-.,C203-.,C204-.,C205-.,C206-.,C207-.,C208-.,C209-.
WORD    C210-.,C211-.,C212-.,C213-.,C214-.,C215-.,C216-.,C217-.,C218-.,C219-.
WORD    C220-.,C221-.,C222-.,C223-.,C224-.,C225-.,C226-.,C227-.,C228-.,C229-.
WORD    C230-.,C231-.,C232-.,C233-.,C234-.,C235-.,C236-.,C237-.,C238-.,C239-.
WORD    C240-.,C241-.,C242-.,C243-.,C244-.,C245-.,C246-.,C247-.,C248-.,C249-.
WORD    C250-.,C251-.,C252-.,C253-.,C254-.,C255-.

CRCB:
WORD    0                               ; Final zero at end
       PAGE
;***************************
;
;   Special CRT Functions
;
;***************************
;
; NOTE: special functions blinking/reverse_video/underlining are coded to
;       print an extraneous space.  This makes the driver compatible with
;       such software as AlphaMENU that is coded in expectation of
;       TeleVideo-type graphics that print an attribute character.
;
;       If the space is not desireable for your application, remove all
;       references to the number 40.  For example:
;
;               change  BYTE ESC,'[,'X,40,0  to  BYTE ESC,'[,'X,0
;
C0:     BYTE    ESC,'<,33,'[,'H
       BYTE    ESC,'[,'J
       BYTE    33,'[,'m,0              ;ED  clear screen
C1:     BYTE    ESC,'[,'H,0             ;CUP cursor home
C2:     BYTE    ESC,'E,0                ;NEL cursor return
C3:     BYTE    ESC,'[,'1,'A,0          ;CUU cursor up
C4:     BYTE    ESC,'[,'1,'B,0          ;CUD cursor down
C5:     BYTE    ESC,'[,'1,'D,0          ;CUB cursor left
C6:     BYTE    ESC,'[,'1,'C,0          ;CUF cursor right
C7:                                     ;XXX lock keyboard
C8:     BYTE    0                       ;XXX unlock keyboard
C9:     BYTE    ESC,'[,'0,'K,0          ;EL  erase to end of line
C10:    BYTE    ESC,'[,'0,'J,0          ;ED  erase to end of screen
C11:    BYTE    ESC,'[,';,'1,0          ;SGR enter backgroud display mode (reduced intensity).
C12:    BYTE    ESC,'[,'m,0             ;SGR enter foreground display mode (normal intensity)
C13:                                    ;XXX enable protected  fields
C14:    BYTE    0                       ;XXX disable protected fields
C15:    BYTE    ESC,'[,'1,'M,0          ;DL  delete line
C16:    BYTE    ESC,'[,'1,'L,0          ;IL  insert line
C17:    BYTE    ESC,'[,'1,'P,0          ;DCH delete character
C18:    BYTE    ESC,'[,'1,'@,0          ;ICH insert character
C19:                                    ;XXX read cursor address
C20:                                    ;XXX read character at current cursor position
C21:                                    ;SGR start blink field
C22:                                    ;SGR end blink field
C23:    BYTE    0                       ;XXX start line drawing mode (enable alternate character set)
C24:    BYTE    0                       ;XXX end line drawing mode (disable alternate character set)
C25:                                    ;XXX set horizontal position
C26:                                    ;XXX set vertical position
C27:    BYTE    0                       ;XXX set terminal attributes
C28:                                    ;RM  cursor on
C29:                                    ;SM  cursor off
C30:    BYTE    0                       ;SGR start underscore                   [102]
C31:    BYTE    0                       ;SGR end underscore                     [102
C32:    BYTE    0                       ;SGR start reverse video                        [102]
C33:    BYTE    0                       ;SGR end reverse video                  [102]
C34:    BYTE    0                       ;SGR start reverse blink                        [102]
C35:    BYTE    0                       ;SGR end reverse blink                  [102]
C36:                                    ;XXX turn off screen display            [102]
C37:                                    ;XXX turn on screen display             [102]
C38:    BYTE    '+,0                    ; top left corner
C39:    BYTE    '+,0                    ; top right corner
C40:    BYTE    '+,0                    ; bottom left corner
C41:    BYTE    '+,0                    ; bottom right corner
C42:    BYTE    '+,0                    ; top intersect
C43:    BYTE    '+,0                    ; right intersect
C44:    BYTE    '+,0                    ; left intersect
C45:    BYTE    '+,0                    ; bottom intersect
C46:    BYTE    '-,0                    ; horiz line
C47:    BYTE    '|,0                    ; vert line
C48:    BYTE    '+,0                    ; center intersect
C49:    BYTE    0                       ; solid block - Ff
C50:    BYTE    0                       ; slant block - diamond
C51:    BYTE    0                       ; cross-hatch block - checkerboard
C52:    BYTE    '=,0                    ; double horiz line - /=
C53:    BYTE    '|,0                    ; double vert line - Vt
C54:                                    ;XXX send message to function key line
C55:                                    ;XXX send message to shifted function key line
C56:                                    ;XXX set normal display format
C57:                                    ;XXX set horizontal split (follow with row code)
C58:                                    ;XXX set vertical split (39 char columns)
C59:                                    ;XXX set vertical split (40 char columns)
C60:                                    ;XXX set vertical split column to next char
C61:                                    ;XXX activate split segment 0
C62:                                    ;XXX activate split segment 1
C63:                                    ;XXX send message to host message field
C64:    BYTE    0                       ; up arrow - horiz line (scan 3)
C65:    BYTE    0                       ; down arrow - horiz line (scan 7)
C66:    BYTE    0                       ; raised dot - same
C67:    BYTE    0                       ; end of line marker - Nl
C68:    BYTE    0                       ; horiz tab symb - Ht
C69:    BYTE    0                       ; paragraph - Pi
C70:    BYTE    0                       ; daggar - +-
C71:    BYTE    0                       ; section - <=
C72:    BYTE    0                       ; cent sign - >=
C73:    BYTE    0                       ; 1/4 - horiz line (scan 1)
C74:    BYTE    0                       ; 1/2 - horiz line (scan 9)
C75:    BYTE    0                       ; degree - same
C76:    BYTE    0                       ; trademark - UK pound sign
C77:    BYTE    0                       ; copyright - Cr
C78:    BYTE    0                       ; registered - Lf
C79:                                    ;XXX print screen
C80:                                    ;XXX reserved for set to wide mode
C81:                                    ;XXX reserved for set to normal mode
C82:                                    ;XXX enter transparent print mode
C83:                                    ;XXX exit transparent print mode
C84:                                    ;XXX begin writing to alternate page
C85:                                    ;XXX end writing to alternate page
C86:                                    ;XXX toggle page
C87:                                    ;XXX copy to alternate page
C88:                                    ;XXX insert column
C89:                                    ;XXX delete column
C90:                                    ;XXX block fill with attribute
C91:                                    ;XXX block fill with character
C92:                                    ;XXX draw a box
C93:                                    ;XXX scroll box up one line
C94:                                    ;XXX scroll box down one line
C95:                                    ;XXX select jump scroll
C96:                                    ;XXX select fast smooth scroll
C97:                                    ;XXX select med-fast smooth scroll
C98:                                    ;XXX select med-slow smooth scroll
C99:    BYTE    0                       ;XXX select slow smooth scroll
C100:   BYTE    0                       ;SGR start underscore blink                     [102]
C101:   BYTE    0                       ;SGR end underscore blink                       [102]
C102:   BYTE    0                       ;SGR start underscore/reverse                   [102]
C103:   BYTE    0                       ;SGR end underscore/reverse                     [102]
C104:   BYTE    0                       ;SGR start underscore/reverse/blink
C105:   BYTE    0                       ;SGR end underscore/reverse/blink                       [102]
C106:                                   ; start underscore w/o space
C107:                                   ; end underscore w/o space
C108:                                   ; start reverse w/o space
C109:                                   ; end reverse w/o space
C110:                                   ; start reverse/blinking w/o space
C111:                                   ; end reverse/blinking w/o space
C112:                                   ; start underscore/blinking w/o space
C113:                                   ; end underscore/blinking w/o space
C114:                                   ; start underscore/reverse w/o space
C115:                                   ; end underscore/reverse w/o space
C116:                                   ; start underscore/reverse/blinking w/o space
C117:                                   ; end underscore/reverse/blinking w/o space
C118:                                   ; start blink w/o space
C119:   BYTE    0                       ; end blink w/o space
C120:                                   ; set cursor to blinking block
C121:                                   ; set cursor to steady block
C122:                                   ; set cursor to blinking underline
C123:                                   ; set cursor to steady underline
C124:                                   ; spare
C125:                                   ; spare
C126:                                   ; spare
C127:   BYTE    0                       ; spare
C128:                                   ; select top status line
C129:                                   ; end of all status line
C130:                                   ; select unshifted status line
C131:                                   ; select shifted status line
C132:                                   ; select black text
C133:                                   ; select white text
C134:                                   ; select blue text
C135:                                   ; select magneta text
C136:                                   ; select red text
C137:                                   ; select yellow text
C138:                                   ; select green text
C139:                                   ; select cyan text
C140:                                   ; select black reverse text
C141:                                   ; select white reverse text
C142:                                   ; select blue reverse text
C143:                                   ; select magneta reverse text
C144:                                   ; select red reverse text
C145:                                   ; select yellow reverse text
C146:                                   ; select green reverse text
C147:                                   ; select cyan reverse text

; ----- LAST OF ALPHA-MICRO DEFINED TCRTS -----

C148:                                   ; Undefined
C149:
C150:
C151:
C152:
C153:
C154:
C155:
C156:
C157:
C158:
C159:
C160:
C161:
C162:
C163:
C164:
C165:
C166:
C167:
C168:
C169:
C170:
C171:
C172:
C173:
C174:
C175:
C176:
C177:
C178:
C179:
C180:
C181:
C182:
C183:
C184:
C185:
C186:
C187:
C188:
C189:
C190:
C191:
C192:
C193:
C194:
C195:
C196:
C197:
C198:
C199:
C200:
C201:
C202:
C203:
C204:
C205:
C206:
C207:
C208:
C209:
C210:
C211:
C212:
C213:
C214:
C215:
C216:
C217:
C218:
C219:
C220:
C221:
C222:
C223:
C224:
C225:
C226:
C227:
C228:
C229:
C230:
C231:
C232:
C233:
C234:
C235:
C236:
C237:
C238:
C239:
C240:
C241:

C242:                                   ; FLIP off (Don't track screen)
C243:   BYTE
       0                       ; FLIP on  (Start tracking screen)

C244:   BYTE    ESC,'<,33,'[,'H
       BYTE    ESC,'[,'J
       BYTE    33,'[,'m,0              ;ED  clear screen

C245:   BYTE    ' ,0                    ; SPACE  (was 137)
C246:   BYTE    0                       ; Turn L25 Msg ON  (was 153)
C247:   BYTE    0                       ; turn off 25th
C248:   byte    0                       ; set SCREEN BRIGHT (Was 136)
C249:   byte    0                       ; set SCREEN DARK (was 135)
C250:
C251:   BYTE    0                       ; Special FLIP Tcrts
C252:   BYTE    0                       ; line lock on (was 130)
C253:   BYTE    0                       ; line lock off (was 131)
C255:   BYTE    0                       ; unused
C254:   BYTE    0                       ; unused
       EVEN

       END