;
; ++++++++++++++++++++++++++++++++++++++
; +++ KERMIT RECEIVE and GET command +++
; ++++++++++++++++++++++++++++++++++++++
;
read:
	ld	de,data		; Where to put the text (if any.)
	ld	a,cmtxt
	call	comnd		; Get either some text or a confirm.
	 jp	kermt3		; Didn't get anything.
	or	a		; Get any chars?
	jr	z,read1		; Nope, just a regular send.
	ld	(argblk+1),a	; Store the number of chars.
	ex	de,hl		; Get pointer into HL.
	ld	(hl),eot	; Put in a sign for printing.
	call	init		; Clear the line and
				; Initialize the buffers.
	call	prFN		; Print the file name
	ld	a,'1'		; Start with single character checksum
	ld	(curchk),a	; Save the type
	xor	a		; Start a packet zero.
	ld	(argblk),a
	ld	a,'R'		; Receive init packet.
	call	spack		; Send the packet.
	 jp	kermt3		; Die!
	jr	read12
read1:
	call	init		; Clear the line and
read12:
	xor	a
	ld	(czseen),a	; Clear the ^X/^Z flag initially.
	ld	(remtxt),a	; Device flag ditto.
	ld	hl,0
	ld	(numpkt),hl	; Set the number of packets to zero.
	ld	(numrtr),hl	; Set the number of retries to zero.
	ld	(pktnum),a	; Set the packet number to zero.
	ld	(numtry),a	; Set the number of tries to zero.
	ld	de,scrnrt	; Position cursor
	call	prtstr
	ld	hl,0
	call	nout		; Write the number of retries.
	ld	a,'R'
	ld	(state),a	; Set the state to receive initiate.
;
; RECEIVE state table switcher.
;
read2:
	ld	de,scrnp	; Position cursor
	call	prtstr
	ld	hl,(numpkt)
	call	nout		; Write the current packet number.
	ld	a,(state)	; Get the state.
	cp	'D'		; Are we in the DATA receive state?
	jr	nz,read22
	call	rdata
	jr	read2
read22:
	cp	'X'		; F packet but not an F packet ?
	jr	nz,read3
	call	rfile		; 'Get' the file - do not open it
	jr	read2
read3:
	cp	'F'		; Are we in the FILE receive state?
	jr	nz,read4
	call	rfile		; Call receive file.
	jr	read2
read4:
	cp	'R'		; Are we in the Receive-Initiate state?
	jr	nz,read5
	call	rinit
	ld	a,(state)	; Get new state
	cp	'F'		; Went into receive state?
	jr	nz,read2	; No
	ld	de,inms24	; Yes, get receiving... message
	call	finmes		; Go print it
	jr	read2
read5:
	cp	'C'		; Are we in the Receive-Complete state?
	jr	nz,read6
	ld	de,infms3	; Put in "Complete" message.
	ld	a,(czseen)	; Or was it interrupted?
	or	a
	jr	z,read5a	; No.
	xor	a		; Yes, clear flag.
	ld	(czseen),a
	ld	de,inms13	; Issue "interrupted" message.
read5a:
	call	finmes		; Print completion message
				; In right place.
	jp	kermit
read6:
	ld	de,infms4	; Anything else is equivalent to "abort"
	jr	read5a
;
; State R: Receive init
;
rinit:
	ld	a,(numtry)	; Get the number of tries.
	cp	imxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,rinit2
	ld	de,ermes4
errabo:
	call	error3		; Move cursor and print an error message
	jp	abort		; Change the state to abort.
rinit2:
	inc	a		; Increment it.
	ld	(numtry),a	; Save the updated number of tries.
	ld	a,'1'		; Reset block check type to
				; Single character
	ld	(curchk),a	; Store as current type for
				; Initialization
	call	rpack		; Get a packet.
	 jp	nak		; Trashed packet: nak, retry.
	cp	'S'		; Is it a send initiate packet?
	jr	nz,rinit3	; If not see if its an error.
	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)	; Returned packet number.
				; (Synchronize them.)
	call	countp
	ld	a,(argblk+1)	; Get the number of arguments received.
	ld	hl,data		; Get a pointer to the data.
	call	spar		; Get the data into the proper variables
	ld	hl,data		; Get a pointer to our data block.
	call	rpar		; Set up the receive parameters.
	ld	(argblk+1),a	; Store the returned number of arguments
	ld	a,'Y'		; Acknowledge packet.
	call	spack		; Send the packet.
	 jp	abort		; Failed, abort.
	ld	a,(inichk)	; Now switch to agreed upon check-type
	ld	(curchk),a	; For all future packets
	ld	a,'F'		; Set the state to file send.
	ld	(state),a
	ret
rinit3:
	cp	'E'		; Is it an error packet.
	jp	nz,nak0		; If not NAK whatever it is.
rabort:
	call	error		; Fall into abort
	jp	abort
;
; State F: Receive file
;
rfile:
	ld	a,(numtry)	; Get the number of tries.
	cp	maxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,rfile1
	ld	de,ermes5
	jp	errabo		; Change the state to abort.
rfile1:
	inc	a		; Increment it.
	ld	(numtry),a	; Save the updated number of tries.
	call	rpack		; Get a packet.
	 jp	nak		; Trashed packet: nak, retry.
	cp	'S'		; Is it a send initiate packet?
	jr	nz,rfile2	; No, try next type.
	ld	a,(oldtry)	; Get the number of tries.
	cp	imxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,rfil12	; If not proceed.
	ld	de,ermes4
	jp	errabo		; Change the state to abort.
rfil12:
	inc	a		; Increment it.
	ld	(oldtry),a	; Save the updated number of tries.
	ld	a,(pktnum)	; Get the present packet number.
	dec	a		; Decrement.
	ld	b,a
	ld	a,(argblk)	; Get the packet's number
	cp	b		; Is the packet's number one
				; Less than now?
	jp	nz,nak0		; No, NAK and try again.
	call	updrtr		; Update the retry count.
	xor	a
	ld	(numtry),a	; Reset the number of tries.
	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,'Y'		; Acknowledge packet.
	call	spack		; Send the packet.
	 jp	abort		; Failed, abort.
	ret
rfile2:
	cp	'Z'		; Is it an EOF packet?
	jr	nz,rfile3	; No, try next type.
	ld	a,(oldtry)	; Get the number of tries.
	cp	maxtry		; Have we reached the maximum
				; Number of tries?
	jp	m,exrecv	; If not proceed.
	ld	de,ermes6
	jp	errabo		; Change the state to abort.
rfile3:
	cp	'F'		; Start of file?
	jr	nz,rfil3b
	call	compp
	jp	nz,nak0		; No, NAK it and try again.
	call	countp
	call	gofil		; Get a file to write to.
	 jp	abort
	call	init1		; Initialize all the buffers.
	ld	a,(numtry)	; Get the number of tries.
	ld	(oldtry),a	; Save it.
	call	ackp
	ld	a,'D'		; Set the state to data receive.
	ld	(state),a
	ld	a,(czseen)	; Check if we punted a file
	cp	'Z'		; And didn't want any more
	ret	z		; If that was the request, keep telling
				; Other end
	xor	a		; Otherwise, clear flag
				; (Control-X is only for one file)
	ld	(czseen),a	; And store the flag back
	ret
rfil3b:
	cp	'X'		; Start of file but not a file ?
	jr	nz,rfile4
	call	compp
	jp	nz,nak0		; No, NAK it and try again.
	call	countp
	ld	de,data		; lets write the filename to display
	ld	a,(argblk+1)	; get length
	and	a
	jr	z,rfil3c	; no length
	ld	l,a
	ld	h,0
	add	hl,de		; HL points to end
	ld	(hl),'$'	; Dollar terminate
	ld	de,data
	call	prtstr		; write it to the screen
	call	crlf		; then CRLF
rfil3c:
	ld	a,'D'		; expecting a D packet
	ld	(state),a
	ld	(remtxt),a	; set flag to NOT write to disk
	ret
rfile4:
	cp	'B'		; End of transmission.
	jr	nz,errfil
	call	compp
	jp	nz,nak0		; No, NAK it and try again.
	xor	a		; No data.
				; (Packet number already in argblk).
	ld	(argblk+1),a
	ld	a,'Y'		; Acknowledge packet.
	call	spack		; Send the packet.
	 jp	abort
	ld	a,'C'		; Set the state to complete.
	ld	(state),a
	ret
errfil:
	cp	'E'		; Is it an error packet.
	jp	nz,abort
	jp	rabort
;
; State D: Receive data
;
rdata:
	ld	a,(numtry)	; Get the number of tries.
	cp	maxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,rdata1
	ld	de,erms10
	jp	errabo		; Change the state to abort.
rdata1:
	inc	a		; Increment it.
	ld	(numtry),a	; Save the updated number of tries.
	call	rpack		; Get a packet.
	 jp	nak		; Trashed packet: nak, retry.
	cp	'D'		; Is it a data packet?
	jr	nz,rdata2	; No, try next type.
rdat11:
	call	compp
	jr	z,rdat14
	ld	a,(oldtry)	; Get the number of tries.
	cp	maxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,exrecv	; If not proceed.
	ld	de,erms10
	jp	errabo		; Change the state to abort.
exrecv:
	call	tryagn
	ret
rdat14:
	call	countp
	ld	a,(numtry)	; Get the number of tries.
	ld	(oldtry),a	; Save it.
	ld	a,(argblk+1)	; Get the length of the data.
	call	ptchr
	 jp	abort		; Unable to write out chars;abort.
	xor	a
	ld	(numtry),a	; Reset the number of tries.
	ld	(argblk+1),a	; No data.
				; (Packet number still in argblk.)
	ld	c,a		; Assume no data
	ld	a,(czseen)	; Check if control-X typed
	or	a		; .  .  .
	jr	z,rdat15	; Zero if not typed
	ld	c,a		; Get the type of character typed
	ld	a,1		; One data character
	ld	(argblk+1),a	; Save the count
	ld	a,c		; Get the possible data character
	ld	(data),a	; Store in data area
rdat15:
	ld	a,'Y'		; Acknowledge packet.
	call	spack		; Send the packet.
	 jp	abort
	ret
rdata2:
	cp	'F'		; Start of file?
	jr	nz,rdata3	; No, try next type.
	ld	a,(oldtry)	; Get the number of tries.
	cp	maxtry		; Have we reached the maximum
				; Number of tries?
	jr	c,exrecv	; If not proceed.
	ld	de,ermes5
	jp	errabo		; Change the state to abort.
rdata3:
	cp	'Z'		; Is it a EOF packet?
	jp	nz,errfil	; Try and see if its an error.
	call	compp
	jp	nz,nak0		; No, NAK it and try again.
	call	countp
	ld	a,(argblk+1)	; Get the data length
	cp	1		; Have one item?
	jr	nz,rdat33	; If not, ignore data
	ld	a,(data)	; Yes, get the character
	cp	'D'		; Is it a 'D' for discard?
	jr	z,rdat36	; If so, punt file
rdat33:
	ld	hl,(bufpnt)	; Get the dma pointer.
	ld	a,(chrcnt)	; Get the number of chars left
				; In the DMA.
rdat34:
	dec	a		; Lower the count.
	or	a
	jp	m,rdat35	; If full then stop.
	ld	(hl),'Z'-'@'	; Put in a ^Z for EOF.
	inc	hl		; Point to the next space.
	jr	rdat34
rdat35:
	call	outbuf		; Output the last buffer.
	 jp	abort		; Give up if the disk is full.
	ld	de,FCB
	call	close		; Close up the file
	xor	a		; Since we kept the file,
	ld	(czseen),a	; Don't say it was discarded.
rdat36:
	ld	a,(numtry)	; Get the number of tries.
	ld	(oldtry),a	; Save it.
	call	ackp
	ld	a,'F'
	ld	(state),a
	ret
;
; Send NAK
;
nak0:
	call	updrtr		; Update number of retries.
nak:
	ld	a,(pktnum)	; Get the packet number
				; We're waiting for.
	ld	(argblk),a
	xor	a		; No data.
	ld	(argblk+1),a
	ld	a,'N'		; NAK that packet.
	call	spack
	 jp	abort		; Give up.
	ret			; Go around again.

