Skip to Content
The Alchemy Project

src/system/system_bup.c

Saving: flash sectors, parts and the save state. More…

Included by system.c.

Macros

NameDescription
#defineSTART_DMA (source_, destination_, control_)Starts a DMA 3 transfer; control_ is the unit count with the DMA_ flags << 16.
#defineWAIT_DMA () syDMAWait(DMA3)Waits until a START_DMA transfer is done.
#defineBUP_SECOND_PLAY_TIME_OFFSETWhere a file's second copy of the play time lies in its second part: mnSaveBuildFileSummary (window/screens_save.c) writes it into SceneLocalState, which opens the save image's second half, after the part's header.

Enumerations

NameDescription
enumanonymous enum { BUP_FLASH_TIMER, BUP_FLASH_IDENTIFY_ATTEMPTS, BUP_SECTOR_COU … }The save code's timer, retries, sectors and checksum.

Functions

NameDescription
BoolsyBupInitState (void)Allocates the save state (heap slot HEAP_SLOT_BUP), installs the SDK's flash library's BUP_FLASH_TIMER handler, identifies the flash chip (up to eight tries, a tick apart) and scans all 16 sectors into the save state's occupied, partId and counter tables: a sector counts as taken when its signature and checksum are good, and of two copies of a part only the newer stays taken.
Uint32syBupSelectWriteSector (Sint32 partId)Picks the free sector to write part partId to, at random, or returns BUP_SECTOR_COUNT for none.
Uint32syBupWriteSector (Sint32 sector)Programs the save state's sector buffer into flash sector sector and verifies it, through the SDK's flash library's function for this chip.
Sint32syBupReadSector (Sint32 index)Reads flash sector index into the save state's sector buffer.
Uint16syBupEraseSector (Uint16 sector)Unused: erases flash sector sector with the SDK's flash library's function for this chip.
BoolsyBupWritePart (Sint32 partId, void *src)Saves 4080 bytes from src as part partId: writes the new copy with the next counter, then invalidates the old one, updating the save state's sector tables.
Uint32syBupReadPart (Sint32 partId, void *dest)Loads the current copy of part partId (4080 bytes) into dest.
Uint32syBupDeletePart (Sint32 partId)Invalidates the current copy of part partId.
Sint32syBupChecksumSector (void)Sums the first BUP_CHECKSUM_BYTES payload bytes of the save state's sector buffer; the final eight payload bytes are excluded.
Uint32syBupFindLatestSector (Sint32 partId)Returns the occupied sector holding the newest copy of part partId, or BUP_SECTOR_COUNT.
BoolsyBupInvalidateSector (Sint32 index)Overwrites sector index with a header for no part (BUP_PART_NONE) and clears its occupied entry.
Sint32syBupCompareBytes (Uint8 *left, Uint8 *right, Sint32 count)Compares count bytes, as memcmp does.
Uint32syBupGetLatestCounter (Sint32 partId)Returns the highest counter among the copies of part partId.
Sint32syBupLoadFileSummaries (void)Reads the summary at the start of each of the three save files into the save state's summary table for the file list, and each file's second copy of the play time, from BUP_SECOND_PLAY_TIME_OFFSET in its second part, into its secondPartPlayTime (zero when it has none).
Uint32syBupReleaseState (void)Removes the flash timer handler and frees the save state's heap slot, with every block allocated after it in the same pool.

Description

Saving: flash sectors, parts and the save state. Part of system.c.

Macros in detail

START_DMA

#define START_DMA(source_, destination_, control_)

Starts a DMA 3 transfer; control_ is the unit count with the DMA_ flags << 16.

Referenced by syBupInitState(), syBupInvalidateSector(), syBupReadPart() and syBupWritePart().

Definition at line 5 of file system_bup.c.

WAIT_DMA

#define WAIT_DMA() syDMAWait(DMA3)

Waits until a START_DMA transfer is done.

Referenced by syBupInitState(), syBupInvalidateSector(), syBupReadPart(), syBupReadSector() and syBupWritePart().

Definition at line 10 of file system_bup.c.

BUP_SECOND_PLAY_TIME_OFFSET

#define BUP_SECOND_PLAY_TIME_OFFSET

Where a file's second copy of the play time lies in its second part: mnSaveBuildFileSummary (window/screens_save.c) writes it into SceneLocalState, which opens the save image's second half, after the part's header.

Referenced by syBupLoadFileSummaries().

Definition at line 32 of file system_bup.c.

Enumerations in detail

anonymous enum

enum { BUP_FLASH_TIMER, BUP_FLASH_IDENTIFY_ATTEMPTS, BUP_SECTOR_COUNT, BUP_PART_NONE, … }

The save code's timer, retries, sectors and checksum.

EnumeratorValueDescription
BUP_FLASH_TIMER2the hardware timer the SDK's flash library uses
BUP_FLASH_IDENTIFY_ATTEMPTS8tries, a tick apart
BUP_SECTOR_COUNT16sectors scanned; also "none" as a sector number below
BUP_PART_NONE16the part id of a free or invalidated sector
BUP_COUNTER_LIMIT65000highest write counter kept; past it, rewritten as 1
BUP_CHECKSUM_BYTES4072payload bytes checksummed: all 4080 but the last 8

Definition at line 13 of file system_bup.c.

Functions in detail

syBupInitState

Bool syBupInitState(void)

Allocates the save state (heap slot HEAP_SLOT_BUP), installs the SDK's flash library's BUP_FLASH_TIMER handler, identifies the flash chip (up to eight tries, a tick apart) and scans all 16 sectors into the save state's occupied, partId and counter tables: a sector counts as taken when its signature and checksum are good, and of two copies of a part only the newer stays taken.

References BupSignature, IrqHandlers, state, syBupCompareBytes(), syBupReadSector(), syHeapSlotAllocateEWRAM() and sySynch().

Referenced by mnSaveChooseAndLoadFile(), mnSaveCopyFile(), mnSaveCountFilesExcludingSent(), mnSaveEraseSelectedFile(), mnSaveScanFileSummaries(), mnSaveToChosenFile(), mnSaveWriteCurrentFile(), mnSaveWriteFile() and mnSaveWriteLinkTallies().

Definition at line 40 of file system_bup.c.

syBupSelectWriteSector

Uint32 syBupSelectWriteSector(Sint32 partId)

Picks the free sector to write part partId to, at random, or returns BUP_SECTOR_COUNT for none. The last free sector is kept for replacing a part that already has a copy.

References Sint32, syBupFindLatestSector() and syRandom().

Referenced by syBupWritePart().

Definition at line 107 of file system_bup.c.

syBupWriteSector

Uint32 syBupWriteSector(Sint32 sector)

Programs the save state's sector buffer into flash sector sector and verifies it, through the SDK's flash library's function for this chip.

References Sint32 and state.

Referenced by syBupInvalidateSector() and syBupWritePart().

Definition at line 150 of file system_bup.c.

syBupReadSector

Sint32 syBupReadSector(Sint32 index)

Reads flash sector index into the save state's sector buffer. Returns 0 when its checksum is good.

References Sint32, state, syBupChecksumSector() and syDMAXCopy().

Referenced by syBupInitState() and syBupReadPart().

Definition at line 164 of file system_bup.c.

syBupEraseSector

Uint16 syBupEraseSector(Uint16 sector)

Unused: erases flash sector sector with the SDK's flash library's function for this chip.

Definition at line 180 of file system_bup.c.

syBupWritePart

Bool syBupWritePart(Sint32 partId, void *src)

Saves 4080 bytes from src as part partId: writes the new copy with the next counter, then invalidates the old one, updating the save state's sector tables.

References BupHeaderTemplate, Sint32, state, syBupChecksumSector(), syBupFindLatestSector(), syBupGetLatestCounter(), syBupInvalidateSector(), syBupSelectWriteSector() and syBupWriteSector().

Referenced by mnSaveCopyFile(), mnSaveToChosenFile(), mnSaveWriteCurrentFile(), mnSaveWriteFile() and mnSaveWriteLinkTallies().

Definition at line 188 of file system_bup.c.

syBupReadPart

Uint32 syBupReadPart(Sint32 partId, void *dest)

Loads the current copy of part partId (4080 bytes) into dest. Returns 1 when there is none and 0 otherwise.

References Sint32, state, syBupFindLatestSector() and syBupReadSector().

Referenced by mnSaveChooseAndLoadFile(), mnSaveCopyFile() and mnSaveWriteLinkTallies().

Definition at line 246 of file system_bup.c.

syBupDeletePart

Uint32 syBupDeletePart(Sint32 partId)

Invalidates the current copy of part partId. Called when the file list erases a file.

References Sint32, syBupFindLatestSector() and syBupInvalidateSector().

Referenced by mnSaveEraseSelectedFile().

Definition at line 265 of file system_bup.c.

syBupChecksumSector

Sint32 syBupChecksumSector(void)

Sums the first BUP_CHECKSUM_BYTES payload bytes of the save state's sector buffer; the final eight payload bytes are excluded.

References Sint32 and state.

Referenced by syBupReadSector() and syBupWritePart().

Definition at line 283 of file system_bup.c.

syBupFindLatestSector

Uint32 syBupFindLatestSector(Sint32 partId)

Returns the occupied sector holding the newest copy of part partId, or BUP_SECTOR_COUNT.

References Sint32 and state.

Referenced by syBupDeletePart(), syBupLoadFileSummaries(), syBupReadPart(), syBupSelectWriteSector() and syBupWritePart().

Definition at line 309 of file system_bup.c.

syBupInvalidateSector

Bool syBupInvalidateSector(Sint32 index)

Overwrites sector index with a header for no part (BUP_PART_NONE) and clears its occupied entry. Returns 0 on success and 1 on failure.

References BupHeaderTemplate, Sint32, state and syBupWriteSector().

Referenced by syBupDeletePart() and syBupWritePart().

Definition at line 335 of file system_bup.c.

syBupCompareBytes

Sint32 syBupCompareBytes(Uint8 *left, Uint8 *right, Sint32 count)

Compares count bytes, as memcmp does.

References Sint32.

Referenced by syBupInitState().

Definition at line 358 of file system_bup.c.

syBupGetLatestCounter

Uint32 syBupGetLatestCounter(Sint32 partId)

Returns the highest counter among the copies of part partId.

References Sint32 and state.

Referenced by syBupWritePart().

Definition at line 375 of file system_bup.c.

syBupLoadFileSummaries

Sint32 syBupLoadFileSummaries(void)

Reads the summary at the start of each of the three save files into the save state's summary table for the file list, and each file's second copy of the play time, from BUP_SECOND_PLAY_TIME_OFFSET in its second part, into its secondPartPlayTime (zero when it has none).

References Sint32, state and syBupFindLatestSector().

Referenced by mnSaveChooseAndLoadFile(), mnSaveCopyFile(), mnSaveCountFilesExcludingSent(), mnSaveEraseSelectedFile(), mnSaveScanFileSummaries() and mnSaveToChosenFile().

Definition at line 398 of file system_bup.c.

syBupReleaseState

Uint32 syBupReleaseState(void)

Removes the flash timer handler and frees the save state's heap slot, with every block allocated after it in the same pool.

References syHeapSlotRelease() and syIntFunction().

Referenced by mnSaveChooseAndLoadFile(), mnSaveCopyFile(), mnSaveCountFilesExcludingSent(), mnSaveEraseSelectedFile(), mnSaveScanFileSummaries(), mnSaveToChosenFile(), mnSaveWriteCurrentFile(), mnSaveWriteFile() and mnSaveWriteLinkTallies().

Definition at line 436 of file system_bup.c.