; a program to send disk files to a remote computer
;
cpm	equ	0
prtmsg	equ	9
open	equ	15
close	equ	16
read	equ	20
setdma	equ	26
sys	equ	0005h
bios	equ	0e800h
pfm	equ	0f000h
const	equ	bios+6
conin	equ	bios+9
conout	equ	bios+12
siost	equ	pfm+18
sioin	equ	pfm+21
sioout	equ	pfm+24
cr	equ	0dh
lf	equ	0ah
ctlz	equ	1ah
fcb	equ	005ch
;
	org	100h
start	lxi	sp,stack
	lxi	d,fcb
	mvi	c,open
	call	sys
	cpi	4                ; check if file exists
	jc	setpt
	lxi	d,cannot         ; file does not exist
	mvi	c,prtmsg
	call	sys
	jmp	cpm
;
setpt	lxi	h,buffer         ; file exists
	mvi	b,128
	push	b                ; push character count
	push	h                ; push buffer address
	pop	d
	push	d
	mvi	c,setdma
	call	sys
	lxi	d,wrtcom         ; prompt for write command
	mvi	c,prtmsg
	call	sys
comand	call	conin            ; get echoed characters and
	cpi	cr               ;  loop until carriage return
	jz	com1
	push	psw
	mov	c,a
	call	conout
	pop	psw
	call	sioout
	jmp	comand
com1	mvi	a,cr             ; send carriage return and
	call	sioout
	mvi	c,cr             ;  echo carriage return and
	call	conout
	mvi	c,lf             ;  line feed
	call	conout
	mvi	a,250
	sta	stallct
	call	stall
	pop	h                ; get buffer address
	pop	b                ;  and character count
;
write	mov	a,b
	cpi	128
	jz	write2           ; one sector has been sent
write1	push	b
	push	h
	mov	a,m
	cpi	ctlz
	jz	writeb
	cpi	lf               ; check if line feed and
	jz	writea           ;  do not send to remote computer
	push	psw              ; otherwise
	call	sioout           ;  send character to computer
	pop	psw
writea	mov	c,a              ; echo character at console
	call	conout
	pop	h
	pop	b
	inr	b                ; increment character count and
	inx	h                ;  buffer address and
	jmp	write            ;  loop
;
write2	call	stall            ; wait so remote computer's buffer
	lxi	d,fcb            ;  doesn't overflow, then
	mvi	c,read           ;  get another sector
	call	sys
	cpi	0
	jz	write3           ; loop if not EOF
;
;  end of file
;
writeb	lxi	d,eof            ; prompt for EOF character
	mvi	c,prtmsg
	call	sys
	lxi	d,fcb
	mvi	c,close          ; close file
	call	sys
	mvi	a,cr
	call	sioout
	call	conin            ; get EOF character and
	call	sioout           ;  send to remote computer
	lxi	d,endwt
	mvi	c,prtmsg
	call	sys
loop	call	siost
	jz	cpm
	call	sioin
	jmp	loop
;
write3	lxi	h,buffer
	mvi	b,0
	jmp	write1
;
; wait routine to prevent remote computer buffer overflow
;
stall	lda	stallct
s1	call	t500
	dcr	a
	jnz	s1
	ret
;
; inner delay loop
;
t500	push	psw
	mvi	a,255
t500a	xthl
	xthl
	dcr	a
	jnz	t500a
	pop	psw
	ret
;
cannot	db	'cannot open file$'
wrtcom	db	'enter write file command  $'
eof	db	'type end of file character  $'
endwt	db	'write file completed$'
	ds	64
stack	ds	1
stallct	db	0
buffer	ds	1
;
	end	start
