;
; +++++++++++++++++++++++++++
; +++ KERMIT Send command +++
; +++++++++++++++++++++++++++
;
send:
	ld	a,cmifi		; Parse an input file spec.
	ld	de,fcb		; Give the address for the FCB.
	call	comnd
	 jp	kermit		; Give up on bad parse.
	ld	a,false
	ld	(mfflg1),a	; Set 1st time
	call	cfmcmd
	call	mfname		; Handle (multi) files
	jr	nc,send14	; Got a valid file-name
	ld	de,erms15
	call	error3		; Display error msg.
	jp	kermit
send14:
	call	init		; Clear the line and
				; Initialize the buffers.
	xor	a
	ld	(pktnum),a	; Set the packet number to zero.
	ld	(numtry),a	; Set the number of tries to zero.
	ld	(wrn8),a	; We haven't sent the
				; 8-bit-lost warning
	ld	hl,0
	ld	(numpkt),hl	; Set the number of packets to zero.
	ld	(numrtr),hl	; Set the number of retries to zero.
	ld	de,scrnrt	; Position cursor
	call	prtstr
	ld	hl,0
	call	nout		; Write the number of retries.
	ld	a,'1'		; Reset to use single character checksum
	ld	(curchk),a	; For startup
	ld	a,'S'
	ld	(state),a	; Set the state to receive initiate.
;
; SEND state table switcher
;
send2:
	ld	de,scrnp	; Position cursor
	call	prtstr
	ld	hl,(numpkt)
	call	nout		; Write the packet number.
	ld	a,(state)	; Get the state.
	cp	'D'		; Are we in the data send state?
	jr	nz,send3
	call	sdata
	jr	send2
send3:
	cp	'F'		; Are we in the file send state?
	jr	nz,send4
	call	sfile		; Call send file.
	jr	send2
send4:
	cp	'Z'		; Are we in the EOF state?
	jr	nz,send5
	call	seof
	jr	send2
send5:
	cp	'S'		; Are we in the send initiate state?
	jr	nz,send6
	call	sinit
	ld	a,(state)	; Get state back
	cp	'F'		; Into file send state yet?
	jr	nz,send2	; No
	ld	de,inms23	; Yes, print sending...
	call	finmes
	jr	send2
send6:
	cp	'B'		; Are we in the eot state?
	jr	nz,send7
	call	seot
	jr	send2
send7:
	cp	'C'		; Are we in the send complete state?
	jr	nz,send8	; No...
	ld	de,infms3	; Yes, write "Complete" message.
	ld	a,(czseen)	; Or was it interrupted?
	or	a
	jr	z,send7a	; No.
	ld	de,inms13	; Yes, then say "Interrupted" instead.
send7a:
	call	finmes
	jp	kermit
send8:
	ld	de,infms4	; Print	message.
	jr	send7a
;
; State S: Send initiate
;
sinit:
	ld	a,(numtry)	; Get the number of tries.
	cp	imxtry		; Have we reached the
				; Maximum number of tries?
	jr	c,sinit2
	ld	de,erms14
	jp	errabo		; Change the state to abort.
sinit2:
	inc	a		; Increment it.
	ld	(numtry),a	; Save the updated number of tries.
	ld	a,'1'		; Reset to use single character checksum
	ld	(curchk),a	; For startup
	ld	a,(chktyp)	; Get our desired block check type
	ld	(inichk),a	; Store so we tell other end
	ld	hl, data	; Get a pointer to our data block.
	call	rpar		; Set up the parameter information.
	ld	(argblk+1),a	; Save the number of arguments.
	ld	a,(numpkt)	; Get the packet number.
	ld	(argblk),a
	ld	a,'S'		; Send initiate packet.
	call	spack		; Send the packet.
	 jp	abort		; Failed, abort.
	call	rpack		; Get a packet.
	 jp	r		; Trashed packet don't
				; Change state, retry.
	cp	'Y'		; ACK?
	jr	nz,sinit3	; If not try next.
	call	compp
	ret	nz		; If not try again.
	call	countp
	ld	a,(argblk+1)	; Get the number of pieces of data.
	ld	hl,data		; Pointer to the data.
	call	spar		; Read in the data.
	ld	a,(numtry)	; Get the number of tries.
	ld	(oldtry),a	; Save it.
	xor	a
	ld	(numtry),a	; Reset the number of tries.
	ld	a,(inichk)	; Get the agreed upon block check type
	ld	(curchk),a	; Store as type to use for packets now
	ld	a,'F'		; Set the state to file send.
	ld	(state),a
	call	getfil		; Open the file.
	 jp	abort		; Something is wrong, die.
	ret
sinit3:
	cp	'N'		; NAK?
	jp	nz,errfil	; If not see if its an error.
	call	update
	ret	nz
	xor	a
	ld	(numtry),a	; Reset number of tries.
	ld	a,'F'		; Set the state to file send.
	ld	(state),a
	ret
;
; State F: Send file header
;
sfile:
	ld	a,(numtry)	; Get the number of tries.
	cp	maxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,sfile1
	ld	de,erms14
	jp	errabo		; Change the state to abort.
sfile1:
	inc	a		; Increment it.
	ld	(numtry),a	; Save the updated number of tries.
	xor	a		; Clear A
	ld	(czseen),a	; No control-Z or X seen
	ld	hl, data	; Get a pointer to our data block.
	ld	(datptr),hl	; Save it.
	ld	hl,FCB+_NAM	; Pointer to the file name in the FCB.
	ld	(fcbptr),hl	; Save position in FCB.
	ld	b,0		; No chars yet.
	ld	c,0
sfil11:
	ld	a,b
	cp	8		; Is this the ninth char?
	jr	nz,sfil12	; If not proceed.
	ld	a,'.'		; Get a dot.
	ld	hl,(datptr)
	ld	(hl),a		; Put the char in the data packet.
	inc	hl
	ld	(datptr),hl	; Save position in data packet.
	inc	c
sfil12:
	inc	b		; Increment the count.
	ld	a,b
	cp	12		; Twelve?
	jr	nc,sfil13
	ld	hl,(fcbptr)
	ld	a,(hl)
	and	nomsb		; Turn off CP/M high bits.
	inc	hl
	ld	(fcbptr),hl	; Save position in FCB.
	cp	' '+1		; Is it a good character?
	jr	c,sfil11	; If not get the next.
	ld	hl,(datptr)
	ld	(hl),a		; Put the char in the data packet.
	inc	hl
	ld	(datptr),hl	; Save position in data packet.
	inc	c
	jr	sfil11		; Get another.
sfil13:
	ld	a,c		; Number of char in file name.
	ld	(argblk+1),a
	ld	hl,(datptr)
	ld	(hl),eot	; Put in sign for printing.
	call	prFN		; Print the file name
	ld	a,(pktnum)	; Get the packet number.
	ld	(argblk),a
	ld	a,'F'		; File header packet.
	call	spack		; Send the packet.
	 jp	abort		; Failed, abort.
	call	rpack		; Get a packet.
	 jp	r		; Trashed packet don't
				; Change state, retry.
	cp	'Y'		; ACK?
	jr	nz,sfile2	; If not try next.
	call	compp
	ret	nz		; If not hold out for the right one.
sfil14:
	call	countp
	ld	a,(numtry)	; Get the number of tries.
	ld	(oldtry),a	; Save it.
	xor	a
	ld	(numtry),a	; Reset the number of tries.
sfil15:
	xor	a		; Get a zero.
	ld	(FCB+_CR),a	; Set the record number to zero.
	ld	(eoflag),a	; Indicate not EOF.
	ld	a,true
	ld	(filflg),a	; Indicate file buffer empty.
	call	gtchr
	 jp	sfil16		; Error go see if its EOF.
	jr	sfil17		; Got the chars, proceed.
sfil16:
	cp	true		; Is it EOF?
	jp	nz,abort	; If not give up.
	ld	a,'Z'		; Set the state to EOF.
	ld	(state),a
	ret
sfil17:
	ld	(size),a	; Save the size of the data gotten.
	ld	a,'D'		; Set the state to data send.
	ld	(state),a
	ret
sfile2:
	cp	'N'		; NAK?
	jp	nz,errfil	; Try if error packet.
	call	update		; Update the number of retries.
	jr	z,sfil14	; Just as good as a ACK
				; Go to the ACK code.
	ret
;
; State D: Send data
;
sdata:
	ld	a,(numtry)	; Get the number of tries.
	cp	maxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,sdata1
	ld	de,erms14
	jp	errabo		; Change the state to abort.
sdata1:
	inc	a		; Increment it.
	ld	(numtry),a	; Save the updated number of tries.
	ld	hl,data		; Get a pointer to our data block.
	ld	(datptr),hl	; Save it.
	ld	hl,filbuf	; Pointer to chars to be sent.
	ld	(cbfptr),hl	; Save position in char buffer.
	ld	b,1		; First char.
sdat11:
	ld	hl,(cbfptr)
	ld	a,(hl)
	inc	hl
	ld	(cbfptr),hl	; Save position in char buffer.
	ld	c,a		; Preserve character temporarily
	ld	a,(quot8)	; Doing eighth-bit quoting?
	or	a
	ld	a,c		; Restore char
	jr	nz,sdat4	; Using eighth-bit quoting,
				; No warning
	ld	a,(parity)	; Get parity
	cp	parnon		; None?
	ld	a,c		; Restore character
	jr	z,sdat4		; No parity, leave char alone
	ld	a,(wrn8)	; Look at warning flat
	or	a		; Have we already given the
				; Warning?
	jr	nz,sdat5	; Yes, skip this
	ld	a,c		; Restore character...
	and	msb		; Examine parity
	jr	z,sdat5		; No parity, no warning.
	call	parwrn		; ...print warning - parity lost
	ld	a,true		; Remember that we sent the message
	ld	(wrn8),a
sdat5:
	ld	a,c		; Restore character again
	and	nomsb		; Strip parity so not checksummed
sdat4:
	ld	hl,(datptr)
	ld	(hl),a		; Put the char in the data packet.
	inc	hl
	ld	(datptr),hl	; Save position in data packet.
	inc	b		; Increment the count.
	ld	a,(size)	; Get the number of chars in char buffer
	cp	b		; Have we transfered that many?
	jr	nc,sdat11	; If not get another.
	ld	a,(size)	; Number of char in char buffer.
	ld	(argblk+1),a
	ld	a,(pktnum)	; Get the packet number.
	ld	(argblk),a
	ld	a,'D'		; Data packet.
	call	spack		; Send the packet.
	 jp	abort		; Failed, abort.
	call	rpack		; Get a packet.
	 jp	r		; Trashed packet don't
				; Change state, retry.
	cp	'Y'		; ACK?
	jr	nz,sdata2	; If not try next.
	call	compp
	ret	nz		; If not hold out for the right one.
	ld	a,(argblk)	; Get the packet number back
	call	countp
	ld	a,(numtry)	; Get the number of tries.
	ld	(oldtry),a	; Save it.
	xor	a
	ld	(numtry),a	; Reset the number of tries.
	ld	a,(argblk+1)	; Get the data length
	cp	1		; Check if only 1 character?
	jr	nz,sdat15	; If not, just continue
	ld	a,(data)	; Got one character, get it from data
	cp	'Z'		; Want to abort entire stream?
	jr	nz,sdat14	; If not, check for just this file
	ld	(czseen),a	; Yes, remember it
sdat14:
	cp	'X'		; Desire abort of current file?
	jr	nz,sdat15	; If not, just continue
	ld	(czseen),a	; Yes, remember that
sdat15:
	ld	a,(czseen)	; Also get control-Z flag
	or	a		; Check if either given
	jr	z,sdat12	; If neither given, continue
	ld	a,'Z'		; Change state to EOF
	ld	(state),a
	ret			; And return
sdat12:
	call	gtchr
	 jp	sdat13		; Error go see if its EOF.
	ld	(size),a	; Save the size of the data gotten.
	ret
sdat13:
	cp	true		; Is it EOF?
	jp	nz,abort	; If not give up.
	ld	a,'Z'		; Set the state to EOF.
	ld	(state),a
	ret
sdata2:
	cp	'N'		; NAK?
	jp	nz,errfil	; See if is an error packet.
	call	update		; Update the number of retries.
	jr	z,sdat12	; Just as good as a ACK
				; Go to the ACK code.
	ret
;
; State Z: Send EOF
;
seof:
	ld	a,(numtry)	; Get the number of tries.
	cp	maxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,seof1
	ld	de,erms14
	jp	errabo		; Change the state to abort.
seof1:
	inc	a		; Increment it.
	ld	(numtry),a	; Save the updated number of tries.
	ld	a,(pktnum)	; Get the packet number.
	ld	(argblk),a
	xor	a
	ld	(argblk+1),a	; No data.
	ld	a,(czseen)	; Check if C-Z or C-X typed
	or	a
	jr	z,seof14	; If not aborted, just keep going
	ld	a,'D'		; Tell other end to discard packet
	ld	(data),a	; Store in data portion
	ld	a,1		; One character
	ld	(argblk+1),a	; Store the length
seof14:
	ld	a,'Z'		; EOF packet.
	call	spack		; Send the packet.
	 jp	abort		; Failed, abort.
	call	rpack		; Get a packet.
	 jp	r		; Trashed packet don't change
				; State, retry.
	cp	'Y'		; ACK?
	jr	nz,seof2	; If not try next.
	call	compp
	ret	nz		; If not hold out for the right one.
seof12:
	call	countp
	ld	a,(numtry)	; Get the number of tries.
	ld	(oldtry),a	; Save it.
	xor	a
	ld	(numtry),a	; Reset the number of tries.
	ld	de,FCB
	call	close		; Close the file
;
; * Check if successful
;
	ld	a,(czseen)	; Desire abort of entire stream?
	cp	'Z'		; Desire abort of entire stream?
	jr	z,seof13	; If so, just give up now
	call	mfname		; Get the next file.
	jr	c,seof13	; No more.
	call	getfil		; And open it
	 jp	abort		; Something is wrong, die.
	xor	a		; Clear A
	ld	(czseen),a	; Since we have not aborted this file
	ld	a,'F'		; Set the state to file send.
	ld	(state),a
	ret
seof13:
	ld	a,'B'		; Set the state to EOT.
	ld	(state),a
	ret
seof2:
	cp	'N'		; NAK?
	jp	nz,errfil	; Try and see if its an error packet.
	call	update		; Update the number of retries.
	jr	z,seof12	; Just as good as a ACK
				; Go to the ACK code.
	ret
;
; State B: Send EOT
;
seot:
	ld	a,(numtry)	; Get the number of tries.
	cp	maxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,seot1
	ld	de,erms14
	jp	errabo		; Change the state to abort.
seot1:
	inc	a		; Increment it.
	ld	(numtry),a	; Save the updated number of tries.
	ld	a,(pktnum)	; Get the packet number.
	ld	(argblk),a
	xor	a
	ld	(argblk+1),a	; No data.
	ld	a,'B'		; EOF packet.
	call	spack		; Send the packet.
	 jp	abort		; Failed, abort.
	call	rpack		; Get a packet.
	 jp	r		; Trashed packet don't
				; Change state, retry.
	cp	'Y'		; ACK?
	jr	nz,seot2	; If not try next.
	call	compp
	ret	nz		; If not hold out for the right one.
seot12:
	call	countp
	ld	a,(numtry)	; Get the number of tries.
	ld	(oldtry),a	; Save it.
	xor	a
	ld	(numtry),a	; Reset the number of tries.
	ld	a,'C'		; Set the state to file send.
	ld	(state),a
	ret
seot2:
	cp	'N'		; NAK?
	jp	nz,errfil	; Is it error.
	call	update		; Update the number of retries.
	jr	z,seot12	; Just as good as a ACK
				; Go to the ACK code.
	ret
update:
	call	updrtr		; Update the number of retries.
	ld	a,(pktnum)	; Get the present packet number.
	inc	a		; Increment.
	ld	b,a
	ld	a,(argblk)	; Get the packet's number.
	cp	b		; Is the packet's number one
				; More than now?
	ret			; If not go try again.

