Difference between revisions of "LZ3"

From SnesLab
Jump to: navigation, search
(Recovered from SMWiki, improvements are welcome.)
(No difference)

Latest revision as of 23:23, 9 November 2019

LZ3 or LC_LZ3, as named by Lunar Compress, is a lossless data compression format, used by Pokemon Gold/Silver to compress graphics and other data. The LZ3 compression format is similar the LZ2 compression format.


The compressed data consists of "chunks", each with a header:

CCC:   Command bits
LLLLL: Length

If the header byte is $FF, the end of the compressed data has been reached, and decompression will be aborted.

Here is a list of commands bits which the LC_LZ3 decompression function can use during the decompression:

000    "Direct Copy"
       Followed by (L+1) bytes of data
001    "Byte Fill"
       Followed by one byte to be repeated (L+1) times
010    "Word Fill"
       Followed by two bytes. Output first byte, then second, then first,
       then second, etc. until (L+1) bytes has been outputted
011    "Zero Fill"
       Repeat a $00 (L+1) times
100    "Repeat"
       Followed by one or two bytes. The first byte is AYYYYYYY.
       If bit A is set, (L+1) bytes are copied from the current
       address in the output buffer minus (Y+1)
       If bit A is reset, a second byte ZZZZZZZZ is read, and
       (L+1) bytes are copied from the address (Y*0x100)+Z
       relative to the first byte in the output buffer
101    "Bit-Reverse Repeat"
       Same concept as above, except all the copied bytes are
       reversed in bit order. ($AF = %10101111 -> $F5 = %11110101)
110    "Backwards Repeat"
       Same concept as "Repeat", except the bytes are copied in
       reversed order. The address points to the first byte to be copied,
       and it decrements as each byte is copied.
111    "Long length"
       This command has got a two-byte header:
       CCC:        Real command
       LLLLLLLLLL: Length

Seeing that the long length command uses ten bits for its length, the maximum length is 1024 bytes/1kB, while the normal length is 32 bytes.


During the decompression, the decompressed chunks are outputted into a buffer in the RAM.


You can use the LC_LZ3 compression to mainly compress graphics and data. Technically you can compress anything with this format.