Skip to content

Type-1 LZ77 payload

An EMI entry of type 1 holds one LZ77 stream that the loader decodes into RAM at the entry’s load address. All 65 type-1 entries decode: 12,723,580 stored bytes become 21,753,944 decoded bytes, the character sprites. This page covers the stream layout, the frame codec, writing a stream and the decoded container header, not the sprite data inside.

The loader decodes each sector as it arrives, on the scratchpad: LZ77_DecodeStep (exe/slus_004_22@0x80164064) decodes one 1,024-byte half at a time.

The payload is a 6-byte header followed by the control stream.

Offset Size Type Meaning
0x00 4 u32 decoded size
0x04 2 u16 marker 0xFFFF (65 of 65)
0x06 - - control stream, ends with a frame that starts with 0xFF

The 0xFF terminator is the last byte before the archive’s 0x5F padding (EMI container).

A frame is a literal run or a back-reference into the last 2,048 decoded bytes. Its first byte, the control byte, selects the kind.

Control byte Frame
0x10-0x1F literal run of 0x20 - (ctrl & 0x1F) bytes, 1 to 16. A count of 16 is followed by an extension byte added to the count, so runs reach 271
0xFF end of stream
other back-reference: copy bytes from distance bytes back, where distance = 0x800 - (((ctrl & 0xE0) << 3) | next_byte) and length = (ctrl & 0x0F) + 3. A low nibble of 15 is followed by an extension byte added to the length, so lengths reach 273

A back-reference may overlap its own output (distance < length); copy byte by byte so earlier output repeats. The 65 entries hold 633,673 literal frames (lengths 1 to 271) and 922,056 back-references (lengths 3 to 273, distances 1 to 2,047).

Payload 06 00 00 00 FF FF (decoded size 6) then 1E 41 42 E1 FE FF:

Bytes Frame Output so far
1E 41 42 literal run of 0x20 - 0x1E = 2 bytes 41 42
E1 FE distance 0x800 - (0xE0 << 3 | 0xFE) = 2, length (0xE1 & 0x0F) + 3 = 4, overlapping 41 42 41 42 41 42
FF end of stream 6 bytes

0x10 is ambiguous: a length-3 back-reference whose high distance bits are 0xE0 also starts with 0x10, colliding with the 16-byte literal escape. No retail entry uses that back-reference. A writer emits a 16-byte literal as 0x10, a 0x00 extension byte, then the 16 bytes.

A stream decodes in the game if it uses the frame forms above, distances up to 2,047, lengths 3 to 273, literal runs up to 271 and ends with 0xFF. A run of 1 to 15 literals uses one control byte; 16 to 271 uses 0x10, one extension byte and the bytes.

The retail encoder’s match choice is [unknown]. A longest-match, nearest-on-tie search with one byte of lookahead round-trips all 65 entries but reproduces no retail payload byte for byte. Two differences:

  • In BPLD012#7 at decoded offset 93, the retail stream copies 3 bytes from distance 92. A nearest-longest search copies from distance 64.
  • In BRTU467#7 at offset 138, the retail stream defers a 9-byte match to take an 8-byte one at the next byte.

Every decoded type-1 entry opens with this header.

Offset Size Type Meaning
0x00 4 u32 0x0C, constant in 65 of 65
0x04 4 u32 offset of the first large section (0x1B00C in BPLD012)
0x08 4 u32 offset of the second large section (0x3600C in BPLD012)
0x0C 4 u32 offset-table entry count, 16 in 65 of 65
0x10 64 u32[16] 16 section offset-table offsets

The container is [header][section 1][section 2][trailing region]. Both section offsets are 4-byte aligned in all 65 entries, not 16-byte aligned, and differ per character. Section 1 starts between 0xE780 and 0x306F4, and section 2 between 0x21EEC and 0x51B60.

Section 1 to section 2 is 110,592 bytes in 41 entries and 63,696 to 198,376 in the others. The trailing region splits the same way: 110,592 bytes in the same 41 entries and 11,796 to 198,376 in the others. In those 41 every region is a whole number of 4,096-byte pages.

The two large sections are compressed character-sprite data. The container goes to the entry’s load argument: EMI_LoadCompressed (exe/slus_004_22@0x80162618) stores the argument in the sector ring’s slot table at 0x801464B8, and EMI_DecompressSector (exe/slus_004_22@0x80163238) passes that word to LZ77_DecodeBegin (exe/slus_004_22@0x80164028) as the output address. Character sprites lists the BPLCHAR and PLCHAR load arguments; the EMI container lists the archive shapes.

  • The 16-entry offset table’s meaning beyond its shape. Settled by: finding its reader.
  • The retail encoder’s match rule. Settled by: a model that reproduces a retail payload byte for byte.
  • Confidence: medium. The stream format decodes all 65 entries; the encoder’s choices and the offset table’s use are open.
  • Evidence: LZ77_DecodeStep, the output address path through EMI_LoadCompressed, EMI_DecompressSector and LZ77_DecodeBegin, and a decode of every type-1 entry, checked for size, markers, terminator position and padding. A re-encoder round-trips all 65.
  • EMI container: entry types, load arguments and the padding after the stream.
  • Character sprites: what the two large sections of the decoded container hold.
  • Loading: how the loader streams and decodes an entry.