;PAGE	58,132
     .XLIST
;From hplabs!wunder@FORD-WDL1 Sun Jul 10 02:16:57 1983
;Date:  9-Jul-83 01:22:33-PDT
;From: wunder@FORD-WDL1
;Received: by HP-VENUS via CSNET; 9 Jul 1983 20:09:52-PDT (Sat)
;Received: from FORD-WDL1 by udel-relay.ARPA ; 9 Jul 83 05:02:00 EDT (Sat)
;Received: from udel-relay.ARPA by rand-relay.ARPA ; 9 Jul 83 14:33:33 PDT
;To: twc.hplabs@UDEL-RELAY
;Via:  UDel; 9 Jul 83 19:41-PDT
;Return-Path: <>
;Subject: kermit.asm=147607 bytes
;Message-Id: <426654593.7118.hplabs@HP-VENUS>
;1.3A 1-Jul-83 DK32
;DK1 Set Bell Off, DK2 Make it more like a Zenith (I/D L/C,inverse,24x80),
;
;**************************** HP 150 version ****************
;
; [17] 22-Nov-83  convert to run on HP150
;
;******************** Version 1.3 ********************************** 
; [16] Make adjustments to XT changes so that things that used to work
;      still do: pass along characters with the eighth bit on, and in
;      OUTCHR, don't overwrite character after setting the appropriate 
;      parity.
;      Daphne, 6/17/83
; [15] Add tabs stop and clear screen support.  Don't give checksum 
;      warnings.  Give keyboard input higher scheduling priority than 
;      port input.  This allows users to ^S, ^O, ^C, etc. the output 
;      when the port is full of characters.  Add a CMU conditional
;      to swap control-backspace (delete) with backspace since TOPS-20
;      and VMS are more delete than backspace oriented.
;      Glenn Marcy, 6/8/83
; [14] Add CMU changes that allow Kermit to work on the XT.  Plus add
;      a statement omitted previously when comparing packet numbers.   
;      Daphne, 5/26/83  (changes by Dave King, CMU)
;******************** Version 1.2 ********************************** 
; [13] When closing received file, handle case where number of bytes is
;      divisible by 128.
;      Daphne, 5/2/83
;******************** Version 1.1 ********************************** 
; [12] If "SET IBM", set local echo, XON/XOFF, and default (mark) parity.
;      Daphne, 4/15/83
; [11] In RPACK, don't push packet type, but store in temp variable.
;      If push and get an error, you pick up packet type as return addr.
;      Daphne, 4/15/83 
; [10] Add Set Parity Command.
;      Daphne, 4/12/83
; [9]  Allow single-character wildcard in filenames, use "=".
;      Daphne, 4/4/83
; [8]  Add Kermit server support.
;      Daphne, 3/17/83
; [7]  Allow wildcards in filename.
;      Daphne, 3/15/83
; [6]  Make format of "Set Escape" look like the other commands.
;      Daphne, 3/14/83
; [5]  Add Set "End-of-Line" (to the char used to terminate packets I send).
;      Daphne, 3/7/83
; [4]  Ring bell when done (when succeed and when fail).
;      Daphne, 2/10/83
; [3]  Match keyword if "?" is the terminator.
;      Daphne, 2/9/83
; [2]  Change "Inpkt" so ignores bare CR from any system. 
;      Daphne, 2/9/83
; [1]  Add "cmer04" - print error message if run off list of commands allowed.
;      Daphne, 2/9/83     

;******************** Version 1.0 ********************************** 
; KERMIT - KL10 Error-free Reciprocal Micro Interconnect over TTY-lines
; 
;       Program Version 1.3, Kermit Protocol Version 2
;	June 13, 1983
; 
;       Based on the KERMIT Protocol.
; 
;       Copyright (C) 1982,1983 Trustees of Columbia University
;
;       Daphne Tzoar
;       Columbia University Computer Center
;       612 W. 115th St.
;       New York City, NY  10025
; 
;  Special thanks to Frank da Cruz, Bill Catchings, Steve Jensen,
;  Vace Kundakci, and Bernie Eiben for their help and contributions.
 

;  This file is the global data area for all the Kermit modules.
 
 
IBMPC   EQU     1               ; For IBM PC conditional assembly.
 
BELL    EQU     07Q
TAB     EQU     11Q
LF      EQU     12Q
FF      EQU     14Q
CR      EQU     15Q
XON     EQU     21Q
XOFF    EQU     23Q
ESC     EQU     33Q
DEL     EQU     177Q
BS	EQU	08H

DOS     EQU     21H
 
CONIN   EQU     01H
CONOUT  EQU     02H
RDRIN   EQU     03H
PUNOUT  EQU     04H
LSTOUT  EQU     05H
DCONIO  EQU     06H
GTIOB   EQU     07H
PRSTR   EQU     09H
CONSTAT EQU     0BH
OPENF   EQU     0FH
CLOSF   EQU     10H
SFIRST  EQU     11H
SNEXT   EQU     12H
DELF    EQU     13H
READF   EQU     14H             ; Read from the file.
WRITEF  EQU     15H
MAKEF   EQU     16H
SETDMA	EQU	1AH
CFLSZ   EQU     23H
 
MAXPKT  EQU     '~'-' '+2Q      ; Maximum size of a packet.
MAXTRY  EQU     05Q             ; Default number of retries on a packet.
IMXTRY  EQU     20Q             ; Default number of retries send initiate.
DRPSIZ  EQU     5EH             ; Default receive packet size.
DSPSIZ  EQU     20H             ; Default send packet size.
DSTIME  EQU     08H             ; Default send time out interval.
DRTIME  EQU     05H             ; Default receive time out interval.
DSPAD   EQU     00H             ; Default send padding.
DRPAD   EQU     00H             ; Default receive padding.
DSPADC  EQU     00H             ; Default send padding char.
DRPADC  EQU     00H             ; Default receive padding char.
DSEOL   EQU     CR              ; Default send EOL char.
DREOL   EQU     CR              ; Default receive EOL char.
DSQUOT  EQU     '#'             ; Default send quote char.
DRQUOT  EQU     '#'             ; Default receive quote char.

PAREVN	EQU	00H		; Even parity.			[10 start]
PARMRK	EQU	01H		; Mark parity.
PARNON	EQU	02H		; No parity.	
PARODD	EQU	03H		; Odd parity.
PARSPC	EQU	04H		; Space parity.
DEFPAR	EQU	PARNON		; Default parity (none.) 
IBMPAR  EQU	PARMRK		; IBM's parity (mark.)		[10 end]
 
SOH     EQU     01H             ; Start of header char.
BUFSIZ  EQU     80H             ; Size of DMA.
DIASW   EQU     01H             ; Default is diagnostics on.
 
CMKEY   EQU     01H             ; Parse a keyword.
CMIFI   EQU     02H             ; Parse an input file spec (can be wild).
CMOFI   EQU     03H             ; Parse an output file spec.
CMCFM   EQU     04H             ; Parse a confirm.
CMTXT	EQU	05H		; Parse arbitrary text up to CR.   [8]

DEFESC  EQU     ']'-100Q        ; The default escape character.

;
;  HP150 specific equates	; [17]
e_ioctl	equ	44h		; MSODS io control fct
e_rel_cursor_sense equ 20	; aios #
e_cursor_position equ 17
e_quick_esc equ 16
e_dc_buflen equ 256;
e_7_bit_mode equ 1
e_8_bit_mode equ 2
e_transparent_mode equ 3
e_handshake_mode equ 4
e_keycode_mode equ 43
e_defkey equ 40
e_send_break equ 6
e_break equ 54h
;
; end of HP150 specific equates	[17]
STACK   SEGMENT PARA STACK 'STACK'
        DW      256 DUP(0)      ; Initialize stack to all zeros.
STK	EQU	THIS WORD
STACK   ENDS
 
DATAS   SEGMENT PARA PUBLIC 'DATAS'
 
        ; Pure storage.
 
versio  db      'CUCCA'
	db	' HP150 '   		; [17]
	db	' Kermit-86 - Ver. 1.3A',cr,lf,
	db	'Info-HP150 Release 11-22-83',cr,lf,'$'
kerm    db      'Kermit-86>$'
spmes   db	'Spack:  $'
rpmes 	db	'Rpack:  $'
hibit	db	'Warning - Non Ascii char$'
tmp	db	?
foo	db	'$'
crlf    db      cr,lf,'$'
ender	db	bell,bell,'$' 			;  [4]
tmsg1	db	cr,lf,'[Connecting to host, type $' 
tmsg3	db	' C to return to PC]',cr,lf,cr,lf,'$' ;DK2
tmsg2	db	cr,lf,'[Back at micro]',cr,lf,'$'
ermes1  db      cr,lf,'?Unregonized command$'
ermes2  db      cr,lf,'?Illegal character$'
ermes3  db      cr,lf,'?Not confirmed$'
ermes4  db      'Unable to rename file$'
ermes7  db      '?Unable to receive initiate$'
ermes8  db      '?Unable to receive file name$'
ermes9  db      '?Unable to receive end of file$'
erms10  db      '?Unable to receive data$'
erms11  db      '?Disk full$'
erms14  db      '?Unable to receive an acknowledgement from the host$'
erms15  db      '?Unable to find file$'
erms17  db	'Record length exceeds size of buffer$'
erms18	db	cr,lf,'?Unable to tell host that session is finished$'
erms19	db	cr,lf,'?Unable to tell host to logout$'
infms0  db      'Waiting .....$'
infms1  db      'Receiving ...$'
infms2  db      'Sending .....$'
infms3  db      'Completed    $'
infms4  db      'Failed       $'
infms5  db      'Renaming file to $'
 
cfrmes  db      ' Confirm with carriage return $'
filhlp  db      ' Input file spec (possibly wild) $'
esctl	db	'Control-$'         ; [6]
spchar	db	24H,26H,23H,40H,21H,25H,27H,28H,29H,2DH
	db	3CH,3EH,7BH,7DH,5FH,5CH,5EH,7EH,7CH,60H
eschlp  db      cr,lf,'Enter literal value (ex: Cntrl ])  $'
tophlp  db	cr,lf,'BYE to host (LOGOUT) and exit to DOS'
	db      cr,lf,'CONNECT to host on selected port'
        db      cr,lf,'EXIT to DOS'
	db	cr,lf,'FINISH running Kermit on the host'
        db      cr,lf,'HELP by giving this message'
	db	cr,lf,'LOGOUT the host'
        db      cr,lf,'RECEIVE file from host'
        db      cr,lf,'SEND file to host'
        db      cr,lf,'SET a parameter'
;        db      cr,lf,'SHOW the parameters'
        db      cr,lf,'STATUS of Kermit$'
sethlp  db	cr,lf,' '	; was baud [17]
	db	cr,lf,'BELL'	;DK1
	db	cr,lf,'DEBUG'
	db	cr,lf,'END-OF-LINE character'
	db      cr,lf,'ESCAPE character change'
        db      cr,lf,'FILE-WARNING'
	db	cr,lf,'IBM'
        db      cr,lf,'LOCAL-ECHO echoing (half-duplex)'
	db	cr,lf,' '       		; was parity [17]
stshlp  db      cr,lf,'PAD-CHAR'
        db      cr,lf,'PADDING$'
onhlp   db      cr,lf,'OFF      ON$'
yeshlp  db      cr,lf,'NO       YES$'
eolhlp	db	cr,lf,'Decimal digit between 0 and 31$'   ; [5]
eolerr	db	cr,lf,'Illegal end-of-line character$'  ; [5]
inthlp  db      cr,lf,'?  This message'
        db      cr,lf,'C  Close the connection'
        db      cr,lf,'S  Status of the connection'
        db      cr,lf,'Typing the escape character will send it to the host'
        db      cr,lf,cr,lf,'Command>$'
locst   db      cr,lf,'Local echo on$'
remst   db      cr,lf,'Local echo off$'
vtemst  db      cr,lf,'VT52 emulation on$'
novtst  db      cr,lf,'VT52 emulation off$'
ibmst   db      cr,lf,'IBM on$'
noibm   db      cr,lf,'IBM off$'
debon	db	cr,lf,'Debug mode on$'
deboff	db	cr,lf,'Debug mode off$'	
flwon	db	cr,lf,'File Warning on$'
flwoff	db	cr,lf,'File Warning off$'
eolst	db	cr,lf,'End-of-line character is ^$'     ; [5]
escmes  db      cr,lf,'Escape character is  $'          ; [6]
 
 
outlin  db      cr,lf,'                CUCCA'
	db	' HP150 '		; [17]
	db	' Kermit-86 V1.3A',
	db	'  Field Test Version',cr,lf
        db      cr,lf,'Number of packets        (hex)'
        db      cr,lf,'Number of retries        (hex)'
        db      cr,lf,'File name $'
delstr  db      10O,' ',10O,10O,'$'     ; Delete string.
clrspc  db      ' ',10O,'$'             ; Clear space.
escspc	db	10O,' ',10O,'$'		; Clear escape.
clrlin  db      cr,'$'			; Clear line (just the cr part).
prsp	db	' $'			; Print a space.

; Cursor addressing items.

scrhi	dw	0434H			; Err when 8th bit is on.
scrrpr	dw	0700H			; Prompt when Kermit ends.
screrr  dw      0600H		        ; Place for error msgs. 
scrfln  dw      050CH			; Place for file name.
scrnrt  dw      0415H		        ; Place for number of retries.
scrnp   dw      0315H 		        ; Place for number of packets.
scrfr	dw	0600H			; Rename file.
scrst	dw	0334H			; Place for status.
scrsp	dw	0800H			; Place for send packet.
scrrp	dw	0A00H			; Place for receive packet.


        ; COMND tables

comtab  db      0AH             	; Ten entries.
	db	03H,'BYE$'
	dw	bye
        db      07H,'CONNECT$'
        dw      telnet
        db      04H,'EXIT$'
        dw      exit
	db	06H,'FINISH$'
	dw	finish
        db      04H,'HELP$'
        dw      help
	db	06H,'LOGOUT$'
	dw	logout
        db      07H,'RECEIVE$'
        dw      read
        db      04H,'SEND$'
        dw      send
        db      03H,'SET$'
        dw      setcom
        db      06H,'STATUS$'
        dw      status

settab  db      07H             ;DK1 seven entries.
	db	04H,'BELL$'	;DK1
	dw	bellst		;DK1
	db	05H,'DEBUG$'
	dw	debst
	db	0BH,'END-OF-LINE$'	         ;  [5]
	dw	eolset				 ;  [5]
        db      06H,'ESCAPE$'
        dw      escape
	db	0CH,'FILE-WARNING$'
	dw	filwar
        db      03H,'IBM$'
        dw      ibmset
        db      0AH,'LOCAL-ECHO$'
        dw      lcal
 
ontab   db      02H             ; Two entries.
        db      02H,'ON$'
        dw      01H
        db      03H,'OFF$'
        dw      00H
 
yestab  db      02H             ; Two entries.
        db      02H,'NO$'
        dw      00H
        db      03H,'YES$'
        dw      01H

 
; COMND storage
 
cmer00  db      cr,lf,'?Program error   Invalid COMND call$'
cmer01  db      cr,lf,'?Ambiguous$'
cmer02  db      cr,lf,'?Illegal input file spec$'
cmer03	db	cr,lf,'?Unrecognized instruction$'
cmer04	db	cr,lf,'?Invalid command or operand$'     ;  [1]
cmin00  db      ' Confirm with carriage return$'
cmcrlf  db      cr,lf,'$'
 
cmstat  db      ?               ; What is presently being parsed.
cmaflg  db      ?               ; Non-zero when an action char has been found.
cmccnt  db      ?               ; Non-zero if a significant char is found.
cmsflg  db      ?               ; Non-zero when the last char was a space.
cmostp  dw      ?               ; Old stack pointer for reparse.
cmrprs  dw      ?               ; Address to go to on reparse.
cmprmp  dw      ?               ; Address of prompt.
cmptab  dw      ?               ; Address of present keyword table.
cmhlp   dw      ?               ; Address of present help.
cmdbuf  db      80H DUP(?)      ; Buffer for command parsing.
cmfcb   dw      ?               ; Pointer to FCB.
cmfcb2  dw      ?               ; Pointer to position in FCB.
cmcptr  dw      ?               ; Pointer for next char input.
cmdptr  dw      ?               ; Pointer into the command buffer.
cmsiz	dw	?		; Size info of user input.
cmkptr  dw      ?               ; Pointer to keyword.
cmsptr  dw      ?               ; Place to save a pointer.
cmchr	db	?		; Save char when checking ambiguity.
 
; Program storage.
 
oldstk  dw      ?               ; Storage for system stack.
oldsts  dw      ?               ; System stack segment.
ssp	dw	0		; Save SP in Telnet.
bufhex	dw	80H
filsiz	dw	0		; Double word for filesize (in bytes.)
filsiz2 dw      0		; [17] split 32 bits to please MASM
eoflag  db      ?               ; EOF flag; non-zero on EOF.
wldflg	db	0		; Assume no "*" in fn.         [7]
bellfl	db	1		;DK1 Use bell
incmod	db	0		;DK2 Insert Character mode
debug	db	0		; Debugging mode (default off).
hierr	db	0		; Non-ascii char (non-zero if yes).
filflg  db      ?               ; Non-zero when nothing in DMA buffer.
ecoflg  db      0               ; Local echo flag (default off).
parflg  db	defpar		; Parity flag (default none.)  [10]
escflg  db      0               ; Escape flag (start off).

vtflg   db      1               ; VT52 emulation flag (default on).
curatt	dw	0007H		; DK2 Video attributes
baud	dw	0     

flwflg  db      0               ; File warning flag (default off).
ibmflg  db      0               ; IBM flag (default off).
extflg  db      0               ; Exit flag (default off).
escchr  db      defesc          ; Storage for the escape character.
incnt	dw	?		; Number of chars read in from port.
chrcnt  dw      ?               ; Number of chars in the file buffer.
filcnt  dw      ?               ; Number of chars left to fill.
outpnt  dw      ?               ; Position in packet.
bufpnt  dw      ?               ; Position in file buffer.
fcbptr  dw      ?               ; Position in FCB.
datptr  dw      ?               ; Position in packet data buffer.
cbfptr  dw      ?               ; Position in character buffer.
pktptr  dw      ?               ; Poistion in receive packet.
siz     dw      ?               ; Size of data from gtchr.
spsiz   db      dspsiz          ; Send packet size.
rpsiz   db      drpsiz          ; Receive packet size.
stime   db      dstime          ; Send time out.
rtime   db      drtime          ; Receive time out.
spad    db      dspad           ; Send padding.
rpad    db      drpad           ; Receive padding.
spadch  db      dspadc          ; Send padding char.
rpadch  db      drpadc          ; Receive padding char.
seol    db      dseol           ; Send EOL char.
reol    db      dreol           ; Receive EOL char.
squote  db      dsquot          ; Send quote char.
rquote  db      drquot          ; Receive quote char.
pktnum  dw      ?               ; Packet number.
numpkt  dw      ?               ; Total number of packets sent.
numrtr  dw      ?               ; Total number of retries.
numtry  db      ?               ; Number of tries on this packet.
oldtry  db      ?               ; Number of tries on previous packet.
state   db      ?               ; Present state of the automaton.
savsci	dw	?		; Save for serial port interrupt vector. [14]
savscs	dw	?		; Ditto.  [14]  
packet  db      ?,?,?,?         ; Packet (data is part of it).
data    db      5AH DUP(?)      ; Data and checksum field of packet.
recpkt  db      60H DUP(?)      ; Receive packet storage (use the following).
filbuf  db      60H DUP(?)      ; Character buffer.
fcb	db	25H DUP(?)	; Use as our FCB.
cpfcb	db	25H DUP(?)	; Save FCB in case of "*".   [7]
buff	db	80H DUP(?)	; Use as our DAT.
rdbuf	db	80H DUP(?)
cnt	dw	0
temp	dw	0
temp1   dw      ?               ; Temporary storage.
temp2   dw      ?
temp3   dw      ?
temp4   dw      ?
argblk  dw      ?               ; For subroutine arguments.
argbk1  dw      ?
argbk2  dw      ?
argbk3  dw      ?

;
; HP150 specific variables	[17]
;
dc_name db 'COM1',0
dcchar_ptr dw 0;
dcchar_cnt dw 0
dc_handle dw 0
dcbuf db e_dc_buflen dup(?)
aios_buf db 20 dup(?)
tempbuf dw 10 dup(?)
binwrn db 0
;
; end of HP150 specific variables	[17]
 
DATAS   ENDS                    ; End data segment
 
;

 
MAIN    SEGMENT PARA PUBLIC 'MAIN'
START   PROC  FAR
        ASSUME  CS:MAIN,DS:DATAS,SS:STACK,ES:NOTHING
 
        push ds                 ; Save system data area.
        sub ax,ax               ; Get a zero.
        push ax                 ; Put zero return addr on stack.
 
        mov ax,datas            ; Initialize DS.
        mov ds,ax
	sub ax,ax

        mov oldstk,sp           ; Save old stack pointer.

        call cmblnk             ; Clear the screen.
	call locate

        mov dx,offset versio	; Print the version header.
        call strout

	mov ah,setdma		; Set disk transfer address.
	mov dx,offset buff
	int dos

        call open_dc		; open COM1 [17]

; This is the main KERMIT loop.  It prompts for and gets the users commands.

kermit: mov dx,offset kerm
        call prompt             ; Prompt the user.
        mov dx,offset comtab
        mov bx,offset tophlp
        mov ah,cmkey
        call comnd
         jmp kermt2
        call bx                 ; Call the routine returned.
         jmp kermt3
        cmp extflg,0            ;  Check if the exit flag is set.
        jne krmend              ;  If so jump to KRMEND.
        jmp kermit              ; Do it again.
 
kermt2: 
        mov dx,offset ermes1    ;  Give an error.
        call strout
        jmp kermit

kermt3:
        mov dx,offset ermes3    ;  Give an error.
        call strout
        jmp kermit
 
krmend: 
	call close_dc		; [17] close data comm and 
				; disable keycode mode
        mov sp,oldstk
        ret
 
START   ENDP
 
 
; These are some utility routines.
 
;       Abort
 
ABORT   PROC    NEAR
        mov state,'A'           ; Otherwise abort.
        ret
ABORT   ENDP
 
;       NAK
 
NAK     PROC    NEAR
        mov ax,pktnum           ; Get the packet number we're waiting for.
        mov argblk,ax
        mov argbk1,0
        mov ah,'N'              ; NAK that packet.
        call spack
	 jmp abort
        ret                     ; Go around again.
NAK     ENDP

; Position cursor for an error message.

ERPOS	PROC 	NEAR
   	mov dx,screrr
	call set_curpos;	[17]
	ret
ERPOS	ENDP

; Position cursor for error and print message
;
; entry dx -> message
;
ERPRT  proc near
  	push dx
	call erpos
	pop dx
	call strout
	ret
ERPRT   endp
;
; Position cursor for number of retries message.

RTPOS	PROC 	NEAR
	mov dx,scrnrt
	call set_curpos		; [17]
	ret
RTPOS	ENDP

; Print err message that found a non-standard-Ascii char in the file.

BITERR	PROC	NEAR
        cmp binwrn,1
	je biterr90
	push bx
	mov dx,scrhi
	call set_curpos			; [17]
	mov dx,offset hibit
	call strout
	mov binwrn,1
	pop bx
biterr90: ret
BITERR	ENDP		

;	This routine prints out the escape character in readable format.  

ESCPRT	PROC	NEAR		; [6 start]
	mov al,escchr
	cmp al,' '
	jge escpr2
	push ax
	mov dx,offset esctl
	call strout
	pop ax
	add al,040H		; Make it printable.
escpr2:	call hpconout
	ret
ESCPRT	ENDP			; [6 end]
 
;       FCB must be remembered if found "*" in filename.      [7 start]
; 	Copy from place addressed by BX to place addressed by DI.
;	Also use to get the filename to the FCB from the DTA.

FCBCPY	PROC	NEAR
fcbcp1:	cmp cl,0
	je fcbcp2
	mov ah,[bx]
	mov [di],ah
	dec cl
	inc bx
	inc di
	jmp fcbcp1
fcbcp2:	ret
FCBCPY	ENDP	

;       This routine sets up the data for init packet (either the
;       Send_init or ACK packet).
 
RPAR    PROC    NEAR
        mov ah,rpsiz            ; Get the receive packet size.
        add ah,' '              ; Add a space to make it printable.
        mov [bx],ah             ; Put it in the packet.
        mov ah,rtime            ; Get the receive packet time out.
        add ah,' '              ; Add a space.
        mov 1[bx],ah            ; Put it in the packet.
        mov ah,rpad             ; Get the number of padding chars.
        add ah,' '
        mov 2[bx],ah            ; Put it in the packet.
        mov ah,rpadch           ; Get the padding char.
        add ah,100O             ; Uncontrol it.
        and ah,7FH
        mov 3[bx],ah            ; Put it in the packet.
        mov ah,reol             ; Get the EOL char.
        add ah,' '
        mov 4[bx],ah            ; Put it in the packet.
        mov ah,rquote           ; Get the quote char.
        mov 5[bx],ah            ; Put it in the packet.
        mov ah,06H              ; Six pieces of data.
        ret
RPAR    ENDP
 
;       This routine reads in all the send_init packet information.
 
SPAR    PROC    NEAR
        mov temp4,ax            ; Save the number of arguments.
        mov ah,[bx]             ; Get the max packet size.
        sub ah,' '              ; Subtract a space.
        mov spsiz,ah            ; Save it.
        mov ax,temp4
        cmp al,3                ; Fewer than three pieces?
        jge spar1
        ret                     ; If so we are done.
spar1:  mov ah,2[bx]            ; Get the number of padding chars.
        sub ah,' '
        mov spad,ah
        mov ax,temp4
        cmp al,4                ; Fewer than four pieces?
        jge spar2
        ret                     ; If so we are done.
spar2:  mov ah,3[bx]            ; Get the padding char.
        add ah,100O             ; Re-controlify it.
        and ah,7FH
        mov spadch,ah
        mov ax,temp4
        cmp al,5                ; Fewer than five pieces?
        jge spar3
        ret                     ; If so we are done.
spar3:  mov ah,4[bx]            ; Get the EOL char.
        sub ah,' '
        mov seol,ah
        mov ax,temp4
        cmp al,6                ; Fewer than six pieces?
        jge spar4
        ret                     ; If so we are done.
spar4:  mov ah,5[bx]            ; Get the quote char.
        mov squote,ah
        ret
SPAR    ENDP
 
 
;       Initialize buffers and clear line.
 
INIT    PROC    NEAR
	call cmblnk
	call locate
        mov dx,offset outlin	; Put statistics headers on the screen.
        call strout
	mov dx,scrst
        call set_curpos
	mov dx,offset infms0
	call strout
	mov binwrn,0
        call init1
	call dc_transparent
        ret
INIT    ENDP
 
INIT1   PROC    NEAR
        mov chrcnt,bufsiz              ; Number of chars left.
        mov bufpnt,offset buff         ; Addr for beginning.
        ret
INIT1   ENDP

;  Clear out the old filename on the screen. 

CLRFLN	PROC	NEAR
	mov dx,scrfln
	call set_curpos			; [17]
	mov cx,12
clrfln10:
        push cx
        mov al,' '
        call hpconout
        pop cx
	dec cx
        jnz clrfln10
	ret
CLRFLN	ENDP

;       RECEIVE command
 
READ    PROC    NEAR
	mov bx,offset data	; Where to put text (if any).  [8 start]
	mov ah,cmtxt
        call comnd              ; Get text or confirm.
         jmp kermt3
	cmp ah,0		; Read in any chars?
	je read1		; A regular receive.
	mov al,ah
	mov ah,0
	mov argbk1,ax		; Remember number of chars we read.
	mov ah,'$'		; Use for printing.
	mov [bx],ah
	call init		; Clear line and initialize buffers.
	call clrfln		; Prepare to print filename.
	mov dx,offset data	; Print file name.
	call strout
	mov argblk,0		; Start at packet zero.
	mov ah,'R'		; Receive init packet.
	call spack		; Send the packet.
	 jmp kermt3
	jmp read12						; [8 end]
read1:  call init               ; Clear the line and initialize the buffers.
read12: mov numpkt,0            ; Set the number of packets to zero.
        mov numrtr,0            ; Set the number of retries to zero.
        mov pktnum,0            ; Set the packet number to zero.
        mov numtry,0            ; Set the number of tries to zero.
        call rtpos		; Position cursor.
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov state,'R'           ; Set the state to receive initiate.
read2:  
	mov dx,scrnp
	call set_curpos		; [17]
        mov ax,numpkt
        call nout               ; Write the number of packets.
        mov ah,state            ; Get the state.
        cmp ah,'D'              ; Are we in the data send state?
        jne read3
        call rdata
        jmp read2
read3:  cmp ah,'F'              ; Are we in the file receive state?
        jne read4
        call rfile              ; Call receive file.
        jmp read2
read4:  cmp ah,'R'              ; Are we in the receive initiate state?
        jne read5
        call rinit
        jmp read2
read5:  cmp ah,'C'              ; Are we in the receive complete state?
        jne read6
	mov dx,scrst
	call set_curpos		; [17]
        mov dx,offset infms3    ; Plus a little cuteness.
        call strout
	cmp bellfl,0		;DK1 Bell desired?
	je readnb		;DK1 No
	call beep
readnb:				;DK1
	mov dx,scrrpr		; Put prompt here.
	call set_curpos
        jmp rskp
read6: 
	mov dx,scrst
	call set_curpos
	mov dx,offset infms4    ; Plus a little cuteness.
        call strout
	call beep	;  [4]
	mov dx,scrrpr
	call set_curpos
        jmp rskp
READ    ENDP
 
 
;       Receive routines
 
;       Receive init
 
RINIT   PROC    NEAR
        mov ah,numtry           ; Get the number of tries.
        cmp ah,imxtry           ; Have we reached the maximum number of tries?
        jl rinit2
        mov dx,offset ermes7
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
rinit2: inc ah                  ; Increment it.
        mov numtry,ah           ; Save the updated number of tries.
        call rpack              ; Get a packet.
         jmp nak                ;  Trashed packet: nak, retry.
        cmp ah,'S'              ; Is it a send initiate packet?
        jne rinit3              ; If not see if its an error.
        mov ah,numtry           ; Get the number of tries.
        mov oldtry,ah           ; Save it.
        mov numtry,0            ; Reset the number of tries.
        mov ax,argblk           ; Returned packet number.  (Synchronize them.)
        inc ax                  ; Increment it.
        and ax,3FH              ; Turn off the two high order bits.
        mov pktnum,ax           ; Save modulo 64 of the number.
        mov bx,numpkt
        inc bx                  ; Increment the number of packets.
        mov numpkt,bx
        mov ax,argbk1           ; Get the number of arguments received.
        mov bx,offset data      ; Get a pointer to the data.
        call spar               ; Get the data into the proper variables.
        mov bx,offset data      ; Get a pointer to our data block.
        call rpar               ; Set up the receive parameters.
	xchg ah,al
	mov ah,0
        mov argbk1,ax           ; Store the returned number of arguments.
        mov ah,'Y'              ; Acknowledge packet.
        call spack              ; Send the packet.
	 jmp abort
	mov dx,scrst
	call set_curpos		; [17]
        mov dx,offset infms1
        call strout
        mov state,'F'		; Set the state to file send
        ret
rinit3: cmp ah,'E'              ; Is it an error packet?
        jne rinit4
        call error
rinit4: jmp abort
RINIT   ENDP
 

;       Receive file
 
RFILE   PROC    NEAR
        cmp numtry,maxtry       ; Have we reached the maximum number of tries?
        jl rfile1
        mov dx,offset ermes8
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
rfile1: inc numtry              ; Save the updated number of tries.
        call rpack              ; Get a packet.
         jmp nak                ;  Trashed packet: nak, retry.
        cmp ah,'S'              ; Is it a send initiate packet?
        jne rfile2              ;  No, try next type.
        cmp oldtry,imxtry       ; Have we reached the maximum number of tries?
        jl rfil12               ; If not proceed.
        mov dx,offset ermes7
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
rfil12: inc oldtry              ; Save the updated number of tries.
        mov ax,pktnum           ; Get the present packet number.
        dec ax                  ; Decrement.
        cmp ax,argblk           ; Is the packet's number one less than now?
        je rfil13
        jmp nak                 ; No, NAK and try again.
rfil13: call rtpos              ; Position cursor.
        inc numrtr              ; Increment the number of retries.
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov numtry,0            ; Reset the number of tries.
        mov bx,offset data      ; Get a pointer to our data block.
        call rpar               ; Set up the parameter information.
	xchg ah,al
	mov ah,0
        mov argbk1,ax           ; Save the number of arguments.
        mov ah,'Y'              ; Acknowledge packet.
        call spack              ; Send the packet.
	 jmp abort
        ret
rfile2: cmp ah,'Z'              ; Is it an EOF packet?
        jne rfile3              ;  No, try next type.
        cmp oldtry,maxtry       ; Have we reached the maximum number of tries?
        jl rfil21               ; If not proceed.
        mov dx,offset ermes9
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
rfil21: inc oldtry              ; Increment it.
        mov ax,pktnum           ; Get the present packet number.
        dec ax                  ; Decrement.
        cmp ax,argblk           ; Is the packet's number one less than now?
        je rfil24
        jmp nak                 ; No, NAK and try again.
rfil24: call rtpos              ; Position cursor.
        inc numrtr              ; Increment the number of retries
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov numtry,0
        mov argbk1,0            ; No data.  (The packet number is in argblk.)
        mov ah,'Y'              ; Acknowledge packet.
        call spack              ; Send the packet.
	 jmp abort
        ret
rfile3: cmp ah,'F'              ; Start of file?
        jne rfile4
        mov ax,argblk           ; Get the packet number.
        cmp ax,pktnum           ; Is it the right packet number?
        je rfil32
        jmp nak                 ; No, NAK it and try again.
rfil32: inc ax                  ; Increment the packet number.
        and ax,3FH              ; Turn off the two high order bits.
        mov pktnum,ax           ; Save modulo 64 of the number.
        inc numpkt              ; Increment the number of packets.
        call gofil              ; Get a file to write to.
         jmp abort
        call init1              ; Initialize all the buffers.
        mov ah,numtry           ; Get the number of tries.
        mov oldtry,ah           ; Save it.
        mov numtry,0            ; Reset the number of tries.
        mov argbk1,0            ; No data.  (The packet number is in argblk.)
        mov ah,'Y'              ; Acknowledge packet.
        call spack              ; Send the packet.
	 jmp abort
        mov state,'D'           ; Set the state to data receive.
        ret
rfile4: cmp ah,'B'              ; End of transmission.
        jne rfile5
        mov ax,pktnum
        cmp ax,argblk           ; Do we match?
        je rfil41
        jmp nak                 ; No, NAK it and try again.
rfil41: mov argbk1,0            ; No data.  (Packet number already in argblk).
        mov ah,'Y'              ; Acknowledge packet.
        call spack              ; Send the packet.
	 jmp abort
        mov state,'C'           ; Set the state to complete.
        ret
rfile5: cmp ah,'E'              ; Is it an error packet.
        jne rfile6
        call error
rfile6: jmp abort
RFILE   ENDP
 
 
;       Receive data
 
RDATA   PROC    NEAR
        cmp numtry,maxtry       ; Get the number of tries.
        jl rdata1
        mov dx,offset erms10
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
rdata1: inc numtry              ; Save the updated number of tries.
        call rpack              ; Get a packet.
         jmp nak                ;  Trashed packet: nak, retry.
        cmp ah,'D'              ; Is it a data packet?
	je rdat11
        jmp rdata2              ;  No, try next type.
rdat11: mov ax,pktnum           ; Get the present packet number.
        cmp ax,argblk           ; Is the packet's number correct?
        jz rdat14
        cmp oldtry,maxtry       ; Have we reached the maximum number of tries?
        jl rdat12               ; If not proceed.
        mov dx,offset erms10
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
rdat12: inc oldtry              ; Save the updated number of tries.
;        dec pktnum              ; Decrement present packet number.  [14]
        mov ax,pktnum
	dec ax			; [14]
        cmp ax,argblk           ; Is the packet's number one less than now?
        je rdat13
        jmp nak                 ; No, NAK it and try again.
rdat13: call rtpos              ; Position cursor.
        inc numrtr              ; Increment the number of retries
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov numtry,0            ; Reset number of tries.
        mov argbk1,0            ; No data.  (The packet number is in argblk.)
        mov ah,'Y'              ; Acknowledge packet.
        call spack              ; Send the packet.
	 jmp abort
        ret
rdat14: inc ax                  ; Increment the packet number.
        and ax,3FH              ; Turn off the two high order bits.
        mov pktnum,ax           ; Save modulo 64 of the number.
        inc numpkt              ; Increment the number of packets.
        mov ah,numtry           ; Get the number of tries.
        mov oldtry,ah           ; Save it.
        mov ax,argbk1           ; Get the length of the data.
        call ptchr
         jmp abort              ;  Unable to write out chars; abort.
        mov numtry,0            ; Reset the number of tries.
        mov argbk1,0            ; No data.  (Packet number still in argblk.)
        mov ah,'Y'              ; Acknowledge packet.
        call spack              ; Send the packet.
	 jmp abort
        ret
rdata2: cmp ah,'F'              ; Start of file?
        jne rdata3              ;  No, try next type.
        cmp oldtry,maxtry       ; Have we reached the maximum number of tries?
        jl rdat21               ; If not proceed.
        mov dx,offset ermes8
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
rdat21: inc oldtry              ; Save the updated number of tries.
        mov ax,pktnum
	dec ax			; [14 Omitted accidentally - D.T.]
        cmp ax,argblk           ; Is the packet's number one less than now?
        je rdat22
        jmp nak                 ; No, NAK it and try again.
rdat22: call rtpos              ; Position cursor.
        inc numrtr              ; Increment the number of retries
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov numtry,0            ; Reset number of tries.
        mov argbk1,0            ; No data.  (The packet number is in argblk.)
        mov ah,'Y'              ; Acknowledge packet.
        call spack              ; Send the packet.
	 jmp abort
        ret
rdata3: cmp ah,'Z'              ; Is it a EOF packet?
	je rdat3x				; [13]
        jmp rdata4               ; Try and see if its an error. [13]
rdat3x: mov ax,pktnum           ; Get the present packet number. [13]
        cmp ax,argblk           ; Is the packet's number correct?
        je rdat32
        jmp nak                 ; No, NAK it and try again.
rdat32: inc ax                  ; Increment the packet number.
        and ax,3FH              ; Turn off the two high order bits.
        mov pktnum,ax           ; Save modulo 64 of the number.
        inc numpkt
rdat33: mov bx,bufpnt           ; Get the dma pointer.
	mov ax,80H
        sub ax,chrcnt           ; Get the number of chars left in the DMA.
	cmp ax,80H		;   [13 start]
	jne rdat34
	call outbuf		; Write out buffer if no room for ^Z.
	 jmp abort
	mov ax,0		;   [13 end]
rdat34: mov cl,'Z'-100O         ; Put in a ^Z for EOF.
        mov [bx],cl
	inc ax
	dec chrcnt
	mov cx,chrcnt
	mov temp,cx
	inc bx
rdt3:	inc ax
	cmp ax,80H
	jg rdat35		; Pad till full.
	mov cl,0		; Use nulls.
	mov [bx],cl
	inc bx
        jmp rdt3
rdat35: call outbuf             ; Output the last buffer.
         jmp abort              ;  Give up if the disk is full.
	call fixfcb
        mov ah,closf            ; Close up the file.
        mov dx,offset fcb
        int dos
        mov ah,numtry           ; Get the number of tries.
        mov oldtry,ah           ; Save it.
        mov numtry,0            ; Reset the number of tries.
        mov argbk1,0            ; No data.  (The packet number is in argblk.)
        mov ah,'Y'              ; Acknowledge packet.
        call spack              ; Send the packet.
	 jmp abort
        mov state,'F'
        ret
rdata4: cmp ah,'E'                      ; Is it an error packet.
        jne rdata5
        call error
rdata5: jmp abort
RDATA   ENDP

FIXFCB  PROC	NEAR	
	mov bx,offset fcb+18
	mov di,offset filsiz
	mov ax,[bx]
	mov [di],ax
	mov bx,offset fcb+16
	mov ax,[bx]
	mov 2[di],ax
	mov ax,temp		; Get number of chars in last buffer full.
	sub filsiz2,ax		; Get real file size.
	sbb filsiz,0
	mov bx,offset fcb+18
	mov di,offset filsiz
	mov ax,[di]
	mov [bx],ax
	mov bx,offset fcb+16
	mov ax,2[di]
	mov [bx],ax
	ret
FIXFCB	ENDP
 

;       Send command
 
SEND    PROC    NEAR
        mov ah,cmifi            ; Parse an input file spec.
        mov dx,offset fcb       ; Give the address for the FCB.
        call comnd
         jmp r                  ;  Give up on bad parse.
send1:  mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r                  ;  Didn't get a confirm.
send11: mov ah,sfirst           ; Get the first file.
        mov dx,offset fcb
        int dos
        cmp al,0FFH             ; Any found?
        jne send12
	call newln
        mov dx,offset erms15
        call strout
        ret
send12: cmp wldflg,0		; Any wildcards.      [7 start]
	je send16		; Nope, so no problem.
	mov bx,offset fcb	; Remember what FCB looked like.
	mov di,offset cpfcb
	mov cl,37		; Size of FCB.
	call fcbcpy
	mov di,offset fcb+1	; Copy filename	from DTA to FCB.
	mov bx,offset buff+1
	mov cl,11
	call fcbcpy					; [7 end]
send16: call init               ; Clear the line and initialize the buffers.
        mov pktnum,0            ; Set the packet number to zero.
        mov numtry,0            ; Set the number of tries to zero.
        mov numpkt,0            ; Set the number of packets to zero.
        mov numrtr,0            ; Set the number of retries to zero.
        call rtpos              ; Position cursor.
        mov ax,0
        call nout               ; Write the number of retries.
        mov state,'S'           ; Set the state to receive initiate.
send2:  
	mov dx,scrnp
	call set_curpos		; [17]
        mov ax,numpkt
        call nout               ; Write the packet number.
        cmp state,'D'           ; Are we in the data send state?
        jne send3
        call sdata
        jmp send2
send3:  cmp state,'F'           ; Are we in the file send state?
        jne send4
        call sfile              ; Call send file.
        jmp send2
send4:  cmp state,'Z'           ; Are we in the EOF state?
        jne send5
        call seof
        jmp send2
send5:  cmp state,'S'           ; Are we in the send initiate state?
        jne send6
        call sinit
        jmp send2
send6:  cmp state,'B'           ; Are we in the eot state?
        jne send7
        call seot
        jmp send2
send7:  cmp state,'C'           ; Are we in the send complete state?
        jne send8
	mov dx,scrst
	call set_curpos		; [17]
        mov dx,offset infms3    ; Plus a little cuteness.
        call strout
	cmp bellfl,0		;DK1 Bell desired?
	je sendnb		;DK1
	call beep          	; Ring them bells.   [4]
sendnb:				;DK1
	mov dx,scrrpr
	call set_curpos		; [17]
        jmp rskp
send8:
	mov dx,scrst
	call set_curpos		; [17]
        mov dx,offset infms4    ; Plus a little cuteness.
        call strout
	call beep  		; Ring them bells. [4]
	mov dx,scrrpr
	call set_curpos		; [17]
        jmp rskp
SEND    ENDP
 
 
;       Send routines
 
;       Send initiate
 

SINIT   PROC    NEAR
        cmp numtry,imxtry       ; Have we reached the maximum number of tries?
        jl sinit2
        mov dx,offset erms14
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
sinit2: inc numtry              ; Save the updated number of tries.
        mov bx,offset data      ; Get a pointer to our data block.
        call rpar               ; Set up the parameter information.
	xchg ah,al
	mov ah,0
        mov argbk1,ax           ; Save the number of arguments.
        mov ax,numpkt           ; Get the packet number.
        mov argblk,ax
        mov ah,'S'              ; Send initiate packet.
        call spack              ; Send the packet.
	 jmp abort
        call rpack              ; Get a packet.
         jmp r                  ;  Trashed packet don't change state, retry.
        cmp ah,'Y'              ; ACK?
        jne sinit3              ; If not try next.
        mov ax,pktnum           ; Get the packet number.
        cmp ax,argblk           ; Is it the right packet number?
        je sini22
        ret                     ; If not try again.
sini22: inc ax                  ; Increment the packet number.
        and ax,3FH              ; Turn off the two high order bits.
        mov pktnum,ax           ; Save modulo 64 of the number.
        inc numpkt              ; Increment the number of packets.
        mov ax,argbk1           ; Get the number of pieces of data.
        mov bx,offset data      ; Pointer to the data.
        call spar               ; Read in the data.
        mov ah,numtry           ; Get the number of tries.
        mov oldtry,ah           ; Save it.
        call getfil             ; Open the file.
         jmp abort              ;  Something is wrong, die.
        jmp sini32		; success exit
sinit3: cmp ah,'N'              ; NAK?
        jne sinit4              ; If not see if its an error.
        call rtpos              ; Position cursor.
        inc numrtr              ; Increment the number of retries
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov ax,pktnum           ; Get the present packet number.
        inc ax                  ; Increment.
        cmp ax,argblk           ; Get the packet's number.
        je sini32
        ret                     ; If not assume its for this packet, go again.
sini32: mov numtry,0            ; Reset number of tries.
        mov state,'F'           ; Set the state to file send.
	mov dx,scrst
	call set_curpos		; [17]
        mov dx,offset infms2
        call strout
        ret
sinit4: cmp ah,'E'              ; Is it an error packet.
        jne sinit5
        call error
sinit5: jmp abort
SINIT   ENDP
 


;       Send file header
 
SFILE   PROC    NEAR
        cmp numtry,maxtry       ; Have we reached the maximum number of tries?
        jl sfile1
        mov dx,offset erms14
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
sfile1: inc numtry              ; Increment it.
        mov datptr,offset data  ; Get a pointer to our data block.
	mov bx,offset fcb+1		; Pointer to file name in FCB.
	mov fcbptr,bx		; Save position in FCB.
	mov cl,0		; Counter for chars in file name.
	mov ch,0		; Counter for number of chars in FCB.
sfil11:	cmp ch,8H		; Ninth char?
	jne sfil12
	mov ah,'.'
	mov bx,datptr
	mov [bx],ah		; Put dot in data packet.	
	inc bx
	mov datptr,bx		; Save new position in data packet.
	inc cl
sfil12:	inc ch
	cmp ch,0CH		; Twelve?
	jns sfil13
	mov bx,fcbptr
	mov ah,[bx]		; Get char of filename.
	inc bx
	mov fcbptr,bx		; Save position in FCB.
	cmp ah,'!'		; Is it a good char?
	jl sfil11		; If not, get the next.
	mov bx,datptr
	mov [bx],ah		; Put char in data buffer.
	inc cl			; Increment counter.
	inc bx
	mov datptr,bx		; Save new position. 
	jmp sfil11		; Get another char.
sfil13: mov ch,0
	mov argbk1,cx		; Save number of char in filename.
	mov bx,datptr
	mov ah,'$'
	mov [bx],ah		; Put dollar sign for printing.
	call clrfln
	mov dx,offset data	; Print file name.
	call strout

        mov ax,pktnum           ; Get the packet number.
        mov argblk,ax
        mov ah,'F'              ; File header packet.
        call spack              ; Send the packet.
	 jmp abort
        call rpack              ; Get a packet.
         jmp r                  ;  Trashed packet don't change state, retry.
        cmp ah,'Y'              ; ACK?
        jne sfile2              ; If not try next.
        mov ax,pktnum           ; Get the packet number.
        cmp ax,argblk
        je sfil14
        ret                     ; If not hold out for the right one.
sfil14: inc ax                  ; Increment the packet number.
        and ax,3FH              ; Turn off the two high order bits.
        mov pktnum,ax           ; Save modulo 64 of the number.
        inc numpkt              ; Increment the number of packets.
        mov ah,numtry           ; Get the number of tries.
        mov oldtry,ah           ; Save it.
        mov numtry,0            ; Reset the number of tries.

sfil15: mov ah,0                ; Get a zero.
        mov bx,offset fcb
        add bx,20H
        mov [bx],ah             ; Set the record number to zero.
        mov eoflag,ah           ; Indicate not EOF.
        mov ah,0FFH
        mov filflg,ah           ; Indicate file buffer empty.
        call gtchr
         jmp sfil16             ; Error go see if its EOF.
        jmp sfil17              ; Got the chars, proceed.
sfil16: cmp ah,0FFH             ; Is it EOF?
        je sfl161
        jmp abort               ; If not give up.
sfl161: mov state,'Z'           ; Set the state to EOF.
        ret
sfil17: mov siz,ax

        mov state,'D'           ; Set the state to data send.
        ret
sfile2: cmp ah,'N'              ; NAK?
        jne sfile3              ; Try if error packet.
        call rtpos              ; Position cursor.
        inc numrtr              ; Increment the number of retries
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov ax,pktnum           ; Get the present packet number.
        inc ax                  ; Increment.
        cmp ax,argblk           ; Is the packet's number one more than now?
        jz sfil14               ; Just as good as a ACK; go to the ACK code.
        ret                     ; If not go try again.
sfile3: cmp ah,'E'              ; Is it an error packet.
        jne sfile4
        call error
sfile4: jmp abort
SFILE   ENDP
 
 
;       Send data
 
SDATA   PROC    NEAR
        cmp numtry,maxtry       ; Have we reached the maximum number of tries?
        jl sdata1
        mov dx,offset erms14
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
sdata1: inc numtry              ; Increment it.
        mov datptr,offset data  ; Get a pointer to our data block.
        mov cbfptr,offset filbuf ; Pointer to chars to be sent.
        mov cx,1                ; First char.
sdat11: mov bx,cbfptr
        mov ah,[bx]
        inc cbfptr
        mov bx,datptr
        mov [bx],ah             ; Put the char in the data packet.
        inc datptr              ; Save position in data packet.
        inc cx                  ; Increment the count.
        cmp cx,siz              ; Have we transfered that many?
        jle sdat11              ; If not get another.
        mov ax,siz              ; Number of char in char buffer.
        mov argbk1,ax
        mov ax,pktnum           ; Get the packet number.
        mov argblk,ax
        mov ah,'D'              ; Data packet.
        call spack              ; Send the packet.
	 jmp abort
        call rpack              ; Get a packet.
         jmp r                  ;  Trashed packet don't change state, retry.
        cmp ah,'Y'              ; ACK?
        jne sdata2              ; If not try next.
        mov ax,pktnum           ; Get the packet number.
        cmp ax,argblk           ; Is it the right packet number?
        jz sdat12
        ret                     ; If not hold out for the right one.
sdat12: inc ax                  ; Increment the packet number.
        and ax,3FH              ; Turn off the two high order bits.
        mov pktnum,ax           ; Save modulo 64 of the number.
        inc numpkt              ; Increment the number of packets.
        mov ah,numtry           ; Get the number of tries.
        mov oldtry,ah           ; Save it.
        mov numtry,0            ; Reset the number of tries.
        call gtchr
         jmp sdat13             ; Error go see if its EOF.
        mov siz,ax              ; Save the size of the data gotten.
        ret

sdat13: cmp ah,0FFH             ; Is it EOF?
        je sdt131
        jmp abort               ; If not give up.

sdt131: mov state,'Z'           ; Set the state to EOF.
        ret
sdata2: cmp ah,'N'              ; NAK?
        jne sdata3              ; See if is an error packet.
        call rtpos	        ; Position cursor.
        inc numrtr              ; Increment the number of retries
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov ax,pktnum           ; Get the present packet number.
        inc ax                  ; Increment.
        cmp ax,argblk           ; Is the packet's number one more than now?
        jz sdat12               ; Just as good as ACK; goto ACK code.
        ret                     ; If not go try again.
sdata3: cmp ah,'E'              ; Is it an error packet.
        jne sdata4
        call error
sdata4: jmp abort
SDATA   ENDP
 
 
;       Send EOF
 
SEOF    PROC    NEAR
        cmp numtry,maxtry       ; Have we reached the maximum number of tries?
        jl seof1
        mov dx,offset erms14
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
seof1:  inc numtry              ; Increment it.
        mov ax,pktnum           ; Get the packet number.
        mov argblk,ax
        mov argbk1,0            ; No data.
        mov ah,'Z'              ; EOF packet.
        call spack              ; Send the packet.
	 jmp abort
        call rpack              ; Get a packet.
         jmp r                  ;  Trashed packet don't change state, retry.
        cmp ah,'Y'              ; ACK?
        jne seof2               ; If not try next.
        mov ax,pktnum           ; Get the packet number.
        cmp ax,argblk           ; Is it the right packet number?
        jz seof12
        ret                     ; If not hold out for the right one.
seof12: inc ax                  ; Increment the packet number.
        and ax,3FH              ; Turn off the two high order bits.
        mov pktnum,ax           ; Save modulo 64 of the number.
        inc numpkt              ; Increment the number of packets.
        mov ah,numtry           ; Get the number of tries.
        mov oldtry,ah           ; Save it.
        mov numtry,0            ; Reset the number of tries.
        mov ah,closf            ; Close the file.
        mov dx,offset fcb
        int dos

        call gtnfil             ; Get the next file.
         jmp seof13             ;  No more.
        mov state,'F'           ; Set the state to file send.
        ret
seof13: mov state,'B'           ; Set the state to EOT.
        ret
seof2:  cmp ah,'N'              ; NAK?
        jne seof3               ; Try and see if its an error packet.
        call rtpos              ; Position cursor.
        inc numrtr              ; Increment the number of retries
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov ax,pktnum           ; Get the present packet number.
        inc ax                  ; Increment.
        cmp ax,argblk           ; Is the packet's number one more than now?
        jz seof12               ; Just as good as a ACK; go to the ACK code.
        ret                     ; If not go try again.
seof3:  cmp ah,'E'              ; Is it an error packet?
        jne seof4
        call error
seof4:  jmp abort
SEOF    ENDP
 
 
;       Send EOT
 
SEOT    PROC    NEAR
        cmp numtry,maxtry       ; Have we reached the maximum number of tries?
        jl seot1
        mov dx,offset erms14
        call erprt              ; Print an error message.
        jmp abort               ; Change the state to abort.
seot1:  inc numtry              ; Increment it.
        mov ax,pktnum           ; Get the packet number.
        mov argblk,ax
        mov argbk1,0            ; No data.
        mov ah,'B'              ; EOF packet.
        call spack              ; Send the packet.
	 jmp abort
        call rpack              ; Get a packet.
         jmp r                  ; Trashed packet don't change state, retry.
        cmp ah,'Y'              ; ACK?
        jne seot2               ; If not try next.
        mov ax,pktnum           ; Get the packet number.
        cmp ax,argblk           ; Is it the right packet number?
        jz seot12
        ret                     ; If not hold out for the right one.
seot12: inc ax                  ; Increment the packet number.
        and ax,3FH              ; Turn off the two high order bits.
        mov pktnum,ax           ; Save modulo 64 of the number.
        inc numpkt              ; Increment the number of packets.
        mov ah,numtry           ; Get the number of tries.
        mov oldtry,ah           ; Save it.
        mov numtry,0            ; Reset the number of tries.
        mov state,'C'           ; Set the state to file send.
        ret
seot2:  cmp ah,'N'              ; NAK?
        jne seot3               ; Is it error.
        call rtpos              ; Position cursor.
        inc numrtr              ; Increment the number of retries
        mov ax,numrtr
        call nout               ; Write the number of retries.
        mov ax,pktnum           ; Get the present packet number.
        inc ax                  ; Increment.
        cmp ax,argblk           ; Is the packet's number one more than now?
        jz seot12               ; Just as good as a ACK; go to the ACK code.
        ret                     ; If not go try again.
seot3:  cmp ah,'E'              ; Is it an error packet.
        jne seot4
        call error
seot4:  jmp abort
SEOT    ENDP
 

; Here is the bulk of the file I/O.  Good luck.
;       File routines
 
;       Output the chars in a packet.

FILEIO	PROC	NEAR 	

ptchr:  mov temp1,ax            ; Save the size.
        mov bx,offset data      ; Beginning of received packet data.
        mov outpnt,bx           ; Remember where we are.
        mov ch,rquote		; Quote char.
ptchr1: dec temp1               ; Decrement # of chars in packet.
	jnl pt1
        jmp rskp                ; Return successfully if done.
pt1:    dec chrcnt              ; Decrement number of chars in dta.
        jns ptchr2              ; Continue if space left.
        call outbuf             ; Output it if full.
         jmp r                  ;  Error return if disk is full.
ptchr2: mov bx,outpnt           ; Get position in output buffer.
        mov ah,[bx]                     ; Grab a char.
        inc bx
        mov outpnt,bx           ; and bump pointer.
        cmp ah,ch               ; Is it the quote char?
        jne ptchr4              ; If not proceed.
        mov ah,[bx]                     ; Get the quoted character
        inc bx
        mov outpnt,bx           ; and bump pointer.
        dec temp1               ; Decrement # of chars in packet.
        mov dh,ah               ; Save the char.
        and ah,80H                      ; Turn off all but the parity bit.
        mov dl,ah               ; Save the parity bit.
        mov ah,dh                       ; Get the char.
        and ah,7FH                      ; Turn off the parity bit.
        cmp ah,ch               ; Is it the quote char?
        jz ptchr3               ; If so just go write it out.
        mov ah,dh                       ; Get the char.
        add ah,40H                      ; Make it a control char again.
        and ah,7FH                      ; Modulo 128.
ptchr3: or ah,dl                ; Or in the parity bit.
ptchr4: mov bx,bufpnt           ; Destination buffer.
        mov [bx],ah                     ; Store it.
        inc bx
        mov bufpnt,bx           ; Update the pointer
        jmp ptchr1              ; and loop to next char.


        ; output the buffer, reset bufpnt and chrcnt
 
outbuf: push bx
        mov ah,writef           ; The write code.
        mov dx,offset fcb
        int dos		         ; Write the record.
        pop bx
        cmp al,0                        ; Successful.
        jz outbf1
	cmp al,01
	jz outbf0
	mov dx,offset erms17	; Record length exceeds dta.
	call erprt 
	ret
outbf0:
        mov dx,offset erms11    ; Disk full error.
        call erprt 
        ret
outbf1: mov bx,offset buff      ; Addr for beginning.
        mov bufpnt,bx           ; Store addr for beginning.
        mov ax,bufsiz-1         ; Buffer size.
        mov chrcnt,ax           ; Number of chars left.
        jmp rskp


;       Get the chars from the file.
 
gtchr:  mov ch,squote           ; Keep quote char in c.
        mov ah,filflg           ; Get the file flag.
        cmp ah,0                        ; Is there anything in the DMA?
        jz gtchr0               ; Yup, proceed.
        mov cl,0                ; No chars yet.
        call inbuf
         jmp gtceof             ; No more chars, go return EOF.
gtchr0: mov al,spsiz            ; Get the maximum packet size.
        sub al,5                ; Subtract the overhead.
        mov ah,0
        mov temp1,ax            ; Number of chars we're to get.
        mov bx,offset filbuf            ; Where to put the data.
        mov cbfptr,bx           ; Remember where we are.
        mov cl,0                ; No chars.
gtchr1: dec temp1               ; Decrement the number of chars left.
        jns gtchr2              ; Go on if there is more than one left.
        mov al,cl                       ; Return the count in A.
	mov ah,0
        jmp rskp
gtchr2: mov ax,chrcnt
        dec ax
        jl gtchr3
        mov chrcnt,ax
        jmp gtchr4
gtchr3: call inbuf              ; Get another buffer full.
         jmp gtceof
	cmp chrcnt,0
	jne gtchr4
	sub cl,2		; Don't count controllified Z.
	mov al,cl
	mov ah,0
	jmp rskp
gtchr4: mov bx,bufpnt           ; Position in DMA.
        mov ah,[bx]                     ; Get a char from the file.
        inc bx
        mov bufpnt,bx
        mov dh,ah               ; Save the char.
        and ah,80H                      ; Turn off all but parity.
        mov dl,ah               ; Save the parity bit.
        mov ah,dh                       ; Restore the char.
        and ah,7FH                      ; Turn off the parity.
        cmp ah,' '                      ; Compare to a space.
        jl gtchr5               ; If less then its a control char, handle it.
        cmp ah,del                      ; Is the char a delete?
        jz gtchr5               ; Go quote it.
        cmp ah,ch               ; Is it the quote char?
        jne gtchr8              ; If not proceed.
        dec temp1               ; Decrement the char total remaining.
        mov bx,cbfptr           ; Position in character buffer.
        mov [bx],ah                     ; Put the char in the buffer.
        inc bx
        mov cbfptr,bx
        inc cl                  ; Increment the char count.
        jmp gtchr8
gtchr5: or ah,dl                ; Turn on the parity bit.
        cmp ah,('Z'-100O)               ; Is it a ^Z?
        jne gtchr7              ; If not just proceed.
        mov ah,eoflag           ; EOF flag set?
        cmp ah,0
        jz gtchr6               ; If not just go on.
        mov bx,bufpnt
        mov ax,chrcnt
        mov dh,al               ; Get number of chars left in DMA.
gtch51: dec dh
        mov ah,dh
        jns gtch52              ; Any chars left?
        mov chrcnt,0            ; If not, say so.
        mov al,cl                       ; Return the count in A.
	mov ah,0
        jmp rskp
gtch52: mov ah,[bx]                     ; Get the next char.
        inc bx                  ; Move the pointer.
        cmp ah,('Z'-100O)               ; Is it a ^Z?
        jz gtch51               ; If so see if they rest are.
 
gtchr6: mov ah,('Z'-100O)       ; Restore the ^Z.
gtchr7: xchg ah,al
        mov ah,0
        mov temp2,ax            ; Save the char.
        dec temp1               ; Decrement char counter.
        mov bx,cbfptr           ; Position in character buffer.
        mov [bx],ch                     ; Put the quote in the buffer.
        inc bx
        mov cbfptr,bx
        inc cl                  ; Increment the char count.
        mov ax,temp2            ; Get the control char back.
        xchg al,ah
        add ah,40H                      ; Make the non-control.
        and ah,7fH                      ; Modulo 200 octal.
gtchr8: mov bx,cbfptr           ; Position in character buffer.
        or ah,dl                ; Or in the parity bit.
        mov [bx],ah                     ; Put the char in the buffer.
        inc bx
        mov cbfptr,bx
        inc cl                  ; Increment the char count.
        jmp gtchr1              ; Go around again.
 
gtceof: cmp cl,0		; Had we gotten any data?
	je gteof0		; Nope.
	mov al,cl
	mov ah,0
	jmp rskp
gteof0: mov ah,0FFH             ; Get a minus one.
        ret


inbuf:  mov ah,eoflag           ; Have we reached the end?
        cmp ah,0
        jz inbuf0
        ret                     ; Return if set.
inbuf0: push bx                 
	push cx
	mov bx,offset buff      ; Set the r/w buffer pointer.
        mov bufpnt,bx
        mov ah,readf            ; Read a record.
        mov dx,offset fcb
        int dos
	mov cx,filsiz
	cmp cx,0		; Check for 128 chars or less left.
	jne inbuf1		; Still have data left.
	mov ax,ds
	mov es,ax
	mov si,offset filsiz2
	mov di,offset bufhex
	cmps filsiz2,es:bufhex
	ja inbuf1		; More than 128 chars.
	mov eoflag,0FFH		; Set End-of-file.
	mov cx,filsiz2
	mov chrcnt,cx		; Return proper number of chars.
        mov filflg,0		; Buffer not empty.
	pop cx
	pop bx
	jmp rskp
inbuf1:	sub filsiz2,80H		; Sent another 128 chars.
	sbb filsiz,0		; Account for the doubleword.
	mov al,80H		; Use as counter for number of chars read.
	pop cx
	pop bx
	cmp filflg,0		; Ever used DMS?
	jnz inbf21		; Nope, then don't change count.
	dec al			; Fix boundary error.
inbf21: mov ah,0                ; Zero the flag (buffer not empty).
	mov chrcnt,ax		; Number of chars read from file.
        mov filflg,0		; Buffer not empty.
	jmp rskp

getfil: mov ah,0FFH
        mov filflg,ah           ; Nothing in the DMA.
        mov ax,0
        mov eoflag,ah           ; Not the end of file.
        mov bx,offset fcb+0CH
        mov [bx],ax             ; Zero the current block number.
        mov bx,offset fcb+0EH
        mov [bx],ax             ; Ditto for Lrecl.
        mov bx,offset fcb+20H
        mov [bx],ah             ; Zero the current record (of block).
	inc bx
	mov [bx],ax		; Same for record (of file). 
	mov bx,offset fcb+23H
	mov [bx],ax
        mov ah,openf            ; Open the file.
        mov dx,offset fcb
        int dos
	mov bx,offset fcb+18	; File size in bytes (hi order word).
	mov di,offset filsiz	; Where to put the info. 
	mov ax,[bx]
	mov [di],ax
	mov bx,offset fcb+16	; Lo order word.
	mov ax,[bx]
	mov 2[di],ax
	sub filsiz2,1		; Don't count the ^Z.
	sbb filsiz,0
        jmp rskp


gtnfil: cmp wldflg,0		; Was there a "*"?		[7 start]
	je gtn5			; Nope.
	mov bx,offset cpfcb	; Get FCB from last check for file.  
	mov di,offset fcb	; Copy to FCB.
	mov cl,37		; Size of FCB.
	call fcbcpy
gtn2:	mov ah,snext
	mov dx,offset fcb	; More files?
	int dos
	cmp al,0FFH
	je gtn5
	mov bx,offset fcb
	mov di,offset cpfcb
	mov cl,37
	call fcbcpy		; Copy from FCB.
	mov di,offset fcb+1	; Get name of next file to send.
	mov bx,offset buff+1
	mov cl,11
	call fcbcpy
	call getfil		; Initialize
	 jmp r
	jmp rskp			
gtn5:	mov wldflg,0		; Reset wild card flag.
	ret			 			;  [7 end]


;       Get the file name (including host to micro translation)
 
gofil:  mov bx,offset data      ; Get the address of the file name.
        mov datptr,bx           ; Store the address.
        mov bx,offset fcb+1            ; Address of the FCB.
        mov fcbptr,bx           ; Save it.
        mov ax,0
        mov temp1,ax            ; Initialize the char count.
        mov temp2,ax
	mov si,offset fcb
        mov [si],ah             ; Set the drive to default to current.
        mov ch,' '
gofil1: mov [bx],ch                     ; Blank the FCB.
        inc bx
        inc ah
        cmp ah,0BH                      ; Twelve?
        jl gofil1
gofil2: mov bx,datptr           ; Get the NAME field.
        mov ah,[bx]
        inc bx
        mov datptr,bx
        cmp ah,'.'                      ; Seperator?
        jne gofil3
        mov bx,offset fcb+9H
        mov fcbptr,bx
        mov ax,temp1
        mov temp2,ax
        mov temp1,9H
        jmp gofil6
gofil3: cmp ah,0                        ; Trailing null?
        jz gofil7               ; Then we're done.
        mov bx,fcbptr
        mov [bx],ah
        inc bx
        mov fcbptr,bx
        mov ax,temp1            ; Get the char count.
        inc ax
        mov temp1,ax
        cmp ax,8H                       ; Are we finished with this field?
        jl gofil2
gofil4: mov temp2,ax
        mov bx,datptr
        mov ah,[bx]
        inc bx
        mov datptr,bx
        cmp ah,0
        jz gofil7
        cmp ah,'.'                      ; Is this the terminator?
        jne gofil4              ; Go until we find it.
gofil6: mov bx,datptr           ; Get the TYPE field.
        mov ah,[bx]
        inc bx
        mov datptr,bx
        cmp ah,0                        ; Trailing null?
        jz gofil7               ; Then we're done.
        mov bx,fcbptr
        mov [bx],ah
        inc bx
        mov fcbptr,bx
        inc temp1               ; Increment char count.
        cmp temp1,0CH                   ; Are we finished with this field?
        jl gofil6
gofil7: mov bx,datptr
	mov ah,'$'
        mov [bx],ah             ; Put in a dollar sign for printing.
	call clrfln
        mov dx,offset data	; Print the file name
        call strout
        mov ah,flwflg           ; Is file warning on?
        cmp ah,0
	jnz gf7x
        jmp gofil9              ; If not, just proceed.
gf7x:   mov ah,openf            ; See if the file exists.
        mov dx,offset fcb
        int dos
        cmp al,0FFH             ; Does it exist?
	jnz gf8x
        jmp gofil9               ; If not create it.
gf8x:	
	mov dx,scrfr
	call set_curpos		; [17]
        mov dx,offset infms5	; Inform user we rename file.
        call strout
        mov ax,temp2            ; Get the number of chars in the file name.
        cmp ax,0
        jne gofil8
        mov ax,temp1
        mov temp2,ax
gofil8: mov ch,0
        mov cl,al
	mov al,0			; Says if first field is full.
        cmp cl,9H                       ; Is the first field full?
        jne gofl81
        mov al,0FFH             ; Set a flag saying so.
        dec cl
gofl81: mov bx,offset fcb              ; Get the FCB.
        add bx,cx               ; Add in the character number.
	mov ah,'&'
        mov [bx],ah             ; Replace the char with an ampersand.
	push ax
        push bx
        mov ah,openf            ; See if the file exists.
        mov dx,offset fcb
        int dos
        pop bx
        cmp al,0FFH             ; Does it exist?
	pop ax
        jz gofl89               ; If not create it.
        cmp al,0                      ; Get the flag.
        jz gofl83
        dec cl                  ; Decrement the number of chars.
        cmp cl,0
        jz gofl88               ; If no more, die.
        jmp gofl81
gofl83: inc cl                  ; Increment the number of chars.
        cmp cl,9H                       ; Are we to the end?
        jl gofl81               ; If not try again ; else fail. 
 
gofl88:
        mov dx,offset ermes4	; rename failed
        call erprt 
        ret
 
gofl89: mov bx,offset fcb+0CH          ; Point past the end of the file name.
        mov dh,[bx]                     ; Save the present contents.
	mov ah,'$'
        mov [bx],ah             ; Put in a dollar sign.
        push dx
        mov ah,prstr            ; Print the file name.
        mov dx,offset fcb+1
        int dos
        pop dx
        mov bx,offset fcb+0CH          ; Restore over the dollar sign.
        mov [bx],dh
gofil9: mov ah,delf             ; Delete the file if it exists.
        mov dx,offset fcb
        int dos
        mov ax,0
        mov si,offset fcb+0CH
        mov [si],ax             ; Zero current block.
        mov si,offset fcb+0EH
        mov [si],ax             ; Same for Lrecl.
        mov si,offset fcb+20H
        mov [si],ah             ; Zero the current record (within block).
	inc si
	mov [si],ax		; Zero record (within file).
	mov si,offset fcb+23H
	mov [si],ax
        mov ah,makef            ; Now create it.
        mov dx,offset fcb
        int dos
        cmp al,0FFH             ; Is the disk full?
	je gf9x
        jmp rskp
gf9x:  
        mov dx,offset erms11
        call erprt 
        ret

FILEIO	ENDP
 
 
;       Packet routines
 
; Send_Packet
; This routine assembles a packet from the arguments given and sends it
; to the host.
;
; Expects the following:
;       AH     - Type of packet (D,Y,N,S,R,E,F,Z,T)
;       ARGBLK - Packet sequence number
;       ARGBK1 - Number of data characters
; Returns: +1 always
 
SPKT	PROC	NEAR

spack:  push ax                 ; Save the packet type.
        mov bx,offset packet    ; Get address of the send packet.
        mov ah,soh              ; Get the start of header char.
        mov [bx],ah             ; Put in the packet.
        inc bx                  ; Point to next char.
        mov ax,argbk1           ; Get the number of data chars.
	xchg ah,al
        add ah,' '+3            ; Real packet character count made printable.
        mov [bx],ah             ; Put in the packet.
        inc bx                  ; Point to next char.
        mov cl,ah               ; Start the checksum.
        mov ax,argblk           ; Get the packet number.
	xchg ah,al
        add ah,' '              ; Add a space so the number is printable.
        mov [bx],ah             ; Put in the packet.
        inc bx                  ; Point to next char.
        add cl,ah               ; Add the packet number to the checksum.
        pop ax                  ; Get the packet type.
        mov [bx],ah             ; Put in the packet.
        inc bx                  ; Point to next char.
        add cl,ah               ; Add the type to the checksum.
        mov dx,argbk1           ; Get the packet size.
spack2: cmp dx,0                ; Are there any chars of data?
         jz spack3              ;  No, finish up.
        dec dx                  ; Decrement the char count.
        mov ah,[bx]             ; Get the next char.
        inc bx                  ; Point to next char.
        add cl,ah               ; Add the char to the checksum.
	cmp ah,0
	jns spack2
	mov hierr,0FFH		; set err flag. 
        jmp spack2              ; Go try again.
spack3: cmp hierr,0
	je sp3x			; Nothing special to do.
	call biterr
	mov hierr,0		; Reset.
sp3x:	mov ah,cl               ; Get the character total.
	mov ch,cl		; Save here too (need 'cl' for shift).
        and ah,0C0H             ; Turn off all but the two high order bits.
	mov cl,6
        shr ah,cl               ; Shift them into the low order position.
	mov cl,ch
        add ah,cl               ; Add it to the old bits.
        and ah,3FH              ; Turn off the two high order bits.  (MOD 64)
        add ah,' '              ; Add a space so the number is printable.
        mov [bx],ah             ; Put in the packet.
        inc bx                  ; Point to next char.
        mov ah,seol             ; Get the EOL the other host wants.
        mov [bx],ah             ; Put in the packet.
        inc bx                  ; Point to next char.
        mov ah,0                ; Get a null.
        mov [bx],ah             ; Put in the packet.
	cmp debug,0		; debug mode.
	je spack4
	inc bx
	mov ah,'$'
	mov [bx],ah
	mov dx,scrsp
	call set_curpos
	mov dx,offset spmes
	call strout
	mov dx,offset packet
	call strout		; debug end.
spack4: call outpkt             ; Call the system dependent routine.
	 jmp r
	jmp rskp
 
SPKT	ENDP 
;       Write out a packet.
 
OUTPKT  PROC    NEAR
        mov dh,spad             ; Get the number of padding chars.
outpk2: dec dh
        cmp dh,0
        jl outpk3               ; If none left proceed.
        mov al,spadch           ; Get the padding char.
        call hpdcout            ; Output it.
        jmp outpk2
outpk3: mov bx,offset packet    ; Point to the packet.
outlup: mov al,[bx]             ; Get the next character.
        cmp al,0                ; Is it a null?
        jnz outlp2
        jmp rskp
outlp2: call hpdcout            ; Output the character.
        inc bx                  ; Increment the char pointer.
        jmp outlup
OUTPKT  ENDP
 
 
;************************System Dependent****************************
;       Put a char in AH to the port.
INCHR 	PROC  NEAR 
;
;       Get a char from the port and return in AH.
; 	set carry if user wants to quit
	call hpdcin		; Get character if ready.  [14]
	 jnc inchr2		; Got it in AL.  [14]
        call hpconstat          ; Is a char on the console?
         jc inchr  
        cmp al,cr                       ; Is it a carriage return?
        jne inchr              ; If not go look for another char.
	stc			; quit on carriage return
inchr2:
       	ret
inchr   ENDP
 
 
; Receive_Packet
; This routine waits for a packet arrive from the host.  It reads
; chars until it finds a SOH.

RPACK	PROC	NEAR
        call inpkt              ; Read up to a carriage return.
         jmp r                  ;  Return bad.
rpack0: call getchr             ; Get a character.
         jmp r                  ;  Hit the carriage return, return bad.
        cmp ah,soh              ; Is the char the start of header char?
         jne rpack0             ;  No, go until it is.
rpack1: call getchr             ; Get a character.
         jmp r                  ;  Hit the carriage return, return bad.
        cmp ah,soh              ; Is the char the start of header char?
         jz rpack1              ;  Yes, then go start over.
        mov cl,ah               ; Start the checksum.
        sub ah,' '+3            ; Get the real data count.
        mov dh,ah               ; Save it for later.
        mov al,ah               ; Swap halves.
        mov ah,0
        mov argbk1,ax           ; Save the data count.
        call getchr             ; Get a character.
         jmp r                  ;  Hit the carriage return, return bad.
        cmp ah,soh              ; Is the char the start of header char?
         jz rpack1              ;  Yes, then go start over.
        add cl,ah               ; Add it to the checksum.
        sub ah,' '              ; Get the real packet number.
        mov al,ah               ; Swap halves.
        mov ah,0
        mov argblk,ax           ; Save the packet number.
        call getchr             ; Get a character.
         jmp r                  ;  Hit the carriage return, return bad.
        cmp ah,soh              ; Is the char the start of header char?
         jz rpack1              ;  Yes, then go start over.
        mov temp,ax             ; Save the message type. [11]
        add cl,ah               ; Add it to the checksum.
        mov bx,offset data      ; Point to the data buffer.
rpack2: dec dh                  ; Any data characters?
         js rpack3              ;  If not go get the checksum.
        call getchr             ; Get a character.
         jmp r                  ;  Hit the carriage return, return bad.
        cmp ah,soh              ; Is the char the start of header char?
         jz rpack1              ;  Yes, then go start over.
        mov [bx],ah             ; Put the char into the packet.
        inc bx                  ; Point to the next character.
        add cl,ah               ; Add it to the checksum.
        jmp rpack2              ; Go get another.
rpack3: call getchr             ; Get a character.
         jmp r                  ;  Hit the carriage return, return bad.
        cmp ah,soh              ; Is the char the start of header char?
         jz rpack1              ;  Yes, then go start over.
        sub ah,' '              ; Turn the char back into a number.
        mov dh,cl               ; Get the character total.
        and dh,0C0H             ; Turn off all but the two high order bits.
	mov ch,cl
	mov cl,6
        shr dh,cl               ; Shift them into the low order position.
	mov cl,ch
        add dh,cl               ; Add it to the old bits.
        and dh,3FH              ; Turn off the two high order bits.  (MOD 64)
        cmp dh,ah               ; Are they equal?
         jz rpack4              ;  If so finish up.
        ret
rpack4: mov ah,0
        mov [bx],ah             ; Put a null at the end of the data.
        mov ax,temp             ; Get the type.   [11]
        jmp rskp
RPACK   ENDP
 
 
INPKT   PROC    NEAR
        mov bx,offset recpkt    ; Point to the beginning of the packet.
	mov incnt,0
inpkt2: call inchr              ; Get a character.
        jnc inpkt7
        ret                     ;  Return failure.
inpkt7: mov [bx],al             ; Put the char in the packet.
        inc bx
	inc incnt
        cmp al,reol             ; Is it the EOL char?
        jne inpkt2              ; If not loop for another.
	cmp incnt,1		; Ignore bare CR.   [2 start]
	jne inpkt6
	mov incnt,0
	mov bx,offset recpkt
	jmp inpkt2		;                   [2 end]
inpkt6:	cmp ibmflg,0            ; Is this the (dumb) IBM mainframe?
        jz inpkt4               ; If not then proceed.
inpkt5: cmp state,'S'           ; Check if this is the Send-Init packet.
        jz inpkt4               ; If so don't wait for the XON.
inpkt3: call inchr              ; Wait for the turn around char.
        jc inpkt4
        cmp al,xon              ; Is it the IBM turn around character?
        jne inpkt3              ; If not, go until it is.
inpkt4: mov bx,offset recpkt
        mov pktptr,bx           ; Save the packet pointer.
        jmp rskp                ; If so we are done.
INPKT   ENDP

 
GETCHR  PROC    NEAR
        push bx
        mov bx,pktptr           ; Get the packet pointer.
        mov ah,[bx]             ; Get the char.
        inc bx
        mov pktptr,bx
        pop bx                  ; Restore BX.
        cmp ah,reol             ; Is it the EOL char?
        jne getcr2              ; If not return retskp.
        ret                     ; If so return failure.
getcr2: jmp rskp
GETCHR  ENDP
 
 
; This is where we go if we get an error packet.  A call to ERROR 
; positions the cursor and prints the message.  A call to ERROR1
; just prints a CRLF and then the message.  [8]
 
ERROR   PROC    NEAR
        mov state,'A'           ; Set the state to abort.
        call erpos	        ; Position the cursor.
	jmp error2
error1:	call newln  
error2: mov bx,argbk1           ; Get the length of the data.
        add bx,offset data      ; Get to the end of the string.
        mov ah,'$'              ; Put a dollar sign at the end.
        mov [bx],ah
        mov dx,offset data	; print the error msg
        call strout
        ret
ERROR   ENDP
 
; LOGOUT - tell remote KERSRV to logout.

LOGOUT	PROC	NEAR						; [8 start]
	mov ah,cmcfm
	call comnd		; Get a confirm.
	 jmp r
	call logo
	 jmp rskp		; Go get another command whether we ....
	jmp rskp		; .... succeed or fail.
LOGOUT	ENDP

LOGO	PROC	NEAR
	mov numtry,0		; Initialize count.
logo1:	mov ah,numtry
	cmp ah,maxtry		; Too many times?
	js logo3		; No, try it.
logo2:
	mov dx,offset erms19
	call strout
	ret
logo3:	inc numtry		; Increment number of tries.
	mov argblk,0		; Packet number zero.
	mov argbk1,1		; One piece of data.
	mov bx,offset data
	mov ah,'L'
	mov [bx],ah		; Logout the remote host.
	mov ah,'G'		; Generic command packet.
	call spack
	 jmp logo2		; Tell user and die.
	 nop
	call rpack 
	 jmp logo1		; Go try again.
	 nop
	cmp ah,'Y'		; ACK?
	jne logo4
	jmp rskp
logo4:	cmp ah,'E'		; Error packet?	
	jnz logo1		; Try sending the packet again.
	call error1
	ret
LOGO	ENDP

; FINISH - tell remote KERSRV to exit.

FINISH	PROC	NEAR
	mov ah,cmcfm		; Parse a confirm.
	call comnd
	 jmp r
	mov numtry,0		; Initialize count.
fin1:	mov ah,numtry
	cmp ah,maxtry		; Too many times?
	js fin3			; Nope, try it.
fin2:	
	mov dx,offset erms18
	call strout
	jmp rskp		; Go home.
fin3:	inc numtry		; Increment number of tries.
	mov argblk,0		; Packet number zero.
	mov argbk1,1		; One piece of data.
	mov bx,offset data
	mov ah,'F'
	mov [bx],ah		; Finish running Kermit.
	mov ah,'G'		; Generic command packet.
	call spack
	 jmp fin2		; Tell user and die.
	 nop
	call rpack 
	 jmp fin1		; Go try again.
	 nop
	cmp ah,'Y'		; Got an ACK?
	jnz fin4
	jmp rskp		; Yes, then we're done.
fin4:	cmp ah,'E'		; Error packet?
	jnz fin1		; Try sending it again.
	call error1
	jmp rskp
FINISH	ENDP

; BYE command - tell remote KERSRV to logout & exits to DOS.  

BYE	PROC	NEAR
	mov ah,cmcfm		; Parse a confirm.
	call comnd
	 jmp r
	call logo		; Tell the mainframe to logout.
 	 jmp rskp		; Failed - don't exit.
	mov extflg,1		; Set exit flag.
	jmp rskp					; [8 end]
BYE	ENDP

; This is the 'exit' command.  It leaves KERMIT and returns to DOS.
 
EXIT    PROC    NEAR
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r
        mov extflg,1            ;  Set the exit flag.
        jmp rskp                ; Then return to system.
EXIT    ENDP
 
 
; This is the 'help' command.  It gives a list of the commands.
 
HELP    PROC    NEAR
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r
        mov dx,offset tophlp    ; The address of the help message.
        call strout
        jmp rskp
HELP    ENDP
 
;       This is the CONNECT command.
 
TELNET  PROC    NEAR
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r                  ;  Didn't get a confirm.
        call newln
	mov dx,offset tmsg1
	call strout
	call escprt
	mov dx,offset tmsg3
	call strout
	call dc_handshake  	; [17] let firmware do handshake
telnoe: call plup		; Char at port (type to console) ?
        jc telexit
      	call hpconout		; [17]
	jmp telnoe
telexit:
        jmp rskp
TELNET  ENDP


PLUP	PROC	NEAR
	call conchr		; Check keyboard.   [15 begin]
	 jnc plup10		; Exit from Telnet
	mov dx,offset tmsg2
	call strout
	stc      
        ret
;
plup10:
	call hpdcin		; Check serial port
        jc plup			; try again if no data
        ret
PLUP	ENDP

;************************System Dependent****************************
 
CONCHR  PROC    NEAR
	call hpconstat		; [17]
	jc  cnc2x	
        cmp al,escchr           ; Is it an escape char?
        jz intchr               ; If so go process it.
	push ax
        call hpdcout            ; Output the char to the port.
	pop ax
        mov ah,ecoflg           ; Get the echo flag.
        cmp ah,0                        ; Is it turned on?
	jz cnc2x 
        and al,7FH
      	call hpconout           ; [17] echo the char
cnc2x:  clc			; clear carry for ok return
	ret
CONCHR  ENDP
 
CONN	PROC	NEAR	 
intchr: call hpconin            ;[17]                
	cmp al,0                        ; Is the char a null?
        jz intchr               ; If so, go until we get a char.
        mov bh,al               ; Save the actual char.
        and al,137O             ; Convert to upper case.
        cmp al,'C'                      ; Is it close?
        jne intch0
	stc			; return with carry means quit
        ret
intch0: cmp al,'S'                      ; Is it status?
	jnz inc0x
	call stat01
	nop
	nop
	nop
	jmp intch9
inc0x:  mov al,bh                       ; Get the char.
        cmp al,'?'                      ; Is it help?
        jne intch1              ; If not, go to the next check.
        mov dx,offset inthlp    ; If so, get the address of the help message.
        call strout
        jmp intchr              ; Get another char.
intch1:
        cmp al,escchr           ; Is it the escape char?
        jne intch2              ; If not, go send a beep to the user.
        call hpdcout
        jmp intch9              ; Return, we are done here.
intch2: mov al,'G'-100O         ; Otherwise send a beep.
        call hpconout
intch9:
	clc			; clear carry means everything ok
        ret      
CONN	ENDP


 
; This is the SET command.
 
SETCOM  PROC    NEAR
        mov dx,offset settab    ; Parse a keyword from the set table.
        mov bx,offset sethlp
        mov ah,cmkey
        call comnd
         jmp r
        call bx
        jmp rskp
SETCOM  ENDP
 
 
;       This is the ESCAPE character SET subcommand.     [6 start]
 
ESCAPE  PROC    NEAR
	call cmgtch		; Get a char.
	cmp ah,0
	jns es1			; Terminator or no?
	and ah,7FH		; Turn off minus bit.
	cmp ah,'?'
	jne es0
	mov dx,offset eschlp
	call strout
	call newln
	mov dx,offset kerm
	call strout
	mov bx,cmdptr
	mov al,'$'
	mov [bx],al
	mov dx,offset cmdbuf
	call strout
	dec cmcptr		; Ignore dollar sign.
	dec cmccnt
	mov cmaflg,0
	jmp repars
es0:
	mov dx,offset ermes3
	call strout
	ret
es1:  	mov temp,ax
	call cmcfrm
	 jmp es0
	 nop			; Take up 3 bytes.
	mov ax,temp
	mov escchr,ah		; Save new value.
	ret
ESCAPE  ENDP			; [6 end]

; 	This is the End-of-line character SET subcommand.       [5 start]

EOLSET	PROC	NEAR
	call cmgtch		; Get the first char into AH.
	cmp ah,0
	jns eol1
	cmp ah,0BFH		; Question mark?
	je eol4
	jmp eol3
eol1:	sub ah,030H		; Digit --> real number.
	mov temp,ax		; Save the input.
	call cmcfrm
	 jmp eol0		; Got a char.
	mov ax,0
	mov bx,temp
	jmp eol2
eol0:	sub ah,030H		; Digit --> real number.
	mov temp1,ax
	call cmcfrm
	 jmp eol3		; Too many chars.
	mov bx,temp1
	mov ax,temp
	xchg al,ah
	mov ah,0
eol2:	mov temp,ax		; Save our registers.
	mov temp1,bx
	call cmcfrm
	 jmp eol3
	mov bx,temp1
	mov ax,temp
	mov temp2,10		; Get numerical value of char.
	mul temp2
	add al,bh
	mov ah,0
	cmp al,0
	jl eol3
	cmp al,1FH
	jg eol3
	mov seol,al		; Use new eol char.
	ret
eol3:
	mov dx,offset eolerr	; Bad end-of-line char
	call strout
	jmp prserr
eol4:	mov ah,prstr
	mov dx,offset eolhlp
	call strout
	call newln
	mov dx,offset kerm
	call strout
	mov bx,cmdptr
	mov al,'$'
	mov [bx],al
	mov dx,offset cmdbuf
	call strout
	dec cmcptr			; Don't count the dollar sign.
	dec cmccnt			; Or the question mark.
	mov cmaflg,0			; Check for more input.
	jmp repars
EOLSET	ENDP						     ;	[5 end]
 
;       This is the LOCAL echo SET subcommand.
 
LCAL    PROC    NEAR
        mov dx,offset ontab
        mov bx,offset onhlp
        mov ah,cmkey
        call comnd
         jmp r
        push bx                 ; Save the parsed value.
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r                  ;  Didn't get a confirm.
        pop bx
        mov ecoflg,bl           ; Set the local echo flag.
	ret
LCAL    ENDP
 
;       This is the VT52 emulation SET subcommand.
 
VT52EM  PROC    NEAR
        mov dx,offset ontab
        mov bx,offset onhlp
        mov ah,cmkey
        call comnd
         jmp r
        push bx
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r                  ;  Didn't get a confirm.
        pop bx
        mov vtflg,bl            ; Set the VT52 emulation flag.
	ret
VT52EM  ENDP
 
;       This is the SET IBM command.
 
IBMSET  PROC    NEAR
        mov dx,offset ontab
        mov bx,offset onhlp
        mov ah,cmkey
        call comnd
         jmp r
        push bx
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r                  ;  Didn't get a confirm.
        pop bx
        mov ibmflg,bl           ; Set the IBM flag.
	cmp bl,0		; Turning on or off?    [12 start]
	je ibmst1		; If off, set parity & local echo to defaults.
	mov ah,ibmpar		; Set IBM parity.
	mov al,1		; Set local echo on.
	jmp ibmst2
ibmst1:	mov ah,defpar		; Reset parity to default.
	mov al,0		; Turn off local echo.
ibmst2:	mov parflg,ah		; Set parity.
	mov ecoflg,al		; And local echo.	[12 end]	
	ret
IBMSET  ENDP

;       This is the SET File-Warning command.
 
FILWAR  PROC    NEAR
        mov dx,offset ontab
        mov bx,offset onhlp
        mov ah,cmkey
        call comnd
         jmp r
        push bx
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r                  ;  Didn't get a confirm.
        pop bx
        mov flwflg,bl           ; Set the IBM flag.
	ret
FILWAR  ENDP



; Sets debugging mode on and off.

DEBST	PROC    NEAR
        mov dx,offset ontab
        mov bx,offset onhlp
        mov ah,cmkey
        call comnd
         jmp r
        push bx
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r                  ;  Didn't get a confirm.
        pop bx
        mov debug,bl           ; Set the IBM flag.
	ret
DEBST   ENDP

;DK1 Turn bell on or off
BELLST	PROC	NEAR
	mov dx,offset ontab
	mov bx,offset onhlp
	mov ah,cmkey
	call comnd
	 jmp r
	push bx
	mov ah,cmcfm
	call comnd
	 jmp r
	pop bx
	mov bellfl,bl
	ret
BELLST	ENDP

 
;       This is the STATUS command.
 
STATUS  PROC    NEAR
        call stat0
         jmp r
        jmp rskp
STATUS  ENDP
 
STAT0   PROC    NEAR
        mov ah,cmcfm
        call comnd              ; Get a confirm.
         jmp r                  ;  Didn't get a confirm.
stat01: mov dx,offset locst     ; Assume local echo on.
	cmp ecoflg,0            ; Is the local echo flag on?
        jnz stat1               
        mov dx,offset remst     ; If not say so.
stat1:  call strout             ; Print it.
	mov dx,offset ibmst	; Is IBM flag on?
	cmp ibmflg,0
	jnz stat3
	mov dx,offset noibm     ; Say it's not.
stat3:  call strout 
	mov dx,offset deboff	; Assume debug mode is off.
	cmp debug,0
	je stat4
	mov dx,offset debon
stat4:	call strout 
	mov dx,offset flwon	; Assume file-warning on.
	cmp flwflg,0
	jne stat5
	mov dx,offset flwoff
stat5:	call strout 
	mov dx,offset eolst	; Tell the end-of-line char used.  [5 start]
	call strout 
	mov al,seol
	add al,40H
	call hpconout		 ; [5 end]
	mov dx,offset escmes			; Print escape character.  [6]
	call strout
	call escprt
        call newln
        jmp rskp
STAT0   ENDP
 
 
 
;       Utility routines
 
; 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
 
; This routine prints the number in AX on the screen.
;       (Thanks to Jeff Damens)
 
NOUT    PROC    NEAR
        push bx                 ; Save some stuff.
        push cx
        push dx
        push ax
        mov bh,0                ; Don't print leading zeros.
        and ah,0F0H             ; Only want high order nibble.
        mov cl,4
	shr ah,cl
        call pdig               ; Print the digit.
        pop ax                  ; Restore argument.
        and ah,0FH              ; Just the low order nibble.
        call pdig
        mov ah,al
        and ah,0F0H             ; Only want high order nibble.
	mov cl,4
        shr ah,cl
        call pdig               ; Print the digit.
        mov ah,al
        and ah,0FH              ; Just the low order nibble.
        mov bh,0FFH             ; Make sure at least one zero is printed.
        call pdig
        pop dx                  ; Restore some stuff.
        pop cx
        pop bx
        ret
NOUT    ENDP
 
        ; print the digit in register AH
 
PDIG    PROC    NEAR
        push ax                 ; Save it.
        cmp ah,0
        jne pdig2
        cmp bh,0
        jne pdig2
        pop ax
        ret
pdig2:  mov bh,0FFH             ; Set the print zero flag.
        cmp ah,10               ; Do we use digits or a-f?
        jl usedig               ; Digit.
        add ah,'A'-10           ; Compute digit
        jmp havdig              ; and proceed.
usedig: add ah,'0'              ; Convert to digit
havdig: mov al,ah
        call hpconout
        pop ax
        ret
PDIG    ENDP
 

;       This set of routines provides a user oriented way of parsing
;       commands.  It is similar to that of the COMND JSYS in TOPS-20.
 
 
;       This routine prints the prompt in DE and specifies the reparse
;       address.
 
PROMPT	PROC  NEAR
	pop bx                  ; Get the return address.
        push bx                 ; Put it on the stack again.
        mov cmrprs,bx           ; Save as addr to go to on reparse.
        mov bx,0                        ; Clear out register.
        add bx,sp               ; Get the present stack pointer.
        mov cmostp,bx           ; Save for later restoral.
        mov cmprmp,dx           ; Save pointer to the prompt.
        mov bx,offset cmdbuf
        mov cmcptr,bx           ; Initialize the command pointer.
        mov cmdptr,bx
        mov ah,0
        mov cmaflg,ah           ; Zero the flags.
        mov cmccnt,ah
        mov cmsflg,0FFH
        call newln
        mov dx,cmprmp		; Print the prompt
        call strout
        ret
PROMPT	ENDP
 
;       This address is jumped to on reparse.

PARSE	PROC NEAR 
repars: mov sp,cmostp           ; new sp <-- old sp
        mov bx,offset cmdbuf
        mov cmdptr,bx
        mov ah,0FFH
        mov cmsflg,ah
        mov bx,cmrprs           ; Get the reparse address.
        call bx                 ; Go there.
 
;       This address can be jumped to on a parsing error.
 
prserr: mov sp,cmostp           ; Set new sp to old one.
        mov bx,offset cmdbuf
        mov cmcptr,bx           ; Initialize the command pointer.
        mov cmdptr,bx
        mov ah,0
        mov cmaflg,ah           ; Zero the flags.
        mov cmccnt,ah
        mov cmsflg,0FFH
        call newln
        mov dx,cmprmp           ; Get the prompt.
        call strout
; Instead return to before the prompt call.
        mov bx,cmrprs
        call bx
PARSE	ENDP
 
;       This routine parses the specified function in AH. Any additional
;       information is in DX and BX.
;       Returns +1 on success
;               +4 on failure (assumes a JMP follows the call)
 
CMND	PROC NEAR
comnd:  mov cmstat,ah           ; Save what we are presently parsing.
        call cminbf             ; Get chars until an action or a erase char.
	mov ah,cmstat		; Restore 'ah' for upcoming checks.
        cmp ah,cmcfm            ; Parse a confirm?
        jz cmcfrm               ; Go get one.
        cmp ah,cmkey            ; Parse a keyword?
	jnz cm1
        jmp cmkeyw               ; Try and get one.
cm1:    cmp ah,cmifi		; Parse an input file spec?
	jnz cm2
	jmp cmifil		; Go get one.
cm2:	cmp ah,cmofi		; Output file spec?
	jnz cm3
	jmp cmofil		; Go get one.
cm3:	cmp ah,cmtxt		; Parse arbitrary text.   [8]
	jnz cm4
	jmp cmtext
cm4:				; Else give error
	mov dx,offset cmer00	; "?Unrecognized COMND call"
	call strout
	ret

; This routine gets a confirm.

cmcfrm: call cmgtch		; Get a char.
	cmp ah,0		; Is it negative (a terminator; a space or
				; a tab will not be returned here as they
				; will be seen as leading white space.)
        js cmcfr0
        ret                     ; If not, return failure.
cmcfr0: and ah,7FH                      ; Turn off the minus bit.
        cmp ah,esc                      ; Is it an escape?
        jne cmcfr2
        call beep
        mov ah,0
        mov cmaflg,ah           ; Turn off the action flag.
        mov bx,cmcptr           ; Move the pointer to before thee scape.
        dec bx
        mov cmcptr,bx
        mov cmdptr,bx
        dec cmccnt              ; Decremrnt the char count.
        jmp cmcfrm              ; Try again.
cmcfr2: cmp ah,'?'                      ; Curious?
        jne cmcfr3
        mov dx,offset cmin00	; Print some help
        call strout
        call newln		; Print a crlf
        mov dx,cmprmp           ; Reprint the prompt.
        call strout
        mov bx,cmdptr           ; Get the pointer into the buffer.
        mov ah,'$'              ; Put a $ there for printing.
        mov [bx],ah
        mov bx,cmcptr
        dec bx                  ; Decrement & save the buffer pointer.
        mov  cmcptr,bx
        mov dx,offset cmdbuf
        call strout
        mov ah,0                        ; Turn off the action flag.
        mov cmaflg,ah
        jmp repars              ; Reparse everything.
 
cmcfr3: cmp ah,ff                       ; Is it a form feed?
        jne cmcfr4
        call cmblnk             ; If so blank the screen.
cmcfr4: jmp rskp
 
;       This routine parses a keyword from the table pointed
;       to in DX.  The format of the table is as follows:
;
;       addr:   db      n       ; Where n is the # of entries in the table.
;               db      m       ; M is the size of the keyword.
;               db      'string$' ; Where string is the keyword.
;               dw      ab      ; Where ab is data to be returned.
;
;       The keywords must be in alphabetical order.


cmkeyw: mov cmhlp,bx            ; Save the help.
        mov cmptab,dx           ; Save the beginning of keyword table.
        mov bx,dx
        mov ch,[bx]                     ; Get number of entries in table.
        inc bx
	mov dx,cmdptr		; Save command pointer.
	mov cmsptr,dx		; Save pointer's here.
cmky1:  cmp ch,0                ; Any commands left to check?
        jne cmky2
	mov dx,offset cmer04	; Complain.   [1]      
	call strout
        ret                     ; Fail if not.
cmky2:  dec ch
	mov cl,0		; Keep track of how many chars read in so far.
        call cmgtch             ; Get a char.
        cmp ah,0                ; Do we have a terminator?
        jns cmky2x 
	jmp cmky4               ; Negative number means we do.
cmky2x: inc bx                  ; Point to first letter of keyword.
	inc cl			; Read in another char.
        mov al,[bx]                   
        cmp ah,'a'              ; Less than a?
        jl cmky21               ; If so, don't capitalize.
        cmp ah,'z'+1            ; More than z?
        jns cmky21
        and ah,137O             ; Capitalize the letter.
cmky21: cmp ah,al
	je cmky3
	jg cmky2y
        jmp cmky41              ; Fail if ah preceeds al alphabetically.
cmky2y: jmp cmky6               ; Not this keyword - try the next.
cmky3:  inc bx                  ; We match here, how 'bout next char?
        mov al,[bx]
        cmp al,'$'               ; End of keyword?
	jne cmky3x
        jmp cmky7                ; Succeed.
cmky3x:	mov dl,al		; Save al's char here.
        call cmgtch
	inc cl			; Read in another char.
	mov al,dl
	cmp ah,'a'
	jl cmky31
	cmp ah,'z'+1
	jns cmky31
	and ah,137O
cmky31: cmp ah,9BH		; Escape Recognition (escape w/minus bit on)?
	je cmky3y
	cmp ah,0BFH		; A question mark?    [3]
	je cmky3y
	cmp ah,0A0H		; A space?
	je cmky3y
	cmp ah,8DH		; Carriage return?
	je cmky3y
	jmp cmky38
cmky3y:	mov cmkptr,bx		; Save bx here.
	mov cmsiz,cx		; Save size info.
	mov cmchr,ah		; Save char for latter.
	call cmambg		; See if input is ambiguous or not.
	 jmp cmky32		; Succeeded (not ambiguous).
	mov ah,cmchr
	cmp ah,9BH		; Escape?
	je cmky3z
	jmp cmky41		; Fail.
cmky3z:	             		; Else, ring a bell.
	call beep
	mov bx,cmcptr		; Move pointer to before the escape.
	dec bx
	mov cmcptr,bx
	mov cmdptr,bx
	dec cmccnt		; Decrement char count.
	mov bx,cmkptr		; Failed - pretend user never typed ....
	mov cx,cmsiz		; ... in a char.
	dec cl			; Don't count the escape.
	dec bx
	mov cmaflg,0		; Reset the action flag.
	jmp cmky3		; Keep checking.
cmky32: mov cx,cmsiz		; Restore info.
	mov bx,cmkptr		; Our place in the keyword table.
	cmp cmchr,0A0H		; Space?
	je cmky35
	cmp cmchr,0BFH		; Question mark?     [3]
	je cmky35
	cmp cmchr,8DH		; Carriage return?
	je cmky35
	dec cmcptr		; Pointer into buffer of input.
	mov dx,cmcptr
cmky33:	mov ah,[bx]		; Get next char in keyword.
	cmp ah,'$'		; Are we done yet?
	jz cmky34
	mov di,dx
	mov [di],ah
	inc bx
	inc dx
	inc cmccnt
	jmp cmky33
cmky34:	mov ah,' '
	mov di,dx
	mov [di],ah		; Put a blank in the buffer.
	inc dx
	mov cx,cmcptr		; Remember where we were (for printing below).
	mov cmcptr,dx		; Update our pointers.
	mov cmdptr,dx
	mov di,dx
	mov byte ptr [di],'$'	; Add '$' for printing.
	mov dx,cx		; Point to beginning of filled in data.
	call strout
	inc bx			; Point to address we'll need.
	mov bx,[bx]
	mov cmaflg,0 		; Turn off action flag.
	jmp rskp

cmky35:	inc bx
	mov ah,[bx]		; Find end of keyword. 
	cmp ah,'$'
	jne cmky35	
	inc bx
	mov bx,[bx]		; Address of next routine to call.
;	mov cmaflg,0		; Zero the action flag.
	jmp rskp

cmky38:	cmp ah,al
        jne cmky6               ; Go to end of keyword and try next.
        jmp cmky3
           
cmky4:  and ah,7FH              ; Turn off minus bit.
        cmp ah,'?'              ; Need help?
        je cmky5
	cmp ah,' '		; Just a space - no error.
	je cmky51
	cmp ah,cr
	je cmky51
	cmp ah,tab
	je cmky51 
	cmp ah,esc		; Ignore escape?
	je cmky43
cmky41: 
        mov dx,offset cmer03
        call strout
        jmp prserr              ; Parse error - give up.

cmky43:	
	call beep
	mov bx,cmcptr
	dec bx
	mov cmcptr,bx
	mov cmdptr,bx
	dec cmccnt		; Don't count the escape.
	mov cmaflg,0		; Reset action flag.
	inc ch			; Account for a previous 'dec'.
	jmp cmky1		; Start over.

cmky5:
        mov dx,cmhlp            ; Print the help text.
        call strout
	call newln
	mov dx,offset kerm
	call strout
	mov bx,cmdptr		; Get pointer into buffer.
	mov al,'$'
	mov [bx],al		; Add dollar sign for printing.
	mov dx,offset cmdbuf
	call strout
	dec cmcptr		; Don't keep it in the buffer.
	dec cmccnt		; Don't conut it.
	mov cmaflg,0            ; Turn off the action flag.
        jmp repars

cmky51:	jmp prserr

cmky6:  inc bx                  ; Find end of keyword.
        mov al,[bx]
        cmp al,'$'
        jne cmky6             
        inc bx                  ; Beginning of next command.
        inc bx
        inc bx
	mov dx,cmsptr		; Get old cmdptr.
	mov cmdptr,dx		; Restore.
	mov cmsflg,0FFH
        jmp cmky1               ; Keep trying.

cmky7:  call cmgtch             ; Get char.
	cmp ah,0
	js cmky71		; Ok if a terminator.
        dec bx
        jmp cmky6               ; No match - try next keyword.
cmky71: inc bx                  ; Get necessary data.
        mov bx,[bx]
	cmp ah,9BH		; An escape?
	jne cmky72
	mov dx,offset prsp      ; Print a space.
	call strout
	mov di,cmcptr
	dec di
	mov ah,20H
	mov [di],ah		; Replace escape char with space.
	mov cmaflg,0
	mov cmsflg,0FFH		; Pretend they typed a space.
cmky72: jmp rskp

; See if keyword is unambiguous or not from what the user has typed in.

cmambg:	cmp ch,0		; Any keywords left to check?
	jne cmamb0
	ret			; If not then not ambiguous.
cmamb0:	inc bx			; Go to end of keyword ...
	mov al,[bx]		; So we can check the next one.
	cmp al,'$'
	jne cmamb0
	add bx,4		; Point to start of next keyword.
	dec cl			; Don't count escape.
	mov dx,cmsptr		; Buffer with input typed by user.
cmamb1:	mov ah,[bx]		; Keyword char.	
	mov di,dx
	mov al,[di]		; Input char.
	cmp al,'a'		; Do capitalizing.
	jl cmam11
	cmp al,'z'+1
	jns cmam11
	and al,137O
cmam11:	cmp ah,al		; Keyword bigger than input (alphabetically)?
	jle cmamb2		; No - keep checking.
	ret			; Yes - not ambiguous.
cmamb2:	inc bx			; Advance one char.
	inc dx
	dec cl
	jnz cmamb1
	jmp rskp		; Fail - it's ambiguous.

cmifil: mov bx,dx               ; Get the fcb address in bx.
        mov cmfcb,bx            ; Save it.
        mov ch,0                ; Initialize char count.
        mov ah,0
        mov [bx],ah                     ; Set the drive to default to current.
	inc bx
        mov cmfcb2,bx
        mov cl,' '
cmifi0: mov [bx],cl                     ; Blank the FCB.
        inc bx
        inc ah
        cmp ah,0BH                      ; Twelve?
        jl cmifi0
cmifi1: call cmgtch             ; Get another char.
        cmp ah,0                        ; Is it an action character.
        jns cmifi2
        and ah,7FH                      ; Turn off the action bit.
        cmp ah,'?'                      ; A question mark?
        jne cmif12
        mov al,0
        mov cmaflg,al           ; Blank the action flag.
        dec cmcptr           ; Decrement the buffer pointer.
	dec cmccnt		; Decrement count.
;        jmp cmifi8              ; Treat like any other character.
	mov dx,offset filhlp	; Help  message.
	call strout
	call newln  
	mov dx,offset kerm
	call strout
	mov bx,cmdptr
	mov al,'$'
	mov [bx],al		; Put in dollar sign for printing.
	mov dx,offset cmdbuf
	call strout
	jmp repars
cmif12: cmp ah,esc                      ; An escape?
        jne cmif13
        mov ah,0
        mov cmaflg,ah           ; Turn off the action flag.
        call beep       ; Ring the bell.
        mov bx,cmcptr           ; Move the pointer to before the escape.
        dec bx
        mov cmcptr,bx
        mov cmdptr,bx
        dec cmccnt              ; Decrement char count.
        jmp repars
cmif13: mov ah,ch                       ; It must be a terminator.
        cmp ah,0                        ; Test the length of the file name.
	jnz cmf3x
        jmp cmifi9               ; If zero complain.
cmf3x:  cmp ah,0DH
        js cmf3y
	jmp cmifi9              ; If too long complain.
cmf3y:  jmp rskp                ; Otherwise we have succeeded.
cmifi2: cmp ah,'.'
        jne cmifi3
        inc ch
        mov ah,ch
        cmp ah,1H                       ; Any chars yet?
      	jnz cmf2x
	jmp cmifi9               ; No, give error.
cmf2x:  cmp ah,0AH                      ; Tenth char?
      	js cmf2y
	jmp cmifi9              ; Past it, give an error.
cmf2y:  mov dl,9H
        mov dh,0
        mov bx,cmfcb
        add bx,dx               ; Point to file type field.
        mov cmfcb2,bx
        mov ch,9H               ; Say we've gotten nine.
        jmp cmifi1              ; Get the next char.
cmifi3: cmp ah,':'
        jne cmifi4
        inc ch
        cmp ch,2H                       ; Is it in right place for a drive?
	je cmif3x
        jmp cmifi9              ; If not, complain.
cmif3x: mov ch,0		; Reset char count.
	mov bx,cmfcb2
	sub bx,1
        mov ah,[bx]                     ; Get the drive name.
        sub ah,'@'              ; Get the drive number.
	mov cmfcb2,bx
 	mov bx,cmfcb
        mov [bx],ah                     ; Put it in the fcb.
        jmp cmifi1
cmifi4: cmp ah,'*'
        jne cmifi7
        mov ah,ch
        cmp ah,8H                       ; Is this in the name or type field?
        jz cmifi9               ; If its where the dot should be give up.
        jns cmifi5              ; Type.
        mov cl,8H               ; Eight chars.
        jmp cmifi6
cmifi5: mov cl,0CH              ; Three chars.
cmifi6: mov wldflg,0FFH		; Remember we had a wildcard.
	mov bx,cmfcb2           ; Get a pointer into the FCB.
        mov ah,'?'
        mov [bx],ah                     ; Put a question mark in.
        inc bx
        mov cmfcb2,bx
        inc ch
        mov ah,ch
        cmp ah,cl
        jl cmifi6               ; Go fill in another.
        jmp cmifi1              ; Get the next char.
cmifi7: cmp ah,03DH		; Equals sign (wildcard)?
	jne cmif7x
	mov ah,'?'
	mov wldflg,0FFH		; Say we have a wildcard.
	jmp cmifi8		; Put into FCB.
cmif7x:	cmp ah,'0'
        jl cmif8x
        cmp ah,'z'+1
        jns cmif8x
        cmp ah,'A'                      ; Don't capitalize non-alphabetics.
        jl cmifi8
        and ah,137O             ; Capitalize.
cmifi8: mov bx,cmfcb2           ; Get the pointer into the FCB.
        mov [bx],ah                     ; Put the char there.
        inc bx
        mov cmfcb2,bx
        inc ch
        jmp cmifi1

cmif8x:	push es
	mov cx,ds
	mov es,cx		; Scan uses ES register.
	mov di,offset spchar    ; Special chars.
	mov cx,20		; Twenty of them.
	mov al,ah		; Char is in al.
	repnz scasb		; Search string for input char.
	cmp cx,0		; Was it there?
	pop es
	jnz cmifi8
 
cmifi9: 
        mov dx,offset cmer02
        call strout
        ret
 
cmofil: jmp cmifil              ; For now, the same as CMIFI.

; Parse arbitrary text up to a CR.  Put chars into data buffer sent to
; the host (pointed to by BX).   Return updated pointer in BX and 
; input size in AH.

cmtext:	mov cmptab,bx		; Save pointer to data buffer.   [8 start]
	mov cl,0		; Init the char count.
cmtxt1:	call cmgtch		; Get a char.
	cmp ah,0		; Terminator?
	jns cmtxt5		; Nope, put into the buffer.
	and ah,07FH
	cmp ah,esc		; An escape?
	jne cmtxt2
	call beep  		; Ring a bell.
	mov cmaflg,0		; Reset action flag.
	dec cmcptr		; Move pointer to before the escape.
	dec cmdptr
	dec cmccnt		; Decrement count.
	jmp cmtxt1		; Try again.
cmtxt2:	cmp ah,'?'		; Asking a question?
	jz cmtxt3
	cmp ah,ff		; Formfeed?
	jne cmtx2x
	call cmblnk
cmtx2x: mov ah,cl		; Return count in AH.
	mov bx,cmptab		; Return updated pointer.
	jmp rskp
cmtxt3:	mov cmaflg,0		; Reset action flag to zero.
cmtxt5: inc cl			; Increment the count.
	mov bx,cmptab		; Pointer into destination array.
	mov [bx],ah		; Put char into the buffer.
	inc bx
	mov cmptab,bx
	jmp cmtxt1					; [8 end]

cminbf: push dx
        push bx
        mov cx,dx               ; Save value here too.
        mov ah,cmaflg           ; Is the action char flag set?
        cmp ah,0
	je cminb1
        jmp cminb9              ; If so get no more chars.
cminb1: inc cmccnt              ; Increment the char count.
	call hpconin		; [17] HP150 adaptation
	push ax
	call hpconout
	pop ax
	mov ah,al		; Keep char in 'ah'.
        mov bx,cmcptr           ; Get the pointer into the buffer.
        mov [bx],ah                     ; Put it in the buffer.
        inc bx
        mov cmcptr,bx
        cmp ah,25O                      ; Is it a ^U?
        jne cminb2
cmnb12:
	mov dx,offset clrlin
	call strout
	mov al,'K'  	   	   ; [17] clear line
        call quick_esc
        mov dx,cmprmp           ; Print the prompt.
        call strout
        mov bx,offset cmdbuf
        mov cmcptr,bx           ; Reset the point to the start.
	mov cmccnt,0	           ; Zero the count.
        mov dx,cx               ; Preserve original value of dx.
        jmp repars              ; Go start over.
cminb2: cmp ah,10O                      ; Or backspace?
        jz cminb3
        cmp ah,del                      ; Delete?
        jne cminb4
        mov dx,offset delstr	; Print the delete string
        call strout
cminb3: mov ah,cmccnt           ; Decrement the char count by two.
        dec ah
        dec ah
        cmp ah,0                        ; Have we gone too far?
        jns cmnb32              ; If not proceed.
        call beep               ; Ring the bell.
        jmp cmnb12              ; Go reprint prompt and reparse.
cmnb32: mov cmccnt,ah           ; Save the new char count.
        mov dx,offset clrspc	; Erase the char
        call strout
        mov bx,cmcptr           ; Get the pointer into the buffer.
        dec bx                  ; Back up in the buffer.
        dec bx
        mov cmcptr,bx
        jmp repars              ; Go reparse everything.
cminb4: cmp ah,'?'                      ; Is it a question mark.
        jz cminb6
        cmp ah,esc                      ; Is it an escape?
        jz cminb8
        cmp ah,cr                       ; Is it a carriage return?
        jz cminb5
        cmp ah,lf                       ; Is it a line feed?
        jz cminb5
        cmp ah,ff                       ; Is it a formfeed?
        jne cminb7
        call cmblnk
	call locate
cminb5: mov ah,cmccnt           ; Have we parsed any chars yet?
        cmp ah,1
	jnz cminb6
        jmp prserr               ; If not, just start over.
cminb6: mov ah,0FFH             ; Set the action flag.
        mov cmaflg,ah
        jmp cminb9
cminb7: jmp cminb1		; Get another char.

cminb8:             		; Don't print the escape char.
	mov dx,offset escspc
	call strout
	jmp cminb6
 
cminb9: pop bx
        pop dx
        ret
 
cmgtch: push cx
	push bx
        push dx
cmgtc1: mov ah,cmaflg
        cmp ah,0                        ; Is it set.
        jne cmgt10
        call cminbf             ; If the action char flag is not set get more.
cmgt10: mov bx,cmdptr           ; Get a pointer into the buffer.
        mov ah,[bx]                     ; Get the next char.
        inc bx
        mov cmdptr,bx
        cmp ah,' '                      ; Is it a space?
        jz cmgtc2
        cmp ah,tab                      ; Or a tab?
        jne cmgtc3
cmgtc2: mov ah,cmsflg           ; Get the space flag.
        cmp ah,0                        ; Was the last char a space?
        jne cmgtc1              ; Yes, get another char.
        mov ah,0FFH             ; Set the space flag.
        mov cmsflg,ah
        mov ah,' '
        pop dx
        pop bx
        jmp cmgtc5
cmgtc3: mov al,0
        mov cmsflg,al           ; Zero the space flag.
        pop dx
        pop bx
        cmp ah,esc
        jz cmgtc5
        cmp ah,'?'                      ; Is the user curious?
        jz cmgtc4
        cmp ah,cr
        jz cmgtc4
        cmp ah,lf
        jz cmgtc4
	cmp ah,ff
	je cmgtc4
	pop cx
	ret			; Not an action char, just return.
cmgtc4: dec cmdptr
cmgtc5: or ah,80H		; Make the char negative to indicate
	pop cx
	ret			; it is a terminator.
CMND	ENDP

; This routine blanks the screen.

CMBLNK	PROC	NEAR		; [17] clear screen
	call locate
	mov al,'J'		
	call quick_esc
	ret
CMBLNK  ENDP

; Cursor control.

LOCATE  PROC	NEAR
	mov al,'h'		; [17] home up
	call quick_esc
	ret
LOCATE  ENDP
;
; start of HP150 specific code		[17]
;
; special screen handling functions implemented through the ioctl
; function.
;
;  entry: cx -> number of bytes in buffer
;         ds:si -> points to buffer
;
AIOS proc near
  mov ah,e_ioctl
  mov al,3
  xor bx,bx             ; stdin = 0
  mov dx,si
  int 21h
  ret
AIOS endp

;
; set_curpos -> set the cursor location relative to the screen.
;
; entry: dx has requested cursor position. dh = row, dl = column
;
; AIOS wants the following buffer:
;
; (0,17)        cursor position
;  mode         one byte 99H for relative placement
; (column)      full word column
; (row)         full word row
;
SET_CURPOS proc near 
  push bx
  push cx
  push dx
  mov si,offset aios_buf
  mov word ptr [si],e_cursor_position
  mov word ptr [si+2],99H
  mov ah,0
  mov al,dl
  mov [si+3],ax
  mov al,dh
  mov [si+5],ax
  mov cx,7
  call aios
  pop dx
  pop cx
  pop bx
  ret
SET_CURPOS endp
;
; quick_esc -> execute a one character firmware escape sequence
;              via aios
;
; entry: al -> one char escape sequence value
;
; aios wants:
;
; (0,16) execute escape sequence
; (-,op_char)
;

QUICK_ESC proc near
  push bx
  push cx
  push dx
  mov si,offset aios_buf
  mov word ptr [si],e_quick_esc
  mov [si+2],al
  mov cx,3
  call aios
  pop dx
  pop cx
  pop bx
  ret
QUICK_ESC endp

;
; hpdcout -> send one char out data comm
;
; entry: al -> char to send
;
HPDCOUT proc near
  push bx
  push cx
  push dx
  mov byte ptr tempbuf,al
  mov dx,offset tempbuf
  mov ah,40h
  mov cx,1
  mov bx,dc_handle
  int 21H
  pop dx
  pop cx
  pop bx
  ret
HPDCOUT endp
;
; hpdcin  ->read one char from data comm.
; This routine is optimized by using the IOCTL fast read function.
;
; dcbuf db e_dc_buflen dup(?)
; dcchar_cnt dw 0
; dcchar_ptr dw 0
;
; exit: al = char if successful
;       carry set if no data  
;       carry clear if data
HPDCIN proc near
  push bx
  push cx
  push dx
  mov si,dcchar_ptr
  cmp si,dcchar_cnt
  jnz hpdcin10    
;
; here if buffer empty, call for more.
  mov dcchar_ptr,0
  mov ah,e_ioctl
  mov bx,dc_handle
  mov cx,e_dc_buflen
  mov dx,offset dcbuf
  mov al,2
  int 21h
  mov dcchar_cnt,ax
  cmp ax,0
  jnz hpdcin10          ; return carry if no data
  stc
  pop dx
  pop cx
  pop bx
  ret
;
hpdcin10:
  mov si,dcchar_ptr
  mov al,dcbuf[si]
  inc dcchar_ptr
  clc
  pop dx
  pop cx
  pop bx
  ret
HPDCIN endp
;
; open_dc -> open device COM1 and prepare for 8 bit, transparent
;            access.
;
;  initialize the keyboard to keycode mode at the same time.
;
OPEN_DC proc near
  mov dx,offset dc_name
  mov ah,3cH
  mov cx,2              ; read/write attribute
  int 21h
; @@@@@@ check error status ?
  mov dc_handle,ax
; now set raw mode on
  mov  bx,ax
  mov ah,e_ioctl
  mov al,0
  int 21h
  xor dh,dh
  or  dl,20h  
  mov bx,dc_handle
  mov ah,e_ioctl
  mov al,1       
  int 21h
;
; now initialize keyboard
;
; now set raw mode on
  xor bx,bx             ; stdin handle = 0
  mov ah,e_ioctl
  mov al,0
  int 21h
  xor dh,dh
  or  dl,20H  
  xor bx,bx             ; stdin handle = 0
  mov ah,e_ioctl
  mov al,1
  int 21h
; keycode on
  mov si,offset tempbuf
  mov word ptr [si],e_keycode_mode
  mov word ptr [si+2],1
  mov cx,3 
  call aios
;
; set all keys to normal action
;
  mov si,offset tempbuf
  mov word ptr [si],e_defkey
  mov word ptr [si+2],0
  mov word ptr [si+4],0feh
  mov cx,6
  call aios
;
; now set break to intercept
;
  mov si,offset tempbuf
  mov word ptr [si],e_defkey
  mov word ptr [si+2],2
  mov word ptr [si+4],e_break
  mov cx,6
  call aios
;
  ret
OPEN_DC endp
;
; close_dc -> shut down data comm and turn keycode mode off
;
CLOSE_DC proc near
  mov ah,3eh
  mov bx,dc_handle
  int 21h
;
  mov si,offset tempbuf
  mov word ptr [si],e_keycode_mode
  mov word ptr [si+2],0
  mov cx,3
  call aios
;
; clear raw mode
  xor bx,bx             ; stdin handle = 0
  mov ah,e_ioctl
  mov al,0
  int 21h
  and dx,000dfh
  xor bx,bx             ; stdin handle = 0
  mov ah,e_ioctl
  mov al,1
  int 21h
  ret
CLOSE_DC endp
;
;
; dc_ioctl set some data comm ioctl option
;
; entry: al has function code
;
DC_IOCTL proc near
  mov ah,8h
  mov tempbuf,ax
  mov dx,offset tempbuf
  mov ah,e_ioctl
  mov al,3
  mov bx,dc_handle
  mov cx,2
  int 21h
  ret
DC_IOCTL endp
;
; send_break -> send the break signal out data comm
;
SEND_BREAK proc near
  mov al,e_send_break
  jmp  dc_ioctl
SEND_BREAK endp
;
; dc_transparent -> disable firmware handshaking
;
DC_TRANSPARENT proc near
  mov al,e_8_bit_mode
  call dc_ioctl
  mov al,e_transparent_mode
  jmp  dc_ioctl
DC_TRANSPARENT  endp
;
; dc_handshake -> enable firmware handshaking
;
DC_HANDSHAKE proc near
  mov al,e_7_bit_mode
  call dc_ioctl
  mov al,e_handshake_mode
  jmp  dc_ioctl
DC_HANDSHAKE endp
;
;
; conditionally read a char from the keyboard.
;
; exit: set carry if no data  
;       clear carry if data.
;       all keycodes except break are "normal" and will
;       execute immediately. This routine will execute the
;       break key.
HPCONSTAT proc near
  push bx
  push cx
  push dx
  mov ah,e_ioctl        ; call ioctl status
  xor bx,bx             ; stdin = 0
  mov al,6
  int 21h
  cmp al,0
  jz hpconstat90         ; return carry if no data
;
  mov dx,offset tempbuf ; read four bytes of keycode
  mov cx,4
  mov ah,3fh
  xor bx,bx             ; stdin = 0
  int 21h
  mov al,byte ptr tempbuf+2      ; keycode in al
  mov cl,byte ptr tempbuf        ; qualifier in cx
  test cl,80h
  jnz hpconstat_special
  pop dx
  pop cx
  pop bx
  clc
  ret
;
hpconstat_special:
  cmp al,e_break        ; only expected special is break
  jnz hpconstat90
  call send_break
hpconstat90:
  pop dx
  pop cx
  pop bx
  stc
  ret
HPCONSTAT endp
;
; wait for a char from the keyboard.
;   exit: al has char, it has been echoed to screen.
;
HPCONIN proc near
  call hpconstat
  jc hpconin    
  ret
HPCONIN endp
;
; write a char to the screen
;
; entry: al has char
;
HPCONOUT proc near
  push ax
  push dx
  mov dl,al
  mov ah,6
  int 21h
  pop dx
  pop ax
  ret
HPCONOUT endp
;
; end of HP150 specific code
;
; utility routines
;
BEEP proc near
  mov al,bell
  call hpconout
  ret 
BEEP endp
;
; entry -> dx -> points to $ terminated string
;
STROUT proc near
  mov ah,prstr
  int dos
  ret
STROUT endp
;
; print CR LF
;
NEWLN proc near
  mov dx,offset crlf
  call strout
  ret
NEWLN endp
;
MAIN 	ENDS			; End of code section.
	END	START

