;                         EXRDTRAK.ASM
;	Assemble this program using MAC with the Osborne
;	Executive and load it using HEXCOM.  Use it with SID
;	to read all the information on a floppy disk track.
;	See documentation for further details.
;				Paul Woodie
;				Woodie.CPE at dockmaster.arpa
;				(301) 859-6044 day
;				(301) 974-0650 eve
	org	100h
	mvi	c,9		;print msg
	lxi	d,msg		;point to msg
	call	5		;call bdos
	mvi	a,0d0h		;force interrupt
	out	8		;send to fdc
	call	loopl		;long delay
	lxi	d,trkbuf	;(de)=addr of track # buffer
	mvi	c,10		;get console input buffer
	call	5		;bdos call
;	convert to binary track number
	xra	a		;clear a
	sta	tracknum	;make sure we start with 0
	lxi	h,tracknum	;point to binary val for track number
	lda	trkbuf+2	;get first byte that was input
	ani	00000011b	;use only last bits
	jz	byte2		;1st byte was zero
	call	add10		;add binary ten to trkbuf
	sui	1		;subtr 10 from first byte
	jz	byte2		;first byte was 10
	call	add10		;add bin ten to trkbuf
	sui	1		;subtr 10 from first byte
	jz	byte2		;1st byte was 20
	call 	add10		;add bin 10 to trkbuf
;now we've done first (tens) byte, now do units byte
byte2:	lda	trkbuf+3	;get 2nd byte that was input
	ani	00001111b	;use only lower nibble
	add	m		;add trkbuf to reg a
	sta	tracknum	;put back in tracknum
	mvi	a,39		;ensure that trkbuf < 40
	mov	b,m		;get trkbuf
	cmp	b		;compare reg b with reg a
	jnc	cont		;ok, continue
	mvi	c,9		;write error msg
	lxi	d,error	
	call	5		;write error msg
	jmp	fini		;end program

cont:	in	2		;get pia status
	ani	11111001b	;set drive bits to zero
	ori	00000010b	;turn on drive b
	out	2		;send to pia to turn drive on
	call	loopl		;long delay
	call	loopl		;another delay (approx .4 sec)
	lda	tracknum	;load binary for desired trk num
	out	0bh		;send to fdc track reg
	call	loops		;short delay
	mvi	a,1bh		;seek command
	out	8		;send to fdc
	call    loops		;short delay loop
test:	in	8		;is seek command finished?
	rar
	jc	test		;loop to test if not finished
	di			;disable interrupts
	mvi	a,0e0h		;read track command
	out	8		;send to fdc
t3:	in	8		;get status
	rar
	jnc	t3		;wait until fdc says it's busy
	lxi	b,0ffffh	;set up counter for # bytes to record
	lxi	h,6000h		;place to put bytes
t2:	in	8		;get fdc status
	rar
	jnc	fini		;is command finished yet?
	rar
	jnc	t2		;is a byte ready?
	in	0bh		;yes, get it!
	mov	m,a		;put byte in memory
	inx	h
	dcx	b
	mov	a,b
	ora	c
	jnz	t2		;test again for command complete
fini:	
	in	2		;get pia status
	ori	00000110b	;set drive bits to 1 (off)
	out	2		;send to pia to turn drive off
	ei			;enable interrupts
	rst	7		;return to sid

;	long delay loop
loopl:	lxi	b,0ffffh	;loop count
bb:	dcx	b
	mov	a,b
	ora	c
	jnz	bb
	ret
;	short delay loop
loops:	mvi	b,7
lp1:	dcr	b
	jnz	lp1
	ret

add10:	mov	d,a		;store a temporarily
	mvi	b,10		;number to add
add10a:	inr	m		;increment trkbuf
	dcr	b
	mov	a,b
	jnz	add10a
	mov	a,d		;get back a reg
	ret
;
msg:	db	1bh,1ah,0ah,0ah,0ah
	db	'Place disk to be read into drive B and then enter',0dh,0ah
	db	'the two-digit (dec) track number to be read, then <cr>.'
	db	0dh,0ah,'$'
error:
	db	0ah,0ah,0ah,'Track number error -- Sorry!',0dh,0ah,'$'


trkbuf:
	db	2h,0h
	ds	2
tracknum:
	db	0
	end
