;----------------------------------------------------------------------------
;  PCPUT - This CP/M program sends a file from the CP/M machine to a PC 
;	using the serial port. File transfer uses the XMODEM protocol. 
;
;  This program is gutted from the Ward Christenson Modem program.
;
;  Hacked together by Mike Douglas for the Poly-8813 computer
;
;	Ver	Date	Desc
;	1.0    11/9/19	Initial version 
;----------------------------------------------------------------------------

; ASCII equates

SOH	EQU	01h
EOT	EQU	04h
ACK	EQU	06h
NAK	EQU	15h
CTRLC	EQU	03h
LF	EQU	0Ah
CR	EQU	0Dh

ERRLMT	EQU	5		;MAX ALLOWABLE ERRORS

; BIOS Jump Table Offsets. This table is used to compute BIOS entry
;     points by adding the symbols below to the warm boot entry address
;     stored by CP/M at location 1 on page zero.

CONST	equ	06h		;console status
CONIN	equ	09h		;console input
CONOUT	equ	0Ch		;console output
LIST	equ	0Fh		;list output
PUNCH	equ	12h		;punch output
READER	equ	15h		;reader input
LISTST	equ	2Dh		;list status

;  Serial port I/O is performed using calls directly to the BIOS and 
;     manipulating the IOBYTE to force access to the serial port. The
;     IOBYTE below sets CON to the BAT device. This, in turn, maps console
;     input to the RDR which is mapped to the serial port. Calling conSt
;     performs an input status check on the serial port. Similarly, LST
;     is mapped to the serial port, so calling listSt performs an output
;     status check on the serial port.

SERIOB	equ	96h		;access serial port via CON, RDR, and LST
KBDIOB	equ	95h		;access keyboard via CONST, CONIN
IOBYTE	equ	03h		;location of IOBYTE on page zero
WBOOT	equ	0		;warm boot jump location

;----------------------------------------------------------------------------
; Start of code
;----------------------------------------------------------------------------
	org	100h

;  Verify a file name was specified

	lda	PARAM1		;A=1st character of parameter 1
	cpi	' '		;make sure something entered
	jnz	doXfer
	lxi	d,mHelp		;display usage message
	mvi	c,print
	call	bdos
	ret			;return to CPM
	
;  doXfer - Switch to local stack and do the transfer

doXfer	LXI	H,0		;HL=0
	DAD	SP		;HL=STACK FROM CP/M
	SHLD	STACK		;..SAVE IT
	LXI	SP,STACK	;SP=MY STACK

	lda	IOBYTE		;save incoming IOBYTE
	sta	iobSave

	xra	a		
	sta	SECTNO		;initialize sector number to zero
	CALL	OPEN$FILE	;OPEN THE FILE

	lxi	d,mRcv		;DE->start receive message
	MVI	C,PRINT
	CALL	BDOS		;PRINT ID MESSAGE

;  GOBBLE UP GARBAGE CHARS FROM THE LINE

purge	MVI	B,1		;times out after 1 second if no data
	CALL	RECV
	jc	lineClr		;line is clear, go wait for initial NAK
	cpi	ctrlc		;exit if abort requested
	jz	abort
	jmp	purge

; WAIT FOR INITIAL NAK, THEN SEND THE FILE
	
lineClr	xra	a		;clear crc flag = checksum mode
	sta	crcFlag
WAITNAK	MVI	B,1		;TIMEOUT DELAY
	CALL	RECV
	JC	WAITNAK
	cpi	ctrlc		;abort requested?
	jz	abort
	CPI	NAK		;NAK RECEIVED?
	jz	SENDB		;yes, send file in checksum mode
	cpi	'C'		;'C' for CRC mode received?
	JNZ	WAITNAK		;no, keep waiting
	sta	crcFlag		;set CRC flag non-zero = true
				;fall through to start the send operation
;
;*****************SEND A FILE***************
;

;READ SECTOR, SEND IT

SENDB	CALL	READ$SECTOR
	LDA	SECTNO		;INCR SECT NO.
	INR	A
	STA	SECTNO

;SEND OR REPEAT SECTOR

REPTB	MVI	A,SOH
	CALL	SEND
	LDA	SECTNO
	CALL	SEND
	LDA	SECTNO
	CMA
	CALL	SEND
	lxi	h,0		;init crc to zero
	shld	crc16
	mov	c,h		;init checksum in c to zero
	LXI	H,80H
SENDC	MOV	A,M
	CALL	SEND
	call	calCrc		;update CRC
	INX	H
	MOV	A,H
	CPI	1		;DONE WITH SECTOR?
	JNZ	SENDC

; Send checksum or CRC based on crcFlag

	lda	crcFlag		;crc or checksum?
	ora	a
	jz	sndCsum		;flag clear = checksum
	lda	crc16+1		;a=high byte of CRC
	call	SEND		;send it
	lda	crc16		;a=low byte of crc
	jmp	sndSkip		;skip next instruction	
sndCsum	mov	a,c		;send the checksum byte
sndSkip	call	SEND

;GET ACK ON SECTOR

	MVI	B,4		;WAIT 4 SECONDS MAX
	CALL	RECV
	JC	REPTB		;TIMEOUT, SEND AGAIN

;NO TIMEOUT SENDING SECTOR

	CPI	ACK		;ACK RECIEVED?
	JZ	SENDB		;..YES, SEND NEXT SECT
	cpi	ctrlc		;control-c to abort?
	jz	abort
	JMP	REPTB		;PROBABLY NAK - TRY AGAIN
;
;
; S U B R O U T I N E S
;
;OPEN FILE
OPEN$FILE LXI	D,FCB
	MVI	C,OPEN
	CALL	BDOS
	INR	A		;OPEN OK?
	RNZ			;GOOD OPEN
	CALL	ERXIT
	DB	CR,'Can''t Open File',CR,LF,'$'

; - - - - - - - - - - - - - - -
;EXIT PRINTING MESSAGE FOLLOWING 'CALL ERXIT'
ERXIT	lda	iobSave		;restore incoming IOBYTE
	sta	IOBYTE

	POP	D		;GET MESSAGE
	MVI	C,PRINT
	CALL	BDOS		;PRINT MESSAGE

	LHLD	STACK		;GET ORIGINAL STACK
	SPHL			;RESTORE IT
	RET			;--EXIT-- TO CP/M

; - - - - - - - - - - - - - - -
;MODEM RECV
;-------------------------------------
RECV	PUSH	D		;SAVE DE
MSEC	lxi	d,(7 shl 8)	;1024 cycle loop, 143ms/wrap * 7 = 1 second

MWTI	mvi	a,SERIOB	;set IOBYTE for serial I/O
	sta	IOBYTE
	
	mvi	a,CONST		;console status BIOS call (serial status)
	call	goBios		;character received?
	jnz	MCHAR		;yes

	mvi	a,KBDIOB	;set IOBYTE for keyboard input
	sta	IOBYTE
	
	mvi	a,CONST		;console status BIOS call (console status)
	call	goBios		;operator type something?	
	jz	noChar		;no

	mvi	a,CONIN		;console input BIOS call (console input)
	call	goBios		;get the character
	cpi	CTRLC		;abort requested?
	jnz	noChar		;no

	pop	d		;restore d,e
	ret			;carry is clear (no timeout), exit

noChar	DCR	E		;COUNT DOWN
	JNZ	MWTI		;FOR TIMEOUT
	DCR	D
	JNZ	MWTI

	DCR	B		;DCR # OF SECONDS
	JNZ	MSEC

;MODEM TIMED OUT RECEIVING

	POP	D		;RESTORE D,E
	STC			;CARRY SHOWS TIMEOUT
	RET

;GOT MODEM CHAR

MCHAR	mvi	a,SERIOB	;set IOBYTE for serial I/O	
	sta	IOBYTE

	mvi	a,READER	;reader input BIOS call (serial input)
	call	goBios		;get the character
	
	POP	D		;RESTORE DE
	PUSH	PSW		;CALC CHECKSUM
	ADD	C
	MOV	C,A
	POP	PSW
	ORA	A		;TURN OFF CARRY TO SHOW NO TIMEOUT
	RET

; - - - - - - - - - - - - - - -
;MODEM SEND CHAR ROUTINE
;----------------------------------
;
SEND	PUSH	PSW
	ADD	C		;CALC CKSUM
	MOV	C,A

	pop	psw		;restore char to send
	push 	b		;save checksum
	mov	c,a		;C=char to send
	
	mvi	a,SERIOB	;set IOBYTE for serial I/O	
	sta	IOBYTE

	mvi	a,LIST		;list output BIOS call (serial output)
	call	goBios		;send the character in C

	pop	b		;restore checksum
	ret
	
;-------------------------------------------------------------------
; goBios - Call the BIOS entry vector specified in A. All registers
;    preserved except A.
;-------------------------------------------------------------------
goBios	push	b		;preserve registers
	push	d
	push	h
	call	doBios
	pop	h
	pop	d
	pop	b
	ret
	
doBios	lhld	WBOOT+1		;HL->warm start entry in BIOS
	mov	l,a		;HL->desired BIOS entry
	pchl

;
;FILE READ ROUTINE
;
READ$SECTOR:
	LXI	D,FCB
	MVI	C,READ
	CALL	BDOS
	ORA	A
	RZ
	DCR	A		;EOF?
	JNZ	RDERR

;EOF

	XRA	A
	STA	ERRCT
SEOT	MVI	A,EOT
	CALL	SEND
	MVI	B,3		;WAIT 3 SEC FOR TIMEOUT
	CALL	RECV
	JC	EOTTOT		;EOT TIMEOUT
	CPI	ACK
	JZ	XFER$CPLT

;ACK NOT RECIEVED

EOTERR	LDA	ERRCT
	INR	A
	STA	ERRCT
	CPI	ERRLMT
	JC	SEOT
	CALL	ERXIT
	db	CR,LF,CR
	db	'No ACK received on EOT, but transfer is complete.',CR,LF,'$'
;
;TIMEOUT ON EOT
;
EOTTOT	JMP	EOTERR
;
;READ ERROR
;
RDERR	CALL	ERXIT
	DB	CR,LF,CR,'File Read Error',CR,LF,'$'

;DONE - CLOSE UP SHOP

XFER$CPLT:
	CALL	ERXIT
	DB	CR,LF,CR,'Transfer Complete',CR,LF,'$'

abort	call	erxit
	DB	CR,LF,CR,'Transfer Aborted',CR,LF,'$'

;-----------------------------------------------------------------------------
; calCrc - update the 16-bit CRC with one more byte. 
;    (Copied from M. Eberhard)
; On Entry:
;   a has the new byte
;   crc16 is current except this byte
; On Exit:
;   crc16 has been updated
;   Trashes a,de
;-----------------------------------------------------------------------------
calCrc	push	b		;save bc, hl
	push	h
	lhld	crc16		;get CRC so far
	xra	h		;XOR into CRC top byte
	mov	h,a
	lxi	b,1021h		;bc=CRC16 polynomial
	mvi	d,8		;prepare to rotate 8 bits

; do 8 bit shift/divide by CRC polynomial

cRotLp	dad	h		;16-bit shift
	jnc	cClr		;skip if bit 15 was 0
	mov	a,h		;CRC=CRC xor 1021H
	xra	b
	mov	h,a
	mov	a,l
	xra	c
	mov	l,a
cClr	dcr	d
	jnz	cRotLp		;rotate 8 times

; save the updated CRC and exit

	shld	crc16		;save updated CRC
	pop	h		;restore hl, bc
	pop	b
	ret

; Messages

mRcv	db	'Start XMODEM file receive now...$'

mHelp	db	CR,'PCPUT Ver 1.0 for Poly-8813',CR,LF
	db	'  Transmits a file to a PC through the serial',CR,LF
	db	'  port using the XMODEM protocol.',CR,LF,CR
	db	'Usage: PCPUT file.ext',CR,LF,'$'

; Data area

	DS	40		;STACK AREA
STACK	DS	2		;STACK POINTER
SECTNO	DS	1		;CURRENT SECTOR NUMBER 
ERRCT	DS	1		;ERROR COUNT
crcFlag	ds	1		;non-zero if using CRC
crc16	ds	2		;computed crc
iobSave	ds	1		;save of incoming IOBYTE

; BDOS EQUATES (VERSION 2)

RDCON	EQU	1
WRCON	EQU	2
PRINT	EQU	9
BCONST	EQU	11		;CONSOLE STAT
OPEN	EQU	15		;0FFH=NOT FOUND
CLOSE	EQU	16		;   "	"
SRCHF	EQU	17		;   "	"
SRCHN	EQU	18		;   "	"
ERASE	EQU	19		;NO RET CODE
READ	EQU	20		;0=OK, 1=EOF
WRITE	EQU	21		;0=OK, 1=ERR, 2=?, 0FFH=NO DIR SPC
MAKE	EQU	22		;0FFH=BAD
REN	EQU	23		;0FFH=BAD
STDMA	EQU	26
BDOS	EQU	5
REIPL	EQU	0
FCB	EQU	5CH		;SYSTEM FCB
PARAM1	EQU	FCB+1		;COMMAND LINE PARAMETER 1 IN FCB
PARAM2	EQU	PARAM1+16	;COMMAND LINE PARAMETER 2
	END
