	title	CP/M spool program - the remover
	name	('DESPOOL')

; A CP/M spooler

; This is the remover of the spooler RSX

OS	equ	0000h
BDOS	equ	0005h

.conin	equ	 1
.string	equ	 9
.vers	equ	12
.delete	equ	19
.usrcod	equ	32
.rsx	equ	60

_LIST	equ	5

BIOSjp	equ	3

CPMv	equ	31h

SPOLrsx	equ	120

_GETDAT	equ	9		; DESPOOL parameter function

_PERM	equ	14		; Permanent flag in RSX
_NAME	equ	16		; Name offset in RSX

bell	equ	07h
lf	equ	0ah
cr	equ	0dh
eof	equ	1ah
eot	equ	'$'

UPPMASK	equ	11011111b
ALL	equ	11111111b

	jp	MAIN
;
	db	cr,lf,lf,lf,lf,lf,lf,lf,lf,lf,lf,lf,lf
	db	'M.M.L. Systems  Ltd. 11 Sun Str.London EC2M 2PS '
	db	'(C) Copyright   1986            '
;
	db	eof,eof,eof,eof,eof,eof,eof,eof,eof
	db	eof,eof,eof,eof,eof,eof,eof
;
MAIN:
	ld	c,.os
	call	BDOS		; Get OS version
	cp	CPMv		; Verify correct one
	ld	de,$ILL.OS
	ld	c,.string
	jp	nz,BDOS		; Tell error if not
	ld	de,RSXpar
	ld	c,.rsx
	call	BDOS		; Get parameters
	jr	nz,InvName	; Cannot despool
	ld	hl,(RSX.adr)	; Fetch address of RSX
	ld	l,_NAME		; Point to name
	ld	de,$SPOOL
	ld	b,NamLen
TstName:
	ld	a,(de)		; Test name
	cp	(hl)
	jr	nz,InvName	; Cannot despool if name is wrong
	inc	hl
	inc	de
	djnz	TstName
	ld	l,_PERM
	ld	a,(hl)		; Test RSX permanent
	or	a
	jr	nz,InvName	; Cannot despool if not
	call	IsDisabled	; Test to be disabled
	jr	nz,Exit		; Nope
	ld	hl,(OS+1)	; Get BIOS vector
	ld	l,0		; Reset to cold start address
	ex	de,hl
	ld	hl,(BIOS.adr)	; Get address of original BIOS table
	ld	bc,(BIOS.len)	; Get length of BIOS table
	ldir			; Reset table
	ld	hl,BIOS.lstx	; Point to special table
BIOS.reset:
	ld	c,(hl)		; Fetch BIOS index
	inc	hl
	ld	b,(hl)
	inc	hl
	ld	e,(hl)		; Fetch BIOS address
	inc	hl
	ld	d,(hl)
	inc	hl
	ld	a,b		; Test end of table
	or	c
	jr	z,BIOS.done	; Yeap
	push	hl
	ld	hl,(OS+1)	; Get BIOS address
	ld	l,0		; Fix for real start
	add	hl,bc
	ex	de,hl
	ld	bc,BIOSjp
	ldir			; Unpack address
	pop	hl
	jr	BIOS.reset
BIOS.done:
	ld	hl,(RSX.adr)	; Get address of RSX
	ld	l,_PERM
	ld	(hl),-1		; Reset permanent flag
	call	DelFCB		; Delete spooler file
	ld	de,$SPOOL.RM
	jr	SpoolMsg	; Tell despooler ready
InvName:
	ld	de,$SPOOL.NOREM	; Cannot despool
SpoolMsg:
	ld	c,.string
	call	BDOS
Exit:
	ld	c,.os
	jp	BDOS
;
; Test RSX to be disabled - Z set yes yes
;
IsDisabled:
	ld	hl,(OS+1)	; Get address of BIOS table
	ld	l,BIOSjp*_LIST+1
	ld	e,(hl)		; Get address of printer output
	inc	hl
	ld	d,(hl)
	ld	hl,(BIOS.adr)	; Get address of original BIOS table
	ld	bc,BIOSjp*_LIST+1
	add	hl,bc
	ld	a,(hl)		; Fetch address
	inc	hl
	ld	h,(hl)
	ld	l,a
	sbc	hl,de		; Test same
	ret	z		; Ok, disable RSX
	ld	hl,(BIOS.lst)	; Get address of printer output
	inc	hl
	ld	a,(hl)		; Get vector
	inc	hl
	ld	h,(hl)
	ld	l,a
	sbc	hl,de		; Test same
	jr	z,SpoolDis	; Yeap, already disabled
	ld	de,$CRLF
	ld	c,.string
	call	BDOS		; Give new line
SpoolAbort:
	ld	de,$SPOOL.ABORT
	ld	c,.string
	call	BDOS		; Tell spooler active, disable?
	ld	c,.conin
	call	BDOS		; Get answer
	and	UPPMASK
	cp	'Y'
	ret	z		; Ok, disable it
	cp	'N'
	jr	nz,SpoolAbort
	ld	de,$SPOOL.ACT
	ld	c,.string
	call	BDOS		; Tell it stays active
	or	ALL
	ret
SpoolDis:
	ld	de,$SPOOL.DIS	; Spooler already disabled
	ld	c,.string
	call	BDOS
	or	ALL
	ret
;
; Delete spooler file
;
DelFCB:
	ld	hl,(RSX.FCB)	; Get address of spool FCB
	dec	hl		; Fix for user
	ld	e,(hl)
	ld	c,.usrcod
	call	BDOS		; Set user
	ld	c,.delete
	ld	de,(RSX.FCB)	; Get address of spool FCB
	jp	BDOS		; Delete it
;
RSXpar:
	db	SPOLrsx
	db	_GETDAT
RSX.adr:
	dw	0		; Address of SPOOL RSX
RSX.FCB:
	dw	0		; Address of SPOOL FCB
BIOS.adr:
	dw	0		; Address if original BIOS jump table
BIOS.len:
	dw	0		; Length of BIOS table
BIOS.lstx:
	dw	0		; Index to BIOS printer output
BIOS.lst:
	dw	0		; BIOS printer output
	dw	0		; Index to BIOS select memory bank
	dw	0		; BIOS select memory bank
	dw	0		; End marker
$ILL.OS:
	db	'DESPOOL: ? Requires CP/M 3.1',eot
$SPOOL.RM:
	db	cr,lf,'SPOOL removed - output to LST: device not spooled',eot
$SPOOL.DIS:
	db	cr,lf,'DESPOOL Error: SPOOLER already disabled',eot
$SPOOL.ACT:
	db	cr,lf,'<< SPOOLER remains active >>',eot
$SPOOL.NOREM:
	db	cr,lf,'<< Cannot DESPOOL LST: spooler >>',eot
$CRLF:
	db	cr,lf,eot
$SPOOL.ABORT:
	db	cr,bell
	db	'SPOOLER is active - Abort spooler (Y/N) ?',eot
$SPOOL:
	db	'SPOOL   '
NamLen	equ	$-$SPOOL

	end


