; File MSXIBM.ASM
; Kermit system dependent module for IBM-PC
; Edit History
; [2.29] [jrd]
; Remove module VTS since mssset now selects terminal emulation type.
; Add Topview support for some screen handling functions and call smarter
; screen routines in module msyibm.
; Revise Putmod to not advance cursor to last column to avoid unwanted
; cursor wraparound on some machines (Compaq).
; Add DOS keyboard reading if running under IBM PC Netbios (pcnet != 0).
; Move all terminal Set and Status routines to this system dependent file.
; Add Global entry point vtstat to invoke local Status display.
; Remove Autowrap (H-19) as a separate item; a terminals now use Wrap.
; Add Set Term Color for terminal emulation; as a default the system uses
; colors in existence when Kermit is started. [jrd]
; Add extra code to SERINI & SERRST to assert 8250 Uart signals OUT1 and OUT2.
; Normally OUT2 is used to enable recoginition of IRQ3/4 and OUT1 is unused.
; Add code at end of SERRST to clear modem's delta line status bits and
; to reassert DTR, RTS, and OUT1/2.
; Add control of screen refresh as Set Term Color 0 for regular IBM CGA,
; waits for retrace before updating screen, and Set Color 10 for other systems
; not needing to wait. Default is to wait (no snow).
;
; [2.28 jrd/2]
; Modified procedure serini to flush input char before allowing interrupts,
; to stop interrupts from occurring during the procedure body, to stop
; interrupts from occurring due to TX holding buffer becoming empty, and to
; speed our exit from the procedure before an interrupt can occur.
; Modified procedure serrst to do quicker exit before interrupts occur.
; Modified procedure serint to remove unnecessary start-interrupts in proc
; body (it's an interrupt routine folks) and send 8259 end-of-interrupt
; code just before exiting procedure (less exposure time). [jrd]
; Joe R. Doupnik 22 Dec 1985.
; Make keywords mixed case for reading ease. 26 Dec 85 [jrd]
;
;  Make break be 275 ms, DT 5:51pm  Thursday, 6 December 1984

        public  serini, serrst, clrbuf, outchr, coms, vts, vtstat
        public  dodel, ctlu, cmblnk, locate, prtchr, dobaud, clearl,
        public  dodisk, getbaud, beep
        public  count, xofsnt, puthlp, putmod, clrmod, poscur
        public  sendbr, machnam, setktab, setkhlp, lclini, showkey
        include mssdef.h

false   equ     0
true    equ     1
mntrgh  equ     bufsiz*3/4      ; High point = 3/4 of buffer full.

; constants used by serial port handler

BRKBIT  EQU     040H            ; Send-break bit.
TIMER   EQU     40H             ; Use to issue short beep.
PORT_B  EQU     61H             ; Port B address.
MCONF   EQU     11H             ; Machine configuration.
KEYB    EQU     16H
BIOS    EQU     10H

MDMDAT1 EQU     03F8H           ; Address of modem port (data). [19b]
MDMSTS1 EQU     03FDH           ; Address of modem port status. [19b]
MDMCOM1 EQU     03FBH           ; Address of modem port command. [19b]
MDMDAT2 EQU     02F8H           ; Port 2 address. [19b]
MDMSTS2 EQU     02FDH           ; Port 2 status. [19b]
MDMCOM2 EQU     02FBH           ; Port 2 command. [19b]
MDMINP  EQU     1               ; Input ready bit.

MDMINTV EQU     0030H           ; Address of modem port interrupt vector.
MDINTV2 EQU     002CH           ; Address for port 2. [19b]
MDMINTO EQU     0EFH            ; Mask to enable interrupt for modem port.
MDINTO2 EQU     0F7H            ; Enable interrupt level 3. [19b]
MDMINTC EQU     010H            ; Bit to set to disable interrupts for modem.
MDINTC2 EQU     008H            ; Disable IRQ3. [19b]

INTCONT EQU     0021H           ; Address of 8259 interrupt controller ICW2-3.
INTCON1 EQU     0020H           ; Address of 8259 ICW1.
EOICOM  EQU     0064H           ; End of interrupt.
EOICOM2 EQU     0063H           ; End of interrupt for COM2. [19b]

; external variables used:
; drives - # of disk drives on system
; flags - global flags as per flginfo structure defined in pcdefs
; trans - global transmission parameters, trinfo struct defined in pcdefs
; portval - pointer to current portinfo structure (currently either port1
;    or port2)
; port1, port2 - portinfo structures for the corresponding ports

; global variables defined in this module:
; xofsnt, xofrcv - tell whether we saw or sent an xoff.
; setktab - keyword table for redefining keys (should contain a 0 if
;    not implemented)
; setkhlp - help for setktab.

datas   segment public 'datas'
        extrn   drives:byte,flags:byte, trans:byte
        extrn   portval:word, port1:byte, port2:byte
        extrn   pcnet:byte, refresh:byte
        extrn   vtemu:byte                                      ; [jrd]

; structure for status information table sttab.
stent   struc
sttyp   dw      ?               ; type (actually routine to call)
msg     dw      ?               ; message to print
val2    dw      ?               ; needed value: another message, or tbl addr
tstcel  dw      ?               ; address of cell to test, in data segment
basval  dw      0               ; base value, if non-zero
stent   ends

setktab db      12
        mkeyw   'BackSpace',0eh
        mkeyw   'F1',3bh
        mkeyw   'F2',3ch
        mkeyw   'F3',3dh
        mkeyw   'F4',3eh
        mkeyw   'F5',3fh
        mkeyw   'F6',40h
        mkeyw   'F7',41h
        mkeyw   'F8',42h
        mkeyw   'F9',43h
        mkeyw   'F10',44h
        mkeyw   'scan',-1

setkhlp db      cr,lf,'Keyname: "BackSpace", F1, ... F10, or "scan" follwed by '
        db      'decimal scan code$'
brkval  db      0               ; What to send for a break.
brkadr  dw      0               ; Where to send it.
modem   mdminfo <MDMDAT1,MDMSTS1,MDMCOM1,MDMINTO,MDMINTC,EOICOM,MDMINTV>
erms20  db      cr,lf,'?Warning: System has no disk drives$' ; [21a]
erms40  db      cr,lf,'?Warning: Unrecognized baud rate$'
badbd   db      cr,lf,'Unimplemented baud rate$'
prterr  db      '?Unrecognized value$'                  ; [jrd]
machnam db      'IBM-PC$'
crlf    db      cr,lf,'$'
delstr  db      BS,BS,'  ',BS,BS,'$'    ; Delete string. [21d]
clrlin  db      cr,'$'                  ; Clear line (just the cr part).
onmsg   db      'on$'                   ; [jrd]
offmsg  db      'off$'
savsci  dw      ?               ; Save for serial port interrupt vector. [14]
savscs  dw      ?               ; Ditto.  [14]
savbr1  dw      ?               ; "Break" interrupt vector. [25]
savbr2  dw      ?               ; Ditto. [25]
portin  db      0               ; Has comm port been initialized. [21c]
xofsnt  db      0               ; Say if we sent an XOFF.
xofrcv  db      0               ; Say if we received an XOFF.
tmp     db      ?,'$'
temp    dw      0
temp1   dw      ?               ; Temporary storage.
temp2   dw      ?               ; Temporary storage.
rdbuf   db      80 dup (?)      ; temporary storage. [jrd]

                                        ; begin Terminal emulator data set

termtb  db      tttypes+1               ; entries for Status, not Set
        mkeyw   'Heath-19',ttheath
        mkeyw   'None',ttgenrc
        mkeyw   'VT100',ttvt100         ; alias for VT102
        mkeyw   'VT102',ttvt100
        mkeyw   'VT52',ttvt52           ; [jrd]

vttbl   db      14                      ; fourteen entries.
        mkeyw   'Character-set',chaval
        mkeyw   'Color',200H            ; screen fore/back colors; 200H=marker
        mkeyw   'Cursor-style',curval
        mkeyw   'Heath-19',ttheath+100H ; note 100H flag for decoding here
        mkeyw   'Keyclick',keyval
        mkeyw   'Margin-bell',marval
        mkeyw   'None',ttgenrc+100H
        mkeyw   'Newline',newval
        mkeyw   'Screen-background',scrval
        mkeyw   'Tabstops',tabval
        mkeyw   'VT100',ttvt100+100H
        mkeyw   'VT102',ttvt100+100H
        mkeyw   'VT52',ttvt52+100H              ; [jrd]
        mkeyw   'Wrap',wraval

scrtab  db      02H                     ; screen attributes
        mkeyw   'Normal',00H
        mkeyw   'Reverse',01H

curtab  db      02H                     ; cursor attributes
        mkeyw   'Block',00H
        mkeyw   'Underline',01H

chatab  db      02H                     ; character set (pound sign choice)
        mkeyw   'UK-ascii',01H
        mkeyw   'US-ascii',00H          ; US ASCII is default (0).

tabtab  db      02H                     ; label says it all!
        mkeyw   'at',0FFH               ; For setting tab stops.
        mkeyw   'Clear',00H             ; For clearing tab stops.

alltab  db      02H                     ; more tab command decoding
        mkeyw   'all',00H
        mkeyw   'at',01H

vtable  dw      ontab, curtab, chatab, ontab, ontab, ontab, scrtab, 0

vtsflg equ      this byte               ; define small digits xxxval
newval  equ     $-vtsflg                ; and mask for bit in byte
        db      vsnewline
curval  equ     $-vtsflg
        db      vscursor
chaval  equ     $-vtsflg
        db      vsshift3
keyval  equ     $-vtsflg
        db      vskeyclick
wraval  equ     $-vtsflg
        db      vswrap
marval  equ     $-vtsflg
        db      vsmarginbell
scrval  equ     $-vtsflg
        db      vsscreen
numflgs equ     $-vtsflg
tabval  equ     $-vtsflg
        db      0
vtrtns  dw      numflgs dup (flgset), tabset ; dispatch table for vtsflg

clrset  db      ?                       ; Temp for SET Term Tabstops xxx
tmptabs db      80D dup (?)             ; Temporary for unconfirmed tabs.

vthlp   db      ' one of the following:',cr,lf
        db '  terminal types of:  None, Heath-19, VT52, VT100, or VT102',cr,lf
        db '  Newline-mode    Cursor-style        Character-set (US UK)',cr,lf
        db '  Keyclick        Margin-bell         Screen-background',cr,lf
        db '  Tabstops        Wrap (long lines)   Color (fore & background)'
        db '$'

wrpst   db      'Autowrap (Heath-19): $'

clrhlp  db      ' one of the following:'
        db      cr,lf,'  at     (to set tab stops)'
        db      cr,lf,'  clear  (to clear tab stops)$'
allhlp  db      ' one of the following:'
        db      cr,lf,'  at  (to clear tab stops at specific columns)'
        db      cr,lf,'  all (to clear all tab stops)$'
tbshlp  db      '  column number of tab stop to set, 1-79$'
tbchlp  db      '  column number of tab stop to clear, 1-79$'
tbserr  db      cr,lf,'?Column number is not in range 1-79$'
colhlp  db     cr,lf,'  0 for no-snow on IBM CGA or 10 for fast screen update'
        db      cr,lf,'  Foreground color (30-37), Background color (40-47)'
        db      cr,lf,'  colors are  1 = red, 2 = green, 4 = blue'
        db      cr,lf,'  Foreground = 30 + colors, Background = 40 + colors'
        db      cr,lf,'  Ex: 0, 34, 40   IBM CGA, blue chars on black field.$'
colerr  db      cr,lf,'?Value not in range of 0, 10, or 30-37 or 40-47$'
vtwrap  db      'Term wrap-lines: $'    ; 1st item has two spaces from STAT0
vtbell  db      'Term margin-bell: $'   ; this is also on same line as wrap.
vtnewln db      '  Term newline: $'
vtcur   db      '  Term cursor-style: $'
vtcset  db      '  Term character-set: $'
vtclik  db      '  Term key-click: $'
vtscrn  db      '  Term screen-background: $'
colst1  db      '  Term color  foreground:3$'
colst2  db      ' background:4$'
;;;;;;; stent   <srchkw,vtemst,termtb,flags.vtflg>      ; terminal emulator

vtstbl  stent   <srchkb,vtwrap,ontab,,vtsflg+wraval>    ; VT100 line wrap
        stent   <srchkb,vtbell,ontab,,vtsflg+marval>    ; VT100 margin bell
        stent   <srchkb,vtcur,curtab,,vtsflg+curval>    ; VT100 cursor type
        stent   <srchkb,vtnewln,ontab,,vtsflg+newval>   ; VT100 newline
        stent   <srchkb,vtscrn,scrtab,,vtsflg+scrval>   ; VT100 screen
        stent   <srchkb,vtcset,chatab,,vtsflg+chaval>   ; VT100 character set
        stent   <srchkb,vtclik,ontab,,vtsflg+keyval>    ; VT100 keyclick
        stent   <colstat>                               ; VT100 colors
        stent   <tabstat>       ; VT100 tab status - needs one whole line
        dw      0               ; end of table
                                                ; end of Terminal data set


ontab   db      02H             ; Two entries.
        db      03H,'off$'      ; Should be alphabetized.  [19a]
        dw      00H
        db      02H,'on$'
        dw      01H

comptab db      04H
        db      01H,'1$'
        dw      01H
        db      01H,'2$'
        dw      00H
        db      04H,'COM1$'
        dw      01H
        db      04H,'COM2$'
        dw      00H

; this table is indexed by the baud rate definitions given in
; pcdefs.  Unsupported baud rates should contain FF.
bddat   label   word
        dw      0FFH            ; 45.5 baud  -- Not supported.
        dw      900H            ; 50 baud
        dw      600H            ; 75 baud
        dw      417H            ; 110 baud
        dw      359H            ; 134.5 baud
        dw      300H            ; 150 baud
        dw      180H            ; 300 baud
        dw      0C0H            ; 600 baud
        dw      60H             ; 1200 baud
        dw      40H             ; 1800 baud
        dw      3AH             ; 2000 baud
        dw      30H             ; 2400 baud
        dw      18H             ; 4800 baud
        dw      0CH             ; 9600 baud
        dw      06H             ; 19200 baud
        dw      03H             ; 38400 baud

; variables for serial interrupt handler

source  db      bufsiz DUP(?)   ; Buffer for data from port.
srcpnt  dw      0               ; Pointer in buffer (DI).
count   dw      0               ; Number of chars in int buffer.
savesi  dw      0               ; Save SI register here.
telflg  db      0               ; Are we acting as a terminal.
mst     dw      0               ; Modem status address.
mdat    dw      0               ; Modem data address.
mdeoi   db      0               ; End-of-Interrupt value.

rbtrn   db      7fH             ; rubout

shkbuf  db      300 dup (?)     ; room for definition
shkmsg  db      '  Scan code: '
shkmln  equ     $-shkmsg
shkms1  db      cr,lf,'  Definition: '
shkm1ln equ     $-shkms1
datas   ends

code    segment public
        extrn   comnd:near, dopar:near, defkey:near, gss:near, lclyini:near
        extrn   sleep:near, atsclr:near, scrseg:near,scrloc:near, scrsync:near
        extrn   atoi:near, strlen:near, prtscr:near, scroff:near, scron:near
        assume  cs:code,ds:datas

; local initialization

lclini  proc    near
        mov     ax,0eH          ; scan code for arrow key
        mov     si,offset rbtrn ; translate to rubout
        mov     cx,1            ; one char translation
        call    defkey
        mov     brkval,BRKBIT   ; What to send for a break.
        mov     ax,modem.mdcom  ; Where to send it.
        mov     brkadr,ax
        call    lclyini         ; let other modules initialize too...
        ret
lclini  endp

; this is called by Kermit initialization.  It checks the
; number of disks on the system, sets the drives variable
; appropriately.  Returns normally.

DODISK  PROC    NEAR
        int mconf                       ; Get equipment configuration.
        mov ah,al                       ; Store AL value for a bit.
        and al,01H                      ; First, look at bit 0.
        jz dodsk0                       ; No disk drives -- forget it.
        mov al,ah                       ; Get back original value.
        mov cl,6                        ; Shift over bits 6 and 7.
        shr al,cl                       ; To positions 0 and 1.
        inc al                          ; Want 1 thru 4 (not 0 thru 3).
        mov drives,al                   ; Remember how many.
        ret
dodsk0: mov ah,prstr                    ; Print a warning message.
        mov dx,offset erms20            ; I'm not sure if things will
        int dos                         ; work with only a cassette.
        mov drives,0                    ; Say there aren't any drives.
        ret
DODISK  ENDP

; show the definition of a key.  The terminal argument block (which contains
; the address and length of the definition tables) is passed in ax.
; Returns a string to print in AX, length of same in CX.
; Revised to also run under IBM PC Netbios (lose some scan codes by doing
; DOS keyboard reads rather than Bios. Non-Network reads use Bios). [jrd]
; Returns normally.
showkey proc    near
        push    es
        push    ax              ; save the ptr
        mov     bx,ds
        mov     es,bx           ; address data segment
        cld
showk1: cmp     pcnet,0         ; running under Netbios? (0 = no)
        je      showk3          ; e = no.
        xor     bx,bx           ; say shift state is none
        mov     dl,0ffh         ; Network. keyboard input, via DOS
        mov     ah,conin        ; get a char into al
        int     dos
        xor     ah,ah           ; assume no scan code
        cmp     al,0            ; special char?
        jne     showk4          ; ne = no
        mov     ah,conin        ; get second byte, pseudo-scan-code
        int     dos
        xor     ah,ah           ; clear effective shift state
        jmp     showk4          ; and carry on regardless.

showk3: xor     ah,ah           ; Non-Netbios. Bios keyboard input.
        int     keyb            ; read a char
        push    ax              ; save the character
        call    gss             ; get shift state
        pop     bx
        mov     ah,al           ; shift state to ah
        mov     al,bh           ; scan code to al
showk4: push    ax              ; remember scan code
        mov     di,offset shkbuf
        mov     si,offset shkmsg
        mov     cx,shkmln
        rep     movsb           ; copy in initial message
        call    nout            ; write out scan code
        mov     si,offset shkms1
        mov     cx,shkm1ln      ; second message
        rep     movsb
        pop     ax              ; get scan code back
        pop     bx              ; and terminal arg block
        mov     cx,[bx].klen    ; and length
        jcxz    showk2          ; no table, not defined
        push    di              ; remember output ptr
        mov     di,[bx].ktab    ; get key table
        repne   scasw           ; search for a definition for this
        mov     si,di           ; remember result ptr
        pop     di              ; get output ptr back
        jne     showk2          ; not defined, forget it
        sub     si,[bx].ktab    ; compute offset from beginning
        sub     si,2            ; minus 2 for pre-increment
        add     si,[bx].krpl    ; get index into replacement table
        mov     si,[si]         ; pick up replacement
        mov     cl,[si]         ; get length
        mov     ch,0
        inc     si
        rep     movsb           ; copy into buffer
showk2: mov     ax,offset shkbuf ; this is buffer
        mov     cx,di
        sub     cx,ax           ; length
        pop     es
        ret                     ; and return
showkey endp

; Clear the input buffer. This throws away all the characters in the
; serial interrupt buffer.  This is particularly important when
; talking to servers, since NAKs can accumulate in the buffer.
; Returns normally.

CLRBUF  PROC    NEAR
        cli
        mov ax,offset source
        mov srcpnt,ax
        mov savesi,ax
        mov count,0
        sti
        ret
CLRBUF  ENDP

; Clear to the end of the current line.  Returns normally.
; Upgraded for Topview compatibility. [jrd]
CLEARL  PROC    NEAR
        push    ax
        push    bx
        push    dx
        mov ah,3                ; Clear to end of line.
        mov bh,0
        int bios                ; Get current cursor position into dx
        mov     ax,dx           ; Topview compatible clear line
        mov     bh,ah           ; same row
        mov     bl,79           ; last column
        call    atsclr          ; clear from ax to bx, screen coord
        pop     dx
        pop     bx
        pop     ax
        ret
CLEARL  ENDP

; Put the char in AH to the serial port.  This assumes the
; port has been initialized.  Should honor xon/xoff.  Skip returns on
; success, returns normally if the character cannot be written.

outchr: mov bp,portval
        cmp ds:[bp].floflg,0    ; Are we doing flow control.
        je outch2               ; No, just continue.
        mov cl,trans.rtime      ; receive timeout interval
        mov ch,0
outch1: cmp xofrcv,true         ; Are we being held?
        jne outch2              ; No - it's OK to go on.
        mov al,1                ; else sleep for a second
        call sleep
        loop outch1             ; and try it again
        mov xofrcv,false        ; timed out, force it off and fall thru.
outch2: push dx                 ; Save register.
        sub cx,cx
        mov al,ah               ; Parity routine works on AL.
        call dopar              ; Set parity appropriately.
        mov ah,al               ; Don't overwrite character with status.
        mov dx,modem.mdstat     ; Get port status.
outch3: in al,dx
        test al,20H             ; Transmitter ready?
        jnz outch4              ; Yes
        loop outch3
         jmp outch5             ; Timeout
outch4: mov al,ah               ; Now send it out
        mov dx,modem.mddat
        out dx,al
        pop dx
        jmp rskp
outch5: pop dx
        ret

; This routine blanks the screen.  Returns normally.
; Upgraded to Topview compatiblity. [jrd]
CMBLNK  PROC    NEAR
        push    ax
        push    bx
        xor     ax,ax           ; from screen loc 0,0
        mov     bx,184fH        ; to 24,79
        call    atsclr          ; do Topview compatible clear, in msyibm
        pop     bx
        pop     ax
        ret
CMBLNK  ENDP

; Locate: homes the cursor.  Returns normally.

LOCATE  PROC    NEAR
        mov dx,0                ; Go to top left corner of screen.
        jmp poscur
LOCATE  ENDP

; write a line in inverse video at the bottom of the screen...
; the line is passed in dx, terminated by a $.  Returns normally.
putmod  proc    near
        push    dx              ; preserve message
        mov     cx,1800h
        mov     dx,184fh
        mov     ax,600h         ; scroll to clear the line
        mov     bh,70h          ; inverse video
        int     bios
        mov     dx,1800h        ; now address line 24
        call    poscur
        pop     si              ; get message back
        mov     cx,1            ; only one char at a time
        xor     bh,bh           ; page 0
putmo1: lodsb                   ; get a byte
        cmp     al,'$'          ; end of string?
        je      putmo2
        push    si              ; save si
        push    ax              ; and the char
        call    poscur
        inc     dl              ; increment for next write
        pop     ax              ; recover char
        mov     ah,9            ; try this
        mov     bx,0070h        ; inverse video, page 0
        int     bios
        pop     si              ; recover pointer
        jmp     putmo1
putmo2: ret
putmod  endp

; clear the mode line written by putmod.  Returns normally.
clrmod  proc    near
        mov     cx,1800h
        mov     dx,184fh
        mov     ax,600h
        mov     bh,7h
        int     bios
        ret
clrmod  endp

; put a help message on the screen.  This one uses reverse video...
; pass the message in ax, terminated by a null.  Returns normally.
puthlp  proc    near
        push    ax              ; preserve this
        mov     si,ax           ; point to it
        mov     dh,1            ; init counter
puthl1: lodsb                   ; get a byte
        cmp     al,lf           ; linefeed?
        jne     puthl2          ; no, keep going
        inc     dh              ; count it
        jmp     puthl1          ; and keep looping
puthl2: cmp     al,0            ; end of string?
        jne     puthl1          ; no, keep going
        mov     temp1,dx        ; save number of lines [jrd]
        mov     ax,600h         ; scroll to clear window
        xor     cx,cx           ; from top left
        mov     dl,4fh          ; to bottom right of needed piece
        mov     bh,70h          ; inverse video
        int     bios
        call    scroff          ; turn off color display [jrd]
        call    locate          ; home cursor
        pop     si              ; point to string again
puthl3: lodsb                   ; get a byte
        cmp     al,0            ; end of string?
        je      puthl4          ; yes, stop
        mov     ah,14           ; int 10h, tty screen write.
        int     bios            ; else write to screen
        jmp     puthl3          ; and keep going
puthl4: mov     dx,temp1        ; recover number of lines [jrd]
        mov     dl,2            ; column 2 (left edge+2)
;;;     mov     dx,24 * 100H    ; go to last line
        call    poscur          ; position and return
        call    scron           ; turn on color display [jrd]
        ret
puthlp  endp

; Set the baud rate for the current port, based on the value
; in the portinfo structure.  Returns normally.

DOBAUD  PROC    NEAR
        mov bp,portval
        mov temp1,ax            ; Don't overwrite previous rate. [25]
        mov ax,ds:[bp].baud     ; Check if new rate is valid. [25]
        mov tmp,2
        mul tmp                 ; Get index into baud table.
        mov bx,offset bddat     ; Start of table.
        add bx,ax
        mov ax,[bx]             ; The data to output to port.
        cmp ax,0FFH             ; Unimplemented baud rate.
        jne dobd0
        mov ax,temp1            ; Get back orginal value.
        mov ds:[bp].baud,ax     ; Leave baud rate as is.
        mov ah,prstr
        mov dx,offset badbd     ; Give an error message.
        int dos
        ret
dobd0:  mov temp1,ax            ; Remember value to output. [25]
        mov dx,modem.mdcom      ; LCR -- Initialize baud rate. [19b]
        in al,dx
        mov bl,al
        or ax,80H
        out dx,al
        mov dx,modem.mddat      ; [19b]
        mov ax,temp1
        out dx,al
        inc dx
        mov al,ah
        out dx,al
        mov dx,modem.mdcom      ; [19b]
        mov al,bl
        out dx,al
        ret
DOBAUD  ENDP

; Get the current baud rate from the serial card and set it
; in the portinfo structure for the current port.  Returns normally.
; This is used during initialization.

GETBAUD PROC    NEAR
        mov dx,modem.mdcom      ; Get current Line Control Register value.
        in al,dx
        mov bl,al               ; Save it.
        or ax,80H               ; Turn on to access baud rate generator.
        out dx,al
        mov dx,modem.mddat      ; Divisor latch.
        inc dx
        in al,dx                ; Get hi order byte.
        mov ah,al               ; Save here.
        dec dx
        in al,dx                ; Get lo order byte.
        push ax
        mov dx,modem.mdcom
        mov al,bl               ; Restore old value.
        out dx,al
        pop ax
        cmp ax,0FFFFH           ; Who knows what this is.
        je getb2
        mov bx,offset bddat     ; Find rate's offset into table.
        mov cl,0                ; Keep track of index.
getb0:  cmp ax,[bx]
        je getb1
        inc cl
        cmp cl,baudsiz          ; At the end of the list.
        jge getb2
        add bx,2
        jmp getb0
getb1:  mov ch,0
        mov bp,portval
        mov ds:[bp].baud,cx     ; Set baud rate.
        ret
getb2:  mov ah,prstr
        mov dx,offset erms40
        int dos
        ret
GETBAUD ENDP

; skip returns if no character available at port,
; otherwise returns with char in al, # of chars in buffer in dx.
PRTCHR  PROC    NEAR
        call chkxon             ; see if we need to xon
        cmp count,0
        jnz prtch2
        jmp rskp                ; No data - check console.
prtch2: mov si,savesi
        lodsb                   ; get a byte
        cmp si,offset source + bufsiz   ; bigger than buffer?
        jb prtch1               ; no, keep going
        mov si,offset source    ; yes, wrap around
prtch1: dec count
        mov savesi,si
        mov dx,count            ; return # of chars in buffer
        ret
PRTCHR  ENDP

; local routine to see if we have to transmit an xon
chkxon  proc    near
        push    bx
        mov     bx,portval
        cmp     [bx].floflg,0   ; doing flow control?
        je      chkxo1          ; no, skip all this
        cmp     xofsnt,false    ; have we sent an xoff?
        je      chkxo1          ; no, forget it
        cmp     count,mntrgh    ; below trigger?
        jae     chkxo1          ; no, forget it
        mov     ax,[bx].flowc   ; ah gets xon
        call    outchr          ; send it
        nop
        nop
        nop                     ; in case it skips
        mov     xofsnt,false    ; remember we've sent the xon.
chkxo1: pop     bx              ; restore register
        ret                     ; and return
chkxon  endp

; Send a break out the current serial port.  Returns normally.
SENDBR  PROC    NEAR
        push cx
        push dx
        push ax
        xor cx,cx               ; Clear loop counter.
        mov dx,brkadr           ; Port address.  [19b]
        in al,dx                ; Get current setting.
        or al,brkval            ; Set send-break bit(s).
        out dx,al               ; Start the break.
        push ax
        mov ax,275              ; # of ms to wait
        call pcwait             ; hold break for desired interval
        pop ax
        xor al,brkval           ; Clear send-break bit(s).
        out dx,al               ; Stop the break.
        pop ax
        pop dx
        pop cx
        ret                     ; And return.
SENDBR  ENDP

; Wait for the # of milliseconds in ax
; Thanks to Bernie Eiben for this one.
pcwait  proc    near
        mov     cx,240          ; inner loop counter for 1 millisecond
pcwai1: sub     cx,1            ; inner loop takes 20 clock cycles
        jnz     pcwai1
        dec     ax              ; outer loop counter
        jnz     pcwait          ; wait another millisecond
        ret
pcwait  endp

; Position the cursor according to contents of DX:
; DH contains row, DL contains column.  Returns normally.
; Upgraded for Topview notification. [jrd]
POSCUR  PROC    NEAR
        push    bx
        push    cx
        push    dx
        push    di
        push    es
        mov     ah,2            ; Position cursor.
        mov     bh,0            ; page 0
        int     bios
        mov     dl,79           ; say column 79
        call    scrseg          ; get screen buffer address in es:di
        call    scrloc          ; get offset in ax
        add     di,ax           ; final location
        mov     cx,79           ; pretend changed whole line
        call    scrsync         ; synch Topview
        pop     es
        pop     di
        pop     dx
        pop     cx
        pop     bx
        ret
POSCUR  ENDP

; Delete a character from the terminal.  This works by printing
; backspaces and spaces.  Returns normally.

DODEL   PROC    NEAR
        mov ah,prstr
        mov dx,offset delstr    ; Erase weird character.
        int dos
        ret
DODEL   ENDP

; Move the cursor to the left margin, then clear to end of line.
; Returns normally.

CTLU    PROC    NEAR
        mov ah,prstr
        mov dx,offset clrlin
        int dos
        call clearl
        ret
CTLU    ENDP

; set the current port.

COMS    PROC    NEAR
        mov dx,offset comptab
        mov bx,0
        mov ah,cmkey
        call comnd
         jmp r
        push bx
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp comx               ;  Didn't get a confirm.
         nop
        pop bx
        mov flags.comflg,bl     ; Set the comm port flag.
        cmp flags.comflg,1      ; Using Com 1?
        jne coms0               ; Nope.
        mov ax,offset port1
        mov portval,ax
        mov modem.mddat,MDMDAT1 ; Set COM1 defaults.
        mov modem.mdstat,MDMSTS1
        mov modem.mdcom,MDMCOM1
        mov modem.mddis,MDMINTC
        mov modem.mden,MDMINTO
        mov modem.mdmeoi,EOICOM
        mov modem.mdintv,MDMINTV
        mov brkadr,MDMCOM1
        ret
coms0:  mov ax,offset port2
        mov portval,ax
        mov modem.mddat,MDMDAT2 ; Set COM2 defaults.
        mov modem.mdstat,MDMSTS2
        mov modem.mdcom,MDMCOM2
        mov modem.mddis,MDINTC2
        mov modem.mden,MDINTO2
        mov modem.mdmeoi,EOICOM2
        mov modem.mdintv,MDINTV2
        mov brkadr,MDMCOM2
        ret
comx:   pop bx
        ret
COMS    ENDP


                                        ; begin Terminal set & status code

; SET Term parameters, especially for use with VT100 emulator. [jrd]
; Taken from work done originally by James Harvey IUPUI.
; VTS is called only by mssset to set terminal type and characteristics.

VTS     proc    near                    ; SET TERM whatever
        mov     ah,cmkey                ; Parse another keyword.
        mov     bx,offset vthlp         ; Use this help
        mov     dx,offset vttbl         ; Use this table
        call    comnd
         jmp    r                       ; VSETUP always returns +1.
        cmp     bx,100H                 ; marker for terminal type?
        jb      vsetu1                  ; b = no.
        cmp     bx,200h                 ; marker for set term color?
        je      vsetu2                  ; e = yes
        push bx                         ; yes
        mov ah,cmcfm
        call comnd                      ; Get a confirm.
         jmp vsetu0                     ; Didn't get a confirm.
         nop
        pop bx
        mov flags.vtflg,bl              ; Set the terminal emulation type
        ret                             ; in standard global byte.
vsetu0: pop bx
        ret

vsetu1: sal     bx,1                    ; Make bx a word index.
        call    vtrtns[bx]              ; Dispatch.
         jmp    r                       ; VSETUP always returns +1.
        jmp     r                       ; VSETUP always returns +1.

                                ; Set Term Color foreground, background
vsetu2: mov     ah,cmtxt                ; get number(s) after set term color
        mov     dx,offset colhlp        ; use this help
        mov     bx,offset rdbuf         ; temp buffer
        mov     byte ptr [bx],0         ; clear the buffer
        call    comnd
         jmp    r
        cmp     ah,0                    ; anything given?
        jne     vsetu2a                 ; ne = yes.
        jmp     r                       ; else give not confirmed msg
vsetu2a:mov     ah,cmcfm                ; Parse confirm
        mov     bx,0                    ; Use default help.
        call    comnd
         jmp    r                       ; not confirmed, complain.
        mov     si,offset rdbuf         ; si = place where atoi wants text
vsetu2c:mov     dx,si
        call    strlen                  ; current length of text
        jcxz    vsetu2x                 ; nothing left
        mov     ah,cl                   ; put length where Atoi wants it
        call    atoi                    ; convert text to numeric in ax
         nop
         nop
         nop
        cmp     ax,0                    ; regualr IBM CGA refresh?
        je      vsetu2j                 ; e = yes
        cmp     ax,10                   ; fast refresh?
        je      vsetu2k                 ; e = yes
        cmp     ax,30                   ; check range
        jb      colbad                  ; b = too small. complain
        cmp     ax,37
        jna     vsetu2b                 ; 30-37 is foreground color
        cmp     ax,40
        jb      colbad
        cmp     ax,47                   ; Compare as unsigned.
        ja      vsetu2b                 ; 40-47 is background, else error

vsetu2b:push    bx
        mov     bx,vtemu.att_ptr        ; Get address of attributes byte
        cmp     al,40                   ; background (40-47)?
        jnb     vsetu2f                 ; nb = yes
        sub     al,30                   ; remove foreground bias
        and     byte ptr [bx],not 07H   ; clear foreground bits
        test    al,1                    ; ANSI red?
        jz      vsetu2d                 ; z = no
        or      byte ptr [bx],4         ; set IBM foreground red
vsetu2d:test    al,2                    ; ANSI & IBM green?
        jz      vsetu2e                 ; z = no
        or      byte ptr [bx],2         ; set green foreground
vsetu2e:test    al,4                    ; ANSI Blue?
        jz      vsetu2i                 ; z = no
        or      byte ptr [bx],1         ; set IBM blue
        jmp     vsetu2i                 ; done with foreground settings
vsetu2f:sub     al,40                   ; remove background bias
        and     byte ptr [bx],not 70H   ; clear background attributes
        test    al,1                    ; ANSI red?
        jz      vsetu2g                 ; z = no
        or      byte ptr [bx],40h       ; set IBM background red
vsetu2g:test    al,2                    ; ANSI & IBM green?
        jz      vsetu2h                 ; z = no
        or      byte ptr [bx],20h       ; set green background
vsetu2h:test    al,4                    ; ANSI Blue?
        jz      vsetu2i                 ; z = no
        or      byte ptr [bx],10h       ; set IBM blue
vsetu2i:pop     bx
        jmp     vsetu2c                 ; get any next value
vsetu2j:mov     refresh,0               ; Regular (slow) screen refresh
        jmp     vsetu2c                 ; get next value
vsetu2k:mov     refresh,1               ; Fast screen refresh
        jmp     vsetu2c
vsetu2x:jmp     rskp                    ; and return normally.

colbad: mov     ah,prstr                ; Not in range - complain and exit.
        mov     dx,offset colerr
        int     dos
        jmp     rskp



; SET Term flags. These are the (near) equivalent of VT100 Setup mode values.

flgset: push    bx                      ; Save bx for a second.
        mov     ah,cmkey                ; Another keyword.
        mov     dx,vtable[bx]           ; The table to use.
        mov     bx,0                    ; Use default help.
        call    comnd
         jmp    flgse3
        push    bx                      ; Save switch value.
        mov     ah,cmcfm                ; Confirm it.
        call    comnd
         jmp    flgse2
        pop     dx                      ; Restore switch value.
        pop     bx                      ; And index.
        sar     bx,1                    ; Make it a byte index.
        mov     al,vtsflg[bx]           ; Get the flag.
        cmp     dx,0                    ; Set or clear?
        je      flgse1                  ; Go clear it.
        or      vtemu.vtflgst,al        ; Set the flag.
        jmp     rskp                    ; Give good return.

flgse1: not     al                      ; Complement
        and     vtemu.vtflgst,al        ; Clear the indicated flag.
        jmp     rskp                    ; Give good return.
flgse2: pop     bx                      ; error exits
flgse3: pop     bx
        ret

;       SET Term Tabstops Clear ALL
;       SET Term Tabstops Clear AT n1, n2, ..., nx
;       SET Term Tabstops AT n1, n2, ..., nx

tabset: cld                             ; Make sure this is clear.
        mov     si,vtemu.vttbst         ; Copy current tab stops.
        mov     di,offset tmptabs
        mov     cx,80                   ; 80 columns
        rep     movsb
        mov     ah,cmkey                ; Parse keyword.
        mov     bx,offset clrhlp        ; Use this help text.
        mov     dx,offset tabtab        ; This table.
        call    comnd
         jmp    r
        mov     clrset,bl               ; Note whether setting or clearing
        cmp     bl,0                    ; Was it set or clear?
        jne     tabse1                  ; SET - go parse column number(s).
        mov     ah,cmkey                ; CLEAR - parse ALL or AT
        mov     bx,offset allhlp        ; Use this help text.
        mov     dx,offset alltab        ; Parse ALL or AT.
        call    comnd
         jmp    r
        cmp     bx,0                    ; ALL?
        jne     tabse1                  ; AT - go clear at specific places.
        mov     al,0                    ; Clear means clear all tab stops.
        mov     cx,80D
        mov     di,offset tmptabs
        rep     stosb
        mov     ah,cmcfm                ; Confirm it.
        call    comnd
         jmp    r
        jmp     tabcpy                  ; Go copy tab stops back.

tabse1: mov     dx,offset tbshlp        ; Tell them we want a column number.
        cmp     clrset,0                ; Clearing?
        jne     tabse2                  ; No - we guessed right on help.
        mov     dx,offset tbchlp        ; Yes - use this help instead.
tabse2: mov     ah,cmtxt                ; get text w/o white space
        mov     bx,offset rdbuf         ; temp buffer
        call    comnd
         jmp    r
        cmp     ah,0                    ; anything given?
        jne     tabse4                  ; ne = yes.
        jmp     r                       ; else give not confirmed msg
tabse4: mov     ah,cmcfm                ; Parse confirm
        mov     bx,0                    ; Use default help.
        call    comnd
         jmp    r                       ; not confirmed, complain.
        mov     si,offset rdbuf         ; si = place where atoi wants text
tabse5: mov     dx,si
        call    strlen                  ; current length of text
        mov     ah,cl                   ; put length where Atoi wants it
        jcxz    tabcpy                  ; nothing left
        call    atoi                    ; convert text to numeric in ax
         nop
         nop
         nop
        mov     bx,ax                   ; for subscripting in code below
        dec     bx                      ; Put column in range 0-79
        cmp     bx,0                    ; check range (1-80 --> 0-79)
        jl      tbsbad                  ; l = too small. complain
        cmp     bx,79                   ; Compare as unsigned.
        jna     tabse3
tbsbad: mov     ah,prstr                ; Not in range - complain and exit.
        mov     dx,offset tbserr
        int     dos
        jmp     rskp

tabse3: mov     al,clrset               ; Get value for setting or clearing.
        mov     tmptabs[bx],al          ; store in tabs temp work array
        jmp     tabse5                  ; look for more

tabcpy: mov     cx,80                   ; Copy tab stops
        mov     si,offset tmptabs       ; from work buffer
        mov     di,vtemu.vttbst         ; to terminal emulator's active buffer
        rep     movsb
        mov     cx,80
        mov     si,offset tmptabs       ; duplicate in emulator's Setup buffer
        mov     di,vtemu.vttbs          ; this is the cold-start buffer
        rep     movsb
        jmp     rskp                    ; Give good return.

VTS     endp                            ; end of Set Term things.

                ; Terminal Status display, called within STAT0: in MSSSET.
VTSTAT  proc    near    ;enter with di within sttbuf, save bx, di
        push    bx
        push    di
        mov     bx,ds
        mov     es,bx
        cld
        mov     bx,offset vtstbl        ; table of things to show
        xor     cx,cx
vtsta1: cmp     word ptr [bx],0         ; end of table?
        je      vtstax                  ; e = yes
        push    bx
        call    [bx].sttyp              ; call appropriate routine
        pop     bx                      ; cx incremented by output count
        cmp     cx,37                   ; place for second display
        jbe     vtsta2                  ; le = only half full
        mov     dx,offset crlf          ; over half full. send cr/lf
        mov     ah,prstr
        int     dos
        xor     cx,cx                   ; say line is empty now
        jmp     vtsta4
vtsta2: push    cx
        mov     cx,37                   ; where we want to be next time
        pop     ax
        sub     cx,ax                   ; compute number of filler spaces
        jcxz    vtsta4                  ; nothing to do
        mov     ah,conout
        mov     dl,' '
vtsta3: int     dos                     ; fill with spaces
        loop    vtsta3                  ; do cx times
        mov     cx,37                   ; current column number
vtsta4:add      bx,size stent           ; look at next entry
        jmp     vtsta1                  ; and do it
vtstax: pop     di
        pop     bx
        add     di,40                   ; say we used a lot of buffer space
        ret                             ; return to STAT0: in MSSSET.

                                        ; foreground/background color status
colstat proc    near
        mov     dx,offset colst1
        mov     ah,prstr
        int     dos                     ; print first part of msg
        xor     ax,ax
        mov     bx,vtemu.att_ptr        ; pointer to attributes byte
        test    byte ptr [bx],1         ; IBM blue foregound?
        jz      colsta1                 ; z = no
        or      al,4                    ; set blue bit
colsta1:test    byte ptr [bx],2         ; IBM green foreground?
        jz      colsta2
        or      al,2
colsta2:test    byte ptr [bx],4         ; IBM red foreground?
        jz      colsta3
        or      al,1
colsta3:add     al,'0'                  ; add ascii bias
        mov     dl,al                   ; move to DOS place
        mov     ah,conout
        int     dos                     ; print the last digit
        mov     dx,offset colst2        ; now do background
        mov     ah,prstr
        int     dos                     ; print second part of msg
        xor     ax,ax
        test    byte ptr [bx],10h       ; IBM blue background?
        jz      colsta4                 ; z = no
        or      al,4                    ; set blue bit
colsta4:test    byte ptr [bx],20h       ; IBM green background?
        jz      colsta5
        or      al,2                    ; set green bit
colsta5:test    byte ptr [bx],40h       ; IBM red background?
        jz      colsta6
        or      al,1                    ; set red bit
colsta6:add     al,'0'                  ; add ascii bias
        mov     dl,al                   ; move to DOS place
        mov     ah,conout
        int     dos
        mov     ah,3                    ; find cursor column
        xor     bh,bh                   ; page zero
        int     14h                     ; screen interrupt
        xor     dh,dh                   ; clear row part
        add     cx,dx                   ; update column counter
        inc     cx                      ; screen coord start at 0,0
        ret
colstat endp
                                        ; Tabs Status display
tabstat proc    near                    ; display tabs ruler for Status
        push    dx
        jcxz    tabsta0                 ; empty line, as it should be
        xor     cx,cx                   ; used line, empty it
        mov     dx,offset crlf          ; cr, lf
        mov     ah,prstr
        int     dos
tabsta0:mov     si,vtemu.vttbst         ; active tabs address, not shadow
        mov     cx,79                   ; loop 79 times
        dec     si                      ; dec for inc below
        xor     ax,ax                   ; tens counter
tabsta1:mov     dl,'.'                  ; default position symbol
        inc     si                      ; start with position 2
        inc     al
        cmp     al,10                   ; time to roll over?
        jb      tabsta2                 ; b = not yet
        mov     al,0                    ; modulo 10
        inc     ah
        mov     dl,ah                   ; display a tens-digit
        add     dl,'0'
tabsta2:cmp     byte ptr [si],0         ; is tab set?
        je      tabsta3                 ; e = no
        mov     dl,'T'                  ; yes, display a 'T'
tabsta3:push    ax
        mov     ah,conout               ; console output
        int     dos
        pop     ax
        loop    tabsta1                 ; loop til done (cx has count)
        pop     dx
        mov     cx,79                   ; say line is full
        ret
tabstat endp

; handler routines for status. All are called with bx/ stat ptr.

onoff   proc    near
        call    stmsg           ; print the message
        mov     si,[bx].basval  ; get base value
        cmp     si,0            ; any there?
        je      onoff1          ; e = no
        mov     si,[si]         ; yes, use as base address
onoff1: add     si,[bx].tstcel  ; add offset of test cell
        mov     al,[si]
        mov     dx,offset onmsg ;
        add     cx,2            ; assume two byte on message
        or      al,al           ; test value
        jnz     onoff2          ; nz = on
        mov     dx,offset offmsg
        inc     cx              ; three byte message
onoff2: mov     ah,prstr        ; display the message
        int     dos
        ret
onoff   endp

; search a keyword table for a bit value, print that value. [jrd]
srchkb  proc    near
        call    stmsg           ; first print message
        call    stbval          ; get bit set or reset
        mov     ah,0            ; al has 0/1, high order is 0
        mov     bx,[bx].val2    ; this is table address
        jmp     prttab          ; and look in table.
srchkb  endp

; get address of test value in stent.  Returns address in si. [jrd]
stbval  proc    near
        mov     si,[bx].basval  ; get address of test value
        cmp     si,0            ; any there?
        je      stbva1          ; no, quit with no match
        mov     al,byte ptr [si]; get byte value
        mov     ah,0
        test    al,vtemu.vtflgst ; bit test value against emulator flags byte
        jz      stbva1          ; z = they don't match
        mov     al,1            ;  match
        ret
stbva1: mov     al,0            ; no match
        ret                     ; and return it
stbval  endp

; get address of test value in stent.  Returns address in si
stval   proc    near
        mov     si,[bx].basval  ; get base value
        cmp     si,0            ; any there?
        je      stva1           ; no, keep going
        mov     si,[si]         ; yes, use as base address
stva1:  add     si,[bx].tstcel  ; add offset of test cell
        ret                     ; and return it
stval   endp

; copy the message to the screen
stmsg   proc    near
        mov     si,[bx].msg     ; get message address
stms1:  lodsb                   ; get a byte
        cmp     al,'$'          ; end of message?
        je      stms2           ; e = yes
        mov     dl,al           ; display the character
        mov     ah,conout
        int     dos
        inc     cx              ; count output chars
        jmp     stms1
stms2:  ret
stmsg   endp

; Print value from table.  BX/address of table, AL/value of variable.
prttab: push    cx              ; save column count
        mov cl,[bx]             ; Number of entries in our table.
        inc bx                  ; Point to the data.
prtt0:  mov dl,[bx]             ; Length of keyword.
        inc bx                  ; Point to keyword.
        mov dh,0
        inc dx                  ; Account for "$" in table.
        mov si,dx               ; Put to index register.
        cmp ax,[bx+si]          ; Is this the one?
        je prtt1
        add bx,dx               ; Go to end of keyword.
        add bx,2                ; Point to next keyword.
        dec cl                  ; Any more keywords to check?
        jnz prtt0               ; Yes, go to it.
        mov bx,offset prterr
prtt1:  mov si,bx
        pop     cx              ; recover column count
        jmp stms1               ; copy in message
        ret                     ; and return
VTSTAT  endp                    ; end of Terminal set & status code


; Initialization for using serial port.  This routine performs
; any initialization necessary for using the serial port, including
; setting up interrupt routines, setting buffer pointers, etc.
; Doing this twice in a row should be harmless (this version checks
; a flag and returns if initialization has already been done).
; SERRST below should restore any interrupt vectors that this changes.
;
; Revised slightly by Joe R. Doupnik 22 Dec 1985 to prevent interrupts
; being enabled until we're done, to stop interrupts from occurring when
; TX holding buffer becomes empty (a useless interrupt for us), and to
; shorten the time between enabling interrupts and our exit. [jrd]
; Returns normally.

SERINI  PROC    NEAR
        cmp portin,0            ; Did we initialize port already? [21c]
        jne serin0              ; Yes, so just leave. [21c]
        cli                     ; Disable interrupts
        cld                     ; Do increments in string operations
        push es                 ; moved here by [jrd]
        xor ax,ax               ; Address low memory
        mov es,ax
        mov bx,modem.mdintv     ; Save serial card interrupt vector. [19b]
        mov ax,es:[bx]
        mov savsci,ax
        mov ax,offset serint    ; And point it to my routine
        mov es:[bx],ax
        add bx,2                ; Save CS register too. [19b]
        mov ax,es:[bx]
        mov savscs,ax
        mov es:[bx],cs
        mov portin,1            ; Remember port has been initialize.

        mov ax,offset source    ; Clear input buffer, but don't touch [jrd]
        mov srcpnt,ax           ;  interrupts (CLRBUF turns them on!). [jrd]
        mov savesi,ax           ; [jrd]
        mov count,0             ; end of clearing input buffer. [jrd]
        pop es                  ; moved here by [jrd]
        mov ax,modem.mdstat
        mov mst,ax              ; Use this address for status.
        mov ax,modem.mddat
        mov mdat,ax             ; Use this address for data.
        mov al,modem.mdmeoi
        mov mdeoi,al            ; Use to signify end-of-interrupt.
        in al,21H               ; Set up 8259 interrupt controller
        and al,modem.mden       ; Enable INT3 or INT4.
        out 21H,al
        mov dx,modem.mdcom      ; Set up the serial card Line Control Reg.
        mov al,3                ; 8 data bits. DLAB = 0. [jrd]
        out dx,al
        mov dl,0F8H             ; Read and ignore (flush) any current [jrd]
        in al,dx                ; char in UART's receive buffer. [jrd]
        mov dl,0F9H
        mov al,1                ; Set up interrupt enable register
        out dx,al               ;  for Data Available only.
        mov dl,0FCH             ; Modem Control Register
        mov al,0fh              ; assert DTR, RTS, OUT1, and OUT2. [jrd]
        out dx,al
        sti                     ; Allow interrupts (AFTER next instr). [jrd]
serin0: ret                     ; We're done. [jrd]
SERINI  ENDP

; Reset the serial port.  This is the opposite of serini.  Calling
; this twice without intervening calls to serini should be harmless.
; Moved push/pop es code to do quicker exit before interrupts enabled. [jrd]
; Returns normally.

SERRST  PROC    NEAR
        cmp portin,0            ; Reset already?
        je srst1                ; Yes, just leave.
        cli                     ; Disable interrupts
        mov dx,03FCH            ; Model Control Register
        cmp flags.comflg,1      ; Using port 1 ?
        je srst0                ; Yes - continue.
        mov dh,02               ; Set for port 2.
srst0:  mov al,0fh              ; set DTR, RTS, OUT1&2 high, just to be safe.
        out dx,al
        in al,21H               ; Interrupt controller
        or al,modem.mddis       ; Inhibit IRQ3 or IRQ4.
        out 21H,al
        xor bx,bx               ; Address low memory
        push es                 ; preserve this. Moved here by [jrd]
        mov es,bx
        mov bx,modem.mdintv     ; Restore the serial card int vector
        mov ax,savsci
        mov es:[bx],ax
        add bx,2                ; Restore CS too.
        mov ax,savscs
        mov es:[bx],ax
        pop es                  ; Moved here by [jrd]
                ; clear modem's delta status bits and reassert DTR etc [jrd]
        cmp flags.comflg,1      ; using port 1?
        je srst2                ; e = yes
        mov dx,02FCH            ; Modem Control Register, port 2
        mov al,0fh              ; reassert DTR,RTS,OUT1, OUT2
        out dx,al
        mov dx,02FEH            ; Modem Status register, port 2
        in al,dx                ; Clear Status reg by reading it.
        jmp srst3               ; and exit
srst2:  mov dx,03FCH            ; Modem Control register, port 1
        mov al,0fh              ; reassert DTR,RTS,OUT1, OUT2
        out dx,al
        mov dx,03FEH            ; Modem Status register, port 1
        in al,dx                ; Clear Status reg by reading it.
srst3:  mov portin,0            ; Reset flag.
        sti                     ; Enable interrupts after next instruct. [jrd]
srst1:  ret                     ; All done.
SERRST  ENDP

; serial port interrupt routine.  This is not accessible outside this
; module, handles serial port receiver interrupts.

SERINT  PROC  NEAR
        push ax                 ; [jrd]
        push ds                 ; do ds and ax first [jrd]
        push bx
        push dx
        push es
        push di
        push bp
        push cx
        cld
        mov ax,seg datas
        mov ds,ax               ; address data segment
        mov es,ax
        mov di,srcpnt           ; Registers for storing data.
        mov dx,mst              ; Asynch status port. [19b]
        in al,dx
        test al,mdminp          ; Data available?
        jz retint               ; Nope.
        mov dx,mdat             ; [19b]
        in al,dx
        cmp telflg,0            ; File transfer or terminal mode? [17c]
        jz srint0
        and al,7FH              ; Terminal mode (7 bits only).
srint0: or al,al
        jz retint               ; Ignore nulls.
        mov ah,al
        and ah,7fH              ; strip parity temporarily
        cmp ah,7FH              ; Ignore rubouts, too.
        jz retint
        mov bp,portval
        cmp ds:[bp].floflg,0    ; Doing flow control?
        je srint2               ; Nope.
        mov bx,ds:[bp].flowc    ; Flow control char (BH = XON, BL = XOFF).
        cmp al,bl               ; Is it an XOFF?
        jne srint1              ; Nope, go on.
        mov xofrcv,true         ; Set the flag.
        jmp retint
srint1: cmp al,bh               ; Get an XON?
        jne srint2              ; No, go on.
        mov xofrcv,false        ; Clear our flag.
        jmp retint
srint2: stosb
        cmp di,offset source + bufsiz
        jb srint3               ; not past end...
        mov di,offset source    ; wrap buffer around
srint3: inc count
        cmp ds:[bp].floflg,0    ; Doing flow control?
        je retint               ; No, just leave.
        cmp xofsnt,true         ; Have we sent an XOFF?
        je retint               ; Yes.
        cmp count,mntrgh        ; Past the high trigger point?
        jbe retint              ; No, we're within our limit.
        mov ah,bl               ; Get the XOFF.
        call outchr             ; Send it.
        nop
        nop
        nop                     ; ignore failure.
        mov xofsnt,true         ; Remember we sent it.
retint: mov srcpnt,di
        pop cx                  ; timing is important here. [jrd]
        pop bp                  ; each pop takes 10 machine cycles. [jrd]
        pop di
        pop es
        pop dx
        pop bx
        mov al,mdeoi            ; [19b] Move closest to iret. [jrd]
        pop ds                  ; intcon1 below is not ds: relative. [jrd]
        out intcon1,al          ; Send End-of-Interrupt to 8259.
        pop ax                  ; [jrd]
intret: iret
SERINT  ENDP

; Produce a short beep.  The PC DOS bell is long enough to cause a loss
; of data at the port.  Returns normally.

BEEP    PROC    NEAR
        mov al,10110110B        ; Gen a short beep (long one losses data.)
        out timer+3,al          ; Code snarfed from Technical Reference.
        mov ax,533H
        out timer+2,al
        mov al,ah
        out timer+2,al
        in al,port_b
        mov ah,al
        or al,03
        out port_b,al
        sub cx,cx
        mov bl,1
beep0:  loop beep0
        dec bl
        jnz beep0
        mov al,ah
        out port_b,al
        ret
BEEP    ENDP

; put the number in ax into the buffer pointed to by di.  Di is updated
nout    proc    near
        mov     dx,0            ; high order is always 0.
        mov     bx,10
        div     bx              ; divide to get digit
        push    dx              ; save remainder digit
        or      ax,ax           ; test quotient
        jz      nout1           ; zero, no more of number
        call    nout            ; else call for rest of number
nout1:  pop     ax              ; get digit back
        add     al,'0'          ; make printable
        stosb                   ; drop it off
        ret                     ; and return
nout    endp

; Jumping to this location is like retskp.  It assumes the instruction
;   after the call is a jmp addr.

RSKP    PROC    NEAR
        pop bp
        add bp,3
        push bp
        ret
RSKP    ENDP

; Jumping here is the same as a ret.

R       PROC    NEAR
        ret
R       ENDP

code    ends
        end



