NEW
  AUTO 3,1
*--------------------------------------
* ZPInBufPtr = Ptr to Shunk Data
* ZPOutBufPtr = Ptr to Uncompressed Data
*--------------------------------------
* ZPnCnt = !ZPULen
* ZPPtr1
* ZPPtr2
* ZPInMask,ZPBLOfsLBits,ZPBLOfsHBits,ZPBLLenBits
*--------------------------------------
X.Unpak			jsr X.Unpak.GetByte		CHNK.DATA.T
				tax
				bne X.Unpak.PAK
*--------------------------------------
X.Unpak.STORE	jsr X.Unpak.GetULEN	

				ldx ZPnCnt				!LEN LO
				
				ldy #0
				
.1				inx
				bne .2
				
				inc ZPnCnt+1
				beq .8
								
.2				lda (ZPInBufPtr),y
				sta (ZPOutBufPtr),y
				iny
				bne .1
				
				inc ZPInBufPtr+1
				inc ZPOutBufPtr+1

				bra .1

.8				clc
				rts
*--------------------------------------
X.Unpak.PAK		pha						A = BLBITS
				and #$70
				lsr
				lsr
				lsr
				lsr
				sta ZPBLLenBits
				
				pla
				and #$0f
				
				ldx #0
				
				cmp #8
				bcc .10
		
				and #7
				tax
				lda #8
				
.10				sta ZPBLOfsLBits
				stx ZPBLOfsHBits
				
				jsr X.Unpak.GetULEN
				
				jsr X.Unpak.GetByte		get TOPCNT
				
				ldx ZPInBufPtr			ZPPtr1 = TOP table
				stx ZPPtr1
				ldx ZPInBufPtr+1
				stx ZPPtr1+1
				
				clc
				adc ZPInBufPtr			skip TOP table
				sta ZPInBufPtr
				bcc .11
				
				inc ZPInBufPtr+1

.11				lda #$80
				sta ZPInMask
*--------------------------------------	
.2				jsr X.Unpak.GetBitInC
				bcs .3					->TOP	
* BYTE8
				ldx #8
				jsr X.Unpak.GetXBitInA
				bra .52
*--------------------------------------			
.3				jsr X.Unpak.GetBitInC
				bcs .4					->TOP16
* TOP8
				ldx #3
				jsr X.Unpak.GetXBitInA
				bra .51
*--------------------------------------				
.4				jsr X.Unpak.GetBitInC
				bcs .5					->TOP32
* TOP16
				ldx #3
				jsr X.Unpak.GetXBitInA
				ora #%1000
				bra .51
*--------------------------------------				
.5				jsr X.Unpak.GetBitInC
				bcs .6					->BACKLINK
* TOP32				
				ldx #4
				jsr X.Unpak.GetXBitInA
				ora #%10000

.51				tay			
				lda (ZPPtr1),y

.52				jsr X.Unpak.PutByte
				bne .80
				
.99				sec
				rts
*--------------------------------------
* BACKLINK : 1111 OfsLBits OfsHBits LenBits

.6				ldx ZPBLOfsLBits

				jsr X.Unpak.GetXBitInA

				eor #$ff
				sec
				adc ZPOutBufPtr
				sta ZPPtr2
				
				lda ZPBLOfsHBits
				beq .61
				
				php
				tax
				jsr X.Unpak.GetXBitInA
				plp
				
.61				eor #$ff
				adc ZPOutBufPtr+1
				sta ZPPtr2+1
				
				ldx ZPBLLenBits
				jsr X.Unpak.GetXBitInA

				inc						+1
				inc						+1
				inc						+1

				tax
				ldy #0
				
.62				lda (ZPPtr2),y
				jsr X.Unpak.PutByte
				bne .63
				
				dex
				bne .99

				clc
				rts
				
.63				iny
				dex
				bne .62
				
.80				lda ZPnCnt
				and ZPnCnt+1
				inc
				bne .2
				
				clc
				rts
*--------------------------------------
X.Unpak.GetULEN	jsr X.Unpak.GetByte		ULEN LO
				eor #$ff
				sta ZPnCnt

				jsr X.Unpak.GetByte		ULEN HI
				eor #$ff
				sta ZPnCnt+1
				rts
*--------------------------------------			
X.Unpak.GetXBitInA
				lda #0
				
.1				pha
				jsr X.Unpak.GetBitInC
				pla
				rol
				dex
				bne .1
				
				rts
*--------------------------------------
X.Unpak.GetBitInC
				clc
				lda (ZPInBufPtr)
				and ZPInMask
				beq .1

				sec
				
.1				php

				lsr ZPInMask
				bcc .8
				
				ror ZPInMask
				
				jsr X.Unpak.NextByte
				
.8				plp
				rts
*--------------------------------------
X.Unpak.GetByte	lda (ZPInBufPtr)
X.Unpak.NextByte
				inc ZPInBufPtr
				bne .8
				inc ZPInBufPtr+1

.8				rts
*--------------------------------------
X.Unpak.PutByte	inc ZPnCnt
				bne .1
				inc ZPnCnt+1
				beq .8
				
.1				sta (ZPOutBufPtr)
			
				inc ZPOutBufPtr
				bne .8
				inc ZPOutBufPtr+1

.8				rts
*--------------------------------------
*				.LIST ON
*X.Unpak.Size	.EQ *-X.Unpak
*				.LIST OFF
*--------------------------------------
MAN
SAVE usr/src/shared/x.unpak.s
LOAD usr/src/bin/unpak.s
ASM
