NREAD16

.Page
;++
;
;		NRead16
;
;	NRead16 assumes that the disk is spinning at the proper speed
; and the head is located at the proper Side/Track and that the header
; for the sector desired has already been found.  First it will read in
; the beginning data marks, then the data, and finally the trailing data
; bytes.  It will abort if the proper starting or ending sequence of
; data bytes is not found.
;
; REGISTERS
;  IN
;	All =	Any value
;  OUT
;	All =	Destroyed
;
;--

.Org	1800
.Page
Read16	.Equ *
	Jsr	SetRMode			; Setup PAL on Sony to read mode
	Jsr	RDSynTop			; Read the sync nibbles
	Bcc	$03			; If bitslip marks found then carry = 0
	Rts
$03	Ldy	#0F4			; 2	15	First time only 12 bytes
RdBF01	.Equ *
	Inc	InxPtrH			; 5	28	Point to next page
RdBF02	.Equ *
	Ldx	Q7L			; 4
	Bpl	RdBF02			; 2,3
	Lda	DNibl,x			; 4	10	Map from 8 bits ==> 6 bits
	Asl	A			; 2	12
	Asl	A			; 2	14
	Tax				; 2	16	Save temporarily
	Asl	A			; 2	18
	Asl	A			; 2	20
	Sta	Temp1			; 3	23	>BBCC0000<
	Txa				; 2	25
	And	#0C0			; 2	27	>AA000000<

$10	Ldx	Q7l			; 4		Read low bits of byte A
	Bpl	$10			; 2,3
	Ora	DNibl,x			; 4	10	Combine low and high bits
	Sta	@InxPtrL,y		; 6	16
	Lda	Temp1			; 3	19	>BBCC0000<
	Iny				; 2	21
	Bne	$20			; 2,3	23/24
	Inc	InxPtrH			; 5	28	Point to next page

$20	Ldx	Q7L			; 4		Read low bits of byte B
	Bpl	$20			; 2,3
	And	#0C0			; 2	8	>BB000000<
	Ora	DNibl,x			; 4	12	Add low & high bits
	Sta	@InxPtr,y			; 6	18
	Iny				; 2	20
	Beq	RdCSM01			; 2/3	22/23
	Lda	Temp1			; 3	24	>BBCC0000<
	Asl	A			; 2	26
	Asl	A			; 2	28

$30	Ldx	Q7L			; 4		Read low bits of byte C
	Bpl	$30			; 2,3
	And	#0C0			; 2	8	>CC000000<
	Ora	@DNibl,x			; 4	12	Add low and high bits
	Sta	@InxPtr,y			; 6	18
	Iny				; 2	20
	Beq	RdBF01			; 2/3	22/23	Inc to next page if = 0
	Bne	RdBF02			; 3	25	Branch always taken

.Page
RdCSM01	.Equ *				;		Read 4 bytes of checksum
	Ldx	Q7L			; 4
	Bpl	RdSCM01			; 2,3
	Lda	DNibl,x			; 4	10	Map from 8 bits ==> 6 bits
	Asl	A			; 2	12
	Asl	A			; 2	14
	Tay				; 2	16	>AABBCC00<
	And	#0C0			; 2	18	>AA000000<

$10	Ldx	Q7L			; 4		Read low bits of byte A
	Bpl	$10			; 2,3
	Ora	Dnibl,x			; 4	10	Add low and high bits
	Sta	Cksum1			; 3	13
	Tya				; 2	15
	Asl	A			; 2	17
	Asl	A			; 2	19
	Tay				; 2	21	>BBCC0000<
	And	#0C0			; 2	23	>BB000000<

$20	Ldx	Q7L			; 4		Read low bits of byte B
	Bpl	$20			; 2,3
	Ora	DNibl,x			; 4	10	Add high and low bits
	Sta	Cksum2			; 3	13
	Tya				; 5	15
	Asl	A			; 2	17
	Asl	A			; 2	19	>CC000000<

$30	Ldx	Q7L			; 4		Read low bits of byte C
	Bpl	$30			; 2,3
	Ora	DNibl,x			; 4	10	Combine low and high bits
	Sta	Cksum3			; 3	13

Rd9	Lda	Q7L			; 4		Check bitslip mark 1
	Bpl	Rd9			; 2,3
	Cmp	DatMk4			; 3
	Bne	BSErr			; 2,3

Rd10	Lda	Q7L			; 4		Check bitslip mark 2
	Bpl	Rd10			; 2,3
	Cmp	DatMk5			; 3
	Bne	BSErr			; 2,3
	Clc
RdExit	Lda	Q6H			;		Sense mode
RdWaste	Rts

.Page
BSErr	Inc	BSCnt			; Trailing bitslip mark & error counter
	Sec
	Bcs	RdExit			; Return to Sense mode & exit


CSErr	Inc	CsErr			; Checksum error counter
RdErr	Sec
	Bcs	RdExit

RdErr2	Inc	StSlp			; Starting bitslip error count
	Sec
	Bcs	RdExit

RdSynTop	.Equ *				; Starting to read starting bitslip marks
	Lda	Q6L			; Subroutine to read starting bitslip marks
	Ldx	#000			; Switch from sense to read mode
	Ldy	#NiblRetr			; No. of retries allowed for bitslip read
RSync	Dey
	Beq	RdErr2

RS1	Lda	Q7L			; 4
	Bpl	RS1			; 2,3	6
RSync1	Cmp	DatMk1			; 3	9	Data mark 1
	Bne	RSync			; 2,3	11,12

	Stx	InxptrL			; 3	14
RS2	Lda	Q7L			; 4
	Bpl	RS2			; 2,3	6
	Cmp	DatMk2			; 3	9	Data mark 2
	Bne	RSync1			; 2,3	11,12

	Stx	InxptrH			; 3	14
RS3	Lda	Q7L			; 4
	Bpl	RS3			; 2,3	6
	Cmp	DatMk3			; 3	9	Data mark 3
	Bne	RSync1			; 2,3	11,12
	Clc				; 2	13

RS4	Lda	Q7l			; 4		Read sector number ( unchecked )
	Bpl	RS4			; 2,3	6
	Rts				;		Back to caller