Yaz0 to LZO/RC4

From en64 wiki
Jump to: navigation, search

Taken from the now defunct 64.vg. (original post here)


I have implemented RC4 encryption and LZO compression in Zelda 64 based games (Ocarina of Time and Majora's Mask). At the moment, I only have it fully working in the Master Quest debug build (ZELOOTMA), but the code is easily adapted to another game. The hack works by replacing the function call to the compressed file loader with its own routine. I won't go into detail about the underlying method used to patch my code into the ROM, but you can read more about it here.


I'm not sure if LZO is quicker than Nintendo's Yaz0 (especially with RC4 lumped in there), but it certainly provides better compression. Here are some examples.
Standard Ocarina of Time (US, v1.0):

$ z64repack -rnT -I OOT.z64 -O OOT-repacked.z64 -t 0x00007430 -l 24160 -k 'Kodewerx'
[##################################################] 1510/1510 100.00%
Old size: 31.48M
New size: 30.20M
Saved:    1.28M
Percent:  95.93%
Time:     5.94s

And with Majora's Mask (US):

$ z64repack -rnT -I MM.z64 -O MM-repacked.z64 -t 0x0001A500 -l 24832 -k 'Kodewerx'
[##################################################] 1552/1552 100.00%
Old size: 30.63M
New size: 29.16M
Saved:    1.47M
Percent:  95.19%
Time:     5.51s

The debug build of Master Quest also compresses quite nicely. However, since it is not compressed at all to begin with, we have to explicitly blacklist some files from compression, as such:

$ z64repack -nT -I ZELOOTMA.n64 -O ZELOOTMA-repacked.n64 -t 0x00012F70 -l 24512 -k 'Kodewerx' -E 0-27,942,944,946,948,950,952,954,956,958,960,962,964,
966,968,970,972,974,976,978,980,982,984,986,988,990,992,994,996,998,1000,1002,1004
[##################################################] 1532/1532 100.00%
Old size: 51.74M
New size: 30.80M
Saved:    20.94M
Percent:  59.52%
Time:     5.44s

Most importantly, LZO compression is faster than the Yaz0 compressors by orders of magnitude.

How to use

"How to use" is a bit misleading. What I'm going to write about here is how to apply the patch I've included. The gameplay is, of course, unaltered, so if you wish to observe the changes I suggest you fire up Nemu's debugger and set a breakpoint on 0x807b80e8. As mentioned earlier, the only game with full support is the Master Quest debug ROM. Acquire this, then download and compile "z64repack".

$ wget -q -O - 'http://hg.64.vg/files/z64repack-1.0.0.tar.gz' | tar -xzf -
$ cd z64repack-1.0.0
$ mkdir obj; cd obj
$ ../configure --prefix=$HOME/.local
$ make install CFLAGS='-O3'


If you are a Windows user, you will probably have to go into Cygwin or something similar (Interix?).
Use the following command can be used to compress/encrypt the ROM. Do not change the encryption key as the binary contained within the patch is hard-coded to use "Kodewerx" as the key.

$ z64repack -nT -I ZELOOTMA.n64 -O ZELOOTMA-repacked.n64 -t 0x00012F70 -l 24512 -k 'Kodewerx' -E 0-27,942,944,946,948,950,952,954,956,958,960,962,964,
966,968,970,972,974,976,978,980,982,984,986,988,990,992,994,996,998,1000,1002,1004


Finally, apply the PPF patch down at the bottom (z64ce), and everything should be ready to go.

I have not been bothered to test this out on real hardware yet, but Nemu, Project 64, and Mupen64plus (you must set the core to interpreter in the latter two) seem to be able to handle it.

How to build

Uhh. Compile binutils, gcc, and newlib (in that order) with mips as the target. You can find some scripts here. You'll also need some other tools that I've written, most of which you can find in my mercurial repository or files directory. I'll update this post with more info later if there's any interest.

Downloads

  • z64repack

  Source code: hg | tgz (Relocated)

  • z64ce - This build ONLY supports the debug build of Master Quest.

  Binary: ppf | elf
  Source code: hg | tgz (Relocated)