Alchemy
Loading...
Searching...
No Matches

save_state.h

Go to the documentation of this file.
1#ifndef GUARD_SAVE_STATE_H
2#define GUARD_SAVE_STATE_H
3
4/// @file
5/// The save workspace: slot headers and summaries.
6
9#include "types.h"
10#include "static_assert.h"
11#include "dma.h"
12#include "global_cells.h"
13#include "fixed_math.h"
14#include "flash.h"
15
16/// The header of a save sector in flash: its signature, the record it holds,
17/// its checksum and its sequence number.
19 u8 signature[7]; ///< `CAMELOT`, unterminated
20 /// File n's records are n and n + 3 (n from 0 to 2); 16 marks a free sector.
22 u16 checksum; ///< low half of the payload's checksum
23 u16 sequence; ///< the highest is the current copy; restarts after 65000
24 u8 reserved[4]; ///< not named by TBS code
25};
26
27/// A save file's summary for the file screen: the leader's name, level and
28/// class, the area, play time, coins, Djinn and party.
30 u8 slotHeader[16]; ///< room for the record's SaveSlotHeader
31 u8 name[12]; ///< the leader's
32 u8 level; ///< the leader's; 0 for an empty file
33 u8 classId; ///< the leader's
34 u16 area; ///< area name number (Scene_GetAreaNameIndex)
35 u32 playTime; ///< frames
37 u8 djinn[4]; ///< the party's Djinn of each element
38 s8 party[5]; ///< up to four party members, ended by -1
39 /// Copied from GameState.unk20f, which saving at the end of the game sets;
40 /// the file menu for cleared data lists a file without it as not cleared.
42 s8 flagCount; ///< how many of game flags 48 to 127 are set
43 u8 hasFlag20; ///< FLAG_CHILDHOOD is set; remaps the party's sprites
44 /// bits: 15..8 paletteGlowSub | 7..0 paletteGlow
45 ///@{
46 u16 paletteGlow : 8; ///< GameState.paletteGlow[0]
47 u16 paletteGlowSub : 8; ///< GameState.paletteGlow[1]
48 ///@}
49 /// Low half of GameState.buildStamp; a file below the current stamp is
50 /// listed as from an older build.
52 /// Filled on reading: the play time from the file's second record. A file
53 /// whose playTime differs is listed as corrupted.
55 u32 checksum; ///< the sum of the 968 words from gGameFlagBytes
56};
57
58/// The save work: which of the 16 flash sectors hold a record, their record
59/// ids and sequence numbers, one sector's buffer and the three files'
60/// summaries.
62 u8 occupied[16]; ///< 1 for a sector holding the current copy of a record
63 u8 recordId[16]; ///< each sector's record; 16 for none
64 u16 sequence[16]; ///< each sector's sequence number
65 union {
66 struct {
68 u8 payload[0xff0];
69 } record;
70 u8 bytes[0x1000];
71 } slot; ///< one sector, read from or written to flash
72 struct SaveSummary summary[3]; ///< each file's, read from its first record
73};
74
75STATIC_ASSERT(sizeof(struct SaveSummary) == 64);
79STATIC_ASSERT(offsetof(struct SaveWorkspace, slot) == 0x40);
80STATIC_ASSERT(offsetof(struct SaveWorkspace, summary) == 0x1040);
81
82STATIC_ASSERT(sizeof(struct SaveSlotHeader) == 0x10);
83STATIC_ASSERT(sizeof(struct SaveWorkspace) == 0x1100);
84STATIC_ASSERT(offsetof(struct SaveWorkspace, recordId) == 0x10);
85STATIC_ASSERT(offsetof(struct SaveWorkspace, sequence) == 0x20);
88
89extern struct SaveWorkspace *gSaveWorkspace;
90
91#define DMA3_BUSY 0x80000000
92
93#define START_DMA(source_, destination_, control_) \
94 Dma_Set((const void *)(source_), (void *)(destination_), (control_), REG_DMA3)
95
96static __inline__ void WaitDma(struct DmaRegisters volatile *channel) {
97 while ((channel->control & DMA3_BUSY) != 0) {
98 }
99}
100
101#define WAIT_DMA() WaitDma(DMA3)
102
104
105#endif
DMA channel registers, and the DMA and CPU fills.
16.16 fixed-point constants, sine and cosine, and the library division.
Flash memory: erasing, programming and reading sectors.
The work slots of gWorkSlot, read through their own names.
struct SaveWorkspace * gSaveWorkspace
static __inline__ void WaitDma(struct DmaRegisters volatile *channel)
Definition save_state.h:96
s16 gTitleSendOptionEnabled
#define DMA3_BUSY
Definition save_state.h:91
STATIC_ASSERT, a compile-time check that emits nothing.
#define STATIC_ASSERT(condition)
C89 permits repeated extern declarations of the same array.
#define offsetof(type, member)
Definition stddef.h:19
A DMA channel's registers (DMAxSAD, DMAxDAD, DMAxCNT).
Definition dma.h:11
u32 control
The count in the low half, DMAxCNT_H's flags above it.
Definition dma.h:14
The header of a save sector in flash: its signature, the record it holds, its checksum and its sequen...
Definition save_state.h:18
u8 signature[7]
CAMELOT, unterminated
Definition save_state.h:19
u16 checksum
low half of the payload's checksum
Definition save_state.h:22
u8 reserved[4]
not named by TBS code
Definition save_state.h:24
u16 sequence
the highest is the current copy; restarts after 65000
Definition save_state.h:23
u8 recordId
File n's records are n and n + 3 (n from 0 to 2); 16 marks a free sector.
Definition save_state.h:21
A save file's summary for the file screen: the leader's name, level and class, the area,...
Definition save_state.h:29
u16 paletteGlow
bits: 15..8 paletteGlowSub | 7..0 paletteGlow
Definition save_state.h:46
u8 classId
the leader's
Definition save_state.h:33
u8 name[12]
the leader's
Definition save_state.h:31
u8 unk38[4]
Filled on reading: the play time from the file's second record.
Definition save_state.h:54
u8 hasFlag20
FLAG_CHILDHOOD is set; remaps the party's sprites.
Definition save_state.h:43
u16 frames
Low half of GameState.buildStamp; a file below the current stamp is listed as from an older build.
Definition save_state.h:51
u32 checksum
the sum of the 968 words from gGameFlagBytes
Definition save_state.h:55
s8 sendFlag
Copied from GameState.unk20f, which saving at the end of the game sets; the file menu for cleared dat...
Definition save_state.h:41
u8 djinn[4]
the party's Djinn of each element
Definition save_state.h:37
u8 slotHeader[16]
room for the record's SaveSlotHeader
Definition save_state.h:30
u32 playTime
frames
Definition save_state.h:35
u8 level
the leader's; 0 for an empty file
Definition save_state.h:32
s8 flagCount
how many of game flags 48 to 127 are set
Definition save_state.h:42
u16 paletteGlowSub
GameState.paletteGlow[1].
Definition save_state.h:47
u16 area
area name number (Scene_GetAreaNameIndex)
Definition save_state.h:34
s8 party[5]
up to four party members, ended by -1
Definition save_state.h:38
The save work: which of the 16 flash sectors hold a record, their record ids and sequence numbers,...
Definition save_state.h:61
struct SaveSummary summary[3]
each file's, read from its first record
Definition save_state.h:72
u8 recordId[16]
each sector's record; 16 for none
Definition save_state.h:63
u8 occupied[16]
1 for a sector holding the current copy of a record
Definition save_state.h:62
u16 sequence[16]
each sector's sequence number
Definition save_state.h:64
union SaveWorkspace::@331263107057156204162071305274032234046256201132 slot
one sector, read from or written to flash
The integer types, TRUE, UNUSED and the fixed-address macros.
unsigned int u32
Definition types.h:15
signed char s8
Definition types.h:10
signed short s16
Definition types.h:12
unsigned short u16
Definition types.h:13
unsigned char u8
Definition types.h:11
struct SaveWorkspace::@331263107057156204162071305274032234046256201132::@306255313117204121245047232313241013312013006065 record