WRITE16

.Page
;++
;	Write16
;
; $10	Ldx	Q6L	; 1	1	Assume worst case
;	Bpl	$10	; 3	4	branch taken
;	Ldx	Q6L	; 4	8	bit will be set
;	Bpl	$10	; 2	10	fall thru
;	Sta	Q6H	; 4	14	put the data into reg
; 31 cycles max after bit is set before UnderRun bit is set.  Using above code
; as start of time critical code,
; total time = ((32-14)+31) = 49 cycles
;--
.Page
	.Org	1A00
Write16	.Equ *
	Ldx	IIob+Sector
	Lda	Nibl,x
	Sta	Sv4
	Ldy	#0			; No speed check bytes
	Sty	InxPtrH
	Tya				; Flag will tell Sync20 to use data mark fields
	Iny				; Write only one set of self_sync bytes
	Jsr	Sync20			; 6	6	Turn on write circuitry & write self-sync bytes
	Ldy	#0F4			; 2	8
	Lda	#0AD			; 2	10	Last byte of header field
	Bne	Pg1x			; 3	13

Pg1	.Equ *				; Code to write out las 12 bytes of page #1
	Stx	Sv4			; 3
Pg1x	Ldx	Q6L			; 4	17
	Bpl	Pg1x			; 2,3	19/20
	Sta	Q6H			; 4	14	Worst case possible

	Ldx	Page01+2,y		; 4	18
	Lda	Nibl,x			; 4	22
	Sta	Sv3			; 3	25
	Txa				; 2	27
	Lsr	A			; 2	29
	Lsr	A			; 2	31
	Sta	Temp1			; 3	34
	Ldx	Page01+1,y		; 4	38
	Lda	Nibl,x			; 4	42
	Sta	Sv2			; 3	45
	Txa				; 2	47
	And	#0C0			; 2	49
	Ora	Temp1			; 3	52
	Lsr	A			; 2	54
	Ldx	Sv4			; 3	57	61-14 = 47
	Stx	Q6H			; 4	61	Write last byte of previous loop

	Lsr	A			; 2	2
	Sta	Temp1			; 3	5
	Lda	Page01,y			; 4	9
	And	#0C0			; 2	11
	Ora	Temp1			; 3	14
	Lsr	A			; 2	16
	Lsr	A			; 2	18
	Tax				; 2	20
	Lda	Nibl,x			; 4	24
	Sta	Q6H			; 4	28

	Ldx	Page01,y			; 4	4
	Iny				; 2	6
	Iny				; 2	8
	Lda	Nibl,x			; 2	12
$15	Ldx	Q6L			; 4	16
	Bpl	$15			; 2,3	18
	Sta	Q6H			; 4	22

	Lda	Sv2			; 3	3	Second  byte of loop
	Ldx	Sv3			; 3	6	Third byte
	Iny				; 2	8
	Bne	Pg1			; 2,3	10	Fetch rest of 12 bytes
					;		fall thru $|& start on Page #2
;++
;
;--

Pg2	.Equ *				; Code to write out first 255 bytes of Page #2
	Stx	Sv4			; 3
$40	Ldx	Q6L			; 4
	Bpl	$40			; 2,3
	Sta	Q6H			; 4	14	Worst case possible

	Ldx	Page02,y			; 4	18
	Lda	Nibl,x			; 4	22
	Sta	Sv3			; 3	25
	Txa				; 2	27
	Lsr	A			; 2	29
	Lsr	A			; 2	31
	Sta	Temp1			; 3	34
	Ldx	Page02+1,y		; 4	38
	Lda	Nibl,x			; 4	42
	Sta	Sv2			; 3	45
	Txa				; 2	47
	And	#0C0			; 2	49
	Ora	Temp1			; 3	52
	Lsr	A			; 2	54
	Ldx	Sv4			; 3	57	61-14=47
	Stx	Q6H			; 4	61	Write out last byte of previous loop

	Lsr	A			; 2	2
	Sta	Temp1			; 3	5
	Lda	Page02,y			; 4	9
	And	#0C0			; 2	11
	Ora	Temp1			; 3	14
	Lsr	A			; 2	16
	Lsr	A			; 2	18
	Tax				; 2	20
	Lda	Nibl,x			; 4	24
	Sta	Q6H			; 4	28


	Ldx	Page02			; 4	4
	Iny				; 2	6
	Iny				; 2	8
	Lda	Nibl,x			; 4	12
$55	Ldx	Q6L			; 4	16
	Bpl	$55			; 2,3	18
	Sta	Q6H			; 4	22
	Lda	Sv2			; 3	3	Second byte of loop
	Ldx	Sv3			; 3	6	Third byte
	Iny				; 2	8
	Cpy	#Pg2Len			; 2	10
	Bne	Pg2			; 2,3	12	Fetch rest of 255 bytes

	Jsr	WrAX			; 6	26/6	Write Nibls in A and X
	Lda	Page03			; 4	10
	Lda	Nibl,x			; 4	14
	Sta	Sv4			; 3	17
	Lda	CpBy01			; 3	20
	Jsr	WrNibl			; 6	26/6

	Ldy	#02			; 2	8	For use in Pg3
	Ldx	Page02+0FF		; 4	12
	Jsr	WrByteX			; 6	18/6

	Ldx	Page03			; 4	10
	Lda	Nibl,x			; 4	14
	Bne	Pg3x			; 3	17

WrAX	.Equ *				; Write nibls that are in A & X (Sv3)
	Jsr	WrNibl			; 6	32/6
	Lda	Sv3			; 3	9
	Jmp	WrNibl			; 3	12/6

;++
;
;--

WrByte	.Equ *				; Nibbize byte in A-reg before starting
	Tax				; 2
WrByteX	.Equ *				; Use byte in X-reg
	Lda	Nibl,x			; 4

WrNibl	.Equ *				; Wait for handshake bit, then write data in A-reg
	Ldx	Q6L			; 4
	Bpl	WrNibl			; 2,3
	Sta	Q6H			; 4
	Rts				; 6

	.Align	0100
Pg3	.Equ *				; Code to write out first 254 bytes of page #3
	Stx	Sv4			; 3
Pg3x	Ldx	Q6L			; 4
	Bpl	Pg3x			; 2,3
	Sta	Q6H			; 4	14

	Ldx	Page03+2,y		; 4	18
	Lda	Nibl,x			; 4	22
	Sta	Sv3			; 3	25
	Txa				; 2	27
	Lsr	A			; 2	29
	Lsr	A			; 2	31
	Sta	Temp1			; 3	34
	Ldx	Page03			; 4	38
	Lda	Nibl,x			; 4	42
	Sta	Sv2			; 3	45
	Txa				; 2	47
	And	#0C0			; 2	49
	Ora	Temp1			; 3	52
	Lsr	A			; 2	54
	Ldx	Sv4			; 3	57	61-14 = 47
	Stx	Q6H			; 4	61	Write last byte of previous loop

	Lsr	A			; 2	2
	Sta	Temp1			; 3	5
	Lda	Page03			; 4	9
	And	#0C0			; 2	11
	Ora	Temp1			; 3	14
	Lsr	A			; 2	16
	Lsr	A			; 2	18
	Tax				; 2	20
	Lda	Nibl,x			; 4	24
	Sta	Q6H			; 4	28	Composite of 3 bytes

	Ldx	Page03,y			; 4	4
	Iny				; 2	6
	Iny				; 2	8
	Lda	Nibl,x			; 4	12
$85	Ldx	Q6L			; 4	16
	Bpl	$85			; 2,3	18
	Sta	Q6H			; 4	22

	Lda	Sv2			; 3	3	Second byte of loop
	Ldx	Sv3			; 3	6	Third loop
	Iny				; 2	8
	Cpy	#Pg3Len			; 2	10
	Bne	Pg3			; 2,3	12	Fetch rest of 254 bytes

	Jsr	WrAX			; 6	26/6	Write Nibls in A & X (Sv3)
	Ldx	Page03+0FF		; 4	10
	Lda	Nibl,x			; 4	14
	Sta	Sv3			; 3	17
	Lda	CpBy02			; 3	20
	Jsr	WrNibl			; 6	26/6
	Ldx	Page03+0FE		; 4	10
	Lda	Nibl,x			; 4	14
	Jsr	WrAX			; 6	20/6

	Ldy	#Cksum1			; 2	8	Absolute addr of 3 cksum bytes
	Sty	InxPtrL			; 3	11
	Lda	CpCkSum			; 3	14
$95	Ldx	Q6L			; 4	18
	Bpl	$95			; 2,3	20
	Sta	Q6H			; 4	24
	Ldy	#0			; 2	2
$99	Lda	@InxPtrL,y		; 6	8
	Jsr	WrByte			; 3	11/6
	Iny				; 2	8
	Cpy	#3			; 2	10
	Bne	$99			; 2,3	12
					; Fall thru & write final bitslip marks & end
	Lda	DatMk4			; 3
	Jsr	WrNibl			; 6
	Lda	DatMk5			; 3
	Jsr	WrNibl			; 6

ShtOff	.Equ *				; Write 2 bitslip and return /WrUnderRun
	Lda	#0FF			; 2
	Jsr	WrNibl			; 6
	Jsr	WrNibl			; 6
	Clc				; 2
	Lda	Q6L			; 4	Bit6 = UnderRun bit
	And	#40			; 2	Leave only bit 6
	Bne	$38			; 2,3	If = 1 then no underrun occurred
	Lda	#ErrWrt			; Obscure error code #
	Sec				; 2

$38	Sta	Q6H			; 4	Put into write load state
	Ldx	Q7L			; 4	Now into write protect - Sense state
	Rts				; 6