	title	'Disassembly of POWERSOFT RS Series disk driver'
	header	'external equates'
	psect	3000h
wrprot:	equ	0c0h
reset:	equ	0c1h
data:	equ	0c8h
error:	equ	0c9h
count:	equ	0cah
sector:	equ	0cbh
cyllow:	equ	0cch
cylhi:	equ	0cdh
sdh:	equ	0ceh
cmdstat:equ	0cfh
	

begin:	push	de
	ld	hl,l33ef
	ld	b,7eh
	ld	c,0
l3008:	ld	a,(hl)
	add	a,c
	ld	c,a
	inc	hl
	djnz	l3008
	
	cp 	6fh
	jr	z,l3018
	
	ld	hl,0ffffh
	ld	a,15h
	rst	28h
l3018:	ld	hl,l33f0
	call	l339a
	ld	hl,l3563
	ld	a,65h
	rst	28h
	bit	3,(iy+2)
	pop	iy
	jp	z,l337f
	
	ld	hl,l3599
	ld	a,(iy+0)
	cp 	0c9h
	jp	nz,l337f
	
	ld	a,(iy+3)
	and	9ch
	or 	0ch
	ld	(iy+3),a
	ld	a,(iy+4)
	and	0d0h
	or 	90h
	ld	(iy+4),a
	ld	a,0
	ld	(iy+5),a
	ld	a,98h
	ld	(iy+6),a
	ld	a,1fh
	ld	(iy+7),a
l305b:	ld	hl,l346d
	call	l33cf
	jr	z,l307f
	
	ld	a,(hl)
	sub	31h
	jr	c,l305b
	
	cp 	4
	jr	nc,l305b
	
	or 	(iy+3)
	ld	(iy+3),a
	and	3
	rlca
	rlca
	rlca
	and	18h
	or 	(iy+5)
	ld	(iy+5),a
l307f:	ld	hl,l348a
	call	l33cf
	jr	z,l30aa
	
	call	l33ac
	jr	nz,l307f
	
	ld	a,b
	or 	a
	jr	nz,l307f
	
	or 	c
	jr	z,l307f
	
	cp 	9
	jr	nc,l307f
	
	dec	c
	ld	a,(iy+5)
	and	0f8h
	or 	c
	ld	(iy+5),a
	ld	a,(iy+4)
	and	0f0h
	or 	c
	ld	(iy+4),a
l30aa:	ld	hl,l34a7
	call	l33cf
	jr	z,l30d6
	
	call	l33ac
	jr	nz,l30aa
	
	ld	a,b
	or 	a
	jr	nz,l30aa
	
	ld	a,c
	or 	a
	jr	z,l30aa
	
	ld	a,(iy+5)
	and	7
	add	a,c
	jr	c,l30aa
	
	cp 	9
	jr	nc,l30aa
	
	ld	a,c
	dec	a
	rrca
	rrca
	rrca
	or 	(iy+7)
	ld	(iy+7),a
l30d6:	ld	a,0fh
	ld	(iy+8),a
	ld	hl,l34bf
	call	l33cf
	jr	z,l30f8
	
	call	l33ac
	jr	nz,l30d6
	
	ld	a,b
	or 	a
	jr	nz,l30d6
	
	ld	a,c
	or 	a
	jr	z,l30d6
	
	cp 	21h
	jr	nc,l30d6
	
	dec	a
	ld	(iy+8),a
l30f8:	ld	hl,20h
	ld	a,(iy+7)
	rlca
	rlca
	rlca
	and	7
	inc	a
	call	l33a7
	ld	h,l
	ld	l,a
	ld	a,(iy+8)
	and	1fh
	inc	a
	call	l33a2
	or 	a
	jr	nz,l30d6
	
	ld	a,l
	or 	a
	jr	z,l30d6
	
	cp 	9
	jr	nc,l30d6
	
	dec	a
	rrca
	rrca
	rrca
	ld	c,a
	ld	a,(iy+8)
	and	1fh
	or 	c
	ld	(iy+8),a
l312b:	ld	hl,l34e3
	call	l33cf
	jr	z,l3142
	
	call	l33ac
	jr	nz,l312b
	
	ld	ix,l35f6
	ld	(ix+0),c
	ld	(ix+1),b
l3142:	ld	a,(iy+5)
	ld	(l3604),a
	push	iy
	ld	c,0
	ld	a,51h
	rst	28h
	ld	b,0
l3151:	ld	a,(iy+0)
	cp 	0c3h
	jr	nz,l31a2
	
	ld	h,(iy+2)
	ld	l,(iy+1)
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	ld	a,57h
	cp 	(hl)
	jr	nz,l31a2
	
	inc	hl
	ld	a,44h
	cp 	(hl)
	jr	nz,l31a2
	
	ld	a,b
	ld	(l3289),a
	ld	a,(iy+5)
	ld	c,18h
	and	c
	ex	(sp),iy
	push	bc
	push	af
	ld	a,(iy+5)
	and	c
	ld	b,a
	pop	af
	cp 	b
	pop	bc
	ex	(sp),iy
	jr	nz,l31a2
	
	ld	a,0ffh
	ld	(l3335),a
	ld	de,8h
	add	hl,de
	ld	a,(l35f6)
	cp 	(hl)
	jr	nz,l31a2
	
	inc	hl
	ld	a,(l35f7)
	cp 	(hl)
	jr	nz,l31a2
	
	ld	(l3268),iy
l31a2:	ld	de,0ah
	add	iy,de
	inc	b
	ld	a,b
	cp 	7
	jr	nz,l3151
	
	pop	iy
l31af:	ld	hl,l3508
	call	l33cf
	jr	z,l321f
	
	call	l33ac
	jr	nz,l31af
	
	ld	a,b
	cp 	2
	jr	nc,l31af
	
	ld	a,c
	bit	0,b
	jr	nz,l31d0
	
	cp 	2
	jr	c,l31af
	
	cp 	0cch
	jr	c,l321b
	
	jr	l31d4
	
l31d0:	cp 	97h
	jr	nc,l31af
	
l31d4:	ld	h,b
	ld	l,c
	ld	a,2
	call	l33a2
	or 	a
	jr	nz,l31af
	
	push	hl
	ld	a,(iy+7)
	rlca
	rlca
	rlca
	and	7
	inc	a
	ld	l,a
	Xor	a
l31ea:	add	a,20h
	dec	l
	jr	nz,l31ea
	
	pop	hl
	dec	a
	cp 	80h
	jr	nc,l31af
	
	push	hl
	ld	hl,40h
	ld	a,(iy+7)
	rlca
	rlca
	rlca
	and	7
	inc	a
	call	l33a7
	ld	h,l
	ld	l,a
	ld	a,(iy+8)
	and	1fh
	inc	a
	call	l33a2
	ld	a,l
	cp 	9
	pop	hl
	jr	nc,l31af
	
	set	5,(iy+4)
	ld	a,l
l321b:	dec	a
	ld	(iy+6),a
l321f:	ld	a,(iy+6)
	inc	a
	srl	a
	ld	(iy+9),a
l3228:	ld	hl,l3526
	call	l33cf
	ld	a,0
	jr	z,l3241
	
	call	l33ac
	jr	nz,l3228
	
	ld	a,b
	or 	a
	jr	nz,l3228
	
	ld	a,c
	cp 	10h
	jr	nc,l3228
	
	ld	a,c
l3241:	or 	10h
	ld	(l3614),a
l3246:	ld	hl,l3539
	call	l33cf
	jr	z,l325e
	
	ld	a,(hl)
	call	l33e6
	cp 	4eh
	jr	z,l325e
	
	cp 	59h
	jr	nz,l3246
	
	res	2,(iy+3)
l325e:	call	l35f8
	ld	hl,l3550
	call	l339a
	ld	hl,0h
	l3268:	equ	$-2
	ld	a,h
	or 	l
	jr	z,l327b
	
	inc	hl
	ld	a,(hl)
	ld	(iy+1),a
	inc	hl
	ld	a,(hl)
	ld	(iy+2),a
	jp	l3378
	
l327b:	ld	hl,entry
	ld	bc,0102h
	ld	de,l361d
	ld	ix,l35ce
	ld	a,8
	l3289:	equ	$-1
	cp 	8
	jr	nc,l32bd
	
	ld	c,a
	push	iy
	ld	a,51h
	rst	28h
	ex	de,hl
	ld	l,(iy+1)
	ld	h,(iy+2)
	ld	bc,0ch
	add	hl,bc
	ld	a,(hl)
	inc	hl
	ld	h,(hl)
	ld	l,a
	ld	(l35e0),hl
	ex	de,hl
	pop	iy
	ld	bc,35h
	ld	ix,l35d6
	ld	a,(l3335)
	or 	a
	jr	z,l32bd
	
	ld	bc,10h
	ld	ix,l35de
l32bd:	ex	de,hl
	ld	(l35f4),hl
	push	de
	ld	de,494bh
	ld	a,52h
	rst	28h
	dec	hl
	ld	d,(hl)
	dec	hl
	ld	e,(hl)
	ld	(l32e5),hl
	ld	h,b
	ld	l,c
	add	hl,de
	dec	hl
	ld	(endmod),hl
	push	bc
	ld	bc,1300h
	Xor	a
	sbc	hl,bc
	pop	bc
	jr	nc,l32f2
	
	ld	hl,(endmod)
	inc	hl
	ld	(0h),hl
	l32e5:	equ	$-2
	push	de
	ld	hl,l357c
	call	l339a
	pop	hl
	pop	de
	jr	l330a
	
l32f2:	ld	hl,l358a
	call	l339a
	pop	de
	call	l3387
	ld	(endmod),hl
	or 	a
	sbc	hl,bc
	push	bc
	ld	a,64h
	ld	b,0
	rst	28h
	pop	bc
	inc	hl
l330a:	push	hl
	push	de
	or 	a
	sbc	hl,de
	ex	de,hl
l3310:	ld	l,(ix+0)
	ld	h,(ix+1)
	ld	a,h
	and	l
	inc	a
	jr	z,l3328
	
	ld	a,(hl)
	add	a,e
	ld	(hl),a
	inc	hl
	ld	a,(hl)
	adc	a,d
	ld	(hl),a
	inc	ix
	inc	ix
	jr	l3310
	
l3328:	ld	a,(l36af)
	cp 	0f8h
	jr	nc,l3334
	
	ld	a,2ch
	ld	(l36b3),a
l3334:	ld	a,0
	l3335:	equ	$-1
	or 	a
	jr	nz,l3361
	
	push	iy
	ld	a,65h
	rst	28h
	ld	a,(iy+1ch)
	ld	(l3610),a
	cp 	0c9h
	jr	z,l3351
	
	ld	h,(iy+1eh)
	ld	l,(iy+1dh)
	ld	(l3611),hl
l3351:	ld	a,0c3h
	ld	(iy+1ch),a
	ld	hl,l35f8
	ld	(iy+1eh),h
	ld	(iy+1dh),l
	pop	iy
l3361:	ld	hl,(l35e0)
	ld	a,h
	or 	l
	jr	z,l336b
	
	ld	(l35f4),hl
l336b:	pop	de
	pop	hl
	push	hl
	ex	de,hl
	ldir
	
	pop	hl
	ld	(iy+1),l
	ld	(iy+2),h
l3378:	ld	(iy+0),0c3h
	ld	hl,l3562
l337f:	call	l339a
	Xor	a
	ld	hl,0h
	ret
	
l3387:	push	bc
	ld	a,64h
	ld	b,0
	ld	hl,0h
	rst	28h
	pop	bc
	ret
	
l3392:	push	iy
	ld	a,9
	rst	28h
	pop	iy
	ret
	
l339a:	push	iy
	ld	a,0ah
	rst	28h
	pop	iy
	ret
	
l33a2:	ld	c,a
	ld	a,5eh
	rst	28h
	ret
	
l33a7:	ld	c,a
	ld	a,5bh
	rst	28h
	ret
	
l33ac:	ld	bc,0h
l33af:	ld	a,(hl)
	cp 	0dh
	ret	z
	
	sub	30h
	jr	c,l33cc
	
	cp 	0ah
	jr	nc,l33cc
	
	push	hl
	ld	h,b
	ld	l,c
	add	hl,hl
	add	hl,hl
	add	hl,bc
	add	hl,hl
	ld	c,a
	ld	b,0
	add	hl,bc
	ld	b,h
	ld	c,l
	pop	hl
	inc	hl
	jr	l33af
	
l33cc:	or 	0ffh
	ret
	
l33cf:	call	l339a
	ld	hl,l35e2
	ld	b,5
	call	l3392
	jr	c,l33df
	
	Xor	a
	cp 	b
	ret
	
l33df:	pop	af
	ld	hl,l35be
	jp	l337f
	
l33e6:	cp 	61h
	ret	c
	
	cp 	7bh
	ret	nc
	
	and	5fh
	ret
	
l33ef:	ret	z
	
l33f0:	inc	e
	rra 
	defm	'Wd/dct - Western digital rigid i/o Driv'
	defm	'er - Version 3.1/sVc'
	defb	0ah
	defm	'copyright (c) 1983 - breeze/qsd, inc. -'
	defm	' dallas, tX'
	defb	0ah
	defm	'series rs'
	defb	0ah
	defb	0ah
	defb	0dh
l346d:	defm	'drive select address (1-4)? '
	inc	bc
l348a:	defm	'starting head number (1-8)? '
	inc	bc
l34a7:	defm	'number of heads (1-8)? '
	inc	bc
l34bf:	defm	'number of sectors per gran (4-32)? '
	inc	bc
l34e3:	defm	'starting cylinder number (0-65535)? '
	inc	bc
l3508:	defm	'number of cylinders (2-406)? '
	inc	bc
l3526:	defm	'step rate (0-15)? '
	inc	bc
l3539:	defm	'removable disk (y/n)? '
	inc	bc
l3550:	defm	'driver installed '
	inc	bc
l3562:	defb	0dh
l3563:	defm	'Must install via systeM!'
	defb	0dh
l357c:	defm	'in loW memory'
	inc	bc
l358a:	defm	'in high memory'
	inc	bc
l3599:	defm	'requested drive slot already in use!'
	defb	0dh
l35be:	defm	'program aborted'
	defb	0dh
l35ce:	call	p,2135h
	
	ld	(hl),0c3h
	ld	(hl),0afh
	ld	(hl),1
	l35d6:	equ	$-1
	ld	(hl),0ah
	ld	(hl),0eh
	ld	(hl),57h
	inc	sp
l35de:	rst	38h
	rst	38h
l35e0:	defb	0
	defb	0
l35e2:	defm	'xxxxxx'

	header	'actual driver'

entry:	jr	start
	
endmod:	nOp
	nop
	defb	2
	defm	'Wd'
	nop
	nop
	nop
	nop

start:	call	0h			;Patched in mini-driver
l35f4:	equ	$-2

l35f6:	nop				;ZERO OFFSET - HOW STUPID!
l35f7:	nop

l35f8:	ld	a,10h			;reset controller
	out	(reset),a
	ld	a,0ch			;drop reset and enable
	out	(reset),a
	call	l3617

	ld	a,0
	l3604:	equ	$-1
	out	(sdh),a		;load sdh

	ld	a,6
	call	l3613
	Xor	a
	call	l3613
l3610:	ret
	
l3611:	ld	b,h
	ld	c,b

l3613:	or 	0
l3614:	equ	$-1

	out	(cmdstat),a		;issue command

l3617:	in	a,(cmdstat)		;read status
	rlca
	jr	c,l3617			;While busy, loop
	
	ret				;command complete, exit
	
l361d:	ex	(sp),ix
	push	hl
	call	chkcmd
	pop	hl
	pop	ix
	ret
	
chkcmd:	ld	a,b
	cp 	9
	jr	z,rdsec			;read a sector
	
	cp 	0dh
	jr	nc,wrsec		;Write any type of sector
	
	cp 	0ah
	jr	z,rdsec			;Verify a sector
	
	Xor	a			;return z for everything else
	ret
	
wrsec:	ld	a,(iy+3)
	and	3
	push	bc
	ld	b,a
	in	a,(wrprot)
	jr	z,l3644
	
l3641:	rlca
	djnz	l3641
	
l3644:	pop	bc
	bit	7,a
	ld	a,0fh
	ret	nz
	
	bit	7,(iy+3)
	ret	nz
	
rdsec:	ld	c,0
	push	de
	bit	5,(iy+4)
	jr	z,l3668
	
	sla	d
	jr	nc,l365d
	
	inc	c
l365d:	ld	a,e
	sub	(iy+7)
	jr	c,l3668
	
	jr	z,l3668
	
	ld	e,a
	dec	e
	inc	d
l3668:	in	a,(cmdstat)
	rlca
	jr	c,l3668
	
	ld	a,e
	and	1fh
	out	(sector),a
	Xor	e
	rlca
	rlca
	rlca
	add	a,(iy+5)
	out	(sdh),a
	ld	a,d
	add	a,(ix+0)
	out	(cyllow),a
	ld	a,c
	adc	a,(ix+1)
	out	(cylhi),a
	pop	de
	in	a,(cmdstat)
	cpl 
	rrca
	and	20h
	ret	nz
	
	bit	2,b
	jr	z,l36be
	
	ld	a,b
	cp 	0fh
	ld	a,30h
	jr	nz,l369c
	
	ld	a,50h
l369c:	out	(cmdstat),a
	ld	bc,data
	otir
	
	dec	b
l36a4:	in	a,(cmdstat)
	rlca
	jr	c,l36a4
	
	in	a,(cmdstat)
	and	1
	ret	z
	
	ld	hl,l36e1
	l36af:	equ	$-2
	in	a,(error)
l36b3:	inc	hl
	rla 
	jr	nc,l36b3
	
	ld	a,(hl)
	bit	2,b
	ret	nz
	
	sub	8
	ret
	
l36be:	ld	a,20h
	out	(cmdstat),a
	call	l36a4
	push	af
	bit	0,b
	ld	bc,data
	jr	z,l36d1
	
	inir
	
	jr	l36d5
	
l36d1:	in	a,(data)
	djnz	l36d1
	
l36d5:	pop	af
	ret	nz
	
	ld	a,d
	sub	(iy+9)
	jr	z,l36df
	
	Xor	a
	ret
	
l36df:	or 	6
l36e1:	ret
	
	rrca

	defb	0ch
	defb	09h
	defb	0dh
	defb	08h
	defb	0bh
	defb	0ah
	defb	0dh
end:	end	begin
