Super Mario 64 Source
A Super Mario 64 decompilation, brought to you by a bunch of clever folks.
Macros | Functions | Variables
save_file.c File Reference
#include <ultra64.h>
#include "sm64.h"
#include "game.h"
#include "main.h"
#include "engine/math_util.h"
#include "area.h"
#include "level_update.h"
#include "save_file.h"
#include "sound_init.h"

Macros

#define MENU_DATA_MAGIC   0x4849
 
#define SAVE_FILE_MAGIC   0x4441
 

Functions

 STATIC_ASSERT (sizeof(struct SaveBuffer)==EEPROM_SIZE, "eeprom buffer size must match")
 
 STATIC_ASSERT (ARRAY_COUNT(gLevelToCourseNumTable)==LEVEL_COUNT - 1, "change this array if you are adding levels")
 
static void no_op (void)
 
static s32 read_eeprom_data (void *buffer, s32 size)
 Read from EEPROM to a given address. More...
 
static s32 write_eeprom_data (void *buffer, s32 size)
 Write data to EEPROM. More...
 
static s32 calc_checksum (u8 *data, s32 size)
 Sum the bytes in data to data + size - 2. More...
 
static s32 verify_save_block_signature (void *buffer, s32 size, u16 magic)
 Verify the signature at the end of the block to check if the data is valid. More...
 
static void add_save_block_signature (void *buffer, s32 size, u16 magic)
 Write a signature at the end of the block to make sure the data is valid. More...
 
static void restore_main_menu_data (s32 srcSlot)
 Copy main menu data from one backup slot to the other slot. More...
 
static void save_main_menu_data (void)
 
static void wipe_main_menu_data (void)
 
static s32 get_coin_score_age (s32 fileIndex, s32 courseIndex)
 
static void set_coin_score_age (s32 fileIndex, s32 courseIndex, s32 age)
 
static void touch_coin_score_age (s32 fileIndex, s32 courseIndex)
 Mark a coin score for a save file as the newest out of all save files. More...
 
static void touch_high_score_ages (s32 fileIndex)
 Mark all coin scores for a save file as new. More...
 
static void restore_save_file_data (s32 fileIndex, s32 srcSlot)
 Copy save file data from one backup slot to the other slot. More...
 
void save_file_do_save (s32 fileIndex)
 
void save_file_erase (s32 fileIndex)
 
void save_file_copy (s32 srcFileIndex, s32 destFileIndex)
 
void save_file_load_all (void)
 
void save_file_reload (void)
 Reload the current save file from its backup copy, which is effectively a a cached copy of what has been written to EEPROM. More...
 
void save_file_collect_star_or_key (s16 coinScore, s16 starIndex)
 Update the current save file after collecting a star or a key. More...
 
s32 save_file_exists (s32 fileIndex)
 
u32 save_file_get_max_coin_score (s32 courseIndex)
 Get the maximum coin score across all files for a course. More...
 
s32 save_file_get_course_star_count (s32 fileIndex, s32 courseIndex)
 
s32 save_file_get_total_star_count (s32 fileIndex, s32 minCourse, s32 maxCourse)
 
void save_file_set_flags (s32 flags)
 
void save_file_clear_flags (s32 flags)
 
s32 save_file_get_flags (void)
 
s32 save_file_get_star_flags (s32 fileIndex, s32 courseIndex)
 Return the bitset of obtained stars in the specified course. More...
 
void save_file_set_star_flags (s32 fileIndex, s32 courseIndex, s32 starFlags)
 Add to the bitset of obtained stars in the specified course. More...
 
s32 save_file_get_course_coin_score (s32 fileIndex, s32 courseIndex)
 
s32 save_file_is_cannon_unlocked (void)
 Return TRUE if the cannon is unlocked in the current course. More...
 
void save_file_set_cannon_unlocked (void)
 Sets the cannon status to unlocked in the current course. More...
 
void save_file_set_cap_pos (s16 x, s16 y, s16 z)
 
s32 save_file_get_cap_pos (Vec3s capPos)
 
void save_file_set_sound_mode (u16 mode)
 
u16 save_file_get_sound_mode (void)
 
void save_file_move_cap_to_default_location (void)
 
void disable_warp_checkpoint (void)
 
void check_if_should_set_warp_checkpoint (struct WarpNode *warpNode)
 Checks the upper bit of the WarpNode->destLevel byte to see if the game should set a warp checkpoint. More...
 
s32 check_warp_checkpoint (struct WarpNode *warpNode)
 Checks to see if a checkpoint is properly active or not. More...
 

Variables

struct SaveBuffer gSaveBuffer
 
struct WarpCheckpoint gWarpCheckpoint
 
s8 gMainMenuDataModified
 
s8 gSaveFileModified
 
u8 gLastCompletedCourseNum = 0
 
u8 gLastCompletedStarNum = 0
 
s8 sUnusedGotGlobalCoinHiScore = 0
 
u8 gGotFileCoinHiScore = 0
 
u8 gCurrCourseStarFlags = 0
 
u8 gSpecialTripleJump = 0
 
s8 gLevelToCourseNumTable []
 

Macro Definition Documentation

◆ MENU_DATA_MAGIC

#define MENU_DATA_MAGIC   0x4849

◆ SAVE_FILE_MAGIC

#define SAVE_FILE_MAGIC   0x4441

Function Documentation

◆ add_save_block_signature()

static void add_save_block_signature ( void buffer,
s32  size,
u16  magic 
)
static

Write a signature at the end of the block to make sure the data is valid.

◆ calc_checksum()

static s32 calc_checksum ( u8 data,
s32  size 
)
static

Sum the bytes in data to data + size - 2.

The last two bytes are ignored because that is where the checksum is stored.

◆ check_if_should_set_warp_checkpoint()

void check_if_should_set_warp_checkpoint ( struct WarpNode warpNode)

Checks the upper bit of the WarpNode->destLevel byte to see if the game should set a warp checkpoint.

◆ check_warp_checkpoint()

s32 check_warp_checkpoint ( struct WarpNode warpNode)

Checks to see if a checkpoint is properly active or not.

This will also update the level, area, and destination node of the input WarpNode. returns TRUE if input WarpNode was updated, and FALSE if not.

◆ disable_warp_checkpoint()

void disable_warp_checkpoint ( void  )

◆ get_coin_score_age()

static s32 get_coin_score_age ( s32  fileIndex,
s32  courseIndex 
)
static

◆ no_op()

static void no_op ( void  )
static

◆ read_eeprom_data()

static s32 read_eeprom_data ( void buffer,
s32  size 
)
static

Read from EEPROM to a given address.

The EEPROM address is computed using the offset of the destination address from gSaveBuffer. Try at most 4 times, and return 0 on success. On failure, return the status returned from osEepromLongRead. It also returns 0 if EEPROM isn't loaded correctly in the system.

◆ restore_main_menu_data()

static void restore_main_menu_data ( s32  srcSlot)
static

Copy main menu data from one backup slot to the other slot.

◆ restore_save_file_data()

static void restore_save_file_data ( s32  fileIndex,
s32  srcSlot 
)
static

Copy save file data from one backup slot to the other slot.

◆ save_file_clear_flags()

void save_file_clear_flags ( s32  flags)

◆ save_file_collect_star_or_key()

void save_file_collect_star_or_key ( s16  coinScore,
s16  starIndex 
)

Update the current save file after collecting a star or a key.

If coin score is greater than the current high score, update it.

Compares the coin score as a 16 bit value, but only writes the 8 bit

◆ save_file_copy()

void save_file_copy ( s32  srcFileIndex,
s32  destFileIndex 
)

◆ save_file_do_save()

void save_file_do_save ( s32  fileIndex)

◆ save_file_erase()

void save_file_erase ( s32  fileIndex)

◆ save_file_exists()

s32 save_file_exists ( s32  fileIndex)

◆ save_file_get_cap_pos()

s32 save_file_get_cap_pos ( Vec3s  capPos)

◆ save_file_get_course_coin_score()

s32 save_file_get_course_coin_score ( s32  fileIndex,
s32  courseIndex 
)

◆ save_file_get_course_star_count()

s32 save_file_get_course_star_count ( s32  fileIndex,
s32  courseIndex 
)

◆ save_file_get_flags()

s32 save_file_get_flags ( void  )

◆ save_file_get_max_coin_score()

u32 save_file_get_max_coin_score ( s32  courseIndex)

Get the maximum coin score across all files for a course.

The lower 16 bits of the returned value are the score, and the upper 16 bits are the file number of the save file with this score.

◆ save_file_get_sound_mode()

u16 save_file_get_sound_mode ( void  )

◆ save_file_get_star_flags()

s32 save_file_get_star_flags ( s32  fileIndex,
s32  courseIndex 
)

Return the bitset of obtained stars in the specified course.

If course is -1, return the bitset of obtained castle secret stars.

◆ save_file_get_total_star_count()

s32 save_file_get_total_star_count ( s32  fileIndex,
s32  minCourse,
s32  maxCourse 
)

◆ save_file_is_cannon_unlocked()

s32 save_file_is_cannon_unlocked ( void  )

Return TRUE if the cannon is unlocked in the current course.

◆ save_file_load_all()

void save_file_load_all ( void  )

◆ save_file_move_cap_to_default_location()

void save_file_move_cap_to_default_location ( void  )

◆ save_file_reload()

void save_file_reload ( void  )

Reload the current save file from its backup copy, which is effectively a a cached copy of what has been written to EEPROM.

This is used after getting a game over.

◆ save_file_set_cannon_unlocked()

void save_file_set_cannon_unlocked ( void  )

Sets the cannon status to unlocked in the current course.

◆ save_file_set_cap_pos()

void save_file_set_cap_pos ( s16  x,
s16  y,
s16  z 
)

◆ save_file_set_flags()

void save_file_set_flags ( s32  flags)

◆ save_file_set_sound_mode()

void save_file_set_sound_mode ( u16  mode)

◆ save_file_set_star_flags()

void save_file_set_star_flags ( s32  fileIndex,
s32  courseIndex,
s32  starFlags 
)

Add to the bitset of obtained stars in the specified course.

If course is -1, add ot the bitset of obtained castle secret stars.

◆ save_main_menu_data()

static void save_main_menu_data ( void  )
static

◆ set_coin_score_age()

static void set_coin_score_age ( s32  fileIndex,
s32  courseIndex,
s32  age 
)
static

◆ STATIC_ASSERT() [1/2]

STATIC_ASSERT ( sizeof(struct SaveBuffer = =EEPROM_SIZE,
"eeprom buffer size must match"   
)

◆ STATIC_ASSERT() [2/2]

STATIC_ASSERT ( ARRAY_COUNT(gLevelToCourseNumTable = =LEVEL_COUNT - 1,
"change this array if you are adding levels"   
)

◆ touch_coin_score_age()

static void touch_coin_score_age ( s32  fileIndex,
s32  courseIndex 
)
static

Mark a coin score for a save file as the newest out of all save files.

◆ touch_high_score_ages()

static void touch_high_score_ages ( s32  fileIndex)
static

Mark all coin scores for a save file as new.

◆ verify_save_block_signature()

static s32 verify_save_block_signature ( void buffer,
s32  size,
u16  magic 
)
static

Verify the signature at the end of the block to check if the data is valid.

◆ wipe_main_menu_data()

static void wipe_main_menu_data ( void  )
static

◆ write_eeprom_data()

static s32 write_eeprom_data ( void buffer,
s32  size 
)
static

Write data to EEPROM.

The EEPROM address is computed using the offset of the source address from gSaveBuffer. Try at most 4 times, and return 0 on success. On failure, return the status returned from osEepromLongWrite. Unlike read_eeprom_data, return 1 if EEPROM isn't loaded.

Variable Documentation

◆ gCurrCourseStarFlags

u8 gCurrCourseStarFlags = 0

◆ gGotFileCoinHiScore

u8 gGotFileCoinHiScore = 0

◆ gLastCompletedCourseNum

u8 gLastCompletedCourseNum = 0

◆ gLastCompletedStarNum

u8 gLastCompletedStarNum = 0

◆ gLevelToCourseNumTable

s8 gLevelToCourseNumTable[]

◆ gMainMenuDataModified

s8 gMainMenuDataModified

◆ gSaveBuffer

struct SaveBuffer gSaveBuffer

◆ gSaveFileModified

s8 gSaveFileModified

◆ gSpecialTripleJump

u8 gSpecialTripleJump = 0

◆ gWarpCheckpoint

struct WarpCheckpoint gWarpCheckpoint

◆ sUnusedGotGlobalCoinHiScore

s8 sUnusedGotGlobalCoinHiScore = 0