NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF ROOT/UNPAK
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/LIBPAK.I
*--------------------------------------
ZPPakHdrPtr		.EQ ZPBIN
ZPPakDataPtr	.EQ ZPBIN+2
ZPPakSrcPtr		.EQ ZPBIN+4

*--------------------------------------
UNPAK.Mask		.EQ ZPBIN+8
UNPAK.LastByte	.EQ ZPBIN+9
UNPAK.Cnt		.EQ ZPBIN+10
UNPAK.Ofs		.EQ ZPBIN+12
UNPAK.BLOfs		.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.LIBPAK		.DA LIBPAK
L.CONST.PI		.DA CONST.PI
L.CONST.3		.DA CONST.3
L.MSG1			.DA MSG1
L.MSG2			.DA MSG2
L.MSG3			.DA MSG3
L.MSG4			.DA MSG4
L.MSG5			.DA MSG5
L.MSG6			.DA MSG6
L.MSG.UnpakErr	.DA MSG.UnpakErr
L.MSG.DEBUG.S3	.DA MSG.DEBUG.S3
				.DA 0
*--------------------------------------
CS.INIT			>LDYA L.LIBPAK
				>SYSCALL LoadLib
				bcs .9
				sta hLIBPAK
				
				clc
.9				rts
*--------------------------------------
CS.RUN			ldy	#S.PS.ARGC
				lda (pPs),y
				bne CS.RUN.Pak
				jmp CS.RUN.Math
*--------------------------------------
CS.RUN.Pak		>PUSHWZ				Aux type
				>PUSHBI 0
				>PUSHBI	O.RDONLY

				lda #1
				>SYSCALL ArgV
				>SYSCALL RealPath
				>PUSHYA
				txa
				>STA.G PAK.hFileName
				
				>SYSCALL LoadFile
				bcs .9
				
				>STYA.G PAK.FileSize
				txa
				>STA.G PAK.hFileBuf
				
				>LDYA.G PAK.FileSize
				>SYSCALL getmem
				bcs .9
				>STYA ZPPakSrcPtr
				txa
				>STA.G PAK.hOutBuf
				
				>LDA.G PAK.hFileBuf
				>SYSCALL GetMemPtr
				>PUSHYA
				
				>PUSHW.G PAK.FileSize

				>LDA.G PAK.hOutBuf
				>SYSCALL GetMemPtr
				>STYA ZPPakHdrPtr
				>PUSHYA
				>PUSHEA.G PAK.Stat

				>LIBCALL hLIBPAK,LIBPAK.Pak
				bcc .1
.9				rts
				
.1				>PUSHW.G PAK.Stat+S.PAKSTAT.STORE
				>PUSHW.G PAK.Stat+S.PAKSTAT.S4
				>PUSHW.G PAK.Stat+S.PAKSTAT.S3
				>PUSHW.G PAK.Stat+S.PAKSTAT.REP
				>PUSHW.G PAK.Stat+S.PAKSTAT.BL
				>PUSHW.G PAK.Stat+S.PAKSTAT.PASS2
				>PUSHW.G PAK.Stat+S.PAKSTAT.PASS1
				>PUSHW.G PAK.FileSize
				>PUSHBI 16
				>LDYA L.MSG6
				>SYSCALL printf
*--------------------------------------
CS.RUN.Unpak	lda ZPPakHdrPtr
				clc
				adc #S.PAKSTAT
				sta ZPPakDataPtr

				lda ZPPakHdrPtr+1
				adc /S.PAKSTAT
				sta ZPPakDataPtr+1
				
				lda (ZPPakHdrPtr)
				eor #$ff
				sta UNPAK.Cnt

				ldy #1
				lda (ZPPakHdrPtr),y
				eor #$ff
				sta UNPAK.Cnt+1
				
				lda #$80
				sta UNPAK.Mask
				
				>LDA.G PAK.hFileBuf
				>SYSCALL GetMemPtr
				>STYA ZPPakSrcPtr
				
				stz	UNPAK.Ofs
				stz	UNPAK.Ofs+1
				
.1				inc UNPAK.Cnt
				bne .2
				inc UNPAK.Cnt+1
				bne .2
				
				lda #0
				sec
				rts				
				
.2				lda #'>'
				>SYSCALL PutChar
				
				jsr CS.RUN.UnpakGetBitInC
				bcs .3
*--------------------------------------
* SHORT3				
				ldx #3
				jsr CS.RUN.UnpakGetXBitInA
				clc
				adc #S.PAKHDR.SHORT3
				tay
				lda (ZPPakHdrPtr),y
				sta UNPAK.LastByte
				jsr DEBUG.S3
				jsr CS.RUN.UnpakCmpA
				bcs .9
				bra .1
				
.3				jsr CS.RUN.UnpakGetBitInC
				bcs .4
*--------------------------------------
* SHORT4				
				ldx #4
				jsr CS.RUN.UnpakGetXBitInA
				clc
				adc #S.PAKHDR.SHORT4
				tay
				lda (ZPPakHdrPtr),y
				sta UNPAK.LastByte
				jsr CS.RUN.UnpakCmpA
				bcs .9
				bra .1
				
.4				jsr CS.RUN.UnpakGetBitInC
				bcs .5
*--------------------------------------
* REPn				
				ldx #4
				jsr CS.RUN.UnpakGetXBitInA
				tay
				tax
				lda UNPAK.LastByte
				
.41				jsr CS.RUN.UnpakCmpA
				bcs .9
				dey
				bne .41
				
.42				txa
				clc
				adc UNPAK.Cnt
				sta UNPAK.Cnt
				bcc .2
				inc UNPAK.Cnt+1
				bra .2
				
.5				jsr CS.RUN.UnpakGetBitInC
				bcs .6
*--------------------------------------
* BACKLINK				
				ldx #4
				jsr CS.RUN.UnpakGetXBitInA
				sta UNPAK.BLOfs+1
				jsr CS.RUN.UnpakGetByteInA
				sta UNPAK.BLOfs
				jsr CS.RUN.UnpakGetByteInA
				tax
				tay
				
.51								
				bra .42
*--------------------------------------
* STOREn		
.6				ldx #4
				jsr CS.RUN.UnpakGetXBitInA
				tay
				tax
				
.61				jsr CS.RUN.UnpakGetByteInA
				dey
				bne .61
				
				sta UNPAK.LastByte
				
				bra .42
				
.9				>PUSHW UNPAK.Ofs
				>PUSHBI 2
				>LDYA L.MSG.UnpakErr
				>SYSCALL printf
				
				lda #0
				sec
				rts				
*--------------------------------------
CS.RUN.UnpakGetByteInA
				ldx #8
*--------------------------------------
CS.RUN.UnpakGetXBitInA
				lda #0
				
.1				jsr CS.RUN.UnpakGetBitInC
				rol
				dex
				bne .1
				
				rts
*--------------------------------------
CS.RUN.UnpakGetBitInC
				pha
				clc
				lda (ZPPakDataPtr)
				and UNPAK.Mask
				beq .1
				sec
				
.1				php

				lsr UNPAK.Mask
				bne .8
				
				lda #$80
				sta UNPAK.Mask
				
				inc ZPPakDataPtr
				bne .8
				inc ZPPakDataPtr+1

.8				plp
				pla
				rts
*--------------------------------------
CS.RUN.UnpakCmpA
				cmp (ZPPakSrcPtr)
				bne .9
				inc ZPPakSrcPtr
				bne .1
				inc ZPPakSrcPtr+1
				
.1				inc UNPAK.Ofs
				bne .8
				inc UNPAK.Ofs+1
				
.8				clc
				rts
				
.9				sec
				rts
*--------------------------------------
DEBUG.S3		pha
				phx
				phy
				>PUSHA
				>PUSHBI 1
				>LDYA L.MSG.DEBUG.S3
				>SYSCALL printf
				ply
				plx
				pla
				rts
*--------------------------------------
CS.RUN.Math		>PUSHEA.G ENDPTR
				>LDYA L.CONST.PI
				>SYSCALL StrToF
				>PULLF.G MYFLOAT.PI
				
				>PUSHEA.G ENDPTR
				>LDYA L.CONST.3
				>SYSCALL StrToF
				>PULLF.G MYFLOAT.3
				
				>PUSHF.G MYFLOAT.PI
				>PUSHF.G MYFLOAT.3
				
				>PUSHF FLOAT5
				>PUSHF FLOAT4
				>PUSHF FLOAT3
				>PUSHF FLOAT2
				>PUSHF FLOAT1

				>PUSHBI 35
				>LDYA L.MSG1
				>SYSCALL printf
*--------------------------------------
				>PUSHF.G MYFLOAT.PI
				>PUSHF.G MYFLOAT.3
				>SYSCALL FDiv
				>PULLF.G MYFLOAT3
				
				>PUSHF.G MYFLOAT3
				>PUSHBI 5
				>LDYA L.MSG2
				>SYSCALL printf
*--------------------------------------
				>PUSHF.G MYFLOAT3
				>SYSCALL Cos
				>PULLF.G MYFLOAT3
				
				>PUSHF.G MYFLOAT3
				>PUSHBI 5
				>LDYA L.MSG3
				>SYSCALL printf
*--------------------------------------
				>PUSHF FLOAT1
				>SYSCALL LRIntF
				>PULLL.G MYLONG
				
				>PUSHL.G MYLONG
				>PUSHBI 4
				>LDYA L.MSG4
				>SYSCALL printf
*--------------------------------------
				>PUSHL LONG1
				>SYSCALL Float
*				>PULLF.G MYFLOAT3
				
*				>PUSHF.G MYFLOAT3
				>PUSHBI 5
				>LDYA L.MSG5
				>SYSCALL printf
				lda #0
				sec
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			>LDA.G PAK.hFileName
				beq .1
				
				>SYSCALL FreeMem
				
.1				>LDA.G PAK.hFileBuf
				beq .2
				
				>SYSCALL FreeMem
				
.2				>LDA.G PAK.hOutBuf
				beq .8
				
				>SYSCALL FreeMem
				
.8				lda hLIBPAK
				>SYSCALL UnloadLib
				clc
				rts
*--------------------------------------
CS.END
LIBPAK			.AZ "libpak.o"
hLIBPAK			.BS 1
MSG1			.AS "PrintF:\r\n"
				.AS " %e, %e, %e, %e, %e\r\n"
				.AS "StrToF:\r\n"
				.AZ " %e, %e\r\n"
MSG2			.AZ "PI/3=%e\r\n"
MSG3			.AZ "COS(PI/3)=%e\r\n"
MSG4			.AZ "LRIntF(99,999,999.9)=%L\r\n"
MSG5			.AZ "Float(123456789)=%e\r\n"
CONST.PI		.AZ "3.141592653"
CONST.3			.AZ "3"
FLOAT1			.HS 9B3EBC1FFD  		99,999,999.9
FLOAT2			.HS 9E6E6B27FD  		999,999,999
FLOAT3		  	.HS 9E6E6B2800  		1,000,000,000
FLOAT4			.HS 8100000000			1
FLOAT5			.HS 9080000000			-32768
LONG1			.DA >123456789
*--------------------------------------
MSG6			.AS "Src Length : %D\r\n"
				.AS "Pass #1    : %D\r\n"	
				.AS "Pass #2    : %D\r\n"	
				.AS "BL Count   : %D\r\n"
				.AS "REP Count  : %D\r\n"
				.AS "S3 Count   : %D\r\n"
				.AS "S4 Count   : %D\r\n"
				.AZ "STO Count  : %D\r\n"
MSG.UnpakErr	.AZ "UnPak Error at Offset : %H\r\n" 				
MSG.DEBUG.S3	.AZ "S3:%h\r\n"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
MYFLOAT.PI		.BS 5
MYFLOAT.3		.BS 5
MYFLOAT3		.BS 5
MYLONG			.BS 4
ENDPTR			.BS 2

PAK.hFileName	.BS 1
PAK.FileSize	.BS 2
PAK.hFileBuf	.BS 1
PAK.hOutBuf		.BS 1
PAK.Stat		.BS S.PAKSTAT
DS.END
				.ED
*--------------------------------------
MAN
SAVE BIN/UNPAK.S
ASM
