;***************************************************************************;
;                                                                           ;
;                      Hazeltine 15XX Terminal Driver                       ;
;                                                                           ;
;                         Author: David F. Pallmann                         ;
;            Copyright (C) 1983 by LMS Technical Services, Inc.             ;
;                                                                           ;
;***************************************************************************;
;* this driver supports SuperVUE
;* DEL/underscore has case swapped for model 1500 to avoid shifted-DELetes

NVALU   MODEL

IF      LT,MODEL-1500,ASMERP ?specify /V:model
IF      EQ,MODEL-1500,OBJNAM HZ1500.TDV
IF      EQ,MODEL-1510,OBJNAM HZ1510.TDV
IF      EQ,MODEL-1520,OBJNAM HZ1520.TDV

;universals
SEARCH  SYS
SEARCH  SYSSYM
SEARCH  TRM

LI=126.                                 ;lead-in code (tilde)

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

;HZ15XX
;Terminal driver communications area

HZ15XX: 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

;Following code makes driver SuperVUE compatible

       .TAW.0=0
       .TAW.1=0
       .TAW.2=0
       .TAW.3=0
       .TAW.4=0
       .TAW.5=0
       .TAW.6=0
       .TAW.7=0
       .TAW.8=0
       .TAW.9=0

       .NUM = 0

       .TW0.=.TAW.0
       .TW1.=.TAW.1
       .TW2.=.TAW.2
       .TW3.=.TAW.3
       .TW4.=.TAW.4
       .TW5.=.TAW.5
       .TW6.=.TAW.6
       .TW7.=.TAW.7
       .TW8.=.TAW.8
       .TW9.=.TAW.9

       WORD    .TW1.,.TW0.,.TW3.,.TW2.,.TW5.,.TW4.,.TW7.,.TW6.,.TW9.,.TW8.
       WORD    0,0,0,0,0,0

       RAD50   /SUPER /
       RAD50   /VUE/
       RAD50   /HAZELX/

       ;End SuperVUE compatibility area

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

;model 1500s swap case on DEL/underscore key
IF      EQ,MODEL-1500
       CMPB    D1,#'_                  ;underscore?
       BEQ     100$                    ; yes - swap case
       CMPB    D1,#177                 ;del?
       BNE     110$                    ; no
100$:   XORB    #40,D1                  ;swap case bit
       ENDC

110$:   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._10-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-.
       WORD    C17-.,C18-.,C19-.,C20-.,C21-.,C22-.,C23-.,C24-.
       WORD    C25-.,C26-.,C27-.,C28-.,C29-.,C30-.,C31-.,C32-.
       WORD    C33-.

CRCB:
PAGE
;Macro CRT defines CRT codes and sets SuperVUE attribute bits

DEFINE  CRT     CODE1,CODE2,CODE3
       .NUM=.NUM+1
       .TAW.=<.NUM/16.>&^H0F
       .TAWB.=.NUM-<.TAW.*16.>
       .TAW.\.TAW.=<1_.TAWB.>!.TAW.\.TAW.
       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,30                   ;erase to end of screen
C11:    CRT     LI,31                   ;low intensity
C12:    CRT     LI,37                   ;high intensity
C13:    CRT                             ;enable prot.  fields (unimplemented)
C14:    CRT                             ;disable prot. fields (unimplemented)
C15:    CRT     LI,19.                  ;delete line
C16:    CRT     LI,26.                  ;insert line
C17:    CRT                             ; delete character
C18:    CRT                             ; insert character
C19:    CRT                             ; read cursor address
C20:    CRT                             ; read character at current cursor position
C21:    CRT                             ; start blink field
C22:    CRT                             ; end blink field
C23:    CRT                             ; start line drawing mode (enable alternate character set)
C24:    CRT                             ; end line drawing mode (disable alternate character set)
C25:    CRT                             ; set horizontal position
C26:    CRT                             ; set vertical position
C27:    CRT                             ; set terminal attributes
C28:    CRT                             ; cursor on
C29:    CRT                             ; cursor off
C30:    CRT                             ; start underscore                      [102]
C31:    CRT                             ; end underscore                        [102
C32:    CRT     LI,31,'*                ; start reverse video                   [102]
C33:    CRT     LI,37,40                ; end reverse video                     [102]

       EVEN

       END