Save data management on the Controller Pak is facilitated through a simple proprietary filesystem. Most games that support this Pak have a menu that can be accessed by holding START while turning on the N64. In all official and unofficial Controller Paks, there are 32,768 bytes of SRAM available, which is split into 256-byte sectors referred to as "pages". This allows for a maximum capacity of 128 pages, 5 of which are reserved for the filesystem data, and the remaining 123 for user software. A software can create one or more save files, up to a total of 16 files, referred to as "notes". ==Filesystem data format== ===Overall Structure=== {| class="wikitable" ! colspan="2" |Overall structure (Standard 32 KiB) |- !Page no.!!Description |- |Page 0||ID Sector - critical sanity and identification |- |Page 1||FAT - allocation of pages (chains) |- |Page 2||FAT Backup - exact copy of the FAT |- |Page 3-4 |Note Table - allocation of Note entries |- |Page 5-127 |Note data sectors |} For larger (banked) cpaks, the Index Table can span multiple pages. For instance, in a 512 KiB controller paks, there are of 2048 pages. Since each Index Table page can contain inodes up to 128 pages, we need 2048 / 128 = 16 Index Table pages. The layout thus becomes: {| class="wikitable" ! colspan="2" |Overall structure (512 KiB) |- !Page no.!!Description |- |Page 0||ID Sector - critical sanity and identification |- |Page 1-16||Index Table - allocation of user data sectors |- |Page 17-32||Backup Index Table - exact copy of Index Table |- |Page 33-34 |Note Table - allocation of Note entries |- |Page 35-2047 |Note data sectors |} ===Page 0: ID Sector=== The ID sector primarily houses basic identifying and system information for the file system and the specific Controller Pak. {| class="wikitable" ! colspan="3" |ID sector structure (256 bytes) |- !Offset!!Size !Description |- |0x00||32 |Label area |- |0x20||32 |ID Block (Primary) |- |0x40||32 |''unused'' |- |0x60||32 |ID Block (Backup 1) |- |0x80||32 |ID Block (Backup 2) |- |0xA0||32 |''unused'' |- |0xC0||32 |ID Block (Backup 3) |- |0xE0||32 |''unused'' |} The first 32-bytes of the ID Sector is known as the Label. It was never officially used nor were its specifications ever supplied to developers. It is implied to be a sort of text-based label or tag. In practice, only garbage data is ever written here during a repair operation, or as a side-effect from example code being copy-pasted to produce the numbers 0 to 32. The 32-byte ID block provides system information such as the serial number, device ID, the capacity of the media, and two protective checksums. In total, four identical copies of the ID block are stored in this sector in case one or more become corrupted. {| class="wikitable" ! colspan="3" |ID Block structure (32 bytes) |- !Offset!!Size !Description |- |0x00||24 |Serial number |- |0x18||1 |''unused (normally 0)'' |- |0x19||1 |Device ID |- |0x1A||1 |Bank size (normally 1) |- |0x1B||1 |''unused (normally 0)'' |- |0x1C||2 |Checksum 1 |- |0x1E||2 |Checksum 2 |} The serial number uniquely identifies the particular controller pak. It can be filled with random bytes or any other value that is unique across all extant controller paks. The device ID field identifies the type of device. Bit 0 should always be set to 1 to indicate a controller pak, the other bits are normally 0 but can contain garbage. The bank size describes the capacity of the controller pak in number of 32 KiB banks. Since a standard first-party cpak is 32 KiB, this field will be 1, but in general Nintendo SDK supported up to 62 banks. The first checksum is a 16-bit big endian word computed by summing the first fourteen 16-bit words in the structure. That is every word in the block that is not either of the checksums. The second checksum is also a 16-bit word computed by subtracting the first checksum from the value 0xFFF2. ===Page 1+2: FAT=== The FAT is used as the allocation table for the file system utilizing a page chaining strategy similar to [https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system#File_Allocation_Table FAT16]. The FAT consists of one full page representing an array of 128 two-byte words, known as FAT entries, where the index of the word in the array corresponds to the page with the same index number on the controller pak. {| class="wikitable" ! colspan="3" |Index Table page (256 bytes) |- !Offset!!Size !Description |- |0x00||1 |''unused (always 0)'' |- |0x01||1 |Checksum |- |0x02||254 |Entries for pages 1-127 |} In a standard 32 KiB cpak, the first 5 pages are reserved (see the Overall Structure above), so entries for them will contain 0. The lower-byte of the first entry stores the (8-bit) checksum for the entire table. The checksum is computed by summing all the bytes for all of the 127 remaining entries (i.e. starting at byte offset 0x02 through the end of the page). In a multi-bank cpak, theFAT is made of multiple pages with the exact same structure (see the Overall Structure above). Each page will still have its own checksum, which means that the first page in each bank will be wasted. For instance, in a 512 KiB cpak, there are 35 reserved page (see the Overall Structure above), so the first 35 entries of the first FAT page will be 0 (though the first one will contain the checksum). The second FAT page will contain entries for pages 128-255, but the entry for page 128 is used to store the checksum, so the whole page 128 (first page of the second bank of the cpak) will never be used. Two identical copies of the FAT are stored in case of data corruption. Since each page has its own checksum, readers can switch between the main copy and the backup copy at page granularity, by verifying the checksum. FAT entries contains a value indicating that it is unallocated (not used by a note), a value indicating it is the last in a chain (the last page at the end of a note), or the index of the next page in a multiple page chain. The first page for a note is always stored in the note entry data structure and the entire note can then be found by walking the chain in the FAT until the end value is encountered. {| class="wikitable" ! colspan="2" |FAT entry values |- !Value !Description |- |0 |Indicates a reserved page |- |1 |Indicates this is the last page in sequence. |- |3 |Indicates this page is unallocated free space. |- |>= 5 |Indicates the next page in the sequence |} ===Page 3+4: Note Table=== The Note Table is simply a list of 16 Note entries. Each entry consists of 32 bytes and identifies the start page, game code and other data. The positions in the list correspond to the slot numbers displayed in the memory card manager. Unused entries are zeroed out. {| class="wikitable" ! colspan="3" |Note entry format (32 bytes) |- !Offset!!Size !Description |- |0x00||4 |Game Code - ASCII, e.g. NSME |- |0x04||2 |Publisher Code - ASCII, e.g. 01 |- |0x06||2 |Start page - Index number of the first page of the note |- |0x08 |1 |Status - Bit field of flags |- |0x09 |3 |Unused, contains garbage data? |- |0x0C |4 |File extension - Text using custom encoding |- |0x10 |16 |File name - Text using custom encoding |} The game code is a short ASCII encoded string that identifies the specific game that created and owns the note. This is the same code as the media format code that appears in the [http://en64.shoutwiki.com/wiki/ROM#Cartridge_ROM_Header ROM header] for each game. These codes are specific not only to the particular game but also to the type of media (e.g. cartridge) and geographic region. The publisher code are unique codes that were assigned by Nintendo to their licensed partners. The publisher codes and are also typically ASCII characters but do not appear in the ROM header and so are more difficult to catalogue. '''NOTE:''' Datel GameShark uses non-ASCII gamecode/pubcode (0x3BADD1E5, 0xFADE respectively). In general, most user interfaces won't display those anyway, and nothing wrong happened with the Nintendo SDK. The status field contains bit flags. The flag 0x02 should always be set. The file extension is optional and uses the same custom character encoding used by the file name. Four characters are reserved but the stock controller pak manager used by most games only displays the first character. Some games, such as Ogre Battle 64, which implement their own manager do display the extended characters when present. Unused characters should be padded with NUL. The file name appears as the primary text string describing the contents of the note in the slot when the note is displayed in the controller pak manager of any game. The note text is encoded using a custom character encoding specific to the controller pak file system that includes punctuation plus basic Latin and Japanese characters. Unused characters should be padded with NUL. {| class="wikitable" style="text-align: center" !colspan="17"|Note name text encoding |- ! !!_0 !!_1 !!_2 !!_3 !!_4 !!_5 !!_6 !!_7 !!_8 !!_9 !!_A !!_B !!_C !!_D !!_E !!_F |- !0_ |style="font-size:70%"|NUL || || || || || || || || || || || || || || ||style="font-size:70%"|SPC |- !1_ |0 ||1 ||2 ||3 ||4 ||5 ||6 ||7 ||8 ||9 ||A ||B ||C ||D ||E ||F |- !2_ |G ||H ||I ||J ||K ||L ||M ||N ||O ||P ||Q ||R ||S ||T ||U ||V |- !3_ ||W ||X ||Y ||Z ||! ||" ||# ||' ||* ||+ ||, ||- ||. ||/ ||: ||= |- !4_ |? ||@ ||。 ||゛ ||゜ ||ァ ||ィ ||ゥ ||ェ ||ォ ||ッ ||ャ ||ュ ||ョ ||ヲ ||ン |- !5_ |ア ||イ ||ウ ||エ ||オ ||カ ||キ ||ク ||ケ ||コ ||サ ||シ ||ス ||セ ||ソ ||タ |- !6_ |チ ||ツ ||テ ||ト ||ナ ||ニ ||ヌ ||ネ ||ノ ||ハ ||ヒ ||フ ||ヘ ||ホ ||マ ||ミ |- !7_ |ム ||メ ||モ ||ヤ ||ユ ||ヨ ||ラ ||リ ||ル ||レ ||ロ ||ワ ||ガ ||ギ ||グ ||ゲ |- !8_ |ゴ ||ザ ||ジ ||ズ ||ゼ ||ゾ ||ダ ||ヂ ||ヅ ||デ ||ド ||バ ||ビ ||ブ ||ベ ||ボ |- !9_ |パ ||ピ ||プ ||ペ ||ポ || || || || || || || || || || || |- !A_ || || || || || || || || || || || || || || || || |- !B_ || || || || || || || || || || || || || || || || |- !C_ || || || || || || || || || || || || || || || || |- !D_ || || || || || || || || || || || || || || || || |- !E_ || || || || || || || || || || || || || || || || |- !F_ || || || || || || || || || || || || || || || || |} === Libdragon extensions to note table === Libdragon extended the note entry as follows: {| class="wikitable" ! colspan="3" |Note entry format (32 bytes) |- !Offset!!Size !Description |- |0x00||4 |Game Code - ASCII, e.g. NSME |- |0x04||2 |Publisher Code - ASCII, e.g. 01 |- |0x06||2 |Start page - Index number of the first page of the note |- |0x08 |1 |Status - Bit field of flags |- |'''0x09''' |'''1''' |'''Size of padding bytes in the last page.''' '''This allows to reconstruct the exact file size.''' |- |'''0x0A''' |'''2''' |'''CRC16 (poly 0x5935, not reflected).''' '''Calculated over the whole note including CRC bytes set to 0.''' |- |0x0C |4 |File extension - Text using custom encoding |- |0x10 |16 |File name - Text using custom encoding |} The additional CRC is used as a way to verify that extensions are in use. In fact, we assume that if the CRC doesn't match, the note wasn't written by libdragon and thus extensions cannot be trusted. The only actual extension is the padding size. This byte stores the amount of padding in the last page. By knowing this number, it is possible to reconstruct the actual file size, which provides a nicer interface for developers. ==Additional Documentation== [https://ultra64.ca/files/documentation/online-manuals/man/pro-man/pro26/index26.3.html Official N64 Programming Manual]