2008-08-06 03:09:59 +02:00
|
|
|
/****************************************************************************
|
2008-10-16 03:48:08 +02:00
|
|
|
* Snes9x 1.50
|
2008-08-06 03:09:59 +02:00
|
|
|
*
|
2008-10-16 03:48:08 +02:00
|
|
|
* Nintendo Gamecube Port
|
2008-08-06 03:09:59 +02:00
|
|
|
* softdev July 2006
|
|
|
|
* crunchy2 May-June 2007
|
|
|
|
*
|
2008-10-16 03:48:08 +02:00
|
|
|
* mcsave.cpp
|
2008-08-06 03:09:59 +02:00
|
|
|
*
|
2008-10-16 03:48:08 +02:00
|
|
|
* Memory Card Save Routines.
|
|
|
|
****************************************************************************/
|
2008-08-06 03:09:59 +02:00
|
|
|
#include <gccore.h>
|
|
|
|
#include <ogcsys.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-10-16 03:48:08 +02:00
|
|
|
|
|
|
|
#include "snes9x.h"
|
|
|
|
#include "memmap.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "cpuexec.h"
|
|
|
|
#include "ppu.h"
|
|
|
|
#include "apu.h"
|
|
|
|
#include "display.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "soundux.h"
|
|
|
|
#include "spc700.h"
|
|
|
|
#include "spc7110.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
|
|
|
|
#include "snes9xGX.h"
|
|
|
|
#include "video.h"
|
2008-10-16 03:48:08 +02:00
|
|
|
#include "ftfont.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
#include "s9xconfig.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "menu.h"
|
|
|
|
#include "sram.h"
|
|
|
|
#include "preferences.h"
|
2008-10-16 03:48:08 +02:00
|
|
|
#include "mcsave.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
|
|
|
|
#define VERIFBUFFERSIZE 65536
|
|
|
|
static u8 SysArea[CARD_WORKAREA] ATTRIBUTE_ALIGN (32);
|
2008-10-16 03:48:08 +02:00
|
|
|
unsigned char savebuffer[SAVEBUFFERSIZE] ATTRIBUTE_ALIGN (32);
|
2008-08-06 03:09:59 +02:00
|
|
|
unsigned char verifbuffer[VERIFBUFFERSIZE] ATTRIBUTE_ALIGN (32);
|
|
|
|
card_dir CardDir;
|
|
|
|
card_file CardFile;
|
|
|
|
card_stat CardStatus;
|
|
|
|
|
2008-10-16 03:48:08 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Clear the savebuffer
|
|
|
|
****************************************************************************/
|
|
|
|
void
|
|
|
|
ClearSaveBuffer ()
|
|
|
|
{
|
|
|
|
memset (savebuffer, 0, SAVEBUFFERSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* CardFileExists
|
|
|
|
*
|
|
|
|
* Wrapper to search through the files on the card.
|
|
|
|
* Returns TRUE if found.
|
2008-10-16 03:48:08 +02:00
|
|
|
****************************************************************************/
|
2008-08-06 03:09:59 +02:00
|
|
|
int
|
|
|
|
CardFileExists (char *filename, int slot)
|
|
|
|
{
|
2008-10-16 03:48:08 +02:00
|
|
|
int CardError;
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-10-16 03:48:08 +02:00
|
|
|
CardError = CARD_FindFirst (slot, &CardDir, TRUE);
|
|
|
|
while (CardError != CARD_ERROR_NOFILE)
|
|
|
|
{
|
|
|
|
CardError = CARD_FindNext (&CardDir);
|
2008-08-08 09:57:01 +02:00
|
|
|
|
2008-10-16 03:48:08 +02:00
|
|
|
if (strcmp ((char *) CardDir.filename, filename) == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
2008-08-08 09:57:01 +02:00
|
|
|
|
2008-10-16 03:48:08 +02:00
|
|
|
return 0;
|
2008-08-08 09:57:01 +02:00
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-08 09:57:01 +02:00
|
|
|
/****************************************************************************
|
2008-10-16 03:48:08 +02:00
|
|
|
* MountCard
|
2008-08-08 09:57:01 +02:00
|
|
|
*
|
2008-10-16 03:48:08 +02:00
|
|
|
* Mounts the memory card in the given slot. Error checking is done and
|
|
|
|
* workarounds implemented for when the mount fails.
|
|
|
|
* Returns the result of the last attempted CARD_Mount command.
|
|
|
|
****************************************************************************/
|
|
|
|
int MountCard(int cslot)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int tries;
|
|
|
|
|
|
|
|
EXI_ProbeReset();
|
|
|
|
|
|
|
|
/*** Mount the card ***/
|
|
|
|
ret = CARD_Mount (cslot, SysArea, NULL);
|
|
|
|
|
|
|
|
tries = 0;
|
|
|
|
while ( tries < 3 && ret == CARD_ERROR_IOERROR )
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
2008-10-16 03:48:08 +02:00
|
|
|
if (cslot == CARD_SLOTA)
|
|
|
|
WaitPrompt("Replug card in slot A!");
|
|
|
|
else
|
|
|
|
WaitPrompt("Replug card in slot B!");
|
|
|
|
|
|
|
|
ShowAction("");
|
|
|
|
ret = CARD_Mount (cslot, SysArea, NULL);
|
|
|
|
tries++;
|
2008-08-08 09:57:01 +02:00
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
|
|
|
tries = 0;
|
|
|
|
while ( tries < 5 && ret == CARD_ERROR_NOCARD )
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
2008-10-16 03:48:08 +02:00
|
|
|
ShowAction("Mounting card...");
|
|
|
|
CARD_Unmount (cslot);
|
|
|
|
usleep(500000); // wait half second
|
|
|
|
ShowAction("");
|
|
|
|
usleep(500000); // wait half second
|
|
|
|
ret = CARD_Mount (cslot, SysArea, NULL);
|
|
|
|
tries++;
|
2008-08-08 09:57:01 +02:00
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-10-16 03:48:08 +02:00
|
|
|
tries = 0;
|
|
|
|
while ( tries < 5 && ret == CARD_ERROR_UNLOCKED )
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2008-10-16 03:48:08 +02:00
|
|
|
ShowAction("Waiting for unlock...");
|
|
|
|
usleep(500000); // wait half second
|
|
|
|
ShowAction("");
|
|
|
|
usleep(500000); // wait half second
|
|
|
|
ret = CARD_Probe(cslot);
|
2008-08-06 03:09:59 +02:00
|
|
|
tries++;
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Verify Memory Card file against buffer
|
2008-10-16 03:48:08 +02:00
|
|
|
****************************************************************************/
|
2008-08-06 03:09:59 +02:00
|
|
|
int
|
|
|
|
VerifyMCFile (unsigned char *buf, int slot, char *filename, int datasize)
|
|
|
|
{
|
|
|
|
int CardError;
|
|
|
|
unsigned int blocks;
|
|
|
|
unsigned int SectorSize;
|
|
|
|
char msg[80];
|
|
|
|
int bytesleft = 0;
|
|
|
|
int bytesread = 0;
|
|
|
|
|
|
|
|
/*** Initialize Card System ***/
|
|
|
|
memset (SysArea, 0, CARD_WORKAREA);
|
|
|
|
CARD_Init ("SNES", "00");
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Try to mount the card ***/
|
2008-10-16 03:48:08 +02:00
|
|
|
CardError = MountCard(slot);
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (CardError == 0)
|
|
|
|
{
|
|
|
|
/*** Get Sector Size ***/
|
|
|
|
CARD_GetSectorSize (slot, &SectorSize);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (!CardFileExists (filename, slot))
|
|
|
|
{
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to open file for verify!");
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
memset (&CardFile, 0, sizeof (CardFile));
|
|
|
|
CardError = CARD_Open (slot, filename, &CardFile);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
blocks = CardFile.len;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (blocks < SectorSize)
|
|
|
|
blocks = SectorSize;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (blocks % SectorSize)
|
|
|
|
blocks += SectorSize;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (blocks > (unsigned int)datasize)
|
|
|
|
blocks = datasize;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
memset (verifbuffer, 0, VERIFBUFFERSIZE);
|
|
|
|
bytesleft = blocks;
|
|
|
|
bytesread = 0;
|
|
|
|
while (bytesleft > 0)
|
|
|
|
{
|
|
|
|
CARD_Read (&CardFile, verifbuffer, SectorSize, bytesread);
|
|
|
|
if ( memcmp (buf + bytesread, verifbuffer, (unsigned int)bytesleft < SectorSize ? bytesleft : SectorSize) )
|
|
|
|
{
|
|
|
|
CARD_Close (&CardFile);
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("File did not verify!");
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
bytesleft -= SectorSize;
|
|
|
|
bytesread += SectorSize;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
sprintf (msg, "Verified %d of %d bytes", bytesread, blocks);
|
|
|
|
ShowProgress (msg, bytesread, blocks);
|
|
|
|
}
|
|
|
|
CARD_Close (&CardFile);
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (slot == CARD_SLOTA)
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to Mount Slot A Memory Card!");
|
2008-08-06 03:09:59 +02:00
|
|
|
else
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to Mount Slot B Memory Card!");
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Load savebuffer from Memory Card file
|
2008-10-16 03:48:08 +02:00
|
|
|
****************************************************************************/
|
2008-08-06 03:09:59 +02:00
|
|
|
int
|
|
|
|
LoadBufferFromMC (unsigned char *buf, int slot, char *filename, bool8 silent)
|
|
|
|
{
|
|
|
|
int CardError;
|
|
|
|
unsigned int blocks;
|
|
|
|
unsigned int SectorSize;
|
2008-10-16 03:48:08 +02:00
|
|
|
char msg[80];
|
2008-08-06 03:09:59 +02:00
|
|
|
int bytesleft = 0;
|
|
|
|
int bytesread = 0;
|
|
|
|
|
|
|
|
/*** Initialize Card System ***/
|
|
|
|
memset (SysArea, 0, CARD_WORKAREA);
|
|
|
|
CARD_Init ("SNES", "00");
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Try to mount the card ***/
|
2008-10-16 03:48:08 +02:00
|
|
|
CardError = MountCard(slot);
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (CardError == 0)
|
|
|
|
{
|
|
|
|
/*** Get Sector Size ***/
|
|
|
|
CARD_GetSectorSize (slot, &SectorSize);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (!CardFileExists (filename, slot))
|
|
|
|
{
|
2008-10-16 03:48:08 +02:00
|
|
|
if ( !silent )
|
|
|
|
WaitPrompt ("Unable to open file");
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
memset (&CardFile, 0, sizeof (CardFile));
|
|
|
|
CardError = CARD_Open (slot, filename, &CardFile);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
blocks = CardFile.len;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (blocks < SectorSize)
|
|
|
|
blocks = SectorSize;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (blocks % SectorSize)
|
|
|
|
blocks += SectorSize;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
memset (buf, 0, 0x22000);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
bytesleft = blocks;
|
|
|
|
bytesread = 0;
|
|
|
|
while (bytesleft > 0)
|
|
|
|
{
|
|
|
|
CARD_Read (&CardFile, buf + bytesread, SectorSize, bytesread);
|
|
|
|
bytesleft -= SectorSize;
|
|
|
|
bytesread += SectorSize;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
|
|
|
sprintf (msg, "Read %d of %d bytes", bytesread, blocks);
|
|
|
|
ShowProgress (msg, bytesread, blocks);
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
CARD_Close (&CardFile);
|
|
|
|
CARD_Unmount (slot);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (slot == CARD_SLOTA)
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to Mount Slot A Memory Card!");
|
2008-08-06 03:09:59 +02:00
|
|
|
else
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to Mount Slot B Memory Card!");
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
return bytesread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Write savebuffer to Memory Card file
|
2008-10-16 03:48:08 +02:00
|
|
|
****************************************************************************/
|
2008-08-06 03:09:59 +02:00
|
|
|
int
|
|
|
|
SaveBufferToMC (unsigned char *buf, int slot, char *filename, int datasize, bool8 silent)
|
|
|
|
{
|
|
|
|
int CardError;
|
|
|
|
unsigned int blocks;
|
|
|
|
unsigned int SectorSize;
|
|
|
|
char msg[80];
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Initialize Card System ***/
|
|
|
|
memset (SysArea, 0, CARD_WORKAREA);
|
|
|
|
CARD_Init ("SNES", "00");
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Try to mount the card ***/
|
2008-10-16 03:48:08 +02:00
|
|
|
CardError = MountCard(slot);
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (CardError == 0)
|
|
|
|
{
|
|
|
|
/*** Get Sector Size ***/
|
|
|
|
CARD_GetSectorSize (slot, &SectorSize);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (datasize)
|
2008-10-16 03:48:08 +02:00
|
|
|
{
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Calculate number of blocks required ***/
|
|
|
|
blocks = (datasize / SectorSize) * SectorSize;
|
|
|
|
if (datasize % SectorSize)
|
|
|
|
blocks += SectorSize;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Does this file exist ? ***/
|
|
|
|
if (CardFileExists (filename, slot))
|
|
|
|
{
|
|
|
|
/*** Try to open the file ***/
|
|
|
|
CardError = CARD_Open (slot, filename, &CardFile);
|
|
|
|
if (CardError)
|
|
|
|
{
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to open card file!");
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
|
|
|
// if ( (s32)blocks < CardFile.len ) /*** new data is shorter ***/
|
|
|
|
// {
|
|
|
|
// CARD_Close (&CardFile);
|
|
|
|
//
|
|
|
|
// /*** Delete the existing longer file ***/
|
|
|
|
// CardError = CARD_Delete(slot, filename);
|
|
|
|
// if (CardError)
|
|
|
|
// {
|
|
|
|
// CARD_Unmount (slot);
|
|
|
|
// WaitPrompt ("Unable to delete existing file!");
|
|
|
|
// return 0;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// /*** Create new, shorter file ***/
|
|
|
|
// CardError = CARD_Create (slot, filename, blocks, &CardFile);
|
|
|
|
// if (CardError)
|
|
|
|
// {
|
|
|
|
// CARD_Unmount (slot);
|
|
|
|
// WaitPrompt ("Unable to create updated card file!");
|
|
|
|
// return 0;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if ( (s32)blocks > CardFile.len ) /*** new data is longer ***/
|
|
|
|
{
|
|
|
|
CARD_Close (&CardFile);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Try to create temp file to check available space ***/
|
|
|
|
CardError = CARD_Create (slot, "TEMPFILESNES9XGX201", blocks, &CardFile);
|
|
|
|
if (CardError)
|
|
|
|
{
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Not enough space to update file!");
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Delete the temporary file ***/
|
|
|
|
CARD_Close (&CardFile);
|
|
|
|
CardError = CARD_Delete(slot, "TEMPFILESNES9XGX201");
|
|
|
|
if (CardError)
|
|
|
|
{
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to delete temporary file!");
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Delete the existing shorter file ***/
|
|
|
|
CardError = CARD_Delete(slot, filename);
|
|
|
|
if (CardError)
|
|
|
|
{
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to delete existing file!");
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Create new, longer file ***/
|
|
|
|
CardError = CARD_Create (slot, filename, blocks, &CardFile);
|
|
|
|
if (CardError)
|
|
|
|
{
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to create updated card file!");
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /*** no file existed, create new one ***/
|
|
|
|
{
|
|
|
|
/*** Create new file ***/
|
|
|
|
CardError = CARD_Create (slot, filename, blocks, &CardFile);
|
|
|
|
if (CardError)
|
|
|
|
{
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
if ( CardError = CARD_ERROR_INSSPACE )
|
|
|
|
WaitPrompt ("Not enough space to create file!");
|
2008-08-06 03:09:59 +02:00
|
|
|
else
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to create card file!");
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** Now, have an open file handle, ready to send out the data ***/
|
|
|
|
CARD_GetStatus (slot, CardFile.filenum, &CardStatus);
|
|
|
|
CardStatus.icon_addr = 0x0;
|
|
|
|
CardStatus.icon_fmt = 2;
|
|
|
|
CardStatus.icon_speed = 1;
|
|
|
|
CardStatus.comment_addr = 2048;
|
|
|
|
CARD_SetStatus (slot, CardFile.filenum, &CardStatus);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
int byteswritten = 0;
|
|
|
|
int bytesleft = blocks;
|
|
|
|
while (bytesleft > 0)
|
|
|
|
{
|
|
|
|
CardError =
|
|
|
|
CARD_Write (&CardFile, buf + byteswritten,
|
|
|
|
SectorSize, byteswritten);
|
|
|
|
bytesleft -= SectorSize;
|
|
|
|
byteswritten += SectorSize;
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
sprintf (msg, "Wrote %d of %d bytes", byteswritten, blocks);
|
|
|
|
ShowProgress (msg, byteswritten, blocks);
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
CARD_Close (&CardFile);
|
|
|
|
CARD_Unmount (slot);
|
2008-10-16 03:48:08 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if ( GCSettings.VerifySaves )
|
|
|
|
{
|
|
|
|
/*** Verify the written file, but only up to the length we wrote
|
|
|
|
because the file could be longer due to past writes ***/
|
|
|
|
if ( VerifyMCFile (buf, slot, filename, byteswritten) )
|
|
|
|
return byteswritten;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return byteswritten;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ( !silent )
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("This game does not appear to use SRAM");
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (slot == CARD_SLOTA)
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to Mount Slot A Memory Card!");
|
2008-08-06 03:09:59 +02:00
|
|
|
else
|
2008-10-16 03:48:08 +02:00
|
|
|
WaitPrompt ("Unable to Mount Slot B Memory Card!");
|
|
|
|
|
2008-08-07 05:25:02 +02:00
|
|
|
return 0;
|
2008-08-06 03:09:59 +02:00
|
|
|
|
|
|
|
}
|
2008-10-16 03:48:08 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Memory Card SRAM Load
|
|
|
|
****************************************************************************/
|
|
|
|
void
|
|
|
|
LoadSRAMFromMC (int slot, int silent)
|
|
|
|
{
|
|
|
|
char filename[128];
|
|
|
|
|
|
|
|
ShowAction ("Loading SRAM from MC...");
|
|
|
|
|
|
|
|
/*** Build SRAM filename ***/
|
|
|
|
sprintf (filename, "%s.srm", Memory.ROMName);
|
|
|
|
|
|
|
|
int offset = LoadBufferFromMC (savebuffer, slot, filename, silent);
|
|
|
|
|
|
|
|
if ( offset > 0 )
|
|
|
|
{
|
|
|
|
decodesavedata (offset);
|
|
|
|
if ( !silent )
|
|
|
|
{
|
|
|
|
sprintf (filename, "Loaded %d bytes", offset);
|
|
|
|
WaitPrompt (filename);
|
|
|
|
}
|
|
|
|
S9xSoftReset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Memory Card SRAM Save
|
|
|
|
****************************************************************************/
|
|
|
|
void
|
|
|
|
SaveSRAMToMC (int slot, int silent)
|
|
|
|
{
|
|
|
|
char filename[128];
|
|
|
|
int datasize;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
ShowAction ("Saving SRAM to MC...");
|
|
|
|
|
|
|
|
/*** Build SRAM filename ***/
|
|
|
|
sprintf (filename, "%s.srm", Memory.ROMName);
|
|
|
|
|
|
|
|
datasize = prepareMCsavedata ();
|
|
|
|
offset = SaveBufferToMC (savebuffer, slot, filename, datasize, silent);
|
|
|
|
|
|
|
|
if ( (offset > 0) && (!silent))
|
|
|
|
{
|
|
|
|
sprintf (filename, "Wrote %d bytes", offset);
|
|
|
|
WaitPrompt (filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Memory Card Preferences Load
|
|
|
|
****************************************************************************/
|
|
|
|
void
|
|
|
|
LoadPrefsFromMC (int slot, int silent)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
char msg[80];
|
|
|
|
|
|
|
|
ShowAction ("Loading Prefs from MC...");
|
|
|
|
|
|
|
|
offset = LoadBufferFromMC (savebuffer, slot, PREFS_FILE_NAME, silent);
|
|
|
|
|
|
|
|
if ( offset > 0 )
|
|
|
|
{
|
|
|
|
decodePrefsData ();
|
|
|
|
if ( !silent )
|
|
|
|
{
|
|
|
|
sprintf (msg, "Loaded %d bytes", offset);
|
|
|
|
WaitPrompt (msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Memory Card Preferences Save
|
|
|
|
****************************************************************************/
|
|
|
|
void
|
|
|
|
SavePrefsToMC (int slot, int silent)
|
|
|
|
{
|
|
|
|
int datasize;
|
|
|
|
int offset;
|
|
|
|
char msg[80];
|
|
|
|
|
|
|
|
ShowAction ("Saving Prefs to MC...");
|
|
|
|
|
|
|
|
datasize = preparePrefsData ();
|
|
|
|
offset = SaveBufferToMC (savebuffer, slot, PREFS_FILE_NAME, datasize, silent);
|
|
|
|
|
|
|
|
if ( offset > 0 && !silent)
|
|
|
|
{
|
|
|
|
sprintf (msg, "Wrote %d bytes", offset);
|
|
|
|
WaitPrompt (msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|