From 1722f35bee32d4b1a57f65160ce29541f26ef4eb Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 4 Apr 2016 07:39:46 -0700 Subject: [PATCH] set up compressed rom build --- patch/mm-randomizer | 55 +++++++-- patch/setup.asm | 152 ------------------------ z64yaz0.c | 277 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 320 insertions(+), 164 deletions(-) delete mode 100644 patch/setup.asm create mode 100644 z64yaz0.c diff --git a/patch/mm-randomizer b/patch/mm-randomizer index 4d9f819..1fa7a59 100755 --- a/patch/mm-randomizer +++ b/patch/mm-randomizer @@ -8,33 +8,64 @@ fast=0 inject=../Lua/inject sha1=d6133ace5afaa0882cf214cf88daba39e266c078 extracted=../dump/mm-US10-"$sha1" -rom=../../roms/everything/"Legend of Zelda, The - Majora's Mask (U) [!].z64" +rom=../roms/everything/"Legend of Zelda, The - Majora's Mask (U) [!].z64" lips=../Lua/lib/lips code="0031 V00B3C000" extra="1552 V02EE7040" -if ! [ -d "$extracted" ]; then - ../z64dump.py "$rom" - mv "$sha1" "$extracted" -fi -if [ $fast -eq 0 ]; then +ratio() { + len1="$(wc -c < "$1")" + len2="$(wc -c < "$2")" + let percent=(len2*100)/len1 + echo "ratio: $percent%" +} + +unc() { + [ -e patchme/"$1".Yaz0 ] || return 0 + ../z64yaz0 patchme/"$1".Yaz0 > patchme/"$1" + echo "uncompressed $1" + ratio patchme/"$1" patchme/"$1".Yaz0 + rm patchme/"$1".Yaz0 +} + +comp() { + [ -e patchme/"$1" ] || return 0 + ../z64yaz0 patchme/"$1" > patchme/"$1".Yaz0 + echo "compressed $1" + ratio patchme/"$1" patchme/"$1".Yaz0 + rm patchme/"$1" +} + +#if ! [ -d "$extracted" ]; then +if [ $fast -eq 0 ] || [ ! -d patchme ]; then [ -d patchme ] && rm -r patchme - cp -r "$extracted" patchme + (cd ..; ./z64dump.py -c "$rom") + mv ../"$sha1" patchme fi +#if [ $fast -eq 0 ]; then +# [ -d patchme ] && rm -r patchme +# cp -r "$extracted" patchme +#fi + +unc "$code" # don't copy entire dir; avoid copying dotfiles (.git) mkdir -p lips cp "$lips"/* lips cp "$inject/"{crc32,entrances}.asm . -#touch patchme/"$extra" -dd if=/dev/zero of=patchme/"$extra" bs=370688 count=1 2>/dev/null +touch "extra" luajit patch.lua code.asm patchme/"$code" 0 -luajit patch.lua extra.asm patchme/"$extra" 0x80780000 +luajit patch.lua extra.asm extra 0x80780000 + +dd if=extra of=patchme/"$extra" bs=370688 count=1 2>/dev/null +rm extra if [ $fast -ne 2 ]; then - ../z64dump.py patchme - mv patchme.z64 mm-randomizer.z64 + comp "$code" + comp "$extra" + (cd ..; ./z64dump.py patch/patchme) + dd if=patchme.z64 of=mm-randomizer.z64 bs=$((1024*1024)) count=32 status=none fi diff --git a/patch/setup.asm b/patch/setup.asm deleted file mode 100644 index 4a878af..0000000 --- a/patch/setup.asm +++ /dev/null @@ -1,152 +0,0 @@ -; original code -HEX { -27 BD FF 58 AF B1 00 30 3C 11 80 1F AF B0 00 2C -00 80 80 25 26 31 F6 70 AF BF 00 34 8E 22 3C B0 -8E 0E 00 00 24 01 FF FC 10 41 00 04 AF AE 00 A0 -24 01 FF 9D 54 41 00 16 A2 20 3C A7 92 23 10 0E -24 0A 00 01 24 01 FF 9D 30 6F 00 80 11 E0 00 0A -30 78 00 7F A2 38 10 0E A2 00 00 9B 3C 19 80 81 -27 39 58 20 24 09 02 48 AE 09 00 10 AE 19 00 0C -10 00 02 2F 8F BF 00 34 14 41 00 05 A2 2A 3C A7 -24 0B 00 02 10 00 00 02 AE 2B 3C B0 A2 20 3C A7 -8E 23 00 00 24 01 FF FF 54 61 00 0B 96 22 3F 4A -AE 20 00 00 A2 00 00 9B 3C 0C 80 80 25 8C 3F 30 -24 0D 02 10 AE 0D 00 10 AE 0C 00 0C 10 00 02 1C -8F BF 00 34 96 22 3F 4A 34 01 FF EF 10 41 00 03 -34 01 FF F0 54 41 00 52 02 00 20 25 92 2E 0F 19 -00 03 29 03 00 03 22 43 31 CF 00 80 11 E0 00 1D -30 A5 00 1F 24 01 00 4D 54 81 00 04 24 01 00 4A -10 00 00 18 24 04 00 57 24 01 00 4A 54 81 00 04 -24 01 00 5A 10 00 00 13 24 04 00 45 24 01 00 5A -54 81 00 04 24 01 00 59 10 00 00 0E 24 04 00 5B -24 01 00 59 10 81 00 0A 34 18 FF F0 24 01 00 58 -10 81 00 07 24 01 00 19 10 81 00 05 24 01 00 2F -10 81 00 03 24 01 00 68 54 81 00 03 92 39 0F 0C -A6 38 3F 4A 92 39 0F 0C 24 01 00 42 33 29 00 02 -11 20 00 09 3C 19 80 1C 54 81 00 04 24 01 00 43 -10 00 00 05 24 04 00 06 24 01 00 43 14 81 00 02 -34 0A FF F1 A6 2A 3F 4A 92 2B 0F 2C 24 01 00 10 -31 6C 00 20 51 80 00 05 92 2E 0F 2F 14 81 00 02 -34 0D FF F2 A6 2D 3F 4A 92 2E 0F 2F 24 01 00 34 -31 CF 00 80 11 E0 00 07 00 00 00 00 10 81 00 04 -34 18 FF F0 24 01 00 35 14 81 00 02 00 00 00 00 -A6 38 3F 4A 93 39 20 78 24 01 00 2A 02 39 48 21 -91 2A 00 70 11 40 00 06 00 00 00 00 14 81 00 04 -24 01 54 A0 10 61 00 02 34 0B FF F4 A6 2B 3F 4A -0C 04 C1 DA 30 66 00 0F AE 22 00 00 02 00 20 25 -0C 05 CE 20 00 00 28 25 0C 05 8E 01 02 00 20 25 -0C 05 83 28 00 00 00 00 26 04 00 B8 AF A4 00 44 -0C 04 FB BD 8F A5 00 A0 0C 06 8F B0 00 00 20 25 -0C 04 AA 92 00 00 00 00 0C 04 AB 9A 02 00 20 25 -00 00 28 25 02 00 10 25 24 A5 00 01 28 A1 00 04 -24 42 00 04 14 20 FF FC AC 40 07 FC 26 04 02 20 -26 06 08 30 AF A6 00 3C AF A4 00 40 8F A5 00 44 -0C 03 77 74 02 00 38 25 8F A4 00 40 0C 03 78 C2 -24 05 00 07 00 00 40 25 26 04 03 98 8F A5 00 44 -8F A6 00 3C 02 00 38 25 AF A4 00 48 0C 03 77 74 -AF A8 00 4C 8F A4 00 48 0C 03 78 C2 24 05 01 00 -8F A8 00 4C 8F A4 00 48 24 01 04 68 25 08 01 78 -15 01 FF F2 24 84 01 78 8F A4 00 40 24 05 00 7F -AE 04 08 00 A4 80 01 30 0C 03 7F C6 A6 00 08 10 -02 00 20 25 0C 05 1B 9C 26 05 46 B8 0C 06 AA A8 -02 00 20 25 0C 05 61 B5 02 00 20 25 0C 06 A9 84 -02 00 20 25 0C 03 C0 E4 02 00 20 25 0C 03 BF 98 -02 00 20 25 0C 02 BE 24 02 00 20 25 02 00 20 25 -0C 02 C0 14 24 05 00 64 3C 01 00 01 34 21 88 84 -02 01 28 21 0C 03 89 14 02 00 20 25 3C 01 00 01 -34 21 71 04 02 01 20 21 0C 04 D6 32 AF A4 00 44 -02 00 20 25 0C 03 A8 18 26 05 1F 24 96 22 3F 4A -34 01 FF EF 10 41 00 03 34 0C FF EF AE 22 00 08 -A6 2C 3F 4A 8E 2D 00 08 34 01 FF FD 55 A1 00 03 -96 22 3F 4E AE 20 00 08 96 22 3F 4E 34 01 FF FF -50 41 00 04 96 22 00 0C A6 22 00 0C A6 22 3F 52 -96 22 00 0C 34 01 C0 00 00 41 08 2A 10 20 00 02 -28 41 45 55 10 20 00 03 24 0E 00 01 10 00 00 02 -AE 2E 00 10 AE 20 00 10 0C 03 B7 6C 02 00 20 25 -8E 22 3C A8 10 40 00 03 24 01 00 01 54 41 00 07 -A6 20 3D C0 8E 2F 00 08 34 01 FF F0 01 E1 08 2A -54 20 00 0B AE 20 3C AC A6 20 3D C0 0C 04 57 57 -02 00 20 25 8E 38 00 08 AE 20 00 08 33 19 00 0F -27 29 00 01 10 00 00 02 AE 29 3C AC AE 20 3C AC -8E 22 3C AC 8E 23 00 00 A3 A2 00 87 00 43 20 21 -0C 04 C8 CE 30 84 FF FF 8E 26 00 00 8E 25 3C AC -AF A2 00 60 00 A6 20 21 0C 04 C8 DD 30 84 FF FF -02 00 20 25 8F A5 00 60 0C 05 A4 F5 00 40 30 25 -0C 05 8E 7B 02 00 20 25 0C 04 87 F1 02 00 20 25 -96 22 3F 4E 34 01 FF FF 10 41 00 0F 34 01 80 00 -14 41 00 0C 34 18 FF FD 8E 2A 00 18 8E 2C 00 1C -24 0E 00 01 34 0F FF FE 25 4B 00 01 25 8D 00 01 -AE 2B 00 18 AE 2D 00 1C A2 2E 3F 54 10 00 00 02 -A6 2F 3F 4E A6 38 3F 4E 0C 05 95 82 00 00 00 00 -3C 02 80 1F 24 42 3F 60 8C 59 00 00 3C 01 00 01 -34 21 8B 4C A7 20 01 90 8C 49 00 00 02 01 20 21 -A5 20 01 86 0C 05 BF 4B AF A4 00 4C 3C 05 80 20 -3C 06 80 20 84 C6 BB CE 84 A5 BB CC 8F A4 00 4C -00 00 38 25 AF A0 00 10 0C 05 BF 3C AF A0 00 14 -3C 05 80 20 3C 06 80 20 84 C6 BB CE 84 A5 BB CC -8F A4 00 4C 00 00 38 25 0C 05 BF 58 AF A0 00 10 -3C 0A 80 20 8D 4A BB 90 3C 01 00 02 00 30 08 21 -AC 2A 8E 64 3C 01 00 02 3C 0B 80 78 25 6B 00 00 -00 30 08 21 AC 2B 8E 5C 3C 02 80 78 3C 01 00 02 -24 42 46 00 00 30 08 21 AC 22 8E 68 3C 01 00 02 -00 30 08 21 AC 22 8E 58 3C 01 00 02 00 30 08 21 -AC 22 8E 60 3C 01 80 1F AC 20 6D 10 3C 01 00 02 -00 30 08 21 A0 20 8B 4A 3C 01 80 1D A0 20 0D 54 -0C 04 89 98 26 04 08 28 0C 02 41 08 00 00 00 00 -0C 02 1B F4 00 60 20 25 0C 06 00 58 02 00 20 25 -3C 0C 80 17 3C 0D 80 16 25 8C 8F 64 25 AD 61 3C -3C 01 00 02 AE 0C 00 04 AE 0D 00 08 00 30 08 21 -24 0E FF EC A0 2E 88 75 3C 01 00 02 00 30 08 21 -A4 20 88 76 3C 01 00 02 00 30 08 21 A4 20 88 78 -3C 01 00 02 00 30 08 21 A0 20 88 45 3C 01 00 02 -00 30 08 21 A0 20 88 44 8E 2F 3C A8 24 01 00 01 -51 E1 00 16 3C 01 00 02 92 22 3F 55 24 01 00 FF -93 B8 00 87 14 41 00 0B 24 0A 00 FF 8E 22 00 00 -00 58 20 21 0C 04 C8 E8 30 84 FF FF 00 02 C9 C3 -3C 01 00 02 00 30 08 21 33 29 00 7F 10 00 00 0A -A0 29 88 7F 3C 01 00 02 00 30 08 21 A0 22 88 7F -10 00 00 05 A2 2A 3F 55 3C 01 00 02 00 30 08 21 -24 0B 00 02 A0 2B 88 7F 3C 01 00 01 34 21 8E 48 -02 01 20 21 0C 05 92 0B AF A4 00 4C 8F A4 00 4C -0C 05 92 A6 24 05 00 03 3C 05 A0 A0 34 A5 A0 FF -0C 05 92 A3 8F A4 00 4C 0C 05 91 F4 8F A4 00 4C -3C 04 80 1F 0C 05 06 2C 24 84 6D 18 3C 02 80 1F -3C 01 80 1F 3C 0C 80 1F 24 42 6D 4C A0 20 6D 33 -25 84 6D 38 0C 05 03 A0 AC 44 00 00 3C 02 80 1F -24 42 6D 4C 8C 4D 00 00 44 80 20 00 24 0E 00 01 -02 00 20 25 E5 A4 00 08 8C 4F 00 00 A1 EE 00 00 -8C 58 00 00 A3 00 00 0C 8C 59 00 00 A3 20 00 0D -8C 49 00 00 A1 20 00 0E 8C 4A 00 00 A1 40 00 0F -8C 4B 00 00 A1 60 00 10 8C 4C 00 00 A1 80 00 11 -8C 4D 00 00 A1 A0 00 12 8C 4E 00 00 0C 03 C4 B4 -A1 C0 00 13 26 04 00 74 0C 05 CA C1 AF A4 00 4C -0C 05 CA C1 8F A4 00 4C AF A2 00 94 8F A4 00 4C -0C 05 CA B2 00 40 28 25 8F AF 00 94 24 46 00 08 -24 01 FF F0 00 C1 30 24 01 E6 C0 23 03 02 28 21 -0C 04 0B 6F 00 C0 20 25 3C 01 00 01 34 21 80 00 -02 01 10 21 8C 46 08 50 26 05 1C A0 AF A5 00 4C -AF A2 00 48 0C 02 E4 5C 02 00 20 25 3C 01 00 01 -34 21 86 E0 02 01 28 21 AF A5 00 3C 0C 04 BA AA -02 00 20 25 54 40 00 07 8E 39 00 18 02 00 20 25 -0C 04 BA AA 8F A5 00 3C 50 40 FF FD 02 00 20 25 -8E 39 00 18 24 03 00 05 8F AA 00 48 03 23 00 1A -14 60 00 02 00 00 00 00 00 07 00 0D 24 01 FF FF -14 61 00 04 3C 01 80 00 17 21 00 02 00 00 00 00 -00 06 00 0D 00 00 48 10 24 01 00 01 51 20 00 12 -8E 05 1C CC 91 42 06 E3 8F A4 00 4C 02 00 28 25 -10 41 00 03 24 06 01 5A 54 62 00 0B 8E 05 1C CC -44 80 00 00 AF A0 00 18 AF A0 00 1C 44 07 00 00 -AF A0 00 20 AF A0 00 24 E7 A0 00 10 0C 02 EB 18 -E7 A0 00 14 8E 05 1C CC 8F A4 00 40 0C 03 78 3B -AF A5 00 90 8F A5 00 90 3C 01 80 1D AC 20 0D 50 -84 A6 00 1C 24 01 00 FF 8F A4 00 40 30 C6 00 FF -10 C1 00 03 00 00 00 00 0C 03 7E C5 00 C0 28 25 -0C 03 C5 76 8F A4 00 40 0C 04 4A 79 02 00 20 25 -0C 03 ED D6 02 00 20 25 92 0B 08 14 3C 01 80 1F -02 00 20 25 A0 2B 35 86 92 0C 08 15 3C 01 80 1F -8F A5 00 44 0C 04 D7 BA A0 2C 35 87 0C 03 B6 F8 -02 00 20 25 AE 20 3C B0 3C 01 80 1F 3C 04 80 1F -A0 20 6D FC 0C 05 BF 1E 24 84 6D 50 8F BF 00 34 -8F B0 00 2C 8F B1 00 30 03 E0 00 08 27 BD 00 A8 -00 00 00 00 -} diff --git a/z64yaz0.c b/z64yaz0.c new file mode 100644 index 0000000..0514abe --- /dev/null +++ b/z64yaz0.c @@ -0,0 +1,277 @@ +#include +#include +#include + +//version 1.0 (20050707) +//by shevious +//Thanks to thakis for yaz0dec 1.0. + +typedef unsigned char u8; +typedef unsigned int u32; + +#define MAX_RUNLEN (0xFF + 0x12) + +// simple and straight encoding scheme for Yaz0 +static u32 simpleEnc(u8 *src, int size, int pos, u32 *pMatchPos) +{ + int startPos = pos - 0x1000; + int i, j; + u32 numBytes = 1; + u32 matchPos = 0; + + int end = size - pos; + // maximum runlength for 3 byte encoding + if (end > MAX_RUNLEN) + end = MAX_RUNLEN; + + if (startPos < 0) + startPos = 0; + for (i = startPos; i < pos; i++) { + for (j = 0; j < end; j++) { + if (src[i + j] != src[j + pos]) + break; + } + if (j > numBytes) { + numBytes = j; + matchPos = i; + } + } + + *pMatchPos = matchPos; + + if (numBytes == 2) + numBytes = 1; + + return numBytes; +} + +// a lookahead encoding scheme for ngc Yaz0 +static u32 nintendoEnc(u8 *src, int size, int pos, u32 *pMatchPos) +{ + u32 numBytes = 1; + static u32 numBytes1; + static u32 matchPos; + static int prevFlag = 0; + + // if prevFlag is set, it means that the previous position + // was determined by look-ahead try. + // so just use it. this is not the best optimization, + // but nintendo's choice for speed. + if (prevFlag == 1) { + *pMatchPos = matchPos; + prevFlag = 0; + return numBytes1; + } + + prevFlag = 0; + numBytes = simpleEnc(src, size, pos, &matchPos); + *pMatchPos = matchPos; + + // if this position is RLE encoded, then compare to copying 1 byte and next position(pos+1) encoding + if (numBytes >= 3) { + numBytes1 = simpleEnc(src, size, pos + 1, &matchPos); + // if the next position encoding is +2 longer than current position, choose it. + // this does not guarantee the best optimization, but fairly good optimization with speed. + if (numBytes1 >= numBytes + 2) { + numBytes = 1; + prevFlag = 1; + } + } + return numBytes; +} + +static int encodeYaz0(u8 *src, u8 *dst, int srcSize) +{ + int srcPos = 0; + int dstPos = 0; + int bufPos = 0; + + u8 buf[24]; // 8 codes * 3 bytes maximum + + u32 validBitCount = 0; // number of valid bits left in "code" byte + u8 currCodeByte = 0; + + while (srcPos < srcSize) { + u32 numBytes; + u32 matchPos; + + numBytes = nintendoEnc(src, srcSize, srcPos, &matchPos); + if (numBytes < 3) { + // straight copy + buf[bufPos] = src[srcPos]; + bufPos++; + srcPos++; + //set flag for straight copy + currCodeByte |= (0x80 >> validBitCount); + } else { + //RLE part + u32 dist = srcPos - matchPos - 1; + u8 byte1, byte2, byte3; + + if (numBytes >= 0x12) { // 3 byte encoding + byte1 = 0 | (dist >> 8); + byte2 = dist & 0xFF; + buf[bufPos++] = byte1; + buf[bufPos++] = byte2; + // maximum runlength for 3 byte encoding + if (numBytes > MAX_RUNLEN) + numBytes = MAX_RUNLEN; + byte3 = numBytes - 0x12; + buf[bufPos++] = byte3; + } else { // 2 byte encoding + byte1 = ((numBytes - 2) << 4) | (dist >> 8); + byte2 = dist & 0xFF; + buf[bufPos++] = byte1; + buf[bufPos++] = byte2; + } + srcPos += numBytes; + } + + validBitCount++; + + // write eight codes + if (validBitCount == 8) { + //fwrite(&currCodeByte, 1, 1, dstFile); + //fwrite(buf, 1, bufPos, dstFile); + + dst[dstPos++] = currCodeByte; + for (int j = 0; j < bufPos; j++) + dst[dstPos++] = buf[j]; + + currCodeByte = 0; + validBitCount = 0; + bufPos = 0; + } + } + + if (validBitCount > 0) { + //fwrite(&currCodeByte, 1, 1, dstFile); + //fwrite(buf, 1, bufPos, dstFile); + + dst[dstPos++] = currCodeByte; + for (int j = 0; j < bufPos; j++) + dst[dstPos++] = buf[j]; + + currCodeByte = 0; + validBitCount = 0; + bufPos = 0; + } + + return dstPos; +} + +void decompress(u8 *src, u8 *dst, int uncompressedSize) +{ + int srcPlace = 0, dstPlace = 0; // current read/write positions + + u32 validBitCount = 0; // number of valid bits left in "code" byte + u8 currCodeByte = 0; + + while (dstPlace < uncompressedSize) { + // read new "code" byte if the current one is used up + if (validBitCount == 0) { + currCodeByte = src[srcPlace]; + ++srcPlace; + validBitCount = 8; + } + + if((currCodeByte & 0x80) != 0) { + // straight copy + dst[dstPlace] = src[srcPlace]; + dstPlace++; + srcPlace++; + } else { + // RLE part + u8 byte1 = src[srcPlace]; + u8 byte2 = src[srcPlace + 1]; + srcPlace += 2; + + u32 dist = ((byte1 & 0xF) << 8) | byte2; + u32 copySource = dstPlace - (dist + 1); + + u32 numBytes = byte1 >> 4; + if(numBytes == 0) { + numBytes = src[srcPlace] + 0x12; + srcPlace++; + } else + numBytes += 2; + + // copy run + int i; + for(i = 0; i < numBytes; ++i) { + dst[dstPlace] = dst[copySource]; + copySource++; + dstPlace++; + } + } + + // use next bit from "code" byte + currCodeByte <<= 1; + validBitCount--; + } +} + + +int main(int argc, char *argv[]) +{ + for (int i = 1; i < argc; i++) { + FILE *f = fopen(argv[i], "rb"); + + fseek(f, 0, SEEK_END); + long size = ftell(f); + fseek(f, 0, SEEK_SET); + + u8 *bufi = malloc(size); + fread(bufi, 1, size, f); + + fclose(f); + + if (size > 0x10 + && bufi[0] == 'Y' + && bufi[1] == 'a' + && bufi[2] == 'z' + && bufi[3] == '0') { + long usize = (bufi[4] << 24) + | (bufi[5] << 16) + | (bufi[6] << 8) + | bufi[7]; + u8 *bufo = malloc(usize); + decompress(bufi + 16, bufo, usize); + fwrite(bufo, usize, 1, stdout); + free(bufo); + } else { + // we don't know how big the "compressed" file could get, + // so over-allocate! + // modern systems have more RAM than the largest Yaz0 file, so... + u8 *bufo = malloc(size * 2); + + // write 4 bytes yaz0 header + bufo[0] = 'Y'; + bufo[1] = 'a'; + bufo[2] = 'z'; + bufo[3] = '0'; + + // write 4 bytes uncompressed size + bufo[4] = (size >> 24) & 0xFF; + bufo[5] = (size >> 16) & 0xFF; + bufo[6] = (size >> 8) & 0xFF; + bufo[7] = (size >> 0) & 0xFF; + + // write 8 bytes unused dummy + bufo[8] = 0; + bufo[9] = 0; + bufo[10] = 0; + bufo[11] = 0; + bufo[12] = 0; + bufo[13] = 0; + bufo[14] = 0; + bufo[15] = 0; + + long csize = encodeYaz0(bufi, bufo + 16, size) + 16; + + fwrite(bufo, csize, 1, stdout); + free(bufo); + } + free(bufi); + } +}