	.z80

; SWAPRSX.MAX - Cobmined tihw SWAPCOM.MAC
; Interrogate DRVTBL to display table, or swap entries

	cseg

;------------;
; RSX Header ;
;------------;

	ds	6
	jp	drvtble
bdos:
	jp	$-$
	dw	$-$
	db	0ffh
	db	0
	db	'DRVTBLE '
	db	0,0,0

	dseg

;----------------------------------;
; BDOS SCBPB - Common base address ;
;----------------------------------;
scbpb1:
	db	5dh
	db	00h

;-----------;
; Data area ;
;-----------;
dr1:	dw	0		; 1st drive number (0-15)
dr2:	dw	0		; 2nd drive number (0-15)

table:				; copy of DRVTBLE
	ds	16*2

cr	equ	0dh
lf	equ	0ah

msg0:	db	'DRVTBLE',cr,lf,'$'
msg1:	db	'@: 0000H$'
crlf:	db	cr,lf,'$'

;---------------
; Error messages
;---------------
memerr:	db	'*** Insufficient common memory$'
nodrv:	db	'*** BIOS does not support DRVTBL$'
swapit:	db	'SWAP completed$'

	cseg

;-------
; ERRORs
;-------
nomem:				; RSX not loaded in COMMON
	ld	de,memerr
	jr	strout

baddrv:				; DRVTBL not found
	call	tpabank		; return to TPA bank
				; and breath again
	ld	de,nodrv
strout:
	ld	c,9
	jr	bdos

;============================================================;
; Main RSX program - first check BDOS & RSX function numbers ;
; NOTE - Stack is presumed to be in CCP Loader               ;
;============================================================;

drvtble:
	ld	a,c
	cp	60		; BDOS function 60
	jr	nz,bdos		; -not ours-
	ld	a,(de)
	cp	127		; RSX function 127
	jr	nz,bdos		; -not ours-
	inc	de
	ld	a,(de)
	cp	1		; Test for one parameter word
	jr	nz,check	; -no- so dont fetch any

	inc	de		; collect 1 parameter word
	ld	a,(de)
	ld	(dr1),a		; 1st byte is drive 1
	inc	de
	ld	a,(de)
	ld	(dr2),a		; 2nd byte is drive 2
;-------------------------------------------;
; check this RSX is loaded in COMMON memory ;
;-------------------------------------------;
check:
	ld	de,scbpb1	; Check above COMMON
	ld	c,49		; Get SCB
	call	bdos
	ld	de,bdos		; use this address for check
	ld	a,h
	cp	d
	jr	c,common	; -ok, in common-
	jr	nz,nomem	; -not in common-
	ld	a,e
	cp	l
	jr	c,nomem		; -not in common-

;------------------------------------------------------;
; CRITICAL CODE AS SWITCHING FROM TPA INTO SYSTEM BANK ;
;------------------------------------------------------;
common:	
	ld	hl,(0001)	; BIOS jmp vector address
	ld	(wboot),hl	; saved

	call	sysbank		; select system bank

	ld	de,3*(22-1)	; Return DRV TBL
	call	bios

	ld	a,l		; Test DRVTBL for FFF0-FFFFH
	or	0Fh
	and	h
	inc	a
	jr	z,baddrv	; -no DRVTBL-

	push	hl
	ld	de,table
	ld	bc,16*2
	ldir			; Copy DRVTBL to RSX
	pop	de		; recover DRVTBL in DE

	ld	hl,(dr1)
	ld	a,(dr2)
	cp	l		; Check for DR1 different to DR2
	jr	z,list		; -no- so display DRVTBL
;-----------------------------------;
; SWAP DPH for DR1 with DPH for DR2 ;
;-----------------------------------;
	add	hl,hl
	add	hl,de		; HL -> DPH for DR1
	push	hl
	ld	c,(hl)
	inc	hl
	ld	b,(hl)		; BC = DPH for DR1

	ld	hl,(dr2)
	add	hl,hl
	add	hl,de		; HL -> DPH for DR2
	ld	a,(hl)
	ld	(hl),c
	ld	c,a
	inc	hl
	ld	a,(hl)
	ld	(hl),b		; DR1 DPH moved to DR2
	ld	b,a		; BC = DPH for DR2

	pop	hl		; recover -> DPH for DR1
	ld	(hl),c
	inc	hl
	ld	(hl),b		; DR2 DPH moved to DR1

	call	tpabank		; return to TPA bank
				; and breath again

	ld	de,swapit	; SWAP completed
	jr	strout		; display and terminate

;----------------;
; Display DRVTBL ;
;----------------;
list:
	call	tpabank		; return to TPA bank
				; and breath again
	ld	de,msg0		; Header message
	call	strout

	ld	de,table	; RSX copy of DRVTBL
	ld	b,16		; A: to P: entries
loop:	
	ld	hl,msg1		; prepare message for each drive
	inc	(hl)
	inc	hl
	inc	hl
	inc	hl
	ld	a,(de)
	inc	de
	ld	c,a
	ld	a,(de)		; test DPH = 0000H
	or	c
	ld	a,(de)
	inc	de
	jr	z,nodph		; -yes-
	call	hexm		; convert high DPH into HEX
	ld	a,c
	call	hexm		; convert low DPH into HEX
	jr	lst.dr
nodph:	ld	(hl),'$'	; no DPH so terminate string early
lst.dr:	push	de
	push	bc
	ld	de,msg1
	call	strout
	ld	de,crlf		; add new line
	call	strout
	pop	bc
	pop	de
	djnz	loop		; repeat for all 16 drives
	ret			; terminate RSX

hexm:	push	af		; convert number in A
	rrca			; into HEX ascii in (HL)
	rrca
	rrca
	rrca
	call	hex
	pop	af
hex:	and	0fh
	add	a,90h
	daa
	adc	a,'0'+10h
	daa
	ld	(hl),a		; add ascii HEX character to string
	inc	hl
	ret

sysbank:
	xor	a		; system bank
	jr	setbnk
tpabank:
	ld	a,1		; tpa bank
setbnk:
	ld	de,3*(27-1)	; Select memory bank 
bios:
	ld	hl,(wboot)
	add	hl,de
	jp	(hl)		; go to BIOS function
wboot:
	dw	$-$		; fill with address of wboot


	end
