;>>>>>>>>>>>>>>>>>>>>>>> KERMTERM.TEXT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


;----------------------------------------------------------------------
;----------------------------------------------------------------------
;
;  This procedure is external to the main kermit program.
;
;----------------------------------------------------------------------
;----------------------------------------------------------------------
; PROCEDURE Kerm_Term( BS_to_DEL, Esc_Char, Xon_Char, Xoff_Char : CHAR;
;                      Xoff_Wait : INTEGER;
;                      No_Ffeed, Print, Half_Duplex, Reject_Cntrl_Char,
;                      Emulate : BOOLEAN );
;----------------------------------------------------------------------
;
; This procedure works only in cooperation with my serial card driver
; BIOS routine, that implements the UCSD  UNITSTATUS procedure and buffers
; serial input for various serial cards. ( see REMDRIVER.TEXT )
; It is written in 6502 code in order to be able to work at high baudrates.
; The baudrate limiting factor on the Apple is the screen scrolling routine.
; If one can set the linefeed fill count in the remote host terminal
; definition to an appropiate value, then this procedure works at 4800 baud.
; With the right Xon and Xoff characters ( most often ^Q and ^S ) and Xoff-
; wait count this procedure will echo on request also to a printer, without
; losing characters. Increase Xoff_wait if characters seem to be lost when
; printing.
; If your keyboard doesn`t have a DEL key and your host requires one for
; delete & backspace, then set BS_to_DEL to CHR(127). During the connection
; your BS key (Asci 8) will be translated to DEL (Asci 127).
; If you do not want incoming control characters other than 'bell', 'bs',
; 'lf', 'ff', 'cr' echoed to to your screen : set 'reject_cntrl_char' to
; true.
;----------------------------------------------------------------------
;
; N.B.  EMULATE is not implemented.
;
;----------------------------------------------------------------------
;
          .PROC KERMTERM,10.  ; 10 parameters, see above.
;
BIOSAF    .EQU 0FF5C        ;base of bios jump table. Same in V1.1 &1.2
CREAD     .EQU BIOSAF+0     ;bios console read routine adres
CWRITE    .EQU BIOSAF+3.    ;bios console write routine adres
PWRITE    .EQU BIOSAF+9.    ;bios printer write routine adres
RREAD     .EQU BIOSAF+24.   ;bios remote read routine adres
RWRITE    .EQU BIOSAF+27.   ;bios remote write routine adres
CSTAT     .EQU BIOSAF+42.   ;bios keyboard status routine adres
RSTAT     .EQU BIOSAF+51.   ;bios remote status routine adres
;
;  The BIOS After Fold jump vector will be patched by SYSTEM.ATTACH .
;  From the above routines only RREAD & RSTAT are actually changed.
;
BIOSRAM   .EQU 0C083        ;switch adress for extra 4k language card part.
                            ;contains UCSD BIOS routines.
INTPRAM   .EQU 0C08B        ;switch adress to get back to normal language
                            ;card part. contains UCSD interpreter.
RPTR      .EQU 0BF18        ;read pointer for circular keyboard buffer.
WPTR      .EQU 0BF19        ;write   "     "     "        "      "
BS        .EQU 08           ;backspace character.
LINEFD    .EQU 0A           ;linefeed     "
FF        .EQU 0C           ;formfeed     "
CR        .EQU 0D           ;return       "
BELL      .EQU 07           ;bell         "
TRUE      .EQU 80           ;used as boolean.
;---------------------------
; GET PARAMETERS FROM STACK
;
          PLA               ; pop return adress.
          STA RETURN
          PLA
          STA RETURN+1
          ;-----------------
          PLA               ; pop emulate boolean.
          BEQ $05
          LDA #TRUE
$05       STA EMULATE
          PLA
          ;----------------
          PLA               ; pop Reject_Cntrl_Char Boolean.
          BEQ $01
          LDA #TRUE
$01       STA REJECT
          PLA
          ;-----------------
          PLA               ; pop Half_Duplex Boolean.
          BEQ $02
          LDA #TRUE
$02       STA HALFDUP
          PLA
          ;-----------------
          PLA               ; pop Printer Boolean.
          BEQ $03
          LDA #TRUE
$03       STA PRINTER
          PLA
          ;-----------------
          PLA               ; pop No_Ffeed Boolean.
          BEQ $04
          LDA #TRUE
$04       STA NOFEED
          PLA
          ;-----------------
          PLA               ; pop Xoff_Wait Integer (1..255).
          STA XOFFWAIT
          PLA
          ;-----------------
          PLA               ; pop Xoff_Char Char.
          STA XOFFCHAR
          PLA
          ;-----------------
          PLA               ; pop Xon_Char Char.
          STA XONCHAR
          PLA
          ;-----------------
          PLA               ; pop ExitChar Char.
          STA EXITCHAR
          PLA
          ;-----------------
          PLA               ; pop BS_to_DEL Char.
          STA BSTODEL
          PLA
;---------------------------
          LDA BIOSRAM       ; switch in BIOS RAM
;---------------------------
START     JSR RSTATUS       ; returns in BUFLEN # char's in remin-buffer.
          LDA BUFLEN
          BEQ READKEY       ; read keyboard if buffer empty.
          ;-----------------
          BIT PRINTER       ; is printer on?
          BPL EMPTYRBUF     ; no : start reading remin buffer.
          ;-----------------
XOFFSEND  LDA XOFFCHAR      ; printer is on:
          JSR RWRITE        ; send xoff char to host and
          LDA XOFFWAIT      ; keep checking remin for a certain time,
          STA COUNT         ; because host may send some more char's
WAIT      JSR RSTATUS       ; before it really gets the xoff.
          DEC COUNT
          BNE WAIT
          ;-----------------
EMPTYRBUF LDX #00           ; X=0 : read request.
          JSR RREAD         ; read a char from remin buffer.
          ;-----------------
          BIT REJECT        ; reject control chars?
          BPL ECHO          ; no: echo to console.
          JSR CHECKCTRL     ; yes: check for allowed control char's.
          ;-----------------
ECHO      PHA               ; save char.
          JSR CWRITE        ; echo char to console.
          PLA               ; restore char in accu.
          ;-----------------
          BIT PRINTER       ; is printer on?
          BPL NOPRINT       ; if not, don't print char.
          ;-----------------
          CMP #FF           ; printer is on. is char a formfeed?
          BNE NOFF
          JSR REPLFF        ; yes: replace it if requested.
NOFF      LDX #01           ; write request.
          JSR PWRITE        ; print char.
          ;-----------------
NOPRINT   DEC BUFLEN        ; keep on reading remin char's until
          BNE EMPTYRBUF     ; reminbuffer is empty.
          ;-----------------
          BIT PRINTER       ; is printer on?
          BPL READKEY       ; no  : check keyboard.
          LDA XONCHAR       ; yes : send xon char to host.
          JSR RWRITE
          ;-----------------
READKEY   LDA RPTR          ; if keyboardbuffer readpointer is
          CMP WPTR          ; equal to writepointer then keyboardbuffer is
          BEQ START         ; empty. loop back to start.
          ;-----------------
          JSR CREAD         ; get a char from keyboardbuffer.
          CMP EXITCHAR      ; is it the escape char?
          BEQ EXIT          ; then exit this procedure.
          ;-----------------
          BIT HALFDUP       ; half_duplex mode ?
          BPL FULLDUP       ; if not , don't echo to screen.
          PHA               ; if half_duplex, save char
          JSR CWRITE        ; echo char to screen.
          PLA               ; restore char to accu
          ;-----------------
FULLDUP   CMP #BS           ; is char a backspace?
          BNE NOBS          ; if not, don't change it.
          LDA BSTODEL       ; if a backspace, translate it to BSTODEL value.
NOBS      JSR RWRITE        ; send keyboard char to remote host
          JMP START         ; start listening to remin again.
          ;-----------------
EXIT      LDA INTPRAM       ; switch BIOS RAM off and interpreter RAM on.
          ;-----------------
          LDA RETURN+1      ; push return adress and back to pascal.
          PHA
          LDA RETURN
          PHA
          ;-----------------
          RTS
;---------------------------------------------------------------------------
;---------------------------------------------------------------------------
;
; SUBROUTINES
;
;---------------------------
;RSTATUS : prepare stack and call reminstatus routine in attached driver.
;
RSTATUS   LDA #00           ; push controlword (=1 : statuscall )
          PHA
          LDA #01
          PHA
          ;-----------------
          LDA BUFLENPTR+1   ; push adres of BUFLEN
          PHA
          LDA BUFLENPTR
          PHA
          ;-----------------
          LDX #04           ; X=4 : statusrequest.
          JSR RSTAT
          RTS
;---------------------------
; CHECKCTRL
;
CHECKCTRL CMP #20           ; is it a control char?
          BCS ECHO1         ; no: echo to console.
          CMP #CR           ; pass to console in any case CR,LF,BS,FF,BELL.
          BEQ ECHO1
          CMP #LINEFD
          BEQ ECHO1
          CMP #BS
          BEQ ECHO1
          CMP #FF
          BEQ ECHO1
          CMP #BELL
          BEQ ECHO1
          PLA
          PLA
          JMP NOPRINT       ; do not echo other control characters.
ECHO1     RTS
;---------------------------
;REPLFF : replaces formfeed with 3 lf's and 1 cr,if requested.
;
REPLFF    BIT NOFEED        ; ff to be elimininated?
          BPL NOCHANGE      ; if not, return.
          LDA #03
          STA COUNT         ; set count to 3
$04       LDA #LINEFD       ; send 3 linefeeds to printer
          LDX #01
          JSR PWRITE
          DEC COUNT
          BNE $04
          LDA #CR           ; replace formfeed with cr.
NOCHANGE  RTS               ; and return.
;---------------------------
;--------------------------------------------------------------------------
;
; VARIABLES
;
;--------------------------------------------------------------------------
RETURN    .WORD 00
BUFLEN    .WORD 00
BUFLENPTR .WORD BUFLEN
COUNT     .BYTE 00
HALFDUP   .BYTE 00
PRINTER   .BYTE 00
NOFEED    .BYTE 00
XOFFWAIT  .BYTE 00
XOFFCHAR  .BYTE 00
XONCHAR   .BYTE 00
EXITCHAR  .BYTE 00
BSTODEL   .BYTE 00
REJECT    .BYTE 00
EMULATE   .BYTE 00
;----------------------------------------------------------------------
          .END
