Chunk

Chunks (a.k.a. pages) are the highest level data containers in NSF files. Each chunk is 64KB (0x10000 bytes) in size, in congruence with the 64KB pages allocated to contain them.

Non-texture chunks
Non-texture chunks are containers for 'entries'-the intermediate level data containers in NSF files.

Texture chunks
Texture chunks contain palette and texture data used by game models.

Chunk IDs/CIDs
Each chunk has a unique ID. A chunk ID has been assigned to each chunk according to its index in the NSF file. The formula for a chunk ID, or CID, is: '(index << 1) - 1', where chunk indices start at 1. This is equivalent to '(index << 1) | 1' when chunks are zero-indexed. Thus, a chunk's index is contained in the leftmost 31 bits of its CID. The rightmost bit or LSB in a CID is an indicator bit or tag, and is always 1. A CID is typically stored in a tagged union with its replacement entry pointer as the key in an entry hash table slot. Its replacement pointer locates an entry contained in the chunk with that CID.

Texture chunks do not have a CID field with precomputed value. The texture paging routines manually compute CIDs for texture chunks.

Checksums
Each chunk includes a 32-bit checksum which can be used to verify the integrity of the chunk data. At least some PAL versions of the games enforce these checksums.

The checksum is computed as follows:


 * 1) Start with the value.
 * 2) For each byte of the 64K chunk:
 * 3) * Add that byte to the current value. For the bytes from the checksum field itself (bytes 12 through 15), consider their value to be zero.
 * 4) * Circularly left-shift the current value by three bits.
 * 5) The final value is the correct checksum.

This procedure is also described by this C function: