NEW
PREFIX
AUTO 4,1
				.LIST OFF	
*--------------------------------------
* Sliding Window = 4096
*--------------------------------------
WSIZE			.EQ 4096				power of 2
*--------------------------------------
* Output BITSTREAM :
*  0 : Lit (8)
*  1 : Copy, L (4), P.HI (4), P.LO (8)
*--------------------------------------
Pak.LZ			jsr Pak.In.Init			Init SrcPtr,SrcCnt
				jsr Pak.Out.Init

* future optimization, make P grows from 2 to 12 while increasing W size
				
*				lda #2					Ptr width starts at 2 bits
*				sta Pak.LZ.PWidth
				
*				lda #%00000011			Ptr Mask Starts at %000000000011
*				sta Pak.LZ.PMaskLO
*				stz Pak.LZ.PMaskHI
				
				lda #$ff
				sta Pak.LZ.WCnt
				sta Pak.LZ.WCnt+1		WCnt=!0

.1				jsr Pak.In.GetRH		Z if EOF
				beq .80
				
				cmp #3
				bcc .8					no more than 2 bytes left....store
				
				cmp #19					more than 18 bytes left, limit to 18
				bcc .2
				lda #18
				
.2				tax						X = Read ahead max size
				
				lda ZPSrcPtr			make ZPSrcWPtr=ZPSrcPtr
				sta ZPSrcWPtr
				
				lda ZPSrcPtr
				sta ZPSrcWPtr
				
				lda Pak.LZ.WCnt
				sta ZPCnt
				lda Pak.LZ.WCnt+1
				sta ZPCnt+1
				
				stz Pak.LZ.BestLen
				
.3				inc ZPCnt
				bne .4
				inc ZPCnt+1
				beq .7					reached begining of window....store
				
				lda ZPSrcWPtr			make ZPSrcWPtr-=1
				sec
				sbc #1
				sta ZPSrcWPtr
				bcs .4
				dec ZPSrcWPtr+1
				
.4				ldy #0
				phx						save Read ahead max size

.5				lda (ZPSrcPtr),y		Try to find a match 
				cmp (ZPSrcWPtr),y
				bne .6
				
				iny
				dex						don't exceed 18 or Src Buffer
				bne .5
				
.6				plx						get back Read ahead max size

				cpy #3					matched a least 3 chars ?
				bcc .3					no....try one char back in Wnd
				
				cpy Pak.LZ.BestLen		better than last match ?
				bcc .3					no....try one char back in Wnd
				
				sty Pak.LZ.BestLen
				lda ZPSrcWPtr
				sta Pak.LZ.BestPtr
				lda ZPSrcWPtr+1
				sta Pak.LZ.BestPtr+1
				bra .3
				
.7				lda Pak.LZ.BestLen		do we match something ?
				beq .8
				
				lda Pak.LZ.BestPtr
				sec
				sbc ZPSrcPtr
				sta Pak.LZ.BestPtr
				
				lda Pak.LZ.BestPtr+1
				sbc ZPSrcPtr+1
				sta Pak.LZ.BestPtr+1
				
				tya 					get len
				dec						Adjust range 0-15
				dec
				dec
				ora Pak.LZ.BestPtr+1	merge with P.HI
				
				sec
				jsr Pak.Out.PutCA		Put 9 bits 1+LLLL+PPPP
				bcs .99
				
				lda Pak.LZ.BestPtr		Put 8 bits pppppppp
				
				jsr Pak.Out.PutA
				bcs .99
				
				tya
				bra .81
				
.8				lda (ZPSrcPtr)
				clc
				jsr Pak.Out.PutCA		Put 9 bits 0+xxxxxxxx
				bcs .99
				lda #1
				
.81				jsr Pak.In.MoveFWA
				tya
				jsr LZ.MoveWndY
				bra .3
				bra .71
				
.80				clc
.99				rts
*--------------------------------------
Unpak.LZ


				clc
				rts
*--------------------------------------
LZ.MoveWndY		tya
				sec
				sbc Pak.LZ.WCnt
				tax
				lda #0
				sbc Pak.LZ.WCnt+1
				
				cpx #WSIZE^$FF
				sbc /WSIZE^$FF
				
				bcs .1
				
				ldx #WSIZE^$FF
				lda /WSIZE^$FF
				
.1				stx Pak.LZ.WCnt
				sta Pak.LZ.WCnt+1
				
				rts			 
*--------------------------------------
MAN
SAVE USR/SRC/LIB/LIBPAK.S.LZ
ASM
