; a program to copy a file into memory
;
;      to be used with cpfrmem
;
cpm	equ	0
prtmsg	equ	9
open	equ	15
close	equ	16
read	equ	20
setdma	equ	26
sys	equ	0005h
fcb	equ	005ch
buffer	equ	0800h
;
	org	100h
;
; set up stack and open file
;
start	lxi	sp,stack
	lxi	d,fcb
	mvi	c,open
	call	sys
	cpi	4
	jc	readf
	lxi	d,cannot
	mvi	c,prtmsg
	call	sys
	jmp	cpm
;
; put initial buffer address in h and record number in b
;
readf	lxi	h,buffer
	mvi	b,0
	mov	a,b
	sta	buffer-4
	push	b                ; save buffer address and
	push	h                ; record count on stack
;
loop	pop	d                ; get buffer address
	push	d
	mvi	c,setdma         ; and set DMA
	call	sys
	lxi	d,fcb
	mvi	c,read           ; read a record
	call	sys
	cpi	0
	jz	incrmt           ; and increment if read OK
;
; at end of file
;
	pop	h                ; get address in buffer
	shld	buffer-2         ; and save in memory
	lxi	h,buffer-3
	pop	b                ; get record count
	mov	m,b              ; and save in memory
	lxi	d,eof
	mvi	c,prtmsg
	call	sys
	lxi	d,fcb
	mvi	c,close
	call	sys
	jmp	cpm
;
; incremet address in buffer and record count
;
incrmt	pop	h
	lxi	b,128
	dad	b
	pop	b
	mov	a,b
	cpi	255
	jnz	step
	lda	buffer-4
	inr	a
	sta	buffer-4
	mvi	b,0
	jmp	next
step	inr	b
next	push	b
	push	h
	jmp	loop
;
cannot	db	'cannot open file$'
eof	db	'end of file$'
	ds	64
stack	nop
;
	end	start
