* NEWDISK for Smoke Signal Broadcasting BFD-68(A)
* Disk formatting program for FLEX 2.0 or 3.0
*
* Created from Michael Holley's disassembly of the
* SWTPC MF-68 version of NEWDISK. Updated for the
* BFD-68(A) controller and support added for 40
* track and double side drives.
*
* Mike Douglas, March 2023

* TEMPORARY STORAGE

	ORG	$20
TRACK	RMB	1
SECTOR	RMB	1
BADCNT	RMB	1
DRN	RMB	1		DRIVE NUMBER
SIDE	RMB	1
DBSDF	RMB	1
INDEX1	RMB	2
INDEX2	RMB	2
INDEX3	RMB	2
SECCNT	RMB	2
FSTAVL	RMB	2
LSTAVL	RMB	2
NUMTRK	RMB	1

* General equates used in NEWDISK

FIRST	EQU	$0101		FIRST USED SECTOR
FCS	EQU	30		FCB CURRENT SECTOR
FSB	EQU	64		FCB SECTOR BUFFER
IRS	EQU	16		INFO RECORD START
AVLP	EQU	FSB+IRS+13
DIRSEC	EQU	5		FIRST DIR. SECTOR
RDSS	EQU	9		READ SS FMS CODE
WTSS	EQU	10		WRITE SS FMS CODE

* Work space where one track of data is setup

WORK	EQU	$0100		WORK SPACE
TKSZ	EQU	3008		SIZE OF SD WORK BUFFER
SWKEND	EQU	TKSZ+WORK

TOT35	EQU	340		TOTAL SECTORS SINGLE SIDED
TOT40	EQU	390		TOTAL SECTORS 40 TRACK SS
TOTDS	EQU	780		TOTAL SECTORS 40 TRACK DS

MAXS0	EQU	10		SD MAX SIDE 0 SECTORS
MAXS1	EQU	20		SD MAX SIDE 1 SECTORS

* FLEX LOCATIONS

SSTACK	EQU	$A070
SMONTH	EQU	$AC0E		DOS DATE
SDAYMN	EQU	$AC0F
SYEARN	EQU	$AC10

* FLEX ROUTINES EQUATES

WARMST	EQU	$AD03
GETCHR	EQU	$AD15
PUTCHR	EQU	$AD18
INBUFF	EQU	$AD1B
PSTRNG	EQU	$AD1E
PRCRLF	EQU	$AD24
GETFIL	EQU	$AD2D
OUTDEC	EQU	$AD39
OUTHEX	EQU	$AD3C
GETHEX	EQU	$AD42
INDECM	EQU	$AD48
FMSCAL	EQU	$B406

* FLEX DISK DRIVER ROUTINES

DWRITE	EQU	$BE83		WRITE A SINGLE SECTOR
DRSTOR	EQU	$BE89		RESTORE HEAD
BFDWTRK	EQU	$8032		WRITE TRACK IN BFD ROM
BFDSTEP	EQU	$8075		STEP IN ONE TRACK
BFDDRV	EQU	$A07B		DRIVE FOR ROM (0-3 ARE $08,$10,$20,$40)

*************************************************
* MAIN PROGRAM STARTS HERE
*************************************************
	ORG	$A100
STARTR	BRA	FORM1
VN	FCB	10		VERSION 1.0

FKFCB	FDB	0,0
VOLNAM	FDB	0,0,0,0,0
	FCB	0
VOLNUM	FDB	0

FORM1	LDS	#SSTACK
	JSR	GETHEX		GET DRIVE NUMBER
	BCC	FORM12		BR IF GOOD NUMBER
FORM11	JMP	EXIT
FORM12	STX	INDEX3
	LDAA	INDEX3+1
	CMPA	#3
	BHI	FORM11		ENSURE 0 TO 3
	LDX	#WORK
	STAA	$03,X
	STAA	DRN		DRIVE NUMBER

	LDX	#SURES		ARE YOU SURE
	JSR	PSTRNG
	JSR	GETCHR
	CMPA	#'Y
	BNE	FORM23

	LDX	#SCRDS		SCRATCH DISK
	JSR	PSTRNG
	LDX	#WORK+2
	CLR	0,X
	CLRB
	JSR	OUTDEC		PRINT DRIVE NUMBER
	LDAA	#'?
	JSR	PUTCHR
	LDAA	#' 		SPACE
	JSR	PUTCHR
	JSR	GETCHR
	CMPA	#'Y
	BEQ	FORM24
FORM23	JMP	EXIT

FORM24	LDAA	DRN		GIVE DRIVE NUMBER TO ROM
	LDAB	#$04		CONVERT DRIVE 0-3 TO $08,$10,$20,$40
DRVBIT	ASLB
	DECA
	BPL	DRVBIT
	STAB	BFDDRV		SET NEW DRIVE AS USED BY BFD ROM

	LDX	#TRKSTR		DISPLAY TRACKS?
	JSR	PSTRNG
	JSR	GETCHR
	SUBA	#'1
	CMPA	#3		VERIFY 1-3
	BCS	FRM241
	JMP	EXIT

FRM241	CLR	DBSDF		SINGLE SIDED
	LDAB	#35		35 TRACKS
	LDX	#34*256+10	LAST TRACK,SECTOR
	TSTA
	BEQ	FRM242		YES, 35 TRACKS

	LDAB	#40		40 TRACKS
	LDX	#39*256+10	LAST TRACK,SECTOR
	DECA			TEST 
	BEQ	FRM242		YES, 40 TRACKS

	INC	DBSDF		DOUBLE-SIDED
	LDX	#39*256+20	LAST TRACK,SECTOR

FRM242	STAB	NUMTRK
	STX	LSTAVL

FORM25	LDX	#NMSTR		NAME
	JSR	PSTRNG
	JSR	INBUFF		GET LINE
	LDX	#FKFCB		FAKE FCB
	JSR	GETFIL

FORM27	LDX	#NUMSTR		NUMBER
	JSR	PSTRNG
	JSR	INBUFF
	JSR	INDECM
	BCS	FORM27		GET GOOD VOLUME NUMBER
	STX	VOLNUM
	JSR	PRCRLF

	LDX	#WORK
	JSR	DRSTOR		RESTORE DISK
	BCS	EXIT
	BNE	EXIT4		WRITE PROTECT ERROR

	LDX	#0		WAIT 1 SECOND FOR MOTOR
MOTRDLY	INX
	DEX
	DEX
	BNE	MOTRDLY

************************************************
* ACTUAL FORMAT ROUTINE

	LDX	#WORK
FORM30	CLR	0,X		CLEAR RAM BUFFER
	INX
	CPX	#SWKEND
	BNE	FORM30
	NOP
	SEI
	CLR	TRACK
	CLR	SIDE

FORM32	LDX	#WORK+2
	TST	SIDE
	BNE	FORM4
	CLR	SECTOR
FORM4	BSR	DOSEC		PROCESS RAM WITH INFO
	INC	SECTOR		ADVANCE TO NEXT
	LDAA	SECTOR		CHECK VALUE
	TST	SIDE		CHECK SIDE
	BNE	FORM45
	CMPA	#MAXS0
	BNE	FORM4		REPEAT UNTIL LAST SECTOR
	BRA	FORM47
FORM45	CMPA	#MAXS1
	BNE	FORM4		REPEAT UNTIL LAST SECTOR

FORM47	JSR	WRTTRK
	TST	DBSDF		SINGLE SIDED?
	BEQ	FORM6		YES
	TST	SIDE		ON TOP SIDE?
	BNE	FORM6		YES
	INC	SIDE		ELSE MOVE TO TOP
	BRA	FORM32

FORM6	INC	TRACK
	LDAA	TRACK
	CMPA	NUMTRK
	BNE	FORM7		REPEAT UNTIL LAST TRACK
	JMP	SETUP		DONE...GO FINISH UP

FORM7	STAA	TRACK
	JSR	BFDSTEP		STEP IN ONE TRACK
	CLR	SIDE
	BRA	FORM32

* EXIT ROUTINES

EXIT	LDX	#ABORTS
EXIT2	JSR	PSTRNG
	JMP	WARMST
EXIT4	LDX	#WPST		WRITE PROTECTED
	BRA	EXIT2

* PROCESS SECTOR IN RAM

DOSEC	LDAA	#$FE		ID ADDRESS MARK
	STAA	6,X
	LDAA	#1		SECTOR NUMBER
	STAA	10,X
	LDAA	TRACK
	STAA	7,X
	LDAB	SECTOR
	STX	INDEX1		SAVE INDEX
	LDX	#SSMAP5		POINT TO CORRECT MAP
	BSR	ADDXB		POINT TO SECTOR NUMBER
	LDAB	0,X		GET SECTOR NUMBER
	LDX	INDEX1		RESTOR INDEX
	TST	7,X		TRACK NUMBER
	BNE	DOSEC1		BR NOT TRACK 0
	CMPB	#$01
	BHI	DOSEC1		BR NOT SECTOR 0 OR 1

	DECB
	STAB	9,X		SECTOR NUMBER
	INCB
	BRA	DOSEC2

DOSEC1	STAB	9,X		SECTOR NUMBER
DOSEC2	INCB			INCREMENT SECTOR NUMBER
	TST	DBSDF		DOUBLE SIDED?
	BNE	DOSEC3		YES

	CMPB	#MAXS0+1	END OF TRACK SIDE 0
	BRA	DOSEC4

DOSEC3	CMPB	#MAXS1+1	END OF TRACK SIDE 1

DOSEC4	BNE	DOSEC7		BR IF NOT
	INCA			INC TRACK NUMBER
	LDAB	#1		RESET SECTOR NUMBER
	CMPA	NUMTRK		END OF DISK
	BNE	DOSEC7		BR IF NOT
	CLRA			SET ZERO FORWARD LINK
	CLRB

DOSEC7	STAA	30,X		FOWARD LINK
	STAB	31,X
	LDAA	#$FB		ADDRESS MARK
	STAA	29,X		JUST BEFORE DATA
	LDAA	#$F7		SET CRC CODE
	STAA	11,X
	LDAB	#128
	BSR	ADDXB		ADD 128 TO INDEX
	STAA	128+30,X	STORE "F7" AFTER 256 BYTES
	LDAB	#172


* ADD ACCB TO INDEX
ADDXB	STX	INDEX3
	ADDB	INDEX3+1
	STAB	INDEX3+1
	BCC	ADDXB9
	INC	INDEX3
ADDXB9	LDX	INDEX3
	RTS

SSMAP5	FCB	1,3,5,7,9,2,4,6,8,10
	FCB	11,13,15,17,19,12,14,16,18,20

*************************************************
* DISK TESTING AND TABLE SETUP

* Read all sectors for errors

SETUP	LDX	#TOTDS		TOTAL SECS IF DOUBLE-SIDED
	TST	DBSDF
	BNE	SETU01		BR IF DOUBLE SIDED

	LDX	#TOT35		TOTAL SECTORS IF 35 TRACKS
	LDAA	NUMTRK		ARE WE 35 TRACK?
	CMPA	#35
	BEQ	SETU01		YES
	LDX	#TOT40		ELSE, TOTAL SECS FOR 40 TRACK

SETU01	STX	SECCNT
	LDX	#FIRST		SET FIRST AVAIL
	STX	FSTAVL
	LDAA	DRN		DRIVE NUMBER
	STAA	WORK+3
	CLRA
	STAA	BADCNT
	STAA	TRACK		SET TRACK 0
	STAA	SECTOR		SET SECTOR 0
SETUP2	BSR	CHKSEC		GO CHECK SECTOR
	BNE	REMSEC		ERRORS?
	CLR	BADCNT		CLEAR COUNT

SETUP4	LDAA	TRACK
	LDAB	SECTOR
	BSR	FIXSEC		GET NEXT ADR
	BNE	SETUP5
	JMP	DOTRK		BR IF FINISHED

SETUP5	STAA	TRACK
	STAB	SECTOR
	BRA	SETUP2

* CHECK IF SECTOR GOOD

CHKSEC	LDX	#WORK		POINT TO FCB
	LDAA	TRACK
	STAA	FCS,X		SET CURRENT TRK
	LDAA	SECTOR
	STAA	FCS+1,X		SET CURRENT TRK
	JMP	READSS

* SET TRACK & SECTOR TO NEXT

FIXSEC	INCB
	TSTA
	BNE	FIXSE1
	CMPB	#1
	BNE	FIXSE1
	INCB
FIXSE1	TST	DBSDF
	BNE	FIXSE2		BR IF DOUBLE SIDED
	CMPB	#MAXS0+1	END OF SIDE 0
	BRA	FIXSE3
FIXSE2	CMPB	#MAXS1+1	END OF SIDE 1
FIXSE3	BNE	FIXSE4
	INCA			NEXT TRACK
	LDAB	#1		SECTOR NUMBER 1
	CMPA	NUMTRK		END OF DISK
FIXSE4	RTS

* REMOVE BAD SECTOR FROM FREE SECTOR CHAIN

REMSEC	INC	BADCNT		UPDATE COUNTER
	BEQ	REMSE1		OVERFLOW?
	LDAA	TRACK
	BNE	REMSE2		TRACK 0?
	LDAB	SECTOR
	CMPB	#DIRSEC		PAST DIRECTORY?
	BHI	REMSE2
REMSE1	LDX	#FATERS		REPORT FATAL ERROR
	JMP	EXIT2

REMSE2	LDX	#WORK		POINT TO FCB
	LDAA	FSTAVL		GET 1ST TRACK
	LDAB	FSTAVL+1	AND SECTOR
	CMPA	TRACK		CHECK TRACK
	BNE	REMSE3
	CMPB	SECTOR
	BNE	REMSE3
	BSR	FIXSEC		SET TO NEXT
	STAA	FSTAVL
	STAB	FSTAVL+1
	BRA	REMSE8

REMSE3	LDAA	TRACK
	LDAB	SECTOR
	SUBB	BADCNT
	BEQ	REMS35		UNDERFLOW?
	BPL	REMSE4

REMS35	DECA			DEC TRACK
	TST	DBSDF
	BNE	REMS36
	LDAB	#MAXS0		MAX SIDE 0
	BRA	REMSE4
REMS36	LDAB	#MAXS1		SIDE 1

REMSE4	STAA	FCS,X		SET CURRENT ADR
	STAB	FCS+1,X
	BSR	READSS		GO DO READ
	BNE	REMSE1		ERROR?
	LDAA	FSB,X		GET LINK ADR
	LDAB	FSB+1,X
	BSR	FIXSEC		POINT TO NEXT
	BNE	REMSE6		OVERFLOW?
	LDAA	FCS,X		GET CURRENT ADDRESS
	LDAB	FCS+1,X
	STAA	LSTAVL		SET NEW LAST AVAIL
	STAB	LSTAVL+1
	CLRA			SET END LINK
	CLRB
REMSE6	STAA	FSB,X		SET NEW LINK
	STAB	FSB+1,X
	BSR	WRITSS		GO DO WRITE
	BNE	REMSE1
REMSE8	LDX	SECCNT		GET SEC COUNT
	DEX			SUBTRACT ONE SECTOR
	STX	SECCNT		SAVE NEW COUNT
	LDX	#BADSS		REPORT BAD SECTOR
	JSR	PSTRNG		OUTPUT IT
	LDX	#TRACK		POINT TO ADDRESS
	JSR	OUTHEX
	LDAA	#' 		SPACE
	JSR	PUTCHR
	INX			POINT TO SECTOR
	JSR	OUTHEX
	JMP	SETUP4		CONTINUE

* READ A SECTOR

READSS	LDX	#WORK		POINT TO FCB
	LDAA	#RDSS		SET UP COMMAND
	STAA	0,X
	JMP	FMSCAL		GO DO IT

* WRITE A SECTOR

WRITSS	LDX	#WORK		POINT TO FCB
	LDAA	#WTSS
	STAA	0,X
	JSR	FMSCAL		GO DO IT
	BEQ	READSS		ERRORS?
	RTS			ERROR RETURN

* SETUP SYSTEM INFORMATION RECORD

DOTRK	LDX	#WORK
	CLR	FCS,X		SET TO DIS
	LDAA	#3		SECTOR 3
	STAA	FCS+1,X
	JSR	READSS		READ IN SIR SECTION
	BNE	DOTRK4		ERROR?
	LDX	#WORK		FIX POINTER
	CLR	FSB,X		CLEAR FOWARD LINK
	CLR	FSB+1,X
	LDAA	FSTAVL		ADDR OF 1ST FREE SECTOR
	LDAB	FSTAVL+1
	STAA	AVLP,X		SET IN SIR
	STAB	AVLP+1,X
	LDAA	LSTAVL		ADDR OF LAST FREE SECTOR
	LDAB	LSTAVL+1
	STAA	AVLP+2,X	PUT IN SIR
	STAB	AVLP+3,X
	LDAA	SECCNT		GET TOTAL SECTORS
	LDAB	SECCNT+1
	STAA	AVLP+4,X
	STAB	AVLP+5,X
	TST	DBSDF
	BNE	DOTR21
	LDAA	#MAXS0		MAX SINGLE SIDED
	BRA	DOTR22
DOTR21	LDAA	#MAXS1		MAX DOUBLE SIDED
DOTR22	STAA	AVLP+10,X
	LDAA	NUMTRK
	DECA
	STAA	AVLP+9,X
	LDAA	SMONTH
	LDAB	SDAYMN
	STAA	AVLP+6,X
	STAB	AVLP+7,X
	LDAA	SYEARN
	STAA	AVLP+8,X

	LDAB	#13		LENGHT OF NAME & NUMBER
	LDX	#VOLNAM		POINT TO VOLUME NAME
	STX	INDEX2		SAVE INDEX
	LDX	#WORK
DOTR33	STX	INDEX1		SAVE INDEX
	LDX	INDEX2
	LDAA	0,X
	INX
	STX	INDEX2
	LDX	INDEX1
	STAA	FSB+IRS,X
	INX
	DECB
	BNE	DOTR33

	JSR	WRITSS		WRITE SIR BACK OUT
	BEQ	DIRINT
DOTRK4	JMP	REMSE1		GO REPORT ERROR

* INITIALIZE DIRECTORY

DIRINT	LDX	#WORK
	TST	DBSDF
	BNE	DIRIN1		BR IF DOUBLE SIDED
	LDAA	#MAXS0		MAX SECTORS
	BRA	DIRIN2
DIRIN1	LDAA	#MAXS1		MAX SECTORS DOUBLE SIDED
DIRIN2	STAA	FCS+1,X
	JSR	READSS		READ IN SECTOR
	BNE	DOTRK4		ERRORS?
	LDX	#WORK		RESTORE POINTER
	CLR	FSB,X		CLEAR LINK
	CLR	FSB+1,X
	JSR	WRITSS		WRITE SECTOR BACK
	BNE	DOTRK4		ERRORS?

* SAVE BOOT ON TRACK 0 SECTOR 0

DOBOOT	LDX	#BOOTLOC	POINT TO LOADER
	CLRA			TRACK #0
	CLRB			SECTOR #0
	JSR	DWRITE		WRITE THE SECTOR
	BNE	DOTRK4		ERRORS?

* REPORT TOTAL SECTORS AND EXIT

	LDX	#WORK		SETUP AN FCB
	LDAA	#16		OPEN SIR FUNCTION
	STAA	0,X
	JSR	FMSCAL		OPEN SIR
	BNE	DOTRK4
	LDAA	#7		GET INFO RECORD FUNCTION
	STAA	0,X
	JSR	FMSCAL		GET 1ST INFO RECORD
	BNE	DOTRK4
	LDX	#CMPLTE		FORMATTING COMPLETE
	JSR	PSTRNG		REPORT IT
	LDX	#WORK+21	TOTAL IS IN INFO RECORD
	CLRB
	JSR	OUTDEC
	JMP	WARMST		ALL FINISHED!

**************************************************
* WTCMD - WRITE TRACK ROUTINE FOR SMOKE SIGNAL BFD-68(A)

WRTTRK	LDAA	SIDE		ASSUME SIDE 0
	BEQ	WRTTR1		SIDE 0
	ORAA	#$20		SET SIDE 1 BIT
WRTTR1	STAA	BFDSEC		SELECT SIDE 0 OR 1

	LDX	#WORK		POINT TO BUFFER TO WRITE
	STX	BFDBUF
	JSR	BFDWTRK		WRITE THE TRACK
	RTS

* STRINGS
SURES	FCC	"ARE YOU SURE? "
	FCB	$04
WPST	FCC	"DISK IS PROTECTED!"
	FCB	$04
SCRDS	FCC	"SCRATCH DISK IN DRIVE "
	FCB	$04
FATERS	FCC	"FATAL ERROR --- "
ABORTS	FCC	"FORMATTING ABORTED"
	FCB	$04
BADSS	FCC	"BAD SECTOR AT "
	FCB	$04
CMPLTE	FCC	"FORMATTING COMPLETE"
	FCB	$0D,$0A
	FCC	"TOTAL SECTORS = "
	FCB	$04
TRKSTR	FCC	"  1) 35 TRACKS, SINGLE SIDED"
	FCB	$0D,$0A
	FCC	"  2) 40 TRACKS, SINGLE SIDED"
	FCB	$0D,$0A
	FCC	"  3) 40 TRACKS, DOUBLE SIDED"
	FCB	$0D,$0A
	FCC	"ENTER LAYOUT TYPE: "
	FCB	$04
DBST	FCC	"DOUBLE SIDED DISK? "
	FCB	$04
NMSTR	FCC	"VOLUME NAME? "
	FCB	$04
NUMSTR	FCC	"VOLUME NUMBER? "
	FCB	$04

*--------------------------------------------------------
* FLEXLOAD.ASM
*    Double-sided compatible FLEX 2/3 loader for the
*    Smoke Signal Broadcasting BFD-68(A) floppy disk
*    controller.
*
*    Mike Douglas, 03/25/23
*--------------------------------------------------------
BOOTLOC	equ	$7000		;FLEX loader runs here
STACK	equ	$A070		;stack location (below ROM usage)

* BFD-68 ROM EQUATES

BFDREAD	equ	$8029		;read a sector
BFDREST	equ	$8038		;restore to track 0
BFDTRK	equ	$A07C		;track number
BFDSEC	equ	$A07D		;sector number
BFDBUF	equ	$A07E		;pointer to buffer

*--------------------------------------------------------
* Boot ROM jumps here after loading this from sector 0
*--------------------------------------------------------
	org	BOOTLOC		;boot ROM loads us here

flxBoot	lds	#STACK		;init stack pointer
	bra	loadFlx		;go load Flex

flexTrk	fcb	1		;track for FLEX3.SYS
flexSec	fcb	10		;sector to find FLEX3.SYS
			
pFlex	rmb	2		;pointer to FLEX entry
pDest	rmb	2		;pointer to destination
pSource	rmb	2		;pointer to data in secBuf

*--------------------------------------------------------
* loadFlx - Entry point for loading FLEX from disk. The 
*    drive that loaded this program is used to load FLEX.
*    This allows booting from any drive.
*--------------------------------------------------------
loadFlx	jsr	BFDREST		;restore to track zero
	bsr	read1st		;read first sector of FLEX

load1	bsr	getByte		;get next byte
	cmpa	#$02		;data record header?
	beq	load2		;yes

	cmpa	#$16		;transfer address header?
	bne	load1		;no, keep looking

	bsr	getByte		;get transfer address
	staa	pFlex		;MSB
	bsr	getByte		;LSB
	staa	pFlex+1
	ldx	pFlex		;jump to FLEX
	jmp	0,x

load2	bsr	getByte		;A=load address MSB
	psha
	bsr	getByte		;A=load address LSB
	pulb			;B=load address MSB
	staa	pDest+1		;save load address LSB
	stab	pDest		;MSB

	bsr	getByte		;A=byte count
	tab			;B=byte count
	beq	load1		;byte count was zero

load3	pshb
	bsr	getByte		;get next data byte
	pulb
	ldx	pDest		;X->where to put the data
	staa	0,x
	inx
	stx	pDest
	decb
	bne	load3		;loop for byte count in B

	bra	load1		;get next record

*--------------------------------------------------------
* readSec 
*--------------------------------------------------------
read1st	ldaa	flexTrk		;read 1st sector of Flex
	ldab	flexSec

readSec	staa	BFDTRK		;give track to BFD ROM

	cmpb	#10		;sector show side 0 or 1?
	bls	setSec		;side 0
	orab	#$20		;set BFD side bit
setSec	stab	BFDSEC		;give sector to BFD ROM

	ldx	#secBuf		;give buffer pointer to ROM
	stx	BFDBUF
	jsr	BFDREAD		;read the sector
	bitb	#$1C		;any read errors?
	bne	loadFlx		;yes, start over
	
	ldx	#secBuf+4	;data starts at offset 4
	stx	pSource		;pSource->data in secBuf
	rts

*--------------------------------------------------------
* getByte - Get next byte from sector buffer. Retrieve next
*    sector as needed.
*--------------------------------------------------------
getByte	ldx	pSource		;X->next byte 
	cpx	#secBuf+256	;reached end of sector?
	beq	nextSec		;yes, get the next sector

	ldaa	0,x		;else, return next byte
	inx
	stx	pSource		;save updated pointer
	rts

nextSec	ldaa	secBuf		;A=next track
	ldab	secBuf+1	;B=next sector
	bsr	readSec
	bra	getByte

* Sector buffer starts here

secBuf	equ	*

	end	STARTR
