	title	FCBREP
	subttl	RSX reporting BDOS file functions
	.z80

; Program : FCBREP.MAC

; RSX module for report of BDOS file functions
;
; Program by	Werner Cirsovius
;		Hohe Weide 44
;		D-2000 Hamburg 20

;		Tel.:049/040/4223247

;		Version 1.0, August 1990

; ===== Constants =====

warm	equ	0000h

conin	equ	1
conout	equ	2
print	equ	9
consta	equ	11

ctrlC	equ	'C'-'@'
lf	equ	0ah
cr	equ	0dh
eot	equ	'$'

; ===== RSX starts here =====

; ===== RSX header starts =====

	db	0,0,0,0,0,0	; Six byte serial number
	jp	start		; Jump to RSX program
bdos:	jp	$-$		; Jump to next RSX
	dw	0		; Address of previous RSX
	db	0ffh		; Remove flag (0 keep permanent)
	db	0		; Non bank only flag
	db	'FCBREP  '	; Any eight character name
	db	0		; Loader flag
	dw	0		; Spare (reserved)

; ===== RSX header ends =====

start:
	ex	(sp),hl		; Get callers PC
	ld	(calpc),hl	; .. save it
	ex	(sp),hl
	ld	(calsp),sp	; Save stack
	ld	sp,mystak	; Get local stack
	push	bc		; Push all
	push	de
	push	hl
	push	af
	ld	a,c		; Get BDOS code
	ld	hl,bdost1
	ld	bc,bdosl1
	cpir			; Find code
	call	z,report	; Report it
	call	break		; Test abort
	pop	af
	pop	hl
	pop	de
	pop	bc
	ld	sp,(calsp)
	jr	bdos		; Perform user call
	;
break:	; Test user BREAK requested, abort if verified
	;
	ld	c,consta
	call	bdos		; Get state of console
	or	a		; Any character there ?
	ret	z		; .. no
	ld	c,conin
	call	bdos		; Get it
	cp	ctrlC		; Test Ctrl-C
	ret	nz		; .. no
	ld	de,brkmes
	call	string		; Tell user break found
	ld	c,conin
	call	bdos		; Verify it
	and	05fh
	cp	'Y'
	ld	de,clms
	jr	nz,string	; Test break
	ld	de,dobrk
	call	string		; Tell aborting
	jp	warm		; Exit program
	;
string:	; Output string pointed to by reg DE
	;
	push	bc
	push	hl
	ld	c,print
	call	bdos		; Print it
	pop	hl
	pop	bc
	ret
	;
hexpr:	; Send message from HL, print B bytes from FCB
	;
	push	de
	call	type3		; Close line
	ex	de,hl
	call	string		; .. then give string
	pop	de
..hexp:
	call	hex.pr		; Print a byte
	ld	a,' '
	call	consot		; .. delimiter
	djnz	..hexp
hex.pr:
	ld	a,(de)		; Get byte and print
	inc	de
	;
hexout:	; Print hex byte from accu
	;
	push	af
	rra			; Get hi part
	rra
	rra
	rra
	call	hxnibl
	pop	psw		; .. and lo part
hxnibl:
	and	0fh		; Extract lo
	add	a,'0'		; Set ASCII
	cp	'9'+1		; Test hex
	jr	c,consot
	add	a,7
consot:
	push	bc
	push	de
	push	hl
	ld	e,a
	ld	c,conout
	call	bdos		; Print character
	pop	hl
	pop	de
	pop	bc
	ret
	;
report:	; Perform the main report
	;
	inc	bc		; Fix index
	ld	hl,bdosl1
	or	a
	sbc	hl,bc		; .. make normal
	push	de		; Save FCB
	push	hl		; .. and index
	ld	de,bdospc
	call	string		; Tell PC
	ld	a,(calpc+1)
	call	hexout
	ld	a,(calpc)
	call	hexout
	ld	de,bdosfn
	call	string		; Tell function
	pop	hl
	add	hl,hl		; Adjust index
	add	hl,hl
	ld	bc,bdost2
	add	hl,bc		; Get table index
	ld	e,(hl)		; Get string
	inc	hl
	ld	d,(hl)
	inc	hl
	ld	c,(hl)		; .. and execution address
	inc	hl
	ld	b,(hl)
	push	bc
	call	string		; Tell kinda function
	pop	hl		; Get routine
	pop	de		; .. and FCB
	jp	(hl)		; RUN
	;
type1:	; Execute function with two file names (RENAME)
	;
	call	file		; Tell file
	call	fcbout		; Tell the name
	ld	hl,16
	add	hl,de
	push	hl		; Save 2nd file name
	ld	de,renmes
	call	string		; Tell renamed
	pop	de
	jr	dotyp2
	;
type2:	; Execute function with one file name
	;
	call	file		; Tell file
dotyp2:
	call	fcbout
	;
type3:	; Execute function without name
	;
	ld	de,crlf
	call	string		; Close line
	ret
	;
file:	; Tell file prefix
	;
	push	de
	ld	de,filmes
	call	string
	pop	de
	ret
	;
fcbout:	; Tell file name from FCB pointed to by reg DE
	;
	push	de
	ld	hl,'  '		; Set default
	ld	a,(de)		; Test default drive
	or	a
	jr	z,deffcb	; Yes, skip
	add	a,'@'		; Make real
	ld	h,a
	ld	l,':'
deffcb:
	ld	a,h
	call	consot		; Print drive
	ld	a,l
	call	consot		; .. and delimiter
	inc	de
	ld	b,8
	call	filnam		; Print file name
	ld	a,'.'
	call	consot		; Print delimiter
	ld	b,3
	call	filnam
	ld	hl,sys
	ld	b,4-1
	call	hexpr		; Tell sys values
	ld	hl,block
	ld	b,16-1
	call	hexpr		; Tell block values
	pop	de
	ret
	;
filnam:	; Print portion from FCB
	;
	ld	a,(de)
	and	07fh
	call	consot		; Print name/extension
	inc	de
	djnz	filnam
	ret

; ===== Data fields =====

bdost1:	db	15,16,17,18,19,20,21,22,23,33,34
bdosl1	equ	$-bdost1

bdost2:	dw	bdos15,type2,bdos16,type2,bdos17,type2
	dw	bdos18,type3,bdos19,type2,bdos20,type2
	dw	bdos21,type2,bdos22,type2,bdos23,type1
	dw	bdos33,type2,bdos34,type2

bdos15:	db	'Open file       ',eot
bdos16:	db	'Close file      ',eot
bdos17:	db	'Search file     ',eot
bdos18:	db	'Search next',eot
bdos19:	db	'Delete file     ',eot
bdos20:	db	'Read sequential ',eot
bdos21:	db	'Write sequential',eot
bdos22:	db	'Make file       ',eot
bdos23:	db	'Rename file     ',eot
bdos33:	db	'Read random     ',eot
bdos34:	db	'Write random    ',eot

brkmes:	db	'Control C detected, BREAK [Y/N] ',eot
clms:	db	cr,'                                   ',cr,eot
dobrk:	db	cr,lf,'++ User Break, ABORT ++'
crlf:	db	cr,lf,eot
bdospc:	db	cr,lf,'________________________________'
	db	'__________________________________',cr,lf
	db	'BDOS call at #',eot
bdosfn:	db	', ',eot
filmes:	db	', file : ',eot
renmes:	db	' -> ',eot
sys:	db	'EX.S1.S2.RC      : ',eot
block:	db	'Allocated blocks : ',eot

calpc:	ds	2		; Callers PC
calsp:	ds	2		; Callers stack
	ds	64		; Local stack
mystak:	end

