;******************************
;
;   IBM PC/AT Terminal Driver
;
;******************************
; John Keys
; 8-20-85
; (202) 872-4538
; Works with VUE and SUPER VUE from my IBM PC/AT running under PC/IX's connect
;

       ASMMSG  "IBM PC/AT Terminal Driver"

       SEARCH  SYS
       SEARCH  SYSSYM
       SEARCH  TRM

       OBJNAM  IBMPC.TDV

       ESC     = 33
       TFEATS  = TD$EOL!TD$EOS!TD$BLN!TD$RVA

;  TD$DIM creates a problem in SuperVue if added to TFEATS


;TFEATS = TD$MLT!TD$EOL!TD$EOSTD$!TD$UND!TD$BLN!TD$DIM!TD$RVA!TD$CID!TD$LID
;
;       TD$MLT  =     20        ;   multi-byte keystrokes
;       TD$EOL  =    400        ;   erase to end of line
;       TD$EOS  =   1000        ;   erase to end of screen
;       TD$UND  =   2000        ;   underline
;       TD$BLN  =   4000        ;   blink
;       TD$DIM  =  10000        ;   dim video attribute
;       TD$RVA  =  20000        ;   reverse video
;       TD$CID  =  40000        ;   insert/delete character
;       TD$LID  = 100000        ;   insert/delete line

;*********
;   IBM
;*********
;Terminal driver communications area
IBM:    WORD    TD$NEW                  ; terminal attributes (2000 octal)
       RTN                             ; input routine
       RTN                             ; output routine
       BR      ECHO                    ; echo routine
       BR      JMPCRT                  ; crt control
       RTN                             ; no init routine yet
       WORD    0                       ; no impure area
       BYTE    24.                     ; number of rows
       BYTE    80.                     ; number of columns
       LWORD   TFEATS                  ; terminal has:
                                       ;   dim video attribute
                                       ;   erase to end of screen
                                       ;   erase to end of line
                                       ;   blink
                                       ;   reverse video

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


;********************
;*       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      ; X,Y LEAD IN

       SAVE    D0
       MOV     D1,D0
       RORW    D1,#8.          ; INTEGER VALUE FOR ROW
       AND     #377,D1
       DCVT    0,OT$TRM        ; CONVERT INTERGER ROW TO ASCII STRING

       TTYI
       BYTE    ';,0            ; ROW,COLUMN SEPERATOR

       MOV     D0,D1           ; INTEGER VALUE FOR COLUMN
       AND     #377,D1
       DCVT    0,OT$TRM        ; CONVERT INTERGER COLUMN TO ASCII STRING
       TTYI
       BYTE    'H,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


;Byte offset and data tables follow for all commands
;
       EVEN
CRCA:   WORD    C0-.,  C1-., C2-., C3-., C4-., C5-., C6-., C7-.
       WORD    C8-.,  C9-.,C10-.,C11-.,C12-.,C13-.,C14-.,C15-.
       WORD    C16-.,C17-.,C18-.,C19-.,C20-.,C21-.,C22-.,C23-.
       WORD    C24-.,C25-.,C26-.,C27-.,C28-.,C29-.,C30-.,C31-.
       WORD    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-.
CRCB:
;***************************
;
;   Special CRT Functions
;
;***************************

C0:     BYTE    ESC,'c,0                ;clear screen
C1:     BYTE    ESC,'[,'1,';,'1,'H,0    ;cursor home
C2:     BYTE    15,0                    ;cursor return
C3:     BYTE    ESC,'[,'1,'A,0          ;cursor up
C4:     BYTE    ESC,'[,'1,'B,0          ;cursor down
C5:     BYTE    ESC,'[,'1,'D,0          ;cursor left
C6:     BYTE    ESC,'[,'1,'C,0          ;cursor right
C7:     BYTE    0                       ;lock keyboard
C8:     BYTE    0                       ;unlock keyboard
C9:     BYTE    ESC,'[,'0,'K,0          ;erase to end of line
C10:    BYTE    ESC,'[,'0,'J,0          ;erase to end of screen
C11:    BYTE    ESC,'[,'3,'2,'m,0       ;enter backgroud display mode (bold on IBM)
C12:    BYTE    ESC,'[,'0,'m,0          ;enter foreground display mode (normal intensity)
C13:    BYTE    0                       ;enable protected  fields
C14:    BYTE    0                       ;disable protected fields
C15:    BYTE    0                       ;delete line
C16:    BYTE    0                       ;insert line
C17:    BYTE    0                       ;delete character
C18:    BYTE    0                       ;insert character
C19:    BYTE    0                       ;read cursor address
C20:    BYTE    0                       ;read character at current cursor position
C21:    BYTE    ESC,'[,'5,'m,40,0       ;start blink field
C22:    BYTE    ESC,'[,'0,'m,40,0       ;end blink field
C23:    BYTE    0                       ;start line drawing mode (enable alternate character set)
C24:    BYTE    0                       ;end line drawing mode (disable alternate character set)
C25:    BYTE    0                       ;set horizontal position
C26:    BYTE    0                       ;set vertical position
C27:    BYTE    0                       ;set terminal attributes
C28:    BYTE    0                       ;cursor on
C29:    BYTE    0                       ;cursor off
C30:    BYTE    ESC,'[,'4,'m,0          ;start underscore
C31:    BYTE    ESC,'[,'0,'m,0          ;end underscore
C32:    BYTE    ESC,'[,'7,'m,0          ;start reverse video
C33:    BYTE    ESC,'[,'0,'m,0          ;end reverse video
C34:                                    ;start reverse blink
C35:                                    ;end reverse blink
C36:                                    ;turn off screen display
C37:                                    ;turn on screen display
C38:                                    ;top left corner
C39:                                    ;top right corner
C40:                                    ;bottom left corner
C41:                                    ;bottom right corner
C42:                                    ;top intersect
C43:                                    ;right intersect
C44:                                    ;left intersect
C45:                                    ;bottom intersect
C46:                                    ;horizontal line
C47:                                    ;vertical line
C48:                                    ;intersection
C49:                                    ;solid block
C50:                                    ;slant block
C51:                                    ;cross-hatch block
C52:                                    ;double line horizontal
C53:                                    ;double line vertical
C54:                                    ;send message to function key line
C55:                                    ;send message to shifted function key line
C56:                                    ;set normal display format
C57:                                    ;set horizontal split (follow with row code)
C58:                                    ;set vertical split (39 char columns)
C59:                                    ;set vertical split (40 char columns)
C60:                                    ;set vertical split column to next char
C61:                                    ;activate split segment 0
C62:                                    ;activate split segment 1
C63:                                    ;send message to host message field
C64:                                    ;up-arrow
C65:                                    ;down-arrow
C66:                                    ;raised dot
C67:                                    ;end of line marker
C68:                                    ;horizontal tab symbol
C69:                                    ;paragraph
C70:                                    ;dagger
C71:                                    ;section
C72:                                    ;cent sign
C73:                                    ;one-quarter
C74:                                    ;one-half
C75:                                    ;degree
C76:                                    ;trademark
C77:                                    ;copyright
C78:                                    ;registered
C79:                                    ;print screen
C80:                                    ;reserved for set to wide mode
C81:                                    ;reserved for set to normal mode
C82:                                    ;enter transparent print mode
C83:                                    ;exit transparent print mode
C84:                                    ;begin writing to alternate page
C85:                                    ;end writing to alternate page
C86:                                    ;toggle page
C87:                                    ;copy to alternate page
C88:                                    ;insert column
C89:                                    ;delete column
C90:                                    ;block fill with attribute
C91:                                    ;block fill with character
C92:                                    ;draw a box
C93:                                    ;scroll box up one line
C94:                                    ;scroll box down one line
C95:                                    ;select jump scroll
C96:                                    ;select fast smooth scroll
C97:                                    ;select med-fast smooth scroll
C98:                                    ;select med-slow smooth scroll
C99:                                    ;select slow smooth scroll
C100:                                   ;start underscore/blink
C101:                                   ;end underscore/blink
C102:                                   ;start underscore/reverse
C103:                                   ;end underscore/reverse
C104:                                   ;start underscore/reverse/blink
C105:   BYTE    0                       ;end underscore/reverse/blink
       EVEN

       END