NEW
PREFIX
AUTO 4,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/format
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/MLI.E.I
				.INB INC/KERNEL.I
				.INB INC/LIBBLKDEV.I
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPPtrFD			.BS 2
ZPPtrDevName	.BS 2

Index			.BS 1

hDevID			.BS 1
hBuf			.BS 1
hMem			.BS 1

bLL				.BS 1
bCustomBlkSize 	.BS 1

MediaBlkSize	.BS 2
CatSize			.BS 1
CatBlkCnt		.BS 1
CatOptions		.BS 1

ZS.END
				.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #0					S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #32					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.LIBBLKDEV		.DA LIBBLKDEV
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.OK		.DA MSG.OK
L.MSG.ERR		.DA MSG.ERR
L.MSG.CRLF		.DA MSG.CRLF
L.MSG.NOSIZE	.DA MSG.NOSIZE
L.MSG.INIT		.DA MSG.INIT
L.MSG.LLDISK2	.DA MSG.LLDISK2
L.MSG.LL		.DA MSG.LL
L.MSG.WRITECAT	.DA MSG.WRITECAT
L.FMT.BLANK		.DA FMT.BLANK
				.DA 0
*--------------------------------------
CS.INIT			>LDYA L.LIBBLKDEV
				>SYSCALL LoadLib
				bcs .9
				sta hLIBBLKDEV

.9				rts
*--------------------------------------
CS.RUN			ldy #S.PS.ARGC
				lda (pPs),y
				beq .99

.1				inc Index
				lda Index
				>SYSCALL ArgV
				bcc .11
				
				jmp .8

.11				>STYA ZPPtr1
				lda (ZPPtr1)
				cmp #'-'
				bne .4

				ldy #1 
				lda (ZPPtr1),y
				beq .99

				cmp #'0'
				bcc .2
				cmp #'9'+1
				bcs .2
				and #$0F
				sta CatSize
.10				bra .1

.2				jsr CS.RUN.CheckOpt
				bcs .99
				
				cpy #bCustomBlkSize
				bne .1
				
				inc Index
				lda Index
				>SYSCALL ArgV
				bcs .99
				
				>SYSCALL atoi
				>STYA MediaBlkSize
				bra .1
				
.99				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL printf
				lda #E.SYN
				sec
.9				rts			

.4				lda hDevID
				bne .5					Already have a dev...go get VolName

				>LDYA ZPPtr1
				>SYSCALL GetDevByName
				bcs .9

				>STYA ZPPtrFD
				stx hDevID
				>LDYA ZPPtr1
				>STYA ZPPtrDevName
				bra .10

.5				ldy #$ff

.6				iny
				lda (ZPPtr1),y
				sta (pData),y			VolName
				beq .10

				cpy #16
				bcc .6
				bra .99					VolName too long

.8				lda hDevID
				beq .99
*--------------------------------------
CS.RUN.Format	jsr CS.RUN.GetDevStatus
				bcs .9

				>PUSHW L.MSG.INIT
				>PUSHW ZPPtrDevName
				>PUSHW MediaBlkSize
				>PUSHEA.G VolName
				
				>PUSHBI 6
				>SYSCALL printf
				bcs .9

				bit bLL
				bpl .1
				
				jsr CS.RUN.LL
				bcs .9

.1				jsr CS.RUN.InitCat
				bcs .9

				jsr CS.RUN.BuildCat
				bcs .9

				jsr CS.RUN.WriteCat
				bcs .9

				>LDYA L.MSG.OK
				>SYSCALL puts
				lda #0
				sec
				rts

.9				pha
				>PUSHW L.MSG.ERR
				pla
				pha
				>PUSHA
				>PUSHBI 1
				>SYSCALL printf
				pla
				sec
				rts
*--------------------------------------
CS.RUN.GetDevStatus
				lda bCustomBlkSize
				bmi .8
				
				>PUSHEA.G DIB
				lda hDevID

				>SYSCALL GetDevStatus
				bcs .9

				>LDA.G DIB+S.DIB.SIZE
				tax
				iny
				ora (pData),y
				beq .9
				
				lda (pData),y
				sta MediaBlkSize+1
				stx MediaBlkSize
.8				clc
				rts

.9				>LDYA L.MSG.NOSIZE
				>SYSCALL puts
				lda #MLI.E.IO
				sec
				rts
*--------------------------------------
CS.RUN.LL		>LDA.G DIB+S.DIB.T
				cmp #S.DIB.T.DISKII
				beq CS.RUN.LL.DISKII
				
				>PUSHW L.MSG.LL
				>PUSHBI 0
				>SYSCALL printf
				bcs .9
				
				>PUSHEA.G IOCTL
				>PUSHBI IOCTL.FORMAT
				lda hDevID

				>SYSCALL IOCTL
				
.9				rts
*--------------------------------------
CS.RUN.LL.DISKII
				>LDYAI 4096
				>SYSCALL getmem
				bcs .9
				>STYA ZPPtr1
				stx hMem
				
				stz Index
				
.1				>PUSHW L.MSG.LLDISK2
				>PUSHB Index
				>PUSHBI 1
				>SYSCALL printf
				bcs .9




				inc Index
				lda Index
				cmp #35
				bne .1
				
				>PUSHW L.MSG.CRLF
				>PUSHBI 0
				>SYSCALL printf
.9				rts				
*--------------------------------------
CS.RUN.InitCat	lda CatSize
				bne .1

				lda #4
				sta CatSize

.1				>LDA.G VolName
				bne .8					Volume name ok

				>PUSHEA.G VolName
				>PUSHW L.FMT.BLANK
				>PUSHW A2osX.TIMER16
				>PUSHW A2osX.RANDOM16
				>PUSHBI 4
				>SYSCALL sprintf
				rts

.8				clc
				rts
*--------------------------------------
CS.RUN.BuildCat	jsr .7

				>LIBCALL hLIBBLKDEV,LIBBLKDEV.GetProDOSCatSize			
				bcs .9

				stx CatBlkCnt

				>SYSCALL GetMem			BufferSize
				bcs .9

				>STYA ZPPtr1

				>STYA.G IOCTL+S.IOCTL.BUFPTR
				stx hBuf

				lda CatBlkCnt
				asl
				tax

				lda #0
				tay

.1				sta (ZPPtr1),y
				iny
				bne .1
				inc ZPPtr1+1
				dex
				bne .1

				jsr .7

				>PUSHEA.G VolName
				>PUSHW.G IOCTL+S.IOCTL.BUFPTR
				>LIBCALL hLIBBLKDEV,LIBBLKDEV.BuildProDOSCat
.9				rts

.7				>PUSHW MediaBlkSize
				>PUSHB CatSize
				>PUSHB CatOptions
				rts
*--------------------------------------
CS.RUN.WriteCat	>PUSHW L.MSG.WRITECAT
				>PUSHBI 0
				>SYSCALL printf

.1				>PUSHEA.G IOCTL
				>PUSHBI IOCTL.WRITEBLOCK		
				lda hDevID

				>SYSCALL IOCTL
				bcs .9

				lda #'.'
				>SYSCALL PutChar
				bcs .9

				>LDA.G IOCTL+S.IOCTL.BUFPTR+1
				inc
				inc
				sta (pData),y
				>INC.G IOCTL+S.IOCTL.BLKNUM
				>SLEEP
				dec CatBlkCnt
				bne .1

				clc
.9				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			lda hBuf
				beq .1
				
				>SYSCALL FreeMem
				
.1				lda hMem
				beq .2
				
				>SYSCALL FreeMem
				
.2				lda hLIBBLKDEV
				beq .8
				
				>SYSCALL UnloadLib
.8				clc
				rts
*--------------------------------------
CS.RUN.CheckOpt	ldy #1 
				lda (ZPPtr1),y

				ldx #OptionVars-OptionList-1

.2				cmp OptionList,x
				beq .3
				dex
				bpl .2

				sec
				rts

.3				ldy OptionVars,x
				ldx #$ff
				stx 0,y
				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
LIBBLKDEV		.AZ "libblkdev"
hLIBBLKDEV		.BS 1
*--------------------------------------
MSG.USAGE		.AS "Usage : FORMAT <BLOCKDEV> [VOLUME.NAME]\r\n"
				.AS "   -L : Low-Level Format\r\n"
				.AS "   -B xxxx : override Device block size\r\n"
				.AZ " -1-9 : Catalog Size (Blocks)\r\n"
MSG.OK			.AZ "[OK]"
MSG.ERR			.AS "[%h]"
MSG.CRLF		.AZ "\r\n"
MSG.NOSIZE		.AZ "Unable to get media size."
MSG.INIT		.AZ "Formatting %s (%D Blks), Volname:%s\r\n"
MSG.LLDISK2		.AZ "\rTrack %02d..."
MSG.LL			.AZ "\rLow Level Format..."
MSG.WRITECAT	.AZ "Writing Catalog..."
FMT.BLANK		.AZ "BLANK%H%H"
*--------------------------------------
OptionList		.AS "LlBb"
OptionVars		.DA #bLL,#bLL,#bCustomBlkSize,#bCustomBlkSize
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
VolName			.BS 16

IOCTL			.BS S.IOCTL
DIB				.BS S.DIB
DS.END			.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/FORMAT.S
ASM
