From 140529c35eca416d9496616379dc321a96b7ca3c Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Mon, 6 Feb 2023 15:54:12 -0800 Subject: [PATCH] Added checksum --- checksum/checksum-original.tal | 50 ++++++++++++++ checksum/checksum.c | 38 +++++++++++ checksum/checksum.tal | 64 ++++++++++++++++++ checksum/test.txt | 1 + lz/a-1.bin | Bin 0 -> 13 bytes lz/a-2.bin | 1 + lz/a.bin | Bin 0 -> 344 bytes lz/lz.tal | 102 ++++++++++++++++++++++++++++ lz/lz2.tal | 118 +++++++++++++++++++++++++++++++++ lz/target.txt | 1 + 10 files changed, 375 insertions(+) create mode 100644 checksum/checksum-original.tal create mode 100644 checksum/checksum.c create mode 100644 checksum/checksum.tal create mode 100644 checksum/test.txt create mode 100644 lz/a-1.bin create mode 100644 lz/a-2.bin create mode 100644 lz/a.bin create mode 100644 lz/lz.tal create mode 100644 lz/lz2.tal create mode 100644 lz/target.txt diff --git a/checksum/checksum-original.tal b/checksum/checksum-original.tal new file mode 100644 index 0000000..2f07a84 --- /dev/null +++ b/checksum/checksum-original.tal @@ -0,0 +1,50 @@ +|0100 + ;data1a hash emit/long #0a18 DEO + ;data1b hash emit/long #0a18 DEOk DEO + + ;data2a hash emit/long #0a18 DEO + ;data2b hash emit/long #0a18 DEOk DEO + + ;data3a hash emit/long #0a18 DEO + ;data3b hash emit/long #0a18 DEOk DEO + + ;data4a hash emit/long #0a18 DEO + ;data4b hash emit/long #0a18 DEOk DEO + + #010f DEO BRK + +( hash function: run two 16-bit LCGs in parallel ) +( ) +( upper fn: x1=x0*9283+cc ; a is #2443 x0 is #1234 ) +( lower fn: y1=y0*4493+cc ; b is #118d y1 is #abcd ) +@hash ( s* -> x* y* ) + STH2 LIT2 2443 LIT2 1234 ( a* x0* [s*] ) + STH2r LIT2r 118d LIT2r abcd ( a* x0* s* [b* y0*] ) + &loop ( a* x* s* [b* y*] ) + LDAk DUP ?&ok ( a* x* s* c^ [b* y*] ) + POP POP2 NIP2 ( x* [b* y*] ) + STH2r POP2r JMP2r ( x* y* ) + &ok ( a* x* s* c^ [b* y*] ) + DUP STH2 STH2 ( a* x* [b* y* cc* s*] ) + OVR2 MUL2 OVR2r ( a* ax* [b* y* cc* s* cc*] ) + STH2r ADD2 STH2r STH2r ( a* ax+cc* s* cc* [b* y*] ) + OVR2r MUL2r STH2 ADD2r ( a* ax+cc* s* [b* by+cc*] ) + INC2 !&loop ( a* ax+cc s+1* [b* by+cc*] ) + +@emit ( n* -> ) + &long SWP2 emit/short + &short SWP emit/byte + &byte DUP #04 SFT emit/char + &char #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO JMP2r + +@data1a "abc 00 +@data1b "ebc 00 + +@data2a "wigjewigjewigjewigjewigjewigjcncmnbegejgiwejgieg 00 +@data2b "wigjewigjewigjewigjewigjewigjcmcmnbegejgiwejgieg 00 + +@data3a "zzjiewgew 00 +@data3b "zzjiewge 00 + +@data4a "this 20 "is 20 "a 20 "test 00 +@data4b "this 20 "is 20 "a 20 "test. 00 diff --git a/checksum/checksum.c b/checksum/checksum.c new file mode 100644 index 0000000..6ac1042 --- /dev/null +++ b/checksum/checksum.c @@ -0,0 +1,38 @@ +// verified this matches hash.tal for tests + +#include +#include + +uint32_t hash(uint8_t *input) { + uint16_t x = 0x1234; + uint16_t y = 0xabcd; + uint16_t a = 0x2443; + uint16_t b = 0x118d; + uint8_t c; + uint16_t cc; + + while (c = *input) { + cc = (c << 8) | c; + x = (x * a) + cc; + y = (y * b) + cc; + input++; + } + + return (x << 16) | y; +} + +int main(int argc, char **argv) { + printf("data1a %08x\n", hash("abc")); + printf("data1b %08x\n", hash("ebc")); + printf("\n"); + printf("data2a %08x\n", hash("wigjewigjewigjewigjewigjewigjcncmnbegejgiwejgieg")); + printf("data2b %08x\n", hash("wigjewigjewigjewigjewigjewigjcmcmnbegejgiwejgieg")); + printf("\n"); + printf("data3a %08x\n", hash("zzjiewgew")); + printf("data3b %08x\n", hash("zzjiewge")); + printf("\n"); + printf("data4a %08x\n", hash("this is a test")); + printf("data4b %08x\n", hash("this is a test.")); + return 0; +} + diff --git a/checksum/checksum.tal b/checksum/checksum.tal new file mode 100644 index 0000000..67826d2 --- /dev/null +++ b/checksum/checksum.tal @@ -0,0 +1,64 @@ +( usage: uxncli checksum.rom file.bin ) + +|10 @Console &vector $2 &read $1 &pad $5 &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 + + @src $40 + +|0100 ( -> ) + + ;await-src .Console/vector DEO2 + ;dict/input pstr #0a18 DEO + +BRK + +@await-src ( -> ) + + .Console/read DEI .src skey + ?on-ready + +BRK + +@on-ready ( -> ) + + ;src hash plong #0a18 DEO + #010f DEO + +BRK + +@hash ( filename* -- x* y* ) + + .File/name DEO2 + #0001 .File/length DEO2 + #1234 ,&x STR2 #abcd ,&y STR2 + #2443 ,&a STR2 #118d ,&b STR2 + &w + ;&c .File/read DEO2 + .File/success DEI2 #0000 EQU2 ?&end + [ LIT &c $1 ] DUP DUP2 + [ LIT2 &x $2 ] [ LIT2 &a $2 ] MUL2 ADD2 ,&x STR2 + [ LIT2 &y $2 ] [ LIT2 &b $2 ] MUL2 ADD2 ,&y STR2 + !&w + &end + ,&x LDR2 ,&y LDR2 + +JMP2r + +@scap ( str* -- end* ) + LDAk #00 NEQ [ JMP JMP2r ] &w INC2 LDAk ?&w JMP2r +@sput ( chr str* -- ) + scap STA JMP2r +@skey ( key buf -- proc ) + OVR #21 LTH ?&eval #00 SWP sput #00 JMP2r &eval POP2 #01 JMP2r +@pstr ( str* -- ) + &w LDAk #18 DEO INC2 LDAk ?&w POP2 JMP2r +@plong ( a* b* -> ) + SWP2 plong/s + &s SWP plong/b + &b DUP #04 SFT plong/c + &c #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO JMP2r +@dict + &input "Input(.tal): 20 $1 + diff --git a/checksum/test.txt b/checksum/test.txt new file mode 100644 index 0000000..651f559 --- /dev/null +++ b/checksum/test.txt @@ -0,0 +1 @@ +wigjewigjewigjewigjewigjewigjcncmnbegejgiwejgieg diff --git a/lz/a-1.bin b/lz/a-1.bin new file mode 100644 index 0000000000000000000000000000000000000000..0d194d8b7aedc4d7553f9e5f136070ee415c9bca GIT binary patch literal 13 UcmZQbWJ^p+N={BmIq;ty02}uN;Q#;t literal 0 HcmV?d00001 diff --git a/lz/a-2.bin b/lz/a-2.bin new file mode 100644 index 0000000..eb12f2d --- /dev/null +++ b/lz/a-2.bin @@ -0,0 +1 @@ +aabbccddŒ \ No newline at end of file diff --git a/lz/a.bin b/lz/a.bin new file mode 100644 index 0000000000000000000000000000000000000000..cd4809dc017e722c9031a868cd92da83162554bb GIT binary patch literal 344 zcmXv~yH3L}6oo=7;mV*2v0t$Sr2Irlmm)?MR53X6ZEQ$wu&*0sK}lM*6A}v&6JNjw zAg2Bo3tkwH&e3_{Y--5ad(Q4`3Z?dh2EtPc3LPdMNFjK#uI6l<+dv?qh^Ry;LLFH_ z5iik$Hj&E9fKXXi$|yGyL|K9qHHg*l(C9)@%NZ`w)ugm3pb>l6jQbuQf~R&e;_OC0 zA}GB`qo3Cj+pHHun_-W$t4499Jt!M|4AG?4FRRafrH!=JYSVoTGV7K|)Ln0zeeJvN z-rTz8x%(MJLyzTr^L3J28QVA;Ol+8BqN$GKK%q@SP*?(E+w&pW|06DL*K9A{oNd|Y mk~7X`nX)eU_|`O8w7)Ek>mU~(5!XitnzPC4=`K%pZ|{GBP;}$~ literal 0 HcmV?d00001 diff --git a/lz/lz.tal b/lz/lz.tal new file mode 100644 index 0000000..5fe51f6 --- /dev/null +++ b/lz/lz.tal @@ -0,0 +1,102 @@ +( decompressor ) + +|10 @Console &vector $2 &read $1 &pad $5 &write $1 &err $1 +|a0 @File1 &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2 +|b0 @File2 &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2 + +|0000 + + @input-size $2 + + @src $30 + @ptr $1 + +|0100 + +( +@|vectors ) + +@ready-src ( -> ) ;&await .Console/vector DEO2 BRK +&await ( -> ) .Console/read DEI .src skey ?on-ready BRK + +@on-ready ( -> ) + + ;src decompress + ;mem pstr #0a18 DEO + ( halt ) #010f DEO + +BRK + +@decompress ( str* -- ) + + ;mem .ptr STZ2 + .File1/name DEO2 + #0001 .input-size STZ2 + &stream + .input-size LDZ2 .File1/length DEO2 + ;&b + DUP2 .File1/read DEO2 + LDA decompress/run + .File1/success DEI2 ORA ?&stream + +JMP2r + &b $1 + +&run ( byte -- ) + + DUP #80 AND ?&dict + ( literal ) + #00 SWP INC2 + DUP2 .File1/length DEO2 + .ptr LDZ2 + DUP2 .File1/read DEO2 + ADD2 .ptr STZ2 + +JMP2r + +&dict ( byte -- ) + + #7f AND + DUP #40 AND ?&dict3 + ( byte ) + #00 SWP !&opc + +&dict3 ( byte -- ) + + ( hb ) + #3f AND + ( lb ) + #0001 .File1/length DEO2 + ;&b + DUP2 .File1/read DEO2 + LDA + +&opc #0004 ADD2 + + ;&b + DUP2 .File1/read DEO2 + LDA INC + +© ( length* offset -- ) + + #00 SWP #0000 SWP2 SUB2 .ptr LDZ2 ADD2 STH2 + #0000 + &l + DUP2 STH2kr ADD2 LDA .ptr LDZ2 STH2k STA + STH2r INC2 .ptr STZ2 + INC2 GTH2k ?&l + POP2 POP2 + POP2r + +JMP2r + +( +@|stdlib ) + +@pstr ( str* -- ) LDAk ?&w POP2 JMP2r &w LDAk #18 DEO INC2 LDAk ?&w POP2 JMP2r +@skey ( key buf -- proc ) OVR #21 LTH ?&eval #00 SWP sput #00 JMP2r &eval POP2 #01 JMP2r +@scap ( str* -- end* ) LDAk ?&w JMP2r &w INC2 LDAk ?&w JMP2r +@sput ( chr str* -- ) scap INC2k #00 ROT ROT STA STA JMP2r + +@mem + diff --git a/lz/lz2.tal b/lz/lz2.tal new file mode 100644 index 0000000..6efdabf --- /dev/null +++ b/lz/lz2.tal @@ -0,0 +1,118 @@ +( decompressor ) + +|10 @Console &vector $2 &read $1 &pad $5 &write $1 &err $1 +|a0 @File1 &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2 +|b0 @File2 &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2 + +|0000 + + @input-size $2 + + @src $30 + @dst $30 + @ptr $1 + +|0100 + +( +@|vectors ) + +@ready-src ( -> ) ;&await .Console/vector DEO2 BRK + &await ( -> ) .Console/read DEI .src skey ?ready-dst BRK +@ready-dst ( -> ) ;&await .Console/vector DEO2 BRK + &await ( -> ) .Console/read DEI .dst skey ?on-ready BRK + +@on-ready ( -> ) + + ;src decompress + ;mem pstr #0a18 DEO + ( debug ) #010e DEO + ( halt ) #010f DEO + +BRK + +@decompress ( str* -- ) + + ;mem .ptr STZ2 + .File1/name DEO2 + #0001 .input-size STZ2 + &stream + .input-size LDZ2 .File1/length DEO2 + ;&b + DUP2 .File1/read DEO2 + LDA decompress/run + .File1/success DEI2 ORA ?&stream + +JMP2r + &b $1 + +&run ( byte -- ) + + DUP #80 AND ?&dict + ( literal ) + #00 SWP INC2 !do-literal + +&dict ( byte -- ) + + #7f AND + DUP #40 AND ?&dict3 + ( 1 byte ) + #00 SWP #0004 ADD2 + ( offset ) + ;&b .File1/read DEO2 + ;&b LDA INC + +!do-copy + +&dict3 ( byte -- ) + + ( hb ) + #3f AND + ( lb ) + #0001 .File1/length DEO2 + ;&b .File1/read DEO2 + ;&b LDA #0004 ADD2 + ( offset ) + ;&b .File1/read DEO2 + ;&b LDA INC + +!do-copy + +@do-copy ( length* offset -- ) + + #00 SWP #0000 SWP2 SUB2 .ptr LDZ2 ADD2 STH2 + #0000 + &l + DUP2 STH2kr ADD2 LDA do-append + INC2 GTH2k ?&l + POP2 POP2 + POP2r + +JMP2r + +@do-append ( byte -- ) + + .ptr LDZ2 STH2k STA + STH2r INC2 .ptr STZ2 + +JMP2r + +@do-literal ( length* -- ) + + DUP2 .File1/length DEO2 + .ptr LDZ2 + DUP2 .File1/read DEO2 + ADD2 .ptr STZ2 + +JMP2r + +( +@|stdlib ) + +@pstr ( str* -- ) LDAk ?&w POP2 JMP2r &w LDAk #18 DEO INC2 LDAk ?&w POP2 JMP2r +@skey ( key buf -- proc ) OVR #21 LTH ?&eval #00 SWP sput #00 JMP2r &eval POP2 #01 JMP2r +@scap ( str* -- end* ) LDAk ?&w JMP2r &w INC2 LDAk ?&w JMP2r +@sput ( chr str* -- ) scap INC2k #00 ROT ROT STA STA JMP2r + +@mem + diff --git a/lz/target.txt b/lz/target.txt new file mode 100644 index 0000000..9e4cf4d --- /dev/null +++ b/lz/target.txt @@ -0,0 +1 @@ +aabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaab \ No newline at end of file