	title	CP/M spool program - the RSX
	name	('SPOOLRSX')

; A CP/M spooler

; This is the spooler RSX

BDOS	equ	0005h
DMA	equ	0080h

.os	equ	 0
.conin	equ	 1
.condir	equ	 6
.string	equ	 9
.rdline	equ	10
.kbstat	equ	11
.resdsk	equ	13
.seldsk	equ	14
.open	equ	15
.close	equ	16
.delete	equ	19
.make	equ	22
.getdpb	equ	31
.usrcod	equ	32
.rdrnd	equ	33
.wrrnd	equ	34
.filsiz	equ	35
.resdrv	equ	37
.dskfre	equ	46
.chprg	equ	47
.flshbf	equ	48
.scb	equ	49
.rsx	equ	60
.trunc	equ	99

_PSH	equ	15		; Index to PSH in DPB

_EX	equ	12
_S2	equ	14
_CR	equ	32
_RRN	equ	33

reclng	equ	128
;
; SCB offsets
;
SCB@	equ	3ah
;
SCB.xx	equ	01bh		; Unknown SCB pointer
				; Under regular start of SCB
SCB.ME	equ	098h
SCB.HSH	equ	09ch
SCB.DMA	equ	0d8h
SCB.FCB	equ	0dbh
SCB.USR	equ	0e0h
SCB.COM	equ	0f9h
SCB.DOS	equ	0fdh
;
; Unknown pointers to banked BDOS
;
; The banked BDOS for the JYCE starts at 08c00h
;
HOOK1	equ	03032h		; Means 08f32h
HOOK2	equ	03066h		; Means 08f66h
;
HshSavL	equ	 7		; Length of hash bytes to be saved
FCBSavL	equ	13		; Length of FCB bytes to be saved

_CI	equ	 3
_LIST	equ	 5
_SELMEM	equ	27

_get	equ	-1

BIOSjp	equ	3

_JP	equ	0c3h

SPOLrsx	equ	120

_GETDAT	equ	9		; DESPOOL parameter function

SPOOlen	equ	128

RAMdsk	equ	'M'-'@'

lf	equ	0ah
cr	equ	0dh
eot	equ	'$'

uppmask	equ	11011111b
all	equ	11111111b

l0006	equ	06h
l007f	equ	7fh
l0080	equ	80h

; #### RSX HEADER BEGINS ###
;
RSXbase:
	ds	6		; xx00: Serial number
RSXgo:
	jp	BegRSX		; xx06: Jump to RSX
_BDOS:
	jp	BDOS+1		; xx09: Jump to next RSX
	dw	BDOS+2		; xx0C: Address of previous RSX
RSXperm:
	db	-1		; xx0E: Permanent flag
	db	0		; xx0F: Bank flag
RSXname:
	db	'SPOOL   '	; xx10: Name of RSX
RSXld:
	db	0		; xx18: Loader flag
	db	0,0		; xx19: Reserved
;
; #### RSX HEADER ENDS ###
;
SpoolExist:
	ld	de,$SPOOLEXIST	; Spooler already exists
	jr	ErrString
NoMemory:
	ld	de,$NOMEMORY	; Insufficient memory
	jr	ErrString
NoVerify::
	ld	de,$NOVERIFY	; Cannot verify directory
	jr	ErrString
NoRamDsk:
	ld	de,$NORAMDSK	; No RAM disk memory
	jr	ErrString
NoSpace:
	ld	de,$NOSPACE	; Insufficient space on M:
	jr	ErrString
NoDelete:
	ld	de,$NODELETE	; Cannot delete old file
	jr	ErrString
NoCreate:
	ld	de,$NOCREATE	; Cannot create file
	jr	ErrString
NoAccess:
	ld	de,$NOACCESS	; Cannot access file
	jr	ErrString
;
; Print string ^DE on console
;
string:
	ld	c,.string
	jr	_BDOS		; Process thru BDOS
;
; Print string ^DE on console and exit thru warmstart
;
ErrString:
	call	string		; Print error message
	ld	c,.os
	call	_BDOS		; Then exit
	or	all
	ret
;
; #####################
; ### SPOOLER ENTRY ###
; #####################
;
BegRSX:
	ld	a,c		; Get BDOS function
	cp	.rsx		; Test RSX
	jr	nz,_BDOS	; Nope
	ld	a,(de)		; Get RSX number
	cp	SPOLrsx		; Test ours
	jr	z,MyRSX		; Yeap
	jr	_BDOS
MyRSX:
	ld	de,$SPOOL
	call	string		; Give start message
	call	getSCBadr	; Get address of SCB
	ld	hl,RSXbase	; Init my base address
l016a:
	ld	l,_BDOS+2-RSXbase
	ld	h,(hl)		; Get page of next RSX
	ld	l,RSXld-RSXbase
	ld	a,(hl)		; Get loader flag
	cp	-1		; Test loader found
	jp	z,l018c		; Yeap, end of search
	ld	l,RSXname-RSXbase
	ld	de,l0510
	ld	b,RSXld-RSXname
	call	l0184		; Find my RSX
	jp	z,SpoolExist	; RSX already installed
	jr	l016a		; Try next
;
; Compare B characters ^HL:^DE - Z set indicates found
;
l0184:
	ld	a,(de)		; Get character
	cp	(hl)		; Compare
	ret	nz		; No match
	inc	de
	inc	hl
	djnz	l0184
	ret
;
; Install this RSX
;
l018c:
	ld	hl,(SCB.ADR)	; Get SCB address
	ld	l,SCB.COM	; Set pointer to COMMON memory
	ld	e,(hl)		; Fetch address of COMMON
	inc	hl
	ld	d,(hl)
	ld	hl,l0500
	and	a
	sbc	hl,de		; Test enough space
	jr	c,NoMemory	; Nope
	ld	hl,RSXbase
	ld	de,l0500
	ld	bc,RSXgo-RSXbase
	ldir			; Unpack serial number
	inc	hl		; Skip vector
	inc	hl
	inc	hl
	inc	de
	inc	de
	inc	de
	ld	bc,RSXperm-_BDOS
	ldir			; Unpack address block
	ld	e,_get
	ld	c,.usrcod
	call	_BDOS		; Get current user
	ld	(l0643),a
	ld	e,RAMdsk-1
	ld	c,.seldsk
	call	_BDOS		; Select disk
	inc	a		; Test success
	jp	z,NoRamDsk	; No RAM disk memory on error
	ld	c,.getdpb
	call	_BDOS		; Get disk parameter block
	ld	a,h		; Test success
	and	l
	inc	a
	jp	z,NoRamDsk	; No RAM disk memory if not
	ex	de,hl
	ld	hl,_PSH	
	add	hl,de		; Point to PSH
	ld	a,(hl)
	inc	hl		; And PHM
	or	(hl)		; Verify 128 byte sectors
	jp	nz,NoRamDsk	; No RAM disk memory if not
	ld	de,l0644
	ld	hl,_CR
	add	hl,de		; Point to current record
	ld	(hl),0		; Clear it
	ld	c,.open
	call	_BDOS		; Open file
	inc	a		; Test on disk
	jp	z,l021a		; Nope
l01ef:
	ld	de,$DELFILE
	call	string		; Tell file exist - delete?
	ld	c,.conin
	call	_BDOS		; Get answer
	push	af
	ld	de,$CRLF
	call	string		; Give new line
	pop	af
	and	uppmask
	cp	'Y'		; Test deletion
	jr	z,l020e
	cp	'N'		; Test no deletion
	jr	z,l0226		; Append data
	jr	l01ef
;
; Delete old and create new spool file 'spooler $$$'
;
l020e:
	ld	de,l0644
	ld	c,.delete
	call	_BDOS		; Delete file
	inc	a		; Test success
	jp	z,NoDelete	; Nope, error
;
; Create new spool file 'spooler $$$'
;
l021a:
	ld	de,l0644
	ld	c,.make
	call	_BDOS		; Create new file
	inc	a		; Test success
	jp	z,NoCreate	; Nope, error
;
; Append to spool file 'spooler $$$'
;
l0226::
	ld	de,l0644
	ld	c,.filsiz
	call	_BDOS		; Get file size - access it
	inc	a		; Test success
	jp	z,NoAccess	; Nope, error
	ld	hl,(l0644+_RRN)	; Get random record
	ld	(l0668),hl	; Save it
	xor	a
	ld	(l066b),hl
	ld	(l0644+_RRN+2),a
	xor	a
	ld	(l076f),a	; Set empty write buffer
	ld	a,SPOOlen
	ld	(l066e),a	; Set nothing in read buffer
	ld	e,RAMdsk-1
	ld	c,.dskfre
	call	_BDOS		; Get RAM disk free space
	inc	a
	jp	z,NoRamDsk	; No RAM disk memory
	ld	hl,(SCB.ADR)	; Get SCB address
	ld	l,SCB.DMA	; Point to current DMA address
	ld	e,(hl)		; Get DMA address
	inc	hl
	ld	d,(hl)
	inc	de
	inc	de
	ld	a,(de)
	sub	1
	sbc	a,a
	cpl
	ld	b,a
	dec	de
	ld	a,(de)
	sub	1
	sbc	a,a
	cpl
	or	b
	ld	b,a
	dec	de
	ld	a,(de)
	or	b
	jp	p,NoSpace	; Insufficient space on M:
	ld	hl,(SCB.ADR)	; Get SCB address
	inc	h		; Point behind SCB address
	ld	l,0		; Page boundary
	ld	(BIOSbase),hl	; Save for BIOS base address
	ld	de,OS.SAVE
	ld	bc,OS.LEN
	ld	b,0
	ldir			; Unpack vectors
	ld	hl,(BIOSbase)	; Get BIOS base
	ld	l,3*_SELMEM	; Build bank vector
	ld	de,SELMEM
	ld	bc,BIOSjp
	ldir			; Unpack it
	ld	l,3*_LIST	; Build vector to list output
	ld	de,PROUT
	ld	bc,BIOSjp
	ldir			; Unpack it
	ld	hl,(l0509+1)
	ld	de,l0006
	add	hl,de
	ld	de,l0509+2
	ld	(hl),e
	inc	hl
	ld	(hl),d
	ld	hl,l0506
	ld	(_BDOS+1),hl	; Change vectors
	ld	(RSXgo+1),hl
	ld	hl,OS.SAVE
	ld	de,BIOS.SAVE
	ld	bc,OS.LEN
	ld	b,0
	ldir			; Unpack BIOS vectors
	ld	de,BIOS.SAVE
	ld	hl,l031b	; Point to interception table
	ld	b,OS.LEN / 3
l02c6:
	ld	a,(hl)		; Get interception code
	cp	_JP		; Test defined
	jr	nz,l02d4	; Nope
	push	bc
	ld	bc,BIOSjp
	ldir			; Overwrite vector
	pop	bc
	jr	l02da
l02d4:
	inc	hl		; Skip entries
	inc	hl
	inc	hl
	inc	de
	inc	de
	inc	de
l02da:
	djnz	l02c6		; Run thru the code
	ld	hl,PRINT
	ld	(OS.POT+1),hl	; Set printer interception
	ld	hl,(BIOSbase)	; Get BIOS base
	ld	l,3*_SELMEM+1
	ld	de,l07b9
	ld	(hl),e		; Write new selection vector
	inc	hl
	ld	(hl),d
	ld	hl,OS.SAVE
	ld	de,(BIOSbase)	; Get BIOS base
	ld	bc,OS.LEN
	ld	b,0
	ldir			; Write back vectors
	ld	de,$SPOOLOK
	call	string		; Tell spooler implemented now
	xor	a
	ret
;
; Fetch address of SCB
;
getSCBadr:
	ld	de,SCBpb
	ld	c,.scb
	call	_BDOS		; Fetch SCB address
	ld	(SCB.ADR),hl	; Save
	push	hl
	ld	l,SCB.DOS	; Point to BDOS page in bank 0
	ld	h,(hl)		; Fetch it
	ld	l,6		; Set start xx06
	ld	(BankBDOS),hl	; Save this address
	pop	hl
	ret
;
SCBpb:
	db	SCB@
	db	0
;
; BIOS interception table
;
l031b:
	db	0,0,0		;  0: Cold boot
	db	0,0,0		;  1: Warm boot
	jp	MY.CST		;  2: Console state
	jp	MY.CIN		;  3: Console input
	db	0,0,0		;  4: Console output
	jp	MY.POT		;  5: Printer output
	db	0,0,0		;  6: Auxiliary output
	db	0,0,0		;  7: Auxiliary input
	db	0,0,0		;  8: Home disc track
	db	0,0,0		;  9: Select disc drive
	db	0,0,0		; 10: Set track number
	db	0,0,0		; 11: Set sector number
	db	0,0,0		; 12: Set DMA address
	db	0,0,0		; 13: Read a sector
	db	0,0,0		; 14: Write a sector
	jp	MY.PST		; 15: Status of list device
	db	0,0,0		; 16: Sector translation
	db	0,0,0		; 17: Status of console output
	db	0,0,0		; 18: Status of auxiliary input
	db	0,0,0		; 19: Status of auxiliary output
$SPOOLEXIST:
	db	'ERROR - SPOOL already installed',eot
$NOMEMORY:
	db	'*** Cannot load M:DISK - Insufficient common memory',eot
$NOVERIFY:
	db	'ERROR - Cannot verify directory',eot
$NORAMDSK:
	db	'ERROR - No M: RAM DISK MEMORY found',eot
$NOSPACE:
	db	'ERROR - Insufficient space on M:',eot
$NODELETE:
	db	'ERROR - Cannot delete old spooler file',eot
$NOCREATE:
	db	'ERROR - Cannot create spooler file',eot
$NOACCESS:
	db	'ERROR - Error accessing spooler file',eot
$DELFILE:
	db	'Spooler file already exists '
	db	'- Delete it first (Y/N) ? ',eot
$SPOOLOK:
	db	'LST: Spooler implemented in M:',eot
$SPOOL:
	db	'SPOOL: ',eot
$CRLF:
	db	cr,lf,eot
l04xx::
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
;
; ### New header ###
;
l0500:
	ds	6		; xx00: Serial number
l0506:
	jp	l051b		; xx06: Jump to RSX
l0509:
	jp	$-$		; xx09: Jump to next RSX
	ds	2		; xx0C: Address of previous RSX
	ds	1		; xx0E: Permanent flag
	ds	1		; xx0F: Bank flag
l0510:
	db	'SPOOL   '	; xx10: Name of RSX
	db	0		; xx18: Loader flag
	db	0,0		; xx19: Reserved
;
; Intercepted BDOS input
;
l051b::
	ld	a,c		; Get BDOS function
	cp	.conin		; Test related functions
	jr	z,l058e
	cp	.condir
	jr	z,l057c
	cp	.rdline
	jr	z,l058e
	cp	.kbstat
	jr	z,l0584
	cp	.rsx
	jr	z,l0597
	ld	a,(l0644+_S2)
	and	80h
	jr	nz,l0579
	ld	a,c		; Get function again
	cp	.os
	jr	z,l054e
	cp	.resdsk
	jr	z,l054e
	cp	.resdrv
	jr	z,l054e
	cp	.chprg
	jr	z,l054e
	cp	.flshbf
	jr	z,l054e
	jr	nz,l0579
;
; BDOS function WARM_START (0)
; BDOS function RESET_DISK_SYSTEM (13)
; BDOS function RESET_DRIVE (37)
; BDOS function CHAIN_TO_PROGRAM (47)
; BDOS function FLUSH_BUFFERS (48)
;
l054e:
	ld	(l0afe),sp	; Save user stack
	ld	sp,LocStk	; Get own one
	push	bc
	push	de
	call	l09fc
	ld	de,l0644
	ld	c,.close
	call	l0509		; Close file
	ld	de,l0644
	ld	hl,_EX
	add	hl,de
	ld	(hl),0		; Clear extent
	ld	c,.open
	call	l0509		; Open file again
	call	l0a48
	pop	de
	pop	bc
	ld	sp,(l0afe)	; Reset user stack
l0579:
	xor	a
	jr	l0590
;
; BDOS function DIRECT_CONSOLE_INPUT (6)
;
l057c:
	ld	a,e
	cp	0fdh		; Test output
	jp	c,l0509		; Yeap
	jr	z,l058e		; It's wait for character input
;
; BDOS function CONSOLE_STATE (11)
;
l0584:
	ld	a,(l0596)	; Test console
	dec	a
	jr	z,l058e
	ld	a,1
	jr	l0590
;
; BDOS function CONSOLE_INPUT (1 and 6 with 0xFD)
; BDOS function CONSOLE_LINE (10)
;
l058e:
	ld	a,-1
l0590:
	ld	(l0596),a	; Test console flag
	jp	l0509		; Process console I/O
;
l0596:
	db	0
;
; BDOS functions CALL_RSX (60)
;
l0597:
	ld	a,(de)		; Get RSX number
	cp	SPOLrsx		; Test ours
	jp	nz,l0509	; Nope
	inc	de
	ld	a,(de)
	cp	_GETDAT		; Test special DESPOOL request
	jp	nz,l0509	; Nope
;
; DESPOOL request - Unpack internal pointers
;
	inc	de		; Fix pointer
	ld	hl,l05af
	ld	bc,ll05af
	ldir			; Put block into TPA program
	xor	a		; Indicate success
	ret
;
; Parameter block for despooler
;
l05af:
	dw	l0500		; Address of this RSX
	dw	l0644		; Address of spooler FCB
	dw	OS.SAVE		; Address of original BIOS table
	dw	OS.LEN		; Length of original BIOS table
;
	dw	BIOSjp*_LIST	; Relative address of printer output
	dw	PROUT		; Address of printer output
;
	dw	BIOSjp*_SELMEM	; Relative address of select memory bank
	dw	SELMEM		; Address of select memory bank
;
	dw	0		; End marker
ll05af	equ	$-l05af
;
; BIOS table holding original vectors
;
OS.SAVE:
	jp	$-$		;  0: Cold boot
	jp	$-$		;  1: Warm boot
OS.CST:
	jp	$-$		;  2: Console state
OS.CIN:
	jp	$-$		;  3: Console input
OS.COT:
	jp	$-$		;  4: Console output
OS.POT:
	jp	$-$		;  5: Printer output
OS.AOT:
	jp	$-$		;  6: Auxiliary output
OS.AIN:
	jp	$-$		;  7: Auxiliary input
	jp	$-$		;  8: Home disc track
	jp	$-$		;  9: Select disc drive
	jp	$-$		; 10: Set track number
	jp	$-$		; 11: Set sector number
	jp	$-$		; 12: Set DMA address
	jp	$-$		; 13: Read a sector
	jp	$-$		; 14: Write a sector
OS.PST:
	jp	$-$		; 15: Status of list device
	jp	$-$		; 16: Sector translation
OS.COS:
	jp	$-$		; 17: Status of console output
OS.ASI:
	jp	$-$		; 18: Status of auxiliary input
OS.ASO:
	jp	$-$		; 19: Status of auxiliary output
;
OS.LEN	equ	$-OS.SAVE
;
; ------------------------------------------------------------
;
PROUT:
	jp	$-$		;  5: Printer output
SELMEM:
	jp	$-$		; 27: Select memory bank
BIOSbase:
	dw	0		; BIOS base address
BankBDOS:
	dw	0		; Address of banked BDOS
;
; BIOS table holding intercepted vectors
;
BIOS.SAVE:
	jp	$-$		;  0: Cold boot
	jp	$-$		;  1: Warm boot
	jp	$-$		;  2: Console state
	jp	$-$		;  3: Console input
	jp	$-$		;  4: Console output
	jp	$-$		;  5: Printer output
	jp	$-$		;  6: Auxiliary output
	jp	$-$		;  7: Auxiliary input
	jp	$-$		;  8: Home disc track
	jp	$-$		;  9: Select disc drive
	jp	$-$		; 10: Set track number
	jp	$-$		; 11: Set sector number
	jp	$-$		; 12: Set DMA address
	jp	$-$		; 13: Read a sector
	jp	$-$		; 14: Write a sector
	jp	$-$		; 15: Status of list device
	jp	$-$		; 16: Sector translation
	jp	$-$		; 17: Status of console output
	jp	$-$		; 18: Status of auxiliary input
	jp	$-$		; 19: Status of auxiliary output
;
l0643:
	db	0
l0644:
	db	RAMdsk,'spooler $$$'
	ds	24
;
l0668:
	ds	3,0		; Write random number
l066b:
	ds	3,0		; Read random number
l066e:
	db	SPOOlen		; Read index
l066f:
	ds	reclng		; Read buffer
l06ef:
	ds	reclng		; Write buffer
l076f:
	db	0		; Write index
SCB.ADR:
	dw	0
;
; The redirected printer status
;
PRINT:
	push	bc
	call	OS.PST		; Get BIOS status of list device
	pop	bc
	or	a
	jp	nz,PROUT	; BIOS printer output if printer ready
	push	bc
	call	l0783		; Set intercepted vectors to BIOS vectors
	pop	bc
	jp	MY.POT		; Put character to file
;
; Set intercepted vectors to BIOS vectors
;
l0783:
	ld	hl,BIOS.SAVE
	ld	de,(BIOSbase)	; Get BIOS base
	ld	bc,OS.LEN
	ldir
	ret
;
; Set original vectors to BIOS vectors
;
l0790:
	ld	hl,OS.SAVE
	ld	de,(BIOSbase)	; Get BIOS base
	ld	bc,OS.LEN
	ldir
	ret
;
; Put character to printer if any
;
l079d:
	ld	a,(l066e)	; Get read index
	or	a		; Test any there
	ret	m		; Nope
	push	bc
	call	OS.PST		; Get BIOS status of list device
	pop	bc
	or	a
	ret	z		; Not ready
;
; Put character to printer from buffer
;
l07a9:
	push	bc
	ld	hl,l066e
	ld	e,(hl)		; Get read index
	ld	d,0
	inc	(hl)		; Advance entry
	inc	hl
	add	hl,de		; Build buffer address
	ld	c,(hl)		; Get character
	call	PROUT		; BIOS printer output
	pop	bc
	ret
;
; Intercepted BIOS function 27: Select memory bank
;
l07b9:
	ld	(MemBank),a	; Save bank
	jp	SELMEM		; BIOS select memory bank
;
; Intercepted BIOS function 5: Printer output
;
MY.POT::
	call	l079d		; Put character to printer if any
	ld	hl,(l0668)	; Get write random number
	ex	de,hl
	ld	hl,(l066b)	; Get read random number
	and	a
	sbc	hl,de		; Test same
	jp	nz,l0855	; Nope
	ld	hl,l076f
	ld	e,(hl)		; Fetch write index
	ld	hl,l066e
	ld	d,(hl)		; Fetch read index
	ld	a,SPOOlen
	sub	d
	or	e
	jp	z,l0812
	ld	a,SPOOlen
	sub	e
	or	d
	jp	z,l08d0
	ld	a,e
	or	a
	jp	m,l0819
	ld	d,0
	ld	hl,l06ef
	add	hl,de		; Build write buffer address
	ld	(hl),c		; Store character
	ld	hl,l076f
	inc	(hl)		; Advance write index
	inc	e
	ld	a,(l066e)	; Fetch read index
	or	a
	ret	p
	ld	c,e
	ld	b,d
	ld	a,SPOOlen
	sub	e
	ld	e,a
	ld	hl,l066e	; Point to read index
	ld	(hl),e
	inc	hl
	add	hl,de
	ex	de,hl
	ld	hl,l06ef
	ldir			; Unpack entire buffer
	xor	a
	ld	(l076f),a	; Clear write index
	ret
l0812:
	dec	(hl)
	ld	de,l0080
	add	hl,de
	ld	(hl),c
	ret
l0819:
	push	bc
	call	l082b
	pop	bc
	ld	hl,l076f
	ld	e,(hl)		; Get write index
	ld	d,0
	inc	(hl)
	ld	hl,l06ef
	add	hl,de		; Build buffer address
	ld	(hl),c		; Store character
	ret
;
;
;
l082b:
	ld	c,d
	ld	b,0
	ld	hl,l066f
	add	hl,bc		; Build read buffer address
	push	hl
	ld	a,d
	cp	e
	jr	c,l0838
	ld	a,e
l0838:
	ld	c,a
	and	a
	sbc	hl,bc
	ld	a,80h
	sub	d
	add	a,e
	ld	e,c
	ld	c,a
	ld	a,e
	ex	de,hl
	pop	hl
	ldir
	ld	e,a
	ld	hl,l066e	; Point to read index
	ld	a,(hl)
	sub	e
	ld	(hl),a
	ld	hl,l076f	; Point to write index
	ld	a,(hl)
	sub	e
	ld	(hl),a
	ret
l0855:
	ld	hl,l066e	; Point to read index
	ld	a,(hl)		; Get index
	or	a		; Test buffer scanned
	jp	p,l08be		; Nope
	push	bc
	call	l0865
	pop	bc
	jp	MY.POT
;
;
;
l0865:
	ld	(l0afe),sp	; Save user stack
	ld	sp,LocStk	; Get own one
	call	l09d8		; Save system environment
	ld	de,l066f
	call	l0a65		; Set DMA buffer
	ld	hl,(l066b)	; Get read random number
	dec	hl		; Set previous
	ld	(l066b),hl
	ld	(l0644+_RRN),hl
	ld	c,.rdrnd
	call	l0a77		; Read from file
	ld	hl,l066e	; Point to read index
	ld	(hl),0
	ld	de,(l066b)	; Get read random number
	ld	hl,(l0668)	; Get write random number
	and	a
	sbc	hl,de
	jr	z,l0897
	jr	l08b6
l0897:
	ld	hl,l007f
	add	hl,de
	ld	a,h
	or	a
	jr	z,l08ad
	ld	c,.delete
	call	l0a77		; Delete file
	ld	c,.make
	xor	a
	ld	(l0644+_EX),a
	call	l0a77		; Create new one
l08ad:
	ld	hl,0
	ld	(l0668),hl	; Clear write random number
	ld	(l066b),hl	; Clear read random number
l08b6:
	call	l0a26		; Restore system environment
	ld	sp,(l0afe)	; Reset user stack
	ret
l08be:
	ld	hl,l076f
	ld	a,(hl)		; Get write index
	or	a
	jp	m,l08d0
	ld	e,a
	ld	d,0
	inc	(hl)
	ld	hl,l06ef
	add	hl,de		; Build write buffer address
	ld	(hl),c
	ret
l08d0:
	ld	(l0afe),sp	; Save user stack
	ld	sp,LocStk	; Get own one
	push	bc
	call	l09d8		; Save system environment
	ld	de,l06ef
	call	l0a65		; Set DMA buffer
	ld	hl,(l0668)	; Get write random number
	dec	hl
	ld	(l0644+_RRN),hl	; Set it
	ld	c,.wrrnd
	call	l0a77		; Write to file
	or	a
	jr	nz,l0909
	call	l0a26		; Restore system environment
	pop	bc
	ld	sp,(l0afe)	; Reset user stack
	ld	hl,(l0668)	; Get write random number
	dec	hl		; Get previous
	ld	(l0668),hl
	ld	hl,l076f
	ld	(hl),1		; Set write index
	ld	hl,l06ef
	ld	(hl),c		; Store character
	ret
l0909:
	ld	c,.close
	call	l0a77		; Close file
	ld	hl,(l066b)	; Get read random number
	dec	hl		; Get previous
	ld	(l0644+_RRN),hl
	ld	c,.trunc
	call	l0a77		; Truncate file
	ld	c,.open
	xor	a
	ld	(l0644+_EX),a
	call	l0a77		; Open file again
	call	l0a26		; Restore system environment
	pop	bc
	ld	sp,(l0afe)	; Reset user stack
l092b:
	push	bc
l092c:
	call	OS.PST		; Get BIOS status of list device
	or	a
	jr	z,l092c
	pop	bc
	call	l07a9		; Put character to printer from buffer
	ld	a,(l066e)	; Fetch read index
	or	a
	jp	p,l092b
	push	bc
	call	l0865
	pop	bc
	jp	l08d0
;
; Intercepted BIOS function 2: Console state
;
MY.CST::
	call	OS.CST		; Get BIOS console state
	ld	(l0974),a
	or	a
	ret	nz
	ld	a,(l0596)	; Test console flag
	inc	a
	call	z,l0991
	xor	a
	ret
;
; Intercepted BIOS function 3: Console input
;
MY.CIN::
	ld	a,(l0974)
	or	a
	jr	nz,l096b
	call	OS.CST		; Get BIOS console state
	or	a
	jr	nz,l096b
	call	l0991
	ld	hl,(BIOSbase)	; Get BIOS base
	ld	l,3*_CI		; Build console input vector
	jp	(hl)
l096b:
	call	OS.CIN		; Get BIOS console input
	ld	hl,l0974
	ld	(hl),0
	ret
l0974:
	db	0
;
	jp	OS.COT		; BIOS console output
	jp	OS.AOT		; BIOS auxiliary output
	jp	OS.AIN		; BIOS Auxiliary input
;
; Intercepted BIOS function 15: Status of list device
;
MY.PST::
	ld	a,(l076f)	; Get write index
	cp	80h
	jr	nz,l0985
l0985:
	or	0ffh
	ret
l09xx::
	jp	OS.COS		; BIOS status of console output
	jp	OS.ASI		; BIOS status of auxiliary input
	jp	OS.ASO		; BIOS status of auxiliary output
l0991:
	call	OS.PST		; Get BIOS status of list device
	or	a
	ret	z		; Not ready, exit
	ld	hl,l066e	; Point to read index
	ld	a,(hl)
	or	a
	jp	m,l09ad
	inc	(hl)
	ld	e,a
	ld	d,0
	inc	hl
	add	hl,de
	ld	c,(hl)
	call	PROUT		; BIOS printer output
	ld	a,(l066e)	; Fetch read index
	or	a
	ret	p
l09ad:
	ld	hl,(l066b)	; Get read random number
	ex	de,hl
	ld	hl,(l0668)	; Get write random number
	and	a
	sbc	hl,de
	jp	nz,l0865
	ld	a,(l076f)	; Get write index
	ld	e,a
	ld	d,80h
	or	a
	jp	nz,l082b
	ld	a,SPOOlen
	ld	(l066e),a	; Set buffer empty
	xor	a
	ld	(l076f),a	; Clear write index
	ld	h,a
	ld	l,a
	ld	(l066b),hl	; Reset read random number
	ld	(l0668),hl	; Reset write random number
	jp	l0790		; Set original vectors to BIOS vectors
;
; Save system environment
;
l09d8:
	ld	a,(MemBank)	; Get BIOS selected bank
	ld	(l0a85),a
	or	a
	ld	a,0		; Select system bank
	call	nz,SELMEM	; BIOS select memory bank
	ld	hl,(BankBDOS)	; Fetch BDOS entry
	inc	h		; Fix page
	inc	h
	inc	h
	ld	l,LOW HOOK1	; Set pointer
	ld	e,(hl)		; Get system address 1
	inc	hl
	ld	d,(hl)
	ld	(l0a88),de	; Save it
	ld	l,LOW HOOK2
	ld	e,(hl)		; Get system address 2
	inc	hl
	ld	d,(hl)
	ld	(l0a8a),de	; Save it
l09fc:
	ld	hl,(SCB.ADR)	; Get SCB address
	ld	l,SCB.DMA	; Get pointer to current DMA address
	ld	e,(hl)		; Get current address
	inc	hl
	ld	d,(hl)
	ld	(DMAptr),de	; Save it
	ld	l,SCB.HSH	; Point to hash part
	ld	de,HshSave
	ld	bc,HshSavL
	ldir
	ld	l,SCB.FCB	; Point to current FCB
	ld	bc,FCBSavL
	ldir			; Save it
	dec	hl
	ld	(hl),0ffh
	dec	hl
	ld	(hl),1
	ld	l,SCB.USR	; Point to current user number
	ld	a,(l0643)	; Get current user
	ld	(hl),a
	ret
;
; Restore system environment
;
l0a26:
	ld	hl,(BankBDOS)	; Fetch BDOS entry
	inc	h
	inc	h
	inc	h
	ld	l,LOW HOOK1
	ld	de,(l0a88)	; Get system address 1
	ld	(hl),e		; Restore it
	inc	hl
	ld	(hl),d
	ld	l,LOW HOOK2
	ld	de,(l0a8a)	; Get system address 2
	ld	(hl),e		; Restore it
	inc	hl
	ld	(hl),d
	ld	a,(l0a85)	; Get back old bank
	ld	(MemBank),a	; Reset BIOS selected bank
	or	a
	call	nz,SELMEM	; BIOS select memory bank
l0a48:
	ld	de,(DMAptr)	; Get user buffer
	call	l0a65		; Reset DMA buffer
	ld	hl,(SCB.ADR)	; Get SCB address
	ld	l,SCB.HSH	; Point to hash entry
	ld	de,HshSave
	ex	de,hl
	ld	bc,HshSavL
	ldir
	ld	e,SCB.FCB	; Point to current FCB
	ld	bc,FCBSavL
	ldir			; Resore it
	ret
;
; Set DMA buffer ^DE into SCB
;
l0a65:
	ld	hl,(SCB.ADR)	; Get SCB address
	ld	l,SCB.DMA	; Point to current DMA address
	ld	(hl),e		; Store it
	inc	hl
	ld	(hl),d
	ld	l,SCB.ME	; Point to BDOS entry - that is this RSX
	inc	hl
	ld	h,(hl)
	ld	l,SCB.xx
	ld	(hl),e		; Set disk buffer
	inc	hl
	ld	(hl),d
	ret
;
; Perform OS function
;
l0a77:
	ld	de,l0644	; Point to file
	ld	hl,(BankBDOS)	; Get BDOS address
	call	l0a83		; Call it
	ld	a,l
	ld	b,h
	ret
;
; Jump thru HL
;
l0a83:
	jp	(hl)
;
MemBank:
	db	1		; BIOS selected bank
l0a85:
	db	1		; Internal bank
DMAptr:
	dw	DMA
l0a88:
	dw	0		; System address 1
l0a8a:
	dw	0		; System address 2
;
; Hash and FCB save area
;
HshSave:
	ds	HshSavL
;
	db	'?????????????'
;
l0ax1::
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
	ret
LocStk	equ	$
l0afe:
	dw	0

	end


