Saving: flash sectors, records and the save workspace. More...
Go to the source code of this file.
Typedefs | |
| typedef u16(*) | EraseSectorProc(u16) |
Enumerations | |
| enum | { SAVE_FLASH_TIMER = 2 , SAVE_FLASH_IDENTIFY_ATTEMPTS = 8 , SAVE_SECTOR_COUNT = 16 , SAVE_RECORD_NONE = 16 , SAVE_FILE_COUNT = 3 , SAVE_SEQUENCE_LIMIT = 65000 , SAVE_CHECKSUM_BYTES = 0xfe8 } |
Functions | |
| s32 | SaveState_InitializeWorkspace (void) |
| Allocates the save workspace (heap slot HEAP_SLOT_SAVE), installs the flash library's timer 2 handler, identifies the flash chip (up to eight tries, a frame apart) and scans all 16 sectors into the workspace's occupied, recordId and sequence tables: a sector counts as taken when its signature and checksum are good, and of two copies of a record only the newer stays taken. | |
| u32 | SaveState_SelectWriteSlot (s32 mode) |
| Picks the free sector to write record mode to, at random. | |
| u32 | SaveState_WriteWorkspaceSlot (s32 sector) |
| Programs the workspace's sector buffer into flash sector sector and verifies it, through the flash library's routine for this chip. | |
| s32 | SaveState_ReadSlotAndCheckChecksum (s32 index) |
| Reads flash sector index into the workspace's sector buffer. | |
| UNUSED u16 | SaveState_EraseSlotSector (u16 sector) |
| Erases flash sector sector with the flash library's routine for this chip. | |
| s32 | SaveState_WriteRecord (s32 recordId, void *source) |
| Saves 0xff0 bytes from source as record recordId: writes the new copy with the next sequence number, then invalidates the old one, updating the workspace's sector tables. | |
| u32 | SaveState_ReadRecordPayload (s32 recordId, void *destination) |
| Loads the current copy of record recordId into destination. | |
| u32 | SaveState_DeleteRecord (s32 recordId) |
| Invalidates the current copy of record recordId. | |
| s32 | SaveState_ChecksumWorkspace (void) |
| Sums the first 0xfe8 payload bytes of the workspace's sector buffer; the final eight payload bytes are excluded. | |
| u32 | SaveState_FindLatestSlot (s32 recordId) |
| Returns the occupied sector holding the newest copy of record recordId, or 16. | |
| s32 | SaveState_InvalidateSlot (s32 index) |
| Overwrites sector index with a header for no record (id 16) and clears its occupied entry. | |
| s32 | SaveState_CompareBytes (u8 *left, u8 *right, s32 count) |
| Compares count bytes, as memcmp does. | |
| u32 | SaveState_GetLatestSequence (s32 recordId) |
| Returns the highest sequence number among the copies of record recordId. | |
| s32 | SaveState_LoadSummaryRecords (void) |
| Reads the summary at the start of each of the three save files into summary for the file select screen, and four bytes from offset 0x110 of each file's second record (zero when it has none). | |
| u32 | SaveState_ReleaseWorkspace (void) |
| Removes the flash timer handler and frees the save workspace's heap slot. | |
Saving: flash sectors, records and the save workspace.
Part of runtime.c, the runtime object, compiled only through it.
Definition in file save.c.
| anonymous enum |
| s32 SaveState_InitializeWorkspace | ( | void | ) |
Allocates the save workspace (heap slot HEAP_SLOT_SAVE), installs the flash library's timer 2 handler, identifies the flash chip (up to eight tries, a frame apart) and scans all 16 sectors into the workspace's occupied, recordId and sequence tables: a sector counts as taken when its signature and checksum are good, and of two copies of a record only the newer stays taken.
Returns 1 when no flash chip answers, 0 otherwise; either way the caller ends with SaveState_ReleaseWorkspace. Called by the save screens (window/battle_command/save.c) before every save, load, copy or delete.
Definition at line 31 of file save.c.
Referenced by memcpy(), Save_WriteSelectedSlot(), SaveState_CopySlotPair(), SaveState_CountRecordsExcludingFlagged(), SaveState_DeleteSelectedSlot(), SaveState_InitializeWorkspace(), SaveState_LoadRecordIntoWork(), SaveState_ProcessSelectedSlot(), SaveState_ScanRecordFlags(), SaveState_WriteCurrentSlotPair(), and SaveState_WriteSlotPair().
Picks the free sector to write record mode to, at random.
The last free sector only goes to a record that already has a copy, so replacing a save always has room. Called by SaveState_WriteRecord. Returns 16 when there is no sector to use.
Definition at line 100 of file save.c.
Referenced by memcpy(), SaveState_SelectWriteSlot(), and SaveState_WriteRecord().
Programs the workspace's sector buffer into flash sector sector and verifies it, through the flash library's routine for this chip.
Called by SaveState_WriteRecord and SaveState_InvalidateSlot. Returns 0 on success and 1 on failure.
Definition at line 136 of file save.c.
Referenced by memcpy(), SaveState_InvalidateSlot(), SaveState_WriteRecord(), and SaveState_WriteWorkspaceSlot().
Reads flash sector index into the workspace's sector buffer.
Called by SaveState_InitializeWorkspace and SaveState_ReadRecordPayload. Returns 0 when the payload matches the header's checksum.
Definition at line 155 of file save.c.
Referenced by memcpy(), SaveState_InitializeWorkspace(), SaveState_ReadRecordPayload(), and SaveState_ReadSlotAndCheckChecksum().
Erases flash sector sector with the flash library's routine for this chip.
Nothing calls it.
Definition at line 172 of file save.c.
Referenced by memcpy(), and SaveState_EraseSlotSector().
Saves 0xff0 bytes from source as record recordId: writes the new copy with the next sequence number, then invalidates the old one, updating the workspace's sector tables.
When the sequence passes 65000 it restarts at 1, written back over the old sector. Called by the save screens. Returns 0 on success and 1 on failure.
Definition at line 181 of file save.c.
Referenced by memcpy(), Save_WriteSelectedSlot(), SaveState_CopySlotPair(), SaveState_ProcessSelectedSlot(), SaveState_WriteCurrentSlotPair(), SaveState_WriteRecord(), and SaveState_WriteSlotPair().
Loads the current copy of record recordId into destination.
Called by the save screens to load or copy a file. Returns 1 when there is none and 0 otherwise.
Definition at line 236 of file save.c.
Referenced by memcpy(), SaveState_CopySlotPair(), SaveState_LoadRecordIntoWork(), SaveState_ProcessSelectedSlot(), and SaveState_ReadRecordPayload().
Invalidates the current copy of record recordId.
Called by the save screen's delete. Returns 0 on success and 1 when there is none or the write failed.
Definition at line 253 of file save.c.
Referenced by memcpy(), SaveState_DeleteRecord(), and SaveState_DeleteSelectedSlot().
| s32 SaveState_ChecksumWorkspace | ( | void | ) |
Sums the first 0xfe8 payload bytes of the workspace's sector buffer; the final eight payload bytes are excluded.
Called when a sector is read and before one is written.
Definition at line 268 of file save.c.
Referenced by memcpy(), SaveState_ChecksumWorkspace(), SaveState_ReadSlotAndCheckChecksum(), and SaveState_WriteRecord().
Returns the occupied sector holding the newest copy of record recordId, or 16.
Called by every record operation in this file.
Definition at line 294 of file save.c.
Referenced by memcpy(), SaveState_DeleteRecord(), SaveState_FindLatestSlot(), SaveState_LoadSummaryRecords(), SaveState_ReadRecordPayload(), SaveState_SelectWriteSlot(), and SaveState_WriteRecord().
Overwrites sector index with a header for no record (id 16) and clears its occupied entry.
Called by SaveState_WriteRecord and SaveState_DeleteRecord. Returns 0 on success and 1 on failure.
Definition at line 317 of file save.c.
Referenced by memcpy(), SaveState_DeleteRecord(), SaveState_InvalidateSlot(), and SaveState_WriteRecord().
Compares count bytes, as memcmp does.
Called by SaveState_InitializeWorkspace to check each sector's signature.
Definition at line 340 of file save.c.
Referenced by memcpy(), SaveState_CompareBytes(), and SaveState_InitializeWorkspace().
Returns the highest sequence number among the copies of record recordId.
Called by SaveState_WriteRecord to number the new copy.
Definition at line 356 of file save.c.
Referenced by memcpy(), SaveState_GetLatestSequence(), and SaveState_WriteRecord().
| s32 SaveState_LoadSummaryRecords | ( | void | ) |
Reads the summary at the start of each of the three save files into summary for the file select screen, and four bytes from offset 0x110 of each file's second record (zero when it has none).
Called by the save screens. Returns how many save files exist.
Definition at line 376 of file save.c.
Referenced by memcpy(), Save_WriteSelectedSlot(), SaveState_CopySlotPair(), SaveState_CountRecordsExcludingFlagged(), SaveState_DeleteSelectedSlot(), SaveState_LoadRecordIntoWork(), SaveState_LoadSummaryRecords(), and SaveState_ScanRecordFlags().
| u32 SaveState_ReleaseWorkspace | ( | void | ) |
Removes the flash timer handler and frees the save workspace's heap slot.
Called by the save screens after every save operation. Declared as returning a value, it returns none (pop {r1}; bx r1).
Definition at line 409 of file save.c.
Referenced by memcpy(), Save_WriteSelectedSlot(), SaveState_CopySlotPair(), SaveState_CountRecordsExcludingFlagged(), SaveState_DeleteSelectedSlot(), SaveState_LoadRecordIntoWork(), SaveState_ProcessSelectedSlot(), SaveState_ReleaseWorkspace(), SaveState_ScanRecordFlags(), SaveState_WriteCurrentSlotPair(), and SaveState_WriteSlotPair().