NEW
PREFIX
AUTO 4,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF ROOT/FNT2FON
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/GUI.I
				.INB INC/LIBGUI.I
				.INB ROOT/X.FNT.I
*--------------------------------------
ZPPTR1			.EQ ZPBIN
ZPInputBufPtr	.EQ ZPBIN+2
ZPInputTblPtr	.EQ ZPBIN+4
ZPInputBMPtr	.EQ ZPBIN+6
ZPCharBufPtr	.EQ ZPBIN+8
ZPOutPutTblPtr	.EQ ZPBIN+10
ZPOutPutCharOfs	.EQ ZPBIN+12
ZPOutPutBufPtr	.EQ ZPBIN+14
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA 0
				.DA CS.END-CS.START		CS
				.DA DS.END-DS.START		DS
				.DA #64					SS
				.DA #16					ZP
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------				
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT		
				.DA	CS.QUIT
L.MSG.USAGE		.DA MSG.USAGE	
L.MSG.FILE.OK	.DA MSG.FILE.OK			
L.MSG.HEADER.OK	.DA MSG.HEADER.OK
L.MSG.HEADER.KO	.DA MSG.HEADER.KO
L.MSG.RANGE		.DA MSG.RANGE
L.MSG.DUMP		.DA MSG.DUMP
L.MSG.DONE		.DA MSG.DONE
				.DA 0
*--------------------------------------
CS.INIT			
.1				>INC.G Count
				>SYSCALL ArgV
				bcs .7
				
				>STYA ZPPtr1
				
				lda (ZPPtr1)
				cmp #'-'
				bne .4
				
				ldy #1 
				lda (ZPPtr1),y

				ldx OptionList
				
.2				cmp OptionList,x
				beq .3
				dex
				bne .2
				
.99				>LDYA L.MSG.USAGE
				>SYSCALL printf
				lda #E.SYN
				sec
				rts
				
.3				ldy OptionVars-1,x
				lda #$80
				sta (pData),y
				bra .1
				
.4				>LDA.G hInputBuf
				bne .5
				jsr CS.INIT.OPENINPUT
				bcc .1					scan for any other args
				rts
				
.5				>LDA.G hOutputFile
				bne .99
				jsr CS.INIT.CREATEOUTPUT
				bcc .1					scan for any other args
				rts
				
.7				>LDA.G hOutputFile
				beq .99
				
				>LDYAI 8192				max 256x256 pixels
				>SYSCALL getmem
				bcs CS.INIT.RTS
				
				>STYA ZPCharBufPtr
				txa
				>STA.G hCharBuf
				
.8				clc
CS.INIT.RTS		rts
*--------------------------------------
CS.INIT.OPENINPUT
				>PUSHWZ				Aux type
				>PUSHBI 0				Type
				>PUSHBI	O.RDONLY	
				>LDYA ZPPtr1
				>SYSCALL LoadFile
				bcs CS.INIT.RTS
				
				>PUSHYA
				
				txa
				>STA.G hInputBuf
				>SYSCALL GetMemPtr
				>STYA ZPInputBufPtr
				
				>LDYA L.MSG.FILE.OK
				>SYSCALL printf				
				
				ldy #S.FNT.dfVersion
				lda (ZPInputBufPtr),y
				bne .99
				
				iny
				lda (ZPInputBufPtr),y
				cmp #2
				beq .1
				
				cmp #3
				bne .99
				
.1				asl						*2 = Entry Len in Tbl
				>STA.G FNT.Version
				pha
				
				ldy #S.FNT.dfAscent
				lda (ZPInputBufPtr),y
				>STA.G FON.Header+S.FON.Ascent
				>PUSHA

				ldy #S.FNT.dfPixHeight
				lda (ZPInputBufPtr),y
				>STA.G FON.Header+S.FON.PixH
				>PUSHA

				ldy #S.FNT.dfPixWidth
				lda (ZPInputBufPtr),y
				>STA.G FON.Header+S.FON.PixW
				>PUSHA		

				pla
				>PUSHA
				
				>LDYA L.MSG.HEADER.OK
				>SYSCALL printf
				clc
				rts
				
.99				>LDYA L.MSG.HEADER.KO
				>SYSCALL printf
				lda #E.IBIN
				sec
				rts				
*--------------------------------------
CS.INIT.CREATEOUTPUT
				>PUSHWZ				Aux type
				>PUSHBI $CC				FON Type
				>PUSHBI	O.WRONLY+O.CREATE	
				>LDYA ZPPtr1
				>SYSCALL FOpen
				bcs .9
				>STA.G hOutputFile
				
				>LDYAI 256
				>SYSCALL getmem
				bcs .9
				
				>STYA ZPOutPutBufPtr
				txa
				>STA.G hOutPutBuf
				
.9				rts				
*--------------------------------------
CS.RUN			ldy #S.FNT.dfBreakChar
				lda (ZPInputBufPtr),y
				>STA.G FON.Header+S.FON.Break
				>PUSHA

				ldy #S.FNT.dfDefaultChar
				lda (ZPInputBufPtr),y
				>STA.G FON.Header+S.FON.Default
				>PUSHA

				ldy #S.FNT.dfLastChar
				lda (ZPInputBufPtr),y
				>STA.G FON.Header+S.FON.Last
				>PUSHA

				ldy #S.FNT.dfFirstChar
				lda (ZPInputBufPtr),y
				>STA.G FON.Header+S.FON.First
				>STA.G CharIndex
				>PUSHA
				
				>LDYA L.MSG.RANGE
				>SYSCALL printf
				
				>LDA.G FON.Header+S.FON.Last
				sec
				>SBC.G FON.Header+S.FON.First
				inc
				>STA.G CharCount

				asl
				pha
				>STA.G FileOffset
				lda #0
				rol
				iny
				sta (pData),y
				ply
				
				>SYSCALL getmem
				bcs .9
				>STYA ZPOutPutTblPtr
				txa
				>STA.G hTblBuf

				lda #S.FON
				clc
				>ADC.G FileOffset
				sta (pData),y
				sta ZPOutPutCharOfs
				
				iny
				
				lda /S.FON
				adc (pData),y
				sta (pData),y
				sta ZPOutPutCharOfs+1
				
				>LEA.G FileOffset
				>PUSHYA
				>PUSHBI SYS.FSeek.SET
				>LDA.G hOutputFile
				>SYSCALL FSeek
				bcc .1
.9				rts
				
.1				>LDA.G FNT.Version
				ldx #S.FNT.dfCharTable2
				cmp #4
				beq .2
				ldx #S.FNT.dfCharTable3
				
.2				txa
				clc
				adc ZPInputBufPtr
				sta ZPInputTblPtr
				lda #0
				adc ZPInputBufPtr+1
				sta ZPInputTblPtr+1
				
CS.RUN.LOOP		>SLEEP

				>SYSCALL GetChar
				bcs .3

				cmp #$03				Ctrl-C
				beq .9					Abort....
				
				cmp #$13				Ctrl-S
				bne .3
				
				>LDA.G bPause
				eor #$FF
				sta (pData),y

.3				>LDA.G bPause
				bmi CS.RUN.LOOP

				ldy #2
				lda (ZPInputTblPtr),y	Get Char BitMap Offset 
				clc
				adc ZPInputBufPtr
				sta ZPInputBMPtr
				iny
				lda (ZPInputTblPtr),y
				adc ZPInputBufPtr+1
				sta ZPInputBMPtr+1
				
				jsr CS.RUN.GetChar
				jsr CS.RUN.ShiftLChar
				jsr CS.RUN.CutRChar
				jsr CS.RUN.DumpChar
				jsr CS.RUN.StoreChar
				bcs .9
				
				>INC.G CharIndex
				>DEC.G CharCount
				beq .8
				
				lda ZPInputTblPtr
				clc
				>ADC.G FNT.Version
				sta ZPInputTblPtr
				bcc CS.RUN.LOOP
				inc ZPInputTblPtr+1
				bra CS.RUN.LOOP
				
.8				jsr CS.RUN.FlushOutPutBuf
				bcs .9
				
				jsr CS.RUN.WriteBMAndTbl
				bcs .9
				
				>LDYA L.MSG.DONE
				>SYSCALL printf
				
				lda #0
				sec
.9				rts
*--------------------------------------
* ZPInputBMPtr->DATA
*--------------------------------------
CS.RUN.GetChar	lda (ZPInputTblPtr)		Get Char PixW

				>STA.G CharPixW
				>STA.G CharOutPixW

				pha
				lsr
				lsr
				lsr
				tax
				pla
				and #7
				beq .1
				inx
				
.1				txa
				>STA.G CharByteW
				>STA.G CharOutByteW
				
				lda #0					0 -> A -> CharByteW
				
.2				pha
				clc
				adc ZPCharBufPtr
				sta ZPPTR1
				lda #0
				adc ZPCharBufPtr+1
				sta ZPPTR1+1
				
				>LDA.G FON.Header+S.FON.PixH
				
				tax
		
.3				lda (ZPInputBMPtr)		Get one source byte
		
				inc ZPInputBMPtr
				bne .4
				inc ZPInputBMPtr+1
				
.4				tay
				lda TWIST,y
				sta (ZPPTR1)
				lda ZPPTR1
				clc
				>ADC.G CharByteW
				sta ZPPTR1
				bcc .5
				inc ZPPTR1+1
				
.5				dex
				bne .3
		
				pla
				inc
				>CMP.G CharByteW
				bne .2
				
				clc
				rts
*--------------------------------------
* Dump ZPCharBufPtr : CharByteW x FON.Header+S.FON.PixH
*--------------------------------------
CS.RUN.DumpChar	>LDA.G CharOutByteW
				>PUSHA
				>LDA.G CharOutPixW
				>PUSHA
				>LDA.G CharIndex
				>PUSHA
				>PUSHA
				>LDYA L.MSG.DUMP
				>SYSCALL printf
				
				lda ZPCharBufPtr
				sta ZPPTR1
				lda ZPCharBufPtr+1
				sta ZPPTR1+1

				>LDA.G FON.Header+S.FON.PixH
				
.1				pha
				lda #'|'
				>SYSCALL PutChar
				
				>LDA.G CharOutPixW
				>STA.G Count

				ldy #0
				
.2				lda (ZPPTR1),y
				phy
				
				ldx #8
				
.4				lsr
				pha
				lda #' '
				bcc .5
				lda #'O'
				
.5				phx
				>SYSCALL PutChar
				plx
				
				>DEC.G Count
				beq .6
				
				pla						Get back BYTE
				
				dex
				bne .4
				
				ply
				iny
				bra .2
				
.6				pla
				pla

				lda #'|'
				>SYSCALL PutChar
				lda #13
				>SYSCALL PutChar
				lda #10
				>SYSCALL PutChar
				
				lda ZPPTR1
				clc
				>ADC.G CharByteW
				sta ZPPTR1
				bcc .7
				inc ZPPTR1+1

.7				pla
				dec
				bne .1
				
				rts
*--------------------------------------
CS.RUN.ShiftLChar
				>LDA.G bNoShrink
				bmi .9
				
.1				lda ZPCharBufPtr
				sta ZPPTR1
				lda ZPCharBufPtr+1
				sta ZPPTR1+1
				
				>LDA.G FON.Header+S.FON.PixH
				tax

				lda #0
				pha
				
.2				pla
				ora (ZPPTR1)
				pha
				
				lda ZPPTR1
				clc
				>ADC.G CharByteW
				sta ZPPTR1
				bcc .3
				inc ZPPTR1+1
				
.3				dex
				bne .2
				
				pla
				beq .9					No pixel in first COL, must be SPACE or BREAK
				
				lsr				
				bcs .9
				
				lda ZPCharBufPtr
				sta ZPPTR1
				lda ZPCharBufPtr+1
				sta ZPPTR1+1
				
				>LDA.G FON.Header+S.FON.PixH
				tax

.4				>LDA.G CharByteW
				tay
				clc
				
.5				dey
				lda (ZPPTR1),y
				ror
				sta (ZPPTR1),y
				tya
				bne .5
				
				lda ZPPTR1
				clc
				>ADC.G CharByteW
				sta ZPPTR1
				bcc .6
				inc ZPPTR1+1
				
.6				dex
				bne .4
				
				>DEC.G CharOutPixW
				and #7
				bne .1
				>DEC.G CharOutByteW
				bra .1
				
.9				rts
*--------------------------------------
CS.RUN.CutRChar	>LDA.G bNoShrink
				bmi .9
				
.1				lda ZPCharBufPtr
				sta ZPPTR1
				lda ZPCharBufPtr+1
				sta ZPPTR1+1
				
				>LDA.G FON.Header+S.FON.PixH
				tax

				>LDA.G CharOutByteW
				tay
				dey
				
				lda #0
				pha
				
.2				pla
				ora (ZPPTR1),y
				pha
				
				phy
				lda ZPPTR1
				clc
				>ADC.G CharByteW
				sta ZPPTR1
				bcc .3
				inc ZPPTR1+1

.3				ply
				dex
				bne .2
				
				pla
				bmi .9					Bit 7 is colored....exit
				bne .4
				
				tya						No bits in this COL
				beq .9					was COL 0.....SPACE or BREAK

				>DEC.G CharOutByteW		Remove the whole column
				asl
				asl
				asl
				>STA.G CharOutPixW
				bra .1
				
.4				pha
				>LDA.G CharOutByteW
				asl
				asl
				asl
				tax
				pla
				
.5				dex
				asl
				bpl .5
				
				txa
				>STA.G CharOutPixW
				
				pha
				lsr
				lsr
				lsr
				tax
				pla
				and #7
				beq .7
				inx
				
.7				txa
				>STA.G CharOutByteW
				
.9				rts
*--------------------------------------
CS.RUN.StoreChar
				lda ZPOutPutCharOfs
				sta (ZPOutPutTblPtr)
				
				lda ZPOutPutCharOfs+1
				ldy #1
				sta (ZPOutPutTblPtr),y
				
				lda ZPOutPutTblPtr
				clc
				adc #2
				sta ZPOutPutTblPtr
				bcc .1
				
				inc ZPOutPutTblPtr+1
				
.1				>LDA.G CharOutPixW
				jsr CS.RUN.WriteByte
				bcs .99
				
				>LDA.G CharOutByteW
				jsr CS.RUN.WriteByte
				bcs .99
				
				lda ZPCharBufPtr
				sta ZPPTR1
				lda ZPCharBufPtr+1
				sta ZPPTR1+1
				
				>LDA.G FON.Header+S.FON.PixH
				pha

.2				>LDA.G CharOutByteW
				tax
				
				ldy #0
				
.3				lda (ZPPTR1),y
				jsr CS.RUN.WriteByte
				bcs .9
				
				iny
				dex
				bne .3
				
				pla
				dec
				beq .8
				pha
				
				lda ZPPTR1
				clc
				>ADC.G CharByteW
				sta ZPPTR1
				bcc .2
				inc ZPPTR1+1
				bra .2
				
.8				clc				
				rts
				
.9				plx
.99				rts
*--------------------------------------
CS.RUN.WriteByte
				phy
				phx
				pha
				>LDA.G OutPutPtr
				tay
				
				pla
				sta (ZPOutPutBufPtr),y
				tya
				inc
				>STA.G OutPutPtr
				tay
				clc
				bne .8
				
				>PUSHWI 256
				>PUSHW ZPOutPutBufPtr
				>LDA.G hOutputFile
				>SYSCALL FWrite
				bcs .9

.8				inc ZPOutPutCharOfs
				bne .9
				inc ZPOutPutCharOfs+1

.9				plx
				ply
				rts
*--------------------------------------
CS.RUN.FlushOutPutBuf
				clc
				>LDA.G OutPutPtr
				beq .8

				tay
				lda #0
				>PUSHYA
				
				>PUSHW ZPOutPutBufPtr
				>LDA.G hOutputFile
				>SYSCALL FWrite
				
.8				rts
*--------------------------------------
CS.RUN.WriteBMAndTbl
				ldy #FileOffset
				ldx #4
				lda #0
				
.1				sta (pData),y
				iny
				dex
				bne .1
				
				>LEA.G FileOffset
				>PUSHYA
				>PUSHBI SYS.FSeek.SET
				>LDA.G hOutputFile			
				>SYSCALL FSeek
				bcs .9
				
				>PUSHWI S.FON
				>PUSHEA.G FON.Header
				>LDA.G hOutputFile
				>SYSCALL FWrite
				bcs .9
				
				>LDA.G FON.Header+S.FON.Last
				sec
				>SBC.G FON.Header+S.FON.First
				inc
				
				asl
				pha
				lda #0
				rol
				ply
				>PUSHYA

				>LDA.G hTblBuf
				>SYSCALL GetMemPtr
				>PUSHYA

				>LDA.G hOutputFile
				>SYSCALL FWrite
				
				clc
.9				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			>LDA.G hOutPutBuf
				beq .1
				>SYSCALL FreeMem

.1				>LDA.G hOutputFile
				beq .2
				>SYSCALL fclose
				
.2				>LDA.G hInputBuf
				beq .3
				>SYSCALL FreeMem

.3				>LDA.G hCharBuf
				beq .4
				>SYSCALL FreeMem

.4				>LDA.G hTblBuf
				beq .8
				>SYSCALL FreeMem

.8				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
				.INB ROOT/X.TWIST.G
*--------------------------------------
OptionList		>PSTR "NnVv"
OptionVars		.DA #bNoShrink,#bNoShrink,#bVerbose,#bVerbose
MSG.USAGE		.AZ "Usage : FNT2FON FNT-File \r\n   -V : Verbose Mode\r\n   -N : No shrink\r\n"
MSG.FILE.OK		.AZ "FNT File Loaded, %D Bytes Read\r\n"		
MSG.HEADER.OK	.AZ "FNT File Version : %d, Size : %d x %d pixels, Ascent : %d\r\n"		
MSG.HEADER.KO	.AZ "Invalid Input File Format\r\n"
MSG.RANGE		.AZ "First/Last Char : %d/%d, Default : %d, Break : %d\r\n"
MSG.DUMP		.AZ "Caracter ID : %d (0x0%2h), Width(P) : %d, Width(B) : %d\r\n"
MSG.DONE		.AZ "All Done!!!\r\n"		
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
FNT.Version		.BS 1
FON.Header		.BS	S.FON
Count			.BS 1
hInputBuf		.BS 1
hOutputFile		.BS 1
bPause			.BS 1
bVerbose		.BS 1
bNoShrink		.BS 1
hCharBuf		.BS 1
hTblBuf			.BS 1
CharIndex		.BS 1
CharCount		.BS 1
CharPixW		.BS 1
CharByteW		.BS 1
CharOutPixW		.BS 1
CharOutByteW	.BS 1
hOutPutBuf		.BS 1
OutPutPtr		.BS 1
FileOffset		.BS 4

DS.END
				.ED
*--------------------------------------
MAN
SAVE /A2OSX.BUILD/ROOT/FNT2FON.S
ASM
