Skip to Content
The Alchemy Project

src/system/system_bup.c

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

Included by system.c.

Macros

NameDescription
#defineBUP_START_DMA (source_, destination_, control_)Starts DMA 3 copying from source_ to destination_ with control_.
#defineWAIT_DMA () syDMAWait(DMA3)Waits until a BUP_START_DMA transfer is done.
#defineBUP_HEADER_BYTES sizeof(BUP_SECTOR_HEADER)The bytes of a part's first sector that its BUP_SECTOR_HEADER takes, before the payload.
#defineBUP_SECOND_PLAY_TIME_OFFSET BUP_HEADER_BYTESWhere a file's second copy of the play time lies in its part's second sector: mnSaveBuildFileSummary writes it to the first word of SceneLocalState, which opens the save image's second sector, after the part's header.
#defineBUP_BROKEN_SEAL_SECOND_PLAY_TIME_OFFSET (BUP_HEADER_BYTES + 256)Where The Broken Seal reads that copy, 256 bytes further into SceneLocalState: syBupLoadExtraSummary reads part BUP_PART_EXTRA's there.

Functions

NameDescription
static __inline__ voidsyDMAWait (DMA_REGISTERS volatile *channel)Waits until channel's transfer is done.
BoolsyBupInitState (void)Allocates the save workspace (heap slot HEAP_SLOT_BUP), installs the BUP_FLASH_TIMER handler of the SDK's flash library, identifies the flash chip (BUP_FLASH_IDENTIFY_ATTEMPTS tries, a tick apart) and reads each part's place, three sectors at a time, into the workspace's occupied, partId and counter tables: a place 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 place, three sectors from a multiple of three, to write part partId to, at random among them.
Uint32syBupWriteSector (Sint32 sector)Programs the workspace's three-sector buffer into flash from sector sector on and verifies each, through the SDK's flash library's function for this chip, calling the progress function before the first and after each.
Sint32syBupReadSector (Sint32 index)Reads the three flash sectors from index on into the workspace's sector buffer.
Sint32syBupWritePart (Sint32 partId, void *src)Saves the 12,272 bytes at src, three sectors less the header, as part partId: writes the new copy with the next counter, then invalidates the old one, updating the workspace's sector tables.
Uint32syBupReadPart (Sint32 partId, void *dest)Loads the current copy of part partId (12,272 bytes) into dest.
Uint32syBupDeletePart (Sint32 partId)Invalidates the current copy of part partId.
Sint32syBupChecksumSector (void)Sums the 12,272 payload bytes of the workspace's three-sector buffer, the checksum its header keeps.
Uint32syBupFindLatestSector (Sint32 partId)Returns the first sector of the newest copy of part partId, or BUP_SECTOR_COUNT when it has none.
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 of the latest save in each of the BUP_FILE_COUNT files, with its second play time from its next sector, clearing it first; returns how many files have one.
BoolsyBupLoadExtraSummary (void)Unused: reads part BUP_PART_EXTRA's summary into the extra summary of the save's state, with its second play time from its next sector; returns 1, or 0 when the part has no copy.
voidsyBupReleaseState (void)Removes the flash timer handler and frees the save workspace's heap slot, with every block allocated after it in the same pool.
voidsyBupSetProgressCallback (void (*progress)(Sint32 step))Sets the function the save calls between sector writes (0 for none) and starts its count again at 0 of BUP_SECTORS_PER_PART; does nothing while no save workspace (heap slot HEAP_SLOT_BUP) is allocated.

Description

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

Macros in detail

BUP_START_DMA

#define BUP_START_DMA(source_, destination_, control_)

Starts DMA 3 copying from source_ to destination_ with control_.

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

Definition at line 4 of file system_bup.c.

WAIT_DMA

#define WAIT_DMA() syDMAWait(DMA3)

Waits until a BUP_START_DMA transfer is done.

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

Definition at line 17 of file system_bup.c.

BUP_HEADER_BYTES

#define BUP_HEADER_BYTES sizeof(BUP_SECTOR_HEADER)

The bytes of a part's first sector that its BUP_SECTOR_HEADER takes, before the payload.

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

Definition at line 21 of file system_bup.c.

BUP_SECOND_PLAY_TIME_OFFSET

#define BUP_SECOND_PLAY_TIME_OFFSET BUP_HEADER_BYTES

Where a file's second copy of the play time lies in its part's second sector: mnSaveBuildFileSummary writes it to the first word of SceneLocalState, which opens the save image's second sector, after the part's header.

Referenced by syBupLoadFileSummaries().

Definition at line 26 of file system_bup.c.

BUP_BROKEN_SEAL_SECOND_PLAY_TIME_OFFSET

#define BUP_BROKEN_SEAL_SECOND_PLAY_TIME_OFFSET (BUP_HEADER_BYTES + 256)

Where The Broken Seal reads that copy, 256 bytes further into SceneLocalState: syBupLoadExtraSummary reads part BUP_PART_EXTRA's there.

Referenced by syBupLoadExtraSummary().

Definition at line 29 of file system_bup.c.

Functions in detail

syDMAWait

static __inline__ void syDMAWait(DMA_REGISTERS volatile *channel)

Waits until channel's transfer is done.

Definition at line 9 of file system_bup.c.

syBupInitState

Bool syBupInitState(void)

Allocates the save workspace (heap slot HEAP_SLOT_BUP), installs the BUP_FLASH_TIMER handler of the SDK's flash library, identifies the flash chip (BUP_FLASH_IDENTIFY_ATTEMPTS tries, a tick apart) and reads each part's place, three sectors at a time, into the workspace's occupied, partId and counter tables: a place counts as taken when its signature and checksum are good, and of two copies of a part only the newer stays taken. Returns 1 when no flash chip answers, else 0.

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

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

Definition at line 38 of file system_bup.c.

syBupSelectWriteSector

Uint32 syBupSelectWriteSector(Sint32 partId)

Picks the free place, three sectors from a multiple of three, to write part partId to, at random among them. The last free place only goes to a part that already has a copy, so replacing a save always has room. Returns BUP_SECTOR_COUNT when there is no place to use.

References Sint32, syBupFindLatestSector() and syRandom().

Referenced by syBupWritePart().

Definition at line 110 of file system_bup.c.

syBupWriteSector

Uint32 syBupWriteSector(Sint32 sector)

Programs the workspace's three-sector buffer into flash from sector sector on and verifies each, through the SDK's flash library's function for this chip, calling the progress function before the first and after each. Returns 1 when a sector fails, else 0.

References Sint32.

Referenced by syBupInvalidateSector() and syBupWritePart().

Definition at line 150 of file system_bup.c.

syBupReadSector

Sint32 syBupReadSector(Sint32 index)

Reads the three flash sectors from index on into the workspace's sector buffer. Returns 0 when their checksum matches the header's.

References Sint32 and syBupChecksumSector().

Referenced by syBupInitState() and syBupReadPart().

Definition at line 179 of file system_bup.c.

syBupWritePart

Sint32 syBupWritePart(Sint32 partId, void *src)

Saves the 12,272 bytes at src, three sectors less the header, as part partId: writes the new copy with the next counter, then invalidates the old one, updating the workspace's sector tables. Returns 0, or the step that failed: 1 no free place, 2 the new copy, 3 invalidating the old one, and, once the counter passes BUP_COUNTER_LIMIT and the copy is written again with counter 1 over the old place, 4 that write and 5 invalidating the new copy.

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

Referenced by mnSaveWritePartMasked().

Definition at line 198 of file system_bup.c.

syBupReadPart

Uint32 syBupReadPart(Sint32 partId, void *dest)

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

References Sint32, syBupFindLatestSector() and syBupReadSector().

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

Definition at line 254 of file system_bup.c.

syBupDeletePart

Uint32 syBupDeletePart(Sint32 partId)

Invalidates the current copy of part partId. Nothing calls it.

References Sint32, syBupFindLatestSector() and syBupInvalidateSector().

Referenced by mnSaveDeletePartMasked().

Definition at line 271 of file system_bup.c.

syBupChecksumSector

Sint32 syBupChecksumSector(void)

Sums the 12,272 payload bytes of the workspace's three-sector buffer, the checksum its header keeps.

References Sint32.

Referenced by syBupReadSector() and syBupWritePart().

Definition at line 288 of file system_bup.c.

syBupFindLatestSector

Uint32 syBupFindLatestSector(Sint32 partId)

Returns the first sector of the newest copy of part partId, or BUP_SECTOR_COUNT when it has none.

References Sint32.

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

Definition at line 314 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 and syBupWriteSector().

Referenced by syBupDeletePart() and syBupWritePart().

Definition at line 339 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 365 of file system_bup.c.

syBupGetLatestCounter

Uint32 syBupGetLatestCounter(Sint32 partId)

Returns the highest counter among the copies of part partId.

References Sint32.

Referenced by syBupWritePart().

Definition at line 382 of file system_bup.c.

syBupLoadFileSummaries

Sint32 syBupLoadFileSummaries(void)

Reads the summary of the latest save in each of the BUP_FILE_COUNT files, with its second play time from its next sector, clearing it first; returns how many files have one.

References Sint32 and syBupFindLatestSector().

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

Definition at line 403 of file system_bup.c.

syBupLoadExtraSummary

Bool syBupLoadExtraSummary(void)

Unused: reads part BUP_PART_EXTRA's summary into the extra summary of the save's state, with its second play time from its next sector; returns 1, or 0 when the part has no copy.

References syBupFindLatestSector().

Definition at line 435 of file system_bup.c.

syBupReleaseState

void syBupReleaseState(void)

Removes the flash timer handler and frees the save workspace'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(), mnSaveToFile(), mnSaveWriteFile() and mnSaveWriteLinkTallies().

Definition at line 462 of file system_bup.c.

syBupSetProgressCallback

void syBupSetProgressCallback(void (*progress)(Sint32 step))

Sets the function the save calls between sector writes (0 for none) and starts its count again at 0 of BUP_SECTORS_PER_PART; does nothing while no save workspace (heap slot HEAP_SLOT_BUP) is allocated.

References Sint32.

Referenced by mnSaveDeletePartMasked() and mnSaveWritePartMasked().

Definition at line 471 of file system_bup.c.