Cleaned up progress on encoder

This commit is contained in:
neauoire 2023-11-16 11:02:10 -08:00
parent 082b7386a5
commit 1859cd66a5
3 changed files with 39 additions and 12 deletions

View File

@ -12,6 +12,7 @@ then
$LIN decoder.tal $LIN decoder.tal
clang-format -i lz_main.c clang-format -i lz_main.c
clang-format -i ulzdec.c clang-format -i ulzdec.c
clang-format -i ulzenc.c
fi fi
# Building # Building
@ -30,8 +31,7 @@ $ASM ulzdec.tal ulzdec.rom
echo "" echo ""
echo "C Decoder" echo "C Decoder"
echo "" echo ""
./ulzdec a.ulz b.bin ./ulzdec a.ulz b.bin && cat b.bin
cat b.bin
# Uxn Decoding # Uxn Decoding
@ -47,6 +47,10 @@ echo "C Encoder"
echo "" echo ""
./ulzenc example.txt a.ulz ./ulzenc example.txt a.ulz
# Uxn Decoding
uxncli ulzdec.rom a.ulz b.bin && cat b.bin
# ./ulzdec a.ulz b.bin && cat b.bin
rm ./main rm ./main
rm ./ulzdec rm ./ulzdec
rm ./ulzenc rm ./ulzenc

View File

@ -38,16 +38,15 @@
@op-lit ( byte -- ) @op-lit ( byte -- )
#00 SWP INC2 #00 SWP INC2
DUP2 .File/length DEO2 DUP2 .File/length DEO2
.ptr LDZ2 .ptr LDZ2
DUP2 .File/read DEO2 DUP2 .File/read DEO2
ADD2 .ptr STZ2 ADD2 .ptr STZ2
JMP2r JMP2r
@op-cpy ( byte -- ) @op-cpy ( byte -- )
#7f AND #7f AND
DUP #40 AND ?&long DUP #40 AND ?{
#00 SWP !&copy #00 SWP !&copy }
&long ( byte -- )
#3f AND getc #3f AND getc
&copy ( length* -- ) &copy ( length* -- )
.ptr LDZ2 #00 getc INC2 SUB2 STH2 .ptr LDZ2 #00 getc INC2 SUB2 STH2

View File

@ -10,13 +10,37 @@ error(const char *name, const char *msg)
return 0; return 0;
} }
char *mem, *ptr; char *ptr, *a, mem[0x10000];
int
get_lit(int i)
{
return 0;
}
int int
encode_ulz(FILE *src) encode_ulz(FILE *src)
{ {
int i, j, ptr = 0, length = 0;
return 0; a = malloc(0x10000);
/* load */
while(fread(a + length, 1, 1, src) && ++length) {}
/* encode */
for(i = 0; i < length; i++) {
/* try to make a CPY */
/* try to make a LIT */
for(j = i; j - i < 127 && j < length; j++) {}
if(i != j) {
int litlen = j - i;
/* LIT */
mem[ptr++] = litlen;
/* LIT(body) */
for(j = i; j - i < litlen + 1; j++) mem[ptr++] = a[j];
}
i += j - 1;
}
return ptr - 1;
} }
int int
@ -33,7 +57,7 @@ main(int argc, char *argv[])
res = encode_ulz(src); res = encode_ulz(src);
if(!(dst = fopen(argv[2], "wb"))) if(!(dst = fopen(argv[2], "wb")))
return !error("Invalid output file", argv[1]); return !error("Invalid output file", argv[1]);
fwrite(mem, res, 1, dst); fwrite(&mem, res, 1, dst);
printf("Compressed %s -> %s(%d bytes).\n", argv[1], argv[2], res); printf("Compressed %s -> %s(%d bytes).\n", argv[1], argv[2], res);
return 0; return 0;
} }