From 6177f800244aa484072fdf9cb7a74b21c06eff4c Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 5 Jun 2024 10:32:20 -0800 Subject: [PATCH] (ulz) Housekeeping --- cli/lz/build.sh | 4 +- cli/lz/ulzdec.tal | 58 +++++++++---------- cli/lz/ulzenc.c | 6 +- cli/lz/ulzenc.tal | 141 +++++++++++++++++++++++----------------------- 4 files changed, 101 insertions(+), 108 deletions(-) diff --git a/cli/lz/build.sh b/cli/lz/build.sh index 6a80d55..391dcae 100755 --- a/cli/lz/build.sh +++ b/cli/lz/build.sh @@ -10,8 +10,8 @@ if [[ "$*" == *"--lint"* ]] then clang-format -i ulzdec.c clang-format -i ulzenc.c - $LIN decoder.tal - $LIN encoder.tal + $LIN ulzdec.tal + $LIN ulzenc.tal fi # Building diff --git a/cli/lz/ulzdec.tal b/cli/lz/ulzdec.tal index d284de1..03e5679 100644 --- a/cli/lz/ulzdec.tal +++ b/cli/lz/ulzdec.tal @@ -2,38 +2,36 @@ |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 - -|0000 +|000 @src $30 @dst $30 @ptr $1 -|0100 +|100 @on-reset ( -> ) ;meta #06 DEO2 - .Console/type DEI #03 AND ?{ - ;dict/usage / + .Console/type DEI #03 AND ?{ ;dict/usage / #010f DEO } ;&await .Console/vector DEO2 BRK &await ( -> ) - .Console/read DEI .src skey ?ready-dst - BRK + .Console/read DEI .src skey ?ready-dst + BRK @ready-dst ( -> ) ;&await .Console/vector DEO2 BRK &await ( -> ) - .Console/read DEI .dst skey ?on-ready - BRK + .Console/read DEI .dst skey ?on-ready + BRK @meta $1 ( name ) "Ulzdec 0a ( desc ) "ULZ 20 "Decoder 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 @on-ready ( -> ) @@ -58,12 +56,12 @@ @ ( str* -- ) ;mem .ptr STZ2 .File/name DEO2 - &stream ( -- ) + &>stream ( -- ) #0001 .File/length DEO2 ;&b DUP2 .File/read DEO2 .File/success DEI2 ORA ?{ POP2 JMP2r } [ LIT &b $1 ] - !&stream + !&>stream @ ( byte -- ) DUP #80 AND ?op-cpy @@ -78,24 +76,24 @@ #7f AND DUP #40 AND ?{ #00 SWP !© } #3f AND getc © ( length* -- ) - .ptr LDZ2 #00 getc INC2 SUB2 ,&offset STR2 - #0004 ADD2 #0000 + .ptr LDZ2 #00 getc INC2 SUB2 ,&offset STR2 + #0004 ADD2 #0000 &l ( length* i* -- ) - ( get ) DUP2 [ LIT2 &offset $2 ] ADD2 LDA - ( put ) .ptr LDZ2 STAk INC2 .ptr STZ2 - POP INC2 GTH2k ?&l + ( get ) DUP2 [ LIT2 &offset $2 ] ADD2 LDA + ( put ) .ptr LDZ2 STAk INC2 .ptr STZ2 + POP INC2 GTH2k ?&l POP2 POP2 JMP2r ( @|stdlib ) @skey ( key buf -- proc ) - OVR #21 LTH ?&eval - #00 SWP sput #00 JMP2r - &eval POP2 #01 JMP2r + OVR #21 LTH ?{ + #00 SWP sput #00 JMP2r } + POP2 #01 JMP2r @scap ( str* -- end* ) - &w INC2 & LDAk ?&w + INC2 & LDAk ?scap JMP2r @sput ( chr str* -- ) @@ -109,26 +107,25 @@ [ LIT &b $1 ] JMP2r @ ( str* -- ) - &w LDAk #18 DEO - INC2 & LDAk ?&w + LDAk #18 DEO + INC2 & LDAk ? POP2 JMP2r @ ( str* -- ) - &w LDAk #19 DEO - INC2 & LDAk ?&w + LDAk #19 DEO + INC2 & LDAk ? POP2 JMP2r @ ( short* -- ) #2710 [ LIT2r 00fb ] - &w ( -- ) + &>w ( -- ) DIV2k #000a DIV2k MUL2 SUB2 SWPr EQUk OVR STHkr EQU AND ?{ DUP [ LIT "0 ] ADD #19 DEO INCr } - POP2 #000a DIV2 SWPr INCr STHkr ?&w + POP2 #000a DIV2 SWPr INCr STHkr ?&>w POP2r POP2 POP2 JMP2r - -( -@| mem ) + ( @ + | mem ) @dict &usage "usage: 20 "ulzdec.rom 20 "a.bin 20 "b.ulz 0a $1 &decompressed "Decompressed 20 $1 @@ -137,4 +134,3 @@ @mem - diff --git a/cli/lz/ulzenc.c b/cli/lz/ulzenc.c index 0568d40..4a1f8c5 100644 --- a/cli/lz/ulzenc.c +++ b/cli/lz/ulzenc.c @@ -60,7 +60,7 @@ uxn_lz_compress(const void *input, int input_size) /* CPY2 */ if(match_ctl > 0x3F) { *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 *output_ptr++ = match_ctl | 0x80; /* 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 */ } /* LIT */ - else{ + else { /* Combine with previous literal */ if(combine) { if(++*combine == 127) @@ -82,7 +82,7 @@ uxn_lz_compress(const void *input, int input_size) /* The 0 here means literal of length 1. */ *combine = 0; } - + /* Write 1 literal byte from the input to the output. */ *output_ptr++ = *in++; } diff --git a/cli/lz/ulzenc.tal b/cli/lz/ulzenc.tal index c41f811..753670a 100644 --- a/cli/lz/ulzenc.tal +++ b/cli/lz/ulzenc.tal @@ -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 |a0 @File &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2 - -|0000 +|000 @pad $2 @src $30 @@ -15,31 +14,30 @@ @dict-data $2 @dict-len $2 -|0100 +|100 @ready-src ( -> ) ;meta #06 DEO2 - .Console/type DEI #03 AND ?{ - ;dict/usage / + .Console/type DEI #03 AND ?{ ;dict/usage / #010f DEO } ;&await .Console/vector DEO2 BRK &await ( -> ) - .Console/read DEI .src skey ?ready-dst - BRK + .Console/read DEI .src skey ?ready-dst + BRK @ready-dst ( -> ) ;&await .Console/vector DEO2 BRK &await ( -> ) - .Console/read DEI .dst skey ?on-ready - BRK + .Console/read DEI .dst skey ?on-ready + BRK @meta $1 ( name ) "Ulzenc 0a ( desc ) "ULZ 20 "Encoder 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 @on-ready ( -> ) @@ -80,43 +78,43 @@ ;compressed .output-ptr STZ2 ADD2k NIP2 SWP2 &w ( end* start* -- ) - 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 + 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 &for1 ( for ; dict_len; dict++, dict_len-- ) - .dict-len LDZ2 #0000 EQU2 ?&end-for1 - ( Find common prefix length with the string ) #0000 + .dict-len LDZ2 #0000 EQU2 ?&end-for1 + ( Find common prefix length with the string ) #0000 &for2 ( for i = 0;; i++ ) - ( | 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 + ( | 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 &end-for1 &done-search ( -- ) - ( CPY ) .match-len LDZ2 #0003 GTH2 ?op-cpy - ( LIT ) !op-lit - &end POP2 POP2 JMP2r + ( CPY ) .match-len LDZ2 #0003 GTH2 ?op-cpy + ( LIT ) !op-lit + &end POP2 POP2 JMP2r ( @|opcodes ) @@ -126,24 +124,24 @@ .match-len LDZ2 #0004 SUB2 ( | CPY2 ) DUP2 #003f GTH2 ?&cpy2 - ( | *output_ptr++ = match_ctl | 0x80; ) - NIP #80 ORA + ( *output_ptr++ = match_ctl + | 0x80; ) NIP #80 ORA !&cpy-resume &cpy2 ( match-ctl* -- ) - SWP #c0 ORA + SWP #c0 ORA &cpy-resume ( -- ) - ( | *output_ptr++ = in - dict_best - 1; ) - DUP2 .dict-best LDZ2 SUB2 #0001 SUB2 NIP - ( | 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 + ( | *output_ptr++ = in - dict_best - 1; ) + DUP2 .dict-best LDZ2 SUB2 #0001 SUB2 NIP + ( | 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 @op-lit ( in* -- ) .combine LDZ2 ORA ?&combine - ( | start a new literal ) - ( | Store this address, and later use it to increment the literal size. ) + ( start a new literal ) + ( Store this address, and later use it to increment the literal size. ) ( | combine = output_ptr++; ) .output-ptr LDZ2 INC2k .output-ptr STZ2 .combine STZ2 @@ -153,22 +151,22 @@ LDAk INC2 !uxn_lz_compress/w &combine ( -- ) - ( | if ++*combine == 127 ) - .combine LDZ2 LDA INC DUP #7f NEQ ?{ POP #00 } - .combine LDZ2 STA - LDAk - INC2 !uxn_lz_compress/w + ( | if ++*combine == 127 ) + .combine LDZ2 LDA INC DUP #7f NEQ ?{ POP #00 } + .combine LDZ2 STA + LDAk + INC2 !uxn_lz_compress/w ( @|stdlib ) @skey ( key buf -- proc ) - OVR #21 LTH ?&eval - #00 SWP sput #00 JMP2r - &eval POP2 #01 JMP2r + OVR #21 LTH ?{ + #00 SWP sput #00 JMP2r } + POP2 #01 JMP2r @scap ( str* -- end* ) - &w INC2 & LDAk ?&w + INC2 & LDAk ?scap JMP2r @sput ( chr str* -- ) @@ -177,22 +175,22 @@ JMP2r @ ( str* -- ) - &w LDAk #18 DEO - INC2 & LDAk ?&w + LDAk #18 DEO + INC2 & LDAk ? POP2 JMP2r @ ( str* -- ) - &w LDAk #19 DEO - INC2 & LDAk ?&w + LDAk #19 DEO + INC2 & LDAk ? POP2 JMP2r @ ( short* -- ) #2710 [ LIT2r 00fb ] - &w ( -- ) + &>w ( -- ) DIV2k #000a DIV2k MUL2 SUB2 SWPr EQUk OVR STHkr EQU AND ?{ DUP [ LIT "0 ] ADD #19 DEO INCr } - POP2 #000a DIV2 SWPr INCr STHkr ?&w + POP2 #000a DIV2 SWPr INCr STHkr ?&>w POP2r POP2 POP2 JMP2r ( @@ -207,4 +205,3 @@ @compressed -