	title	Screen Saver Driver
	name	('SCRSAV')

; This is the machine part of a BASIC-program written
; by Lawrence Simons,
; published in 8000 Plus, August 1988

; Disassembled by W.Cirsovius

; Program will be called from BASIC by	SCRNSAVE at 0C400H (A$,E%)
;				    or	SCRNLOAD at 0C46DH (A$,E%)

; Where
; A$ is the name of file
; E% is the address of IOResult

; NOTE:	Each line holds 720 bytes, that are 5.625 records of 128 bytes.
;	Therefore a multi sector count of 6 is selected.
;	That results in an overhead of 48 bytes for each line.
;	Total with 32 lines results it in an overhead of 1536 bytes.
BDOS	equ	0005h
FCB	equ	005ch

.open	equ	 15
.close	equ	 16
.rdseq	equ	 20
.wrseq	equ	 21
.make	equ	 22
.setdma	equ	 26
.mulsec	equ	 44
.scb	equ	 49
.parse	equ	152

reclng	equ	128		; CP/M record size

XBIOS	equ	0fc5ah		; Extended BIOS call
ROLLER	equ	0b600h		; Address of ROLLER RAM

SCR_RUN	equ	00e9h		; Extended BIOS function

CONCOL	equ	90		; Screen columns of PCW
CONROW	equ	32		; Screen rows of PCW

XLEN	equ	CONCOL*8	; Size of screen line

RLEN	equ	XLEN+reclng-1

MS	equ	RLEN / reclng	; Multi record cound for one line
;
; Return codes
;
_SUCC	equ	0		; No error
_PARSE	equ	1		; Parse error
_NOFILE	equ	2		; Open error
_IOERR	equ	3		; Transfer error
_CLOSE	equ	4		; Close error

	.phase	0c400h

;
; =================
; ## Save screen ##
; =================
;
SCRNSAVE:
	ld	(UsrStk),sp	; Save entry stack
	ld	sp,LocStk	; Load local stack
	push	de		; Save address of IOResult
	call	Parse		; Parse file
	call	Rewrite		; Create file
	call	PrepFIO		; Prepare file I/O
	xor	a		; Clear row
SavLoop:
	push	af
	ld	bc,Lin2Buff
	call	XBIOS		; Move current line to buffer
	dw	SCR_RUN
	call	WrLine		; Write records to file
	pop	af
	inc	a		; Update row
	cp	CONROW		; Test in range
	jr	c,SavLoop
	call	FClose		; Close file
	ld	de,_SUCC	; Set success
	jr	ScrEnd
;
; Create file
;
Rewrite:
	ld	c,.make
	ld	de,FCB
	call	BDOS		; Create file
	inc	a		; Verify ok
	ret	nz		; Yeap
	pop	de		; Clean stack
	ld	de,_NOFILE	; File open error
	jr	ScrEnd
;
; Write records (one line) to file
;
WrLine:
	ld	c,.wrseq
	ld	de,FCB
	call	BDOS		; Write to file
	and	a		; Verify ok
	ret	z		; Yeap
	pop	de		; Clean stack
	ld	de,_IOERR	; Write error
	jr	ScrEnd
;
; Close file
;
FClose:
	ld	c,.close
	ld	de,FCB
	call	BDOS		; Close file
	inc	a		; Verify ok
	ret	nz		; Yeap
	pop	de		; Clean stack
	ld	de,_CLOSE	; Close error
	jr	ScrEnd
;
; Move current line to buffer
;
Lin2Buff:
	call	VideoAdr	; Get address of current video line
	ex	de,hl
	ld	de,BufLine
	ld	bc,XLEN
	ldir			; Unpack video line
	ret
;
; SCB parameter block for loading current DMA address
;
SCB.DMA:
	db	3ch
	db	0
;
; SCB parameter block for loading current multi sector count
;
SCB.MSEC:
	db	4ah
	db	0
;
; =================
; ## Load screen ##
; =================
;
SCRNLOAD:
	ld	(UsrStk),sp	; Save entry stack
	ld	sp,LocStk	; Load local stack
	push	de		; Save address of IOResult
	call	Parse		; Parse file
	call	Reset		; Open file
	call	PrepFIO		; Prepare file I/O
	xor	a
LodLoop:
	push	af
	call	RdLine		; Read records from file
	pop	af
	push	af
	ld	bc,Buff2Lin
	call	XBIOS		; Move buffer to current line
	dw	SCR_RUN
	pop	af
	inc	a		; Update row
	cp	CONROW		; Test in range
	jr	c,LodLoop
	ld	de,_SUCC	; Set success
;
; End of program
;
ScrEnd:
	push	de
	ld	c,.setdma
	ld	de,(UsrDMA)	; Get back entry DMA address
	call	BDOS
	ld	c,.mulsec
	ld	a,(UsrMulS)	; Get back entry multi sector count
	ld	e,a
	call	BDOS
	pop	de
;
; End of program after parse error
;
ScrRes:
	pop	hl		; Get address of IOREsult
	ld	(hl),e		; Store result
	inc	hl
	ld	(hl),d
	ld	sp,(UsrStk)	; Reset entry stack
	ret
;
; Open file
;
Reset:
	ld	c,.open
	ld	de,FCB
	call	BDOS		; Open file
	inc	a		; Verify ok
	ret	nz		; Yeap
	pop	de
	ld	de,_NOFILE	; File open error
	jr	ScrEnd
;
; Read records (one line) from file
;
RdLine:
	ld	c,.rdseq
	ld	de,FCB
	call	BDOS		; Read from file
	and	a		; Verify ok
	ret	z		; Yeap
	pop	de		; Clean stack
	ld	de,_IOERR	; Read error
	jr	ScrEnd
;
; Move buffer to current line
;
Buff2Lin:
	call	VideoAdr	; Get address of current video line
	ld	hl,BufLine
	ld	bc,XLEN
	ldir			; Unpack buffer line
	ret
;
; Parse file
;
Parse:
	ld	a,(hl)		; Fetch length of filename
	and	a		; Verify not empty
	jr	z,ParsErr	; Error if so
	inc	hl
	ld	e,(hl)		; Fetch address of name
	inc	hl
	ld	d,(hl)
	ld	hl,ParsePB
	ld	(hl),e		; Store address
	inc	hl
	ld	(hl),d
	inc	hl
	ld	(hl),LOW FCB
	inc	hl
	ld	(hl),HIGH FCB
	ld	c,.parse
	ld	de,ParsePB
	call	BDOS		; Parse file
	ld	a,h
	or	l		; Verify valid end
	ret	z		; Yeap
ParsErr:
	pop	de
	ld	de,_PARSE	; Set parse error
	jr	ScrRes
;
; Prepare file I/O
;
PrepFIO:
	ld	c,.scb
	ld	de,SCB.DMA
	call	BDOS		; Load current DMA address
	ld	(UsrDMA),hl	; Save it
	ld	c,.setdma
	ld	de,BufLine
	call	BDOS		; Init new disk buffer
	ld	c,.scb
	ld	de,SCB.MSEC
	call	BDOS		; Load current multi sector count
	ld	(UsrMulS),a	; Save it
	ld	c,.mulsec
	ld	e,MS
	jp	BDOS		; Set multi sector count
;
; Calculate current screen line address
; ENTRY	Accu holds row number
; EXIT	Reg DE points to current line
;
VideoAdr:
	ld	l,a		; Get row
	ld	h,0
	add	hl,hl		; Row * 16
	add	hl,hl
	add	hl,hl
	add	hl,hl
	ld	de,ROLLER
	add	hl,de		; Point into table
	ld	e,(hl)		; Fetch address
	inc	hl
	ld	d,(hl)
	ld	a,e
	and	11111000b	; Mask hi bits
	sla	a		; Fix address
	rl	d
	ld	b,a
	ld	a,e
	and	00000111b	; Mask low bits
	or	b
	ld	e,a
	ret
;
ParsePB:
	ds	2*2		; Parse parameter block
UsrDMA	equ	ParsePB+2*2	; Entry DMA address
UsrMulS	equ	UsrDMA+2	; Entry multi sector count
UsrStk	equ	UsrMulS+2	; Entry stack
;				; ^^^^^^^^^^^
LocStk	equ	UsrStk+2*16	; Local stack
BufLine	equ	LocStk		; Six record buffer starts here

	.dephase

	end
