(ulz) Housekeeping

This commit is contained in:
Devine Lu Linvega 2024-06-05 10:32:20 -08:00
parent ec6fbe4263
commit 6177f80024
4 changed files with 101 additions and 108 deletions

View File

@ -10,8 +10,8 @@ if [[ "$*" == *"--lint"* ]]
then then
clang-format -i ulzdec.c clang-format -i ulzdec.c
clang-format -i ulzenc.c clang-format -i ulzenc.c
$LIN decoder.tal $LIN ulzdec.tal
$LIN encoder.tal $LIN ulzenc.tal
fi fi
# Building # Building

View File

@ -2,38 +2,36 @@
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1 |10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
|a0 @File &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2 |a0 @File &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2
|000
|0000
@src $30 @src $30
@dst $30 @dst $30
@ptr $1 @ptr $1
|0100 |100
@on-reset ( -> ) @on-reset ( -> )
;meta #06 DEO2 ;meta #06 DEO2
.Console/type DEI #03 AND ?{ .Console/type DEI #03 AND ?{ ;dict/usage <perr>/
;dict/usage <perr>/
#010f DEO } #010f DEO }
;&await .Console/vector DEO2 ;&await .Console/vector DEO2
BRK BRK
&await ( -> ) &await ( -> )
.Console/read DEI .src skey ?ready-dst .Console/read DEI .src skey ?ready-dst
BRK BRK
@ready-dst ( -> ) @ready-dst ( -> )
;&await .Console/vector DEO2 ;&await .Console/vector DEO2
BRK BRK
&await ( -> ) &await ( -> )
.Console/read DEI .dst skey ?on-ready .Console/read DEI .dst skey ?on-ready
BRK BRK
@meta $1 @meta $1
( name ) "Ulzdec 0a ( name ) "Ulzdec 0a
( desc ) "ULZ 20 "Decoder 0a ( desc ) "ULZ 20 "Decoder 0a
( auth ) "By 20 "Devine 20 "Lu 20 "Linvega 0a ( auth ) "By 20 "Devine 20 "Lu 20 "Linvega 0a
( date ) "19 20 "Nov 20 "2023 $1 ( date ) "5 20 "Jun 20 "2024 $1
( exts ) 00 ( exts ) 00
@on-ready ( -> ) @on-ready ( -> )
@ -58,12 +56,12 @@
@<decode_ulz> ( str* -- ) @<decode_ulz> ( str* -- )
;mem .ptr STZ2 ;mem .ptr STZ2
.File/name DEO2 .File/name DEO2
&stream ( -- ) &>stream ( -- )
#0001 .File/length DEO2 #0001 .File/length DEO2
;&b DUP2 .File/read DEO2 ;&b DUP2 .File/read DEO2
.File/success DEI2 ORA ?{ POP2 JMP2r } .File/success DEI2 ORA ?{ POP2 JMP2r }
[ LIT &b $1 ] <decode_ulz_byte> [ LIT &b $1 ] <decode_ulz_byte>
!&stream !&>stream
@<decode_ulz_byte> ( byte -- ) @<decode_ulz_byte> ( byte -- )
DUP #80 AND ?op-cpy DUP #80 AND ?op-cpy
@ -78,24 +76,24 @@
#7f AND DUP #40 AND ?{ #00 SWP !&copy } #7f AND DUP #40 AND ?{ #00 SWP !&copy }
#3f AND getc #3f AND getc
&copy ( length* -- ) &copy ( length* -- )
.ptr LDZ2 #00 getc INC2 SUB2 ,&offset STR2 .ptr LDZ2 #00 getc INC2 SUB2 ,&offset STR2
#0004 ADD2 #0000 #0004 ADD2 #0000
&l ( length* i* -- ) &l ( length* i* -- )
( get ) DUP2 [ LIT2 &offset $2 ] ADD2 LDA ( get ) DUP2 [ LIT2 &offset $2 ] ADD2 LDA
( put ) .ptr LDZ2 STAk INC2 .ptr STZ2 ( put ) .ptr LDZ2 STAk INC2 .ptr STZ2
POP INC2 GTH2k ?&l POP INC2 GTH2k ?&l
POP2 POP2 JMP2r POP2 POP2 JMP2r
( (
@|stdlib ) @|stdlib )
@skey ( key buf -- proc ) @skey ( key buf -- proc )
OVR #21 LTH ?&eval OVR #21 LTH ?{
#00 SWP sput #00 JMP2r #00 SWP sput #00 JMP2r }
&eval POP2 #01 JMP2r POP2 #01 JMP2r
@scap ( str* -- end* ) @scap ( str* -- end* )
&w INC2 & LDAk ?&w INC2 & LDAk ?scap
JMP2r JMP2r
@sput ( chr str* -- ) @sput ( chr str* -- )
@ -109,26 +107,25 @@
[ LIT &b $1 ] JMP2r [ LIT &b $1 ] JMP2r
@<pstr> ( str* -- ) @<pstr> ( str* -- )
&w LDAk #18 DEO LDAk #18 DEO
INC2 & LDAk ?&w INC2 & LDAk ?<pstr>
POP2 JMP2r POP2 JMP2r
@<perr> ( str* -- ) @<perr> ( str* -- )
&w LDAk #19 DEO LDAk #19 DEO
INC2 & LDAk ?&w INC2 & LDAk ?<perr>
POP2 JMP2r POP2 JMP2r
@<pdec> ( short* -- ) @<pdec> ( short* -- )
#2710 [ LIT2r 00fb ] #2710 [ LIT2r 00fb ]
&w ( -- ) &>w ( -- )
DIV2k #000a DIV2k MUL2 SUB2 SWPr EQUk OVR STHkr EQU AND ?{ DIV2k #000a DIV2k MUL2 SUB2 SWPr EQUk OVR STHkr EQU AND ?{
DUP [ LIT "0 ] ADD #19 DEO DUP [ LIT "0 ] ADD #19 DEO
INCr } INCr }
POP2 #000a DIV2 SWPr INCr STHkr ?&w POP2 #000a DIV2 SWPr INCr STHkr ?&>w
POP2r POP2 POP2 JMP2r POP2r POP2 POP2 JMP2r
( @
( | mem )
@| mem )
@dict &usage "usage: 20 "ulzdec.rom 20 "a.bin 20 "b.ulz 0a $1 @dict &usage "usage: 20 "ulzdec.rom 20 "a.bin 20 "b.ulz 0a $1
&decompressed "Decompressed 20 $1 &decompressed "Decompressed 20 $1
@ -137,4 +134,3 @@
@mem @mem

View File

@ -60,7 +60,7 @@ uxn_lz_compress(const void *input, int input_size)
/* CPY2 */ /* CPY2 */
if(match_ctl > 0x3F) { if(match_ctl > 0x3F) {
*output_ptr++ = match_ctl >> 8 | 0xc0; /* High byte of the length, with both flags set */ *output_ptr++ = match_ctl >> 8 | 0xc0; /* High byte of the length, with both flags set */
*output_ptr++ = match_ctl; /* Low byte of the length */ *output_ptr++ = match_ctl; /* Low byte of the length */
} else } else
*output_ptr++ = match_ctl | 0x80; *output_ptr++ = match_ctl | 0x80;
/* offset(0 is -1, 1 is -2, ...) */ /* offset(0 is -1, 1 is -2, ...) */
@ -69,7 +69,7 @@ uxn_lz_compress(const void *input, int input_size)
combine = 0; /* Disable combining previous literal, if any */ combine = 0; /* Disable combining previous literal, if any */
} }
/* LIT */ /* LIT */
else{ else {
/* Combine with previous literal */ /* Combine with previous literal */
if(combine) { if(combine) {
if(++*combine == 127) if(++*combine == 127)
@ -82,7 +82,7 @@ uxn_lz_compress(const void *input, int input_size)
/* The 0 here means literal of length 1. */ /* The 0 here means literal of length 1. */
*combine = 0; *combine = 0;
} }
/* Write 1 literal byte from the input to the output. */ /* Write 1 literal byte from the input to the output. */
*output_ptr++ = *in++; *output_ptr++ = *in++;
} }

View File

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