Tilemap compression format

Describes the tilemap-specific compression algorithm used by Donkey Kong Country 3 and Dixie Kong’s Double Trouble.\

Features

This format is a dedicated format specialized for tilemaps; it achieves a high compression ratio by compressing the data separately for each field: horizontal flip flag, vertical flip flag, priority setting, palette index and tile index.

The 3 bytes of data at the start of the compressed data, consisting of the field expansion specifier and the buffer size specifier.

Field expansion specifier

The field expansion specifier is the first byte of the header, and determines whether expansion is performed for each of the priority flag, horizontal flip flag and vertical flip flag fields.

BitContents
0Expand the priority field
1Expand the horizontal flip field
2Expand the vertical flip field

These bits can be left all unset, or several can be combined.
Fields whose bits are not set are not written and become 0.

Buffer size specifier

The buffer size specifier is bytes 2-3 of the header, and specifies the size of the decompressed tilemap in words.
To get the size in bytes, double the value.

Decompressing the data

The data is decompressed in the order priority flag, horizontal flip flag, vertical flip flag, palette index, tile index.
Of these, writing the priority flag, horizontal flip flag and vertical flip flag is optional; only the fields specified in the field expansion specifier are processed.
The palette index and tile index are always processed.

Writing the optional fields

The field expansion specifier is checked starting from the least significant bit, and if a bit is 1, the corresponding field is expanded.
The compressed data of each field is processed bit by bit.

CommandSyntaxContents
00 XWrites X (1 bit) once to the target bit field.
11 X YYYYYYWrites X (1 bit) to the target bit field of YYYYYY (6 bits) consecutive words.
If YYYYYY is 0, writing of the current bit field ends, the write position returns to the start, and processing moves on to the next bit field.

Writing the palette index

Once the optional fields have been written, the palette index is written. The encoding of the palette index is almost identical to that of the optional fields.

CommandSyntaxContents
00 XXXWrites XXX (3 bits) once as the palette index.
11 XXX YYYYWrites XXX (3 bits) to the palette index of YYYY (4 bits) consecutive words.
If YYYY is 0, writing of the palette index ends, the write position returns to the start, and processing moves on to the tile index.

Writing the tile index

Once the palette index has been written, the tile index is written.
The tile index is encoded in the form of a 2-bit command, a 10-bit tile index, and additional operands as needed.

CommandSyntaxContents
00 XWrites the 10-bit tile index X once
11 X YWrites the 10-bit tile index X to Y (6 bits) consecutive words.
If Y is 0, decompression ends.
22 X YWrites the 10-bit tile index X to Y (4 bits) consecutive words.
X is incremented by 1 after each write. In other words, this writes sequentially numbered tiles.
33 X YWrites the 10-bit tile index X to Y (4 bits) consecutive words.
X is decremented by 1 after each write. In other words, this writes sequentially numbered tiles.