NEWRWADDR

.Page
;++
;
;				Write Adr Field Subroutine
;
; (16 sector format) writes 27, 40-usec (10-bit) self-sync nibls, adr fields
; 16-sector start marks (0D5,0AA,096), Body (Track, Sector, Side, Volume,
; Checksum), end field marks, and the write turn off nibl.  It then jumps to
; the "WrSync" code which will write the starting sync for the data field,
; write the data fields and then return to the caller.
;--

	.Org	1200
WrSynTrK	.Equ *			; Write a sync track before first track
	Ldy	#00		; 256 sync bytes * 6 to write (about 2 sectors)
	Beq	WrAdr01

WAdr16	.Equ *
	Ldy	FmtGap		;		Default to 6*5 20 usec 'FF's

WrAdr01	Bit	IIob+Track	;		Look for bit six
	Bvc	$23
	Lda	#01
	Ora	IIob+Side
	Sta	IIob+side		;		set bit zero in side

$23	Lda	#1		;		flag for usage of AdrMk 1 & 2
	Jsr	Sync20		; 6	10/10	Write sync fields
	Lda	AdrMk3		; 3	19	Last byte in starting bitslip
	Jsr	WrNibl1		; 6

	Ldy	#02		; 2
$37	Ldx	IIob+Side,y	; 4		Write Track, Sector, then Side
	Jsr	WrByteX		; 6
	Dey
	Bpl	$37

	Ldx	FmtType		; 3	9	Format type
	Jsr	WrByteX		; 6	13/6
	Lda	IIob+Track	; 3	9	Create Address Checksum
	Eor	IIob+Sector	; 3	12
	Eor	IIob+Side		; 3	15
	Eor	FmtType		; 3	18
	Jsr	WrByte		; 6	24/6	Write the address checksum

	Lda	AdrMk4		; 3	9
	Jsr	WrNibl		; 6
	Lda	AdrMk5		; 3	9	Last byte in address field
	Jsr	WrNibl		; 6

	Jsr	ShtOff		; 6	16	Return to Sense mode
	Lda	#ErrHdr		; 2		UnderRun during header
	Bcs	WstTm		; 2,3		Abort upon error
	Jmp	Write16		; 3		Go & write data to disk


WstTm	Rts			; 6	10

.Page
;++
;			RdAdr
;--

RdAdr	.Equ *
	Lda	#0
	Sta	RangeL
	Lda	#RdAdrTmt
	Sta	RangeH
	Jsr	SetRMode		; Setup PAL on Sony to read mode
	Lda	Q6L		; Switch from SENSE to READ

RdAsyn	Inc	RangeL		; 5
	Bne	RdAd1		; 2,3
	Dec	RangeH		; 5
	Beq	RaErr1		; 2,3

RdAd1	Ldx	#00
$24	Lda	Q7L		; 4...
	Bmi	RdAsn1		; 2,3	Valid if high bit = 1
	Dex			; 2
	Bne	$24		; 2,3	Loop 255 times = 85 bytes
	Beq	RaErr1		; 3

RdAsn1	Cmp	AdrMk1		; 2	Address mark 1?
	Bne	RdAsyn		; 2,3	Branch if not

RdAd2	Lda	Q7L		; 4...
	Bpl	RdAd2		; 2,3
	Cmp	AdrMk2		; 2...	Address mark 2?
	Bne	RdAsn1		; 2,3

RdAd3	Lda	Q7L		; 4...
	Bpl	RdAd3		; 2,3
	Cmp	AdrMk3		; 2...
	Bne	RdAsn1		; 2,3

; Marks read now read address
; Carry is set

	Ldx	#AdrsLen		; 2
	Lda	#000		; 2	Clear CSum
Rfld	Sta	CSum		; 3	27
RdAd4	Ldy	Q7L		; 4
	Bpl	RdAd4		; 2	6	Do again if no valid data
	Lda	DNibl,y		; 4	12	Unpack the data
	Sta	CsmFnd,X		; 4	16	Store in FOUND table
	Eor	CSum		; 3	19	Update the checksum
	Dex			; 2	21	Next field
	Bpl	Rfld		; 2,3	24	Loop until "x" become negative
	Tax			; 2		If "CSum" = 0 then AOK
	Bne	RaErr5		; 2,3		No OK, signal on error

; Now compare against two final bytes and make sure at right track and sector

RaSlp1	Lda	Q7L		; 4...
	Bpl	RaSlp1		; 2,3
	Cmp	AdrMk4		; 2...
	Bne	RaErr2		; 2,3
	Lda	#01		; 2
	Bit	SdFnd		; 3
	Beq	RaSlp2		; 2,3
	Lda	#40
	Ora	TrkFnd
	Sta	TrkFnd

RaSlp2	Lda	Q7L		; 4...
	Bpl	RaSlp2		; 2,3
	Cmp	AdrMk5		; 2...
	Bne	RaErr2		; 2,3
	Lda	IIob+Track	; 3
	Cmp	TrkFnd		; 2
	Bne	RaErr4		; 2
	Lda	IIob+Sector	; 3
	Cmp	SecFnd		; 2
	Bne	RaErr3		; 2
	Clc			; 2... No error
	Lda	VolFnd		; Load the disk ID value just read.
	Sta	Iob+DiskID	; Tell host about what type of disk it is
RaExit	Clv			; Clear the overflow bit (previously used for FATAL)
RaExit1	Lda	Q6H		; Switch back from READ to SENSE
	Rts
;  We abort upon seeing first error-no matter what it is.  These all get reset
;  on seeking (even micro stepping).

RaErr1	Inc	RaStrt		; Start bitslip error -- Fatal error
	Sec
	Bcs	RaExit

RaErr2	Inc	RaEnd		; Ending bitslip error
	Sec
	Bcs	RaExit

RaErr5	Inc	RaCSum		; Checksum error
	Sec
	Bcs	RaExit

RaErr4	Inc	RaTrk		; Track error
RaErr6	Sec	
	Bcs	RaExit