A2osX/LIB/LIBPAK.S.txt

515 lines
8.8 KiB
Plaintext
Raw Normal View History

2019-09-19 15:22:14 +00:00
NEW
AUTO 3,1
.LIST OFF
.OP 65C02
.OR $2000
2019-09-25 15:34:43 +00:00
.TF LIB/LIBPAK
*--------------------------------------
2018-07-23 15:28:42 +00:00
.INB INC/MACROS.I
.INB INC/A2OSX.I
.INB INC/LIBPAK.I
*--------------------------------------
ZPSrcPtr .EQ ZPLIB
ZPSrcWPtr .EQ ZPLIB+2
ZPCnt .EQ ZPLIB+4
2018-02-08 16:39:56 +00:00
ZPHdrPtr .EQ ZPLIB+14
ZPDstPtr .EQ ZPLIB+16
ZPStatPtr .EQ ZPLIB+18
*--------------------------------------
* 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
.DA 0
.DA 0
.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1 .DA LIB.LOAD
.DA LIB.UNLOAD
.DA Pak
.DA 0
*--------------------------------------
LIB.LOAD
LIB.UNLOAD clc
rts
2018-02-05 16:25:25 +00:00
*/--------------------------------------
* # Pak
2018-06-18 15:48:00 +00:00
* **In:**
* ##ASM
2019-09-25 15:34:43 +00:00
* PUSHW = S.PAKSTAT Ptr
* PUSHW = Output Buffer Ptr
* PUSHW = Input Buffer Len
* PUSHW = Input Buffer Ptr
2018-02-05 16:25:25 +00:00
*\--------------------------------------
2019-09-25 15:34:43 +00:00
Pak >PULLW Pak.SrcPtr
>PULLW Pak.SrcLen
>PULLW ZPDstPtr
>PULLW ZPStatPtr
2018-02-09 16:02:47 +00:00
* Reset Byte counters
ldx #0
.1 stz Pak.CntL,x
stz Pak.CntH,x
inx
bne .1
2018-02-08 16:39:56 +00:00
2018-02-09 16:02:47 +00:00
* PASS #1 : REP & BL, no store, update byte counters
2018-02-08 16:39:56 +00:00
2018-02-09 16:02:47 +00:00
stz Pak.bPass2
2018-02-08 16:39:56 +00:00
2019-09-25 15:34:43 +00:00
jsr Pak.InitPass
2018-02-09 07:04:19 +00:00
jsr Pak.Run
bcs .9
2018-02-05 16:25:25 +00:00
2018-02-09 07:04:19 +00:00
jsr Pak.BuildShortTable
2018-02-09 16:02:47 +00:00
* PASS #2 : REP & BL, store with S3,S4
2018-02-08 16:39:56 +00:00
2018-02-09 16:02:47 +00:00
dec Pak.bPass2
2018-02-08 16:39:56 +00:00
2019-09-25 15:34:43 +00:00
jsr Pak.InitPass
jsr Pak.Out.Init Initialize properly for first "PutBit" Call
2018-02-08 16:39:56 +00:00
2018-02-09 16:02:47 +00:00
stz Pak.StoreCnt
2018-02-09 07:04:19 +00:00
jsr Pak.Run
bcs .9
2019-09-25 15:34:43 +00:00
ldy #S.PAKSHNK-1
2018-02-09 07:04:19 +00:00
2019-09-25 15:34:43 +00:00
.2 lda Pak.Shnk,y
2018-02-09 07:04:19 +00:00
sta (ZPHdrPtr),y
dey
2018-02-09 16:02:47 +00:00
bpl .2
2018-02-08 16:39:56 +00:00
ldy #S.PAKSTAT-1
2018-02-09 16:02:47 +00:00
.3 lda Pak.Stat,y
2018-02-08 16:39:56 +00:00
sta (ZPStatPtr),y
dey
2018-02-09 16:02:47 +00:00
bpl .3
2018-02-08 16:39:56 +00:00
clc
rts
2018-02-09 07:04:19 +00:00
2018-10-21 20:54:07 +00:00
.9 lda #E.OOM
2018-02-08 16:39:56 +00:00
sec
rts
*--------------------------------------
Pak.InitPass >LDYA Pak.SrcPtr
>STYA ZPSrcPtr
>STYA Pak.WPtr
2019-09-25 15:34:43 +00:00
lda Pak.SrcLen
eor #$ff
sta Pak.SrcCnt
2018-02-08 16:39:56 +00:00
2019-09-25 15:34:43 +00:00
lda Pak.SrcLen+1
eor #$ff
sta Pak.SrcCnt+1
2018-02-08 16:39:56 +00:00
lda #$ff RepChar invalid
sta Pak.RepCnt
2018-02-09 07:04:19 +00:00
ldx #S.PAKSTAT.PASS2
2018-02-08 16:39:56 +00:00
.1 stz Pak.Stat,x Reset Stats
inx
cpx #S.PAKSTAT
bne .1
rts
*--------------------------------------
2018-02-09 07:04:19 +00:00
Pak.Run lda #$ff
2018-02-08 16:39:56 +00:00
cmp Pak.SrcCnt+1 return 255 if not $ffxx
2018-02-09 07:04:19 +00:00
bne .1
2018-02-08 16:39:56 +00:00
lda Pak.SrcCnt
eor #$ff return !xx if $ffxx
2018-02-09 07:04:19 +00:00
beq .8 0 byte left...
.1 cmp #4 less than 4 chars, give up BL
bcc .4
sta Pak.MaxReadAhead
stz Pak.BestBLLen
ldx #3
.2 inx
jsr Pak.ScanBL
bcs .3
>STYA Pak.BestBL
stx Pak.BestBLLen
cpx Pak.MaxReadAhead
bne .2
.3 lda Pak.BestBLLen
beq .4 NO BL found....
lda Pak.BestBL+1
ora #PAK.B.BACKLINK %1110 + Ofs.HI
jsr Pak.PutA
bcs .9
lda Pak.BestBL get Ofs.LO
jsr Pak.PutA
bcs .9
lda Pak.BestBLLen get back len
jsr Pak.PutA
bcs .9
lda Pak.BestBLLen
jsr Pak.SrcForward
ldx #S.PAKSTAT.BL
jsr Pak.UpdateStats
bra Pak.Run
.4 lda (ZPSrcPtr)
jsr Pak.PutA
bcs .9
lda #1
jsr Pak.SrcForward
bra Pak.Run
2018-02-09 16:02:47 +00:00
.8 lda Pak.RepCnt do we have a pending REP ?
beq .80
ora #PAK.B.REPn yes, add it
jmp Pak.PutA.1
.80 clc
2018-02-09 07:04:19 +00:00
.9 rts
2018-02-08 16:39:56 +00:00
*--------------------------------------
Pak.SrcForward pha Len
2018-02-09 07:04:19 +00:00
2018-02-08 16:39:56 +00:00
clc
adc ZPSrcPtr
sta ZPSrcPtr
bcc .1
clc
inc ZPSrcPtr+1
.1 pla
adc Pak.SrcCnt
sta Pak.SrcCnt
bcc .2
inc Pak.SrcCnt+1
.2 sec
lda ZPSrcPtr
sbc #WSIZE
tay
2018-02-08 16:39:56 +00:00
lda ZPSrcPtr+1
sbc /WSIZE
tax
2018-02-08 16:39:56 +00:00
cpy Pak.WPtr
sbc Pak.WPtr+1
2018-02-09 07:04:19 +00:00
bcc .8
2018-02-08 16:39:56 +00:00
sty Pak.WPtr
stx Pak.WPtr+1
.8 rts
*--------------------------------------
* Scan between Pak.WPtr & ZPSrcPtr-A
* Byte string at ZPSrcPtr (len A)
2018-02-09 07:04:19 +00:00
* IN : X = Byte string length
2018-02-08 16:39:56 +00:00
* OUT : CC: Y,A=offset to BL
*--------------------------------------
2018-02-09 07:04:19 +00:00
Pak.ScanBL stx Pak.WStrLen
2018-02-05 16:25:25 +00:00
2018-02-08 16:39:56 +00:00
sec
lda ZPSrcPtr WLimit=ZPSrcPtr-WStrLen
sbc Pak.WStrLen
sta Pak.WLimit
lda ZPSrcPtr+1
sbc #0
sta Pak.WLimit+1
2018-02-08 16:39:56 +00:00
lda Pak.WLimit
sec
sbc Pak.WPtr
eor #$ff
sta Pak.Cnt
lda Pak.WLimit+1
sbc Pak.WPtr+1
eor #$ff
sta Pak.Cnt+1
2018-02-05 16:25:25 +00:00
2018-02-08 16:39:56 +00:00
bcc .9 WLimit < Pak.WPtr
2018-02-08 16:39:56 +00:00
lda Pak.WPtr
sta ZPSrcWPtr
lda Pak.WPtr+1
sta ZPSrcWPtr+1
ldy #0
.1 lda (ZPSrcWPtr),y
cmp (ZPSrcPtr),y
bne .6
iny
cpy Pak.WStrLen
bne .1
sec
lda ZPSrcPtr
sbc ZPSrcWPtr
tay
lda ZPSrcPtr+1
sbc ZPSrcWPtr+1
clc
rts
.6 inc ZPSrcWPtr
bne .7
inc ZPSrcWPtr+1
.7 inc Pak.Cnt
bne .1
inc Pak.Cnt+1
bne .1
2018-02-08 16:39:56 +00:00
.9 sec
rts
*--------------------------------------
Pak.BuildShortTable
ldy #0
2018-02-08 16:39:56 +00:00
.6 stz Pak.Cnt Init best score to 0
stz Pak.Cnt+1
ldx #0
2018-02-08 16:39:56 +00:00
.7 lda Pak.Cnt
cmp Pak.CntL,x is it better at X
lda Pak.Cnt+1
sbc Pak.CntH,x
2018-02-09 16:02:47 +00:00
bcs .8 not better or equal...
2018-02-08 16:39:56 +00:00
2019-09-25 15:34:43 +00:00
stx Pak.In.Byte save new score index...
lda Pak.CntL,x
2018-02-08 16:39:56 +00:00
sta Pak.Cnt ...and value
lda Pak.CntH,x
sta Pak.Cnt+1
.8 inx
bne .7
2019-09-25 15:34:43 +00:00
lda Pak.In.Byte
sta Pak.Shnk+S.PAKSHNK.SHORT3,y
2018-02-08 16:39:56 +00:00
tax
stz Pak.CntL,x Discard this entry
stz Pak.CntH,x
2018-02-08 16:39:56 +00:00
iny
cpy #24
bne .6
2018-02-05 16:25:25 +00:00
rts
2018-02-08 16:39:56 +00:00
*--------------------------------------
Pak.PutA bit Pak.RepCnt
bpl .1
2018-02-08 16:39:56 +00:00
stz Pak.RepCnt LastByte invalid...
sta Pak.LastByte
bra Pak.PutA.1 send first byte.
2018-02-08 16:39:56 +00:00
.1 cmp Pak.LastByte
beq .3
2018-02-08 16:39:56 +00:00
sta Pak.LastByte save new byte
lda Pak.RepCnt do we have a pending REP ?
beq .2
2018-02-08 16:39:56 +00:00
ora #PAK.B.REPn yes, send it
jsr Pak.PutA.1
bcs .9
ldx #S.PAKSTAT.REP
jsr Pak.UpdateStats
2018-02-08 16:39:56 +00:00
stz Pak.RepCnt
2018-02-08 16:39:56 +00:00
.2 lda Pak.LastByte
bra Pak.PutA.1
2018-02-08 16:39:56 +00:00
.3 inc Pak.RepCnt
lda Pak.RepCnt
cmp #REPMAX
bne .8
2018-02-05 16:25:25 +00:00
2018-02-08 16:39:56 +00:00
dec
ora #PAK.B.REPn
jsr Pak.PutA.1
2018-02-05 16:25:25 +00:00
bcs .9
2018-02-08 16:39:56 +00:00
lda #1
sta Pak.RepCnt
2018-02-08 16:39:56 +00:00
ldx #S.PAKSTAT.REP
jsr Pak.UpdateStats
2018-02-08 16:39:56 +00:00
.8 clc
.9 rts
2018-02-09 16:02:47 +00:00
Pak.PutA.1 bit Pak.bPass2
2018-02-09 07:04:19 +00:00
bmi .10
2018-02-09 16:02:47 +00:00
tax
inc Pak.CntL,x
bne .11
inc Pak.CntH,x
.11 inc Pak.Stat+S.PAKSTAT.PASS1
bne .12
inc Pak.Stat+S.PAKSTAT.PASS1+1
.12 clc
rts
* Pak.PutA.1 PASS #2
2018-02-09 07:04:19 +00:00
.10 ldy #7
2019-09-25 15:34:43 +00:00
.1 cmp Pak.Shnk+S.PAKSHNK.SHORT3,y
2018-02-09 07:04:19 +00:00
beq .3
dey
bpl .1
2018-02-09 07:04:19 +00:00
ldy #15
2019-09-25 15:34:43 +00:00
.2 cmp Pak.Shnk+S.PAKSHNK.SHORT4,y
2018-02-09 07:04:19 +00:00
beq .4
dey
bpl .2
2018-02-09 07:04:19 +00:00
bra .7
.3 tya
asl
asl
asl
asl
ora #PAK.B.SHORT3
ldy #4
ldx #S.PAKSTAT.S3
bra .8
.4 tya
asl
asl
ora #PAK.B.SHORT4
ldy #6
ldx #S.PAKSTAT.S4
bra .8
2018-02-09 16:02:47 +00:00
.7 ldx Pak.StoreCnt
sta Pak.StoreBuf,x
inx
cpx #STOREMAX
beq Pak.Flush
stx Pak.StoreCnt
clc
rts
2019-09-25 15:34:43 +00:00
.8 jsr Pak.UpdateStats
jmp Pak.Out.PutYBits
*--------------------------------------
2018-02-09 16:02:47 +00:00
Pak.Flush ldx Pak.StoreCnt
beq .8
lda #PAK.B.STOREn
ldy #4
2019-09-25 15:34:43 +00:00
jsr Pak.Out.PutYBits
2018-02-09 16:02:47 +00:00
bcs .9
ldx #0
.1 lda Pak.StoreBuf,x
sta Pak.LastByte update last byte for REP
ldy #8
2019-09-25 15:34:43 +00:00
jsr Pak.Out.PutYBits
2018-02-09 16:02:47 +00:00
bcs .9
2018-02-05 16:25:25 +00:00
inc Pak.Stat+S.PAKSTAT.STORE
bne .2
inc Pak.Stat+S.PAKSTAT.STORE+1
2018-02-05 16:25:25 +00:00
.2 inx
dec Pak.StoreCnt
bne .1
2018-02-09 16:02:47 +00:00
stz Pak.RepCnt Don't forget to set last byte valid
2018-02-05 16:25:25 +00:00
.8 clc
.9 rts
*--------------------------------------
2018-02-08 16:39:56 +00:00
Pak.UpdateStats inc Pak.Stat,x
bne .8
2018-02-08 16:39:56 +00:00
inc Pak.Stat+1,x
.8 rts
*--------------------------------------
2019-09-25 15:34:43 +00:00
* .INB USR/SRC/LIB/LIBPAK.S.IN
.INB USR/SRC/LIB/LIBPAK.S.OUT
*--------------------------------------
CS.END
2018-02-08 16:39:56 +00:00
Pak.SrcPtr .BS 2
2019-09-25 15:34:43 +00:00
Pak.SrcLen .BS 2
Pak.SrcCnt .BS 2
2018-02-05 16:25:25 +00:00
Pak.DstCnt .BS 2
2018-02-08 16:39:56 +00:00
Pak.RepCnt .BS 1
Pak.LastByte .BS 1
2018-02-09 16:02:47 +00:00
Pak.StoreCnt .BS 1
Pak.StoreBuf .BS STOREMAX
2018-02-08 16:39:56 +00:00
Pak.Cnt .BS 2
2018-02-09 16:02:47 +00:00
Pak.bPass2 .BS 1
2018-02-09 07:04:19 +00:00
Pak.MaxReadAhead .BS 1
Pak.BestBLLen .BS 1
Pak.BestBL .BS 2
2018-02-08 16:39:56 +00:00
Pak.WPtr .BS 2
Pak.WLimit .BS 2
Pak.WStrLen .BS 1
Pak.In.Byte .BS 1
Pak.In.Mask .BS 1
Pak.Out.Byte .BS 1
Pak.Out.Mask .BS 1
*--------------------------------------
2018-02-08 16:39:56 +00:00
Pak.CntL .BS 256
Pak.CntH .BS 256
2019-09-25 15:34:43 +00:00
Pak.Shnk .BS S.PAKSHNK
2018-02-08 16:39:56 +00:00
Pak.Stat .BS S.PAKSTAT
*--------------------------------------
2019-09-25 15:34:43 +00:00
Pak.In.BitMask .HS 8040201008040201
MAN
2018-11-17 17:17:13 +00:00
SAVE USR/SRC/LIB/LIBPAK.S
ASM