uxn-utils/cli/lz/ulzenc.tal

208 lines
4.5 KiB
Tal
Raw Permalink Normal View History

2024-06-05 14:32:20 -04:00
( uxncli ulzenc.rom example.txt a.ulz )
2023-11-18 14:16:27 -05:00
2023-11-19 14:02:58 -05:00
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
2023-11-18 14:16:27 -05:00
|a0 @File &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2
2024-06-05 14:32:20 -04:00
|000
2023-11-18 14:16:27 -05:00
2023-11-19 14:15:59 -05:00
@pad $2
2023-11-18 14:16:27 -05:00
@src $30
@dst $30
2023-11-18 15:19:30 -05:00
@match-len $2
2023-11-18 16:22:25 -05:00
@output-ptr $2
@dict-best $2
2023-11-18 17:09:09 -05:00
@combine $2
2023-11-19 14:02:58 -05:00
@dict-data $2
2023-11-18 16:22:25 -05:00
@dict-len $2
2023-11-18 14:16:27 -05:00
2024-06-05 14:32:20 -04:00
|100
2023-11-18 14:16:27 -05:00
@ready-src ( -> )
2023-11-19 14:02:58 -05:00
;meta #06 DEO2
2024-06-05 14:32:20 -04:00
.Console/type DEI #03 AND ?{ ;dict/usage <perr>/
2023-11-19 14:02:58 -05:00
#010f DEO }
2023-11-18 14:16:27 -05:00
;&await .Console/vector DEO2
BRK
&await ( -> )
2024-06-05 14:32:20 -04:00
.Console/read DEI .src skey ?ready-dst
BRK
2023-11-18 14:16:27 -05:00
@ready-dst ( -> )
;&await .Console/vector DEO2
BRK
&await ( -> )
2024-06-05 14:32:20 -04:00
.Console/read DEI .dst skey ?on-ready
BRK
2023-11-18 14:16:27 -05:00
2023-11-19 14:02:58 -05:00
@meta $1
( name ) "Ulzenc 0a
( desc ) "ULZ 20 "Encoder 0a
( auth ) "By 20 "Devine 20 "Lu 20 "Linvega 0a
2024-06-05 14:32:20 -04:00
( date ) "5 20 "Jun 20 "2024 $1
2023-11-19 14:02:58 -05:00
( exts ) 00
2023-11-18 14:16:27 -05:00
@on-ready ( -> )
( | load raw )
;src .File/name DEO2
#4000 .File/length DEO2
;raw .File/read DEO2
;raw .File/success DEI2 uxn_lz_compress
2023-11-18 22:54:27 -05:00
( | write )
2023-11-18 22:45:01 -05:00
;dst .File/name DEO2
.output-ptr LDZ2 ;compressed SUB2 .File/length DEO2
;compressed .File/write DEO2
2023-11-19 14:02:58 -05:00
( | summary )
;dict/decompressed <pstr>
2023-11-19 14:15:59 -05:00
;src <pstr>/
2023-11-19 14:02:58 -05:00
;dict/spacer <pstr>
2023-11-19 14:15:59 -05:00
;dst <pstr>/
2023-11-19 14:02:58 -05:00
[ LIT2 "( 18 ] DEO
.output-ptr LDZ2 ;compressed SUB2 <pdec>
;dict/bytes <pstr>
[ LIT2 ") 18 ] DEO
#0a18 DEO
2023-11-18 14:16:27 -05:00
( halt ) #800f DEO
BRK
2023-11-18 15:19:30 -05:00
@<append-byte> ( byte -- )
2023-11-18 22:58:57 -05:00
.output-ptr LDZ2 INC2k .output-ptr STZ2
STA
2023-11-18 15:19:30 -05:00
JMP2r
2023-11-19 14:30:17 -05:00
@<append-short> ( short* -- )
.output-ptr LDZ2 INC2k INC2 .output-ptr STZ2
STA2
JMP2r
2023-11-18 17:09:09 -05:00
@uxn_lz_compress ( input* length* -- )
2023-11-18 22:54:27 -05:00
( | fill variables )
2023-11-18 22:28:42 -05:00
;compressed .output-ptr STZ2
2023-11-18 17:09:09 -05:00
ADD2k NIP2 SWP2
2023-11-18 22:28:42 -05:00
&w ( end* start* -- )
2024-06-05 14:32:20 -04:00
EQU2k ?&end
( | get available dictionary size )
DUP2 ;raw SUB2 #0100 LTH2k ?{ SWP2 }
POP2 .dict-len STZ2
( | size of the string to search for )
SUB2k #3fff #0004 ADD2 LTH2k ?{ SWP2 }
POP2 ,&string-len STR2
( | itterate through the dictionary )
#0000 .match-len STZ2
DUP2 .dict-len LDZ2 SUB2 .dict-data STZ2
2023-11-18 22:58:57 -05:00
&for1 ( for ; dict_len; dict++, dict_len-- )
2024-06-05 14:32:20 -04:00
.dict-len LDZ2 #0000 EQU2 ?&end-for1
( Find common prefix length with the string ) #0000
2023-11-18 22:58:57 -05:00
&for2 ( for i = 0;; i++ )
2024-06-05 14:32:20 -04:00
( | If we reach the end of the string, it's the best possible match. )
DUP2 [ LIT2 &string-len $2 ] NEQ2 ?{
DUP2 .match-len STZ2
.dict-data LDZ2 .dict-best STZ2
POP2 !&done-search }
( | in[i] != dict[i % dict_len] break; )
( a ) ADD2k LDA STH
( b ) DUP2 .dict-len LDZ2 DIV2k MUL2 SUB2 .dict-data LDZ2 ADD2
( res ) LDA STHr NEQ ?&end-for2
INC2 ORAk ?&for2
&end-for2 ( i > match_len )
DUP2 .match-len LDZ2 LTH2 ?{
DUP2 .match-len STZ2
.dict-data LDZ2 .dict-best STZ2 }
POP2 .dict-data LDZ2 INC2 .dict-data STZ2
.dict-len LDZ2 #0001 SUB2 .dict-len STZ2
!&for1
2023-11-18 22:58:57 -05:00
&end-for1
2023-11-18 17:09:09 -05:00
&done-search ( -- )
2024-06-05 14:32:20 -04:00
( CPY ) .match-len LDZ2 #0003 GTH2 ?op-cpy
( LIT ) !op-lit
&end POP2 POP2 JMP2r
2023-11-18 17:09:09 -05:00
(
@|opcodes )
2023-11-18 16:14:23 -05:00
@op-cpy ( in* -- )
2023-11-18 22:54:27 -05:00
( | More numeric range: treat 0 as 4, 1 as 5, etc. )
2023-11-19 14:22:48 -05:00
.match-len LDZ2 #0004 SUB2
2023-11-18 22:54:27 -05:00
( | CPY2 )
2023-11-19 14:22:48 -05:00
DUP2 #003f GTH2 ?&cpy2
2024-06-05 14:32:20 -04:00
( *output_ptr++ = match_ctl
| 0x80; ) NIP #80 ORA <append-byte>
2023-11-18 22:58:57 -05:00
!&cpy-resume
2023-11-19 14:22:48 -05:00
&cpy2 ( match-ctl* -- )
2024-06-05 14:32:20 -04:00
SWP #c0 ORA <append-short>
2023-11-18 22:54:27 -05:00
&cpy-resume ( -- )
2024-06-05 14:32:20 -04:00
( | *output_ptr++ = in - dict_best - 1; )
DUP2 .dict-best LDZ2 SUB2 #0001 SUB2 NIP <append-byte>
( | in += match_len; Advance input by size of the match )
.match-len LDZ2 ADD2
( | Disable combining previous literal, if any )
#0000 .combine STZ2
!uxn_lz_compress/w
2023-11-18 16:14:23 -05:00
@op-lit ( in* -- )
.combine LDZ2 ORA ?&combine
2024-06-05 14:32:20 -04:00
( start a new literal )
( Store this address, and later use it to increment the literal size. )
2023-11-18 22:54:27 -05:00
( | combine = output_ptr++; )
2023-11-18 22:58:57 -05:00
.output-ptr LDZ2 INC2k .output-ptr STZ2
.combine STZ2
2023-11-18 22:54:27 -05:00
( | *combine = 0; )
2023-11-18 16:14:23 -05:00
#00 .combine LDZ2 LDA2 STA
2023-11-18 22:54:27 -05:00
( | *output_ptr++ = *in++; )
2023-11-18 16:14:23 -05:00
LDAk <append-byte>
INC2 !uxn_lz_compress/w
2023-11-18 22:58:57 -05:00
&combine ( -- )
2024-06-05 14:32:20 -04:00
( | if ++*combine == 127 )
.combine LDZ2 LDA INC DUP #7f NEQ ?{ POP #00 }
.combine LDZ2 STA
LDAk <append-byte>
INC2 !uxn_lz_compress/w
2023-11-18 16:14:23 -05:00
2023-11-18 14:16:27 -05:00
(
@|stdlib )
@skey ( key buf -- proc )
2024-06-05 14:32:20 -04:00
OVR #21 LTH ?{
#00 SWP sput #00 JMP2r }
POP2 #01 JMP2r
2023-11-18 14:16:27 -05:00
@scap ( str* -- end* )
2024-06-05 14:32:20 -04:00
INC2 & LDAk ?scap
2023-11-18 14:16:27 -05:00
JMP2r
@sput ( chr str* -- )
scap/ INC2k #00 ROT ROT STA
STA
JMP2r
2023-11-19 14:02:58 -05:00
@<pstr> ( str* -- )
2024-06-05 14:32:20 -04:00
LDAk #18 DEO
INC2 & LDAk ?<pstr>
2023-11-19 14:02:58 -05:00
POP2 JMP2r
@<perr> ( str* -- )
2024-06-05 14:32:20 -04:00
LDAk #19 DEO
INC2 & LDAk ?<perr>
2023-11-19 14:02:58 -05:00
POP2 JMP2r
@<pdec> ( short* -- )
#2710 [ LIT2r 00fb ]
2024-06-05 14:32:20 -04:00
&>w ( -- )
2023-11-19 14:02:58 -05:00
DIV2k #000a DIV2k MUL2 SUB2 SWPr EQUk OVR STHkr EQU AND ?{
DUP [ LIT "0 ] ADD #19 DEO
INCr }
2024-06-05 14:32:20 -04:00
POP2 #000a DIV2 SWPr INCr STHkr ?&>w
2023-11-19 14:02:58 -05:00
POP2r POP2 POP2 JMP2r
2023-11-18 14:16:27 -05:00
(
@|memory )
2023-11-19 14:02:58 -05:00
@dict &usage "usage: 20 "ulzenc.rom 20 "a.ulz 20 "b.bin 0a $1
&decompressed "Compressed 20 $1
&spacer 20 "-> 20 $1
&bytes 20 "bytes $1
2023-11-19 14:15:59 -05:00
@raw $8000
@compressed
2023-11-18 14:16:27 -05:00