2008-08-06 03:09:59 +02:00
|
|
|
/****************************************************************************
|
2008-08-14 00:44:59 +02:00
|
|
|
* Snes9x 1.50
|
2008-08-06 03:09:59 +02:00
|
|
|
*
|
2008-08-14 00:44:59 +02:00
|
|
|
* Nintendo Wii/Gamecube Port
|
|
|
|
*
|
|
|
|
* softdev July 2006
|
|
|
|
* crunchy2 May 2007-July 2007
|
|
|
|
* Tantric August 2008
|
|
|
|
*
|
|
|
|
* freeze.cpp
|
|
|
|
*
|
|
|
|
* Snapshots Memory File System
|
2008-08-06 03:09:59 +02:00
|
|
|
*
|
|
|
|
* This is a single global memory file controller. Don't even think of opening two
|
|
|
|
* at the same time !
|
|
|
|
*
|
|
|
|
* There's just enough here to do SnapShots - you should add anything else you
|
|
|
|
* need.
|
|
|
|
****************************************************************************/
|
|
|
|
#include <gccore.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-08-06 03:39:43 +02:00
|
|
|
#include <fat.h>
|
2008-08-06 03:09:59 +02:00
|
|
|
#include <zlib.h>
|
2008-08-10 10:09:22 +02:00
|
|
|
#include <smb.h>
|
2008-08-06 03:09:59 +02:00
|
|
|
|
|
|
|
#include "snes9x.h"
|
|
|
|
#include "memmap.h"
|
|
|
|
#include "soundux.h"
|
|
|
|
#include "snapshot.h"
|
|
|
|
#include "srtc.h"
|
|
|
|
|
2008-08-22 04:47:08 +02:00
|
|
|
#include "Snes9xGX.h"
|
2008-08-12 05:25:16 +02:00
|
|
|
#include "images/saveicon.h"
|
|
|
|
#include "freeze.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
#include "filesel.h"
|
2008-08-07 07:19:17 +02:00
|
|
|
#include "menudraw.h"
|
2008-08-10 05:14:39 +02:00
|
|
|
#include "smbop.h"
|
2008-08-07 07:19:17 +02:00
|
|
|
#include "fileop.h"
|
2008-08-12 05:25:16 +02:00
|
|
|
#include "memcardop.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
|
|
|
|
#define MEMBUFFER (512 * 1024)
|
|
|
|
|
|
|
|
extern void S9xSRTCPreSaveState ();
|
|
|
|
extern void NGCFreezeStruct ();
|
|
|
|
extern bool8 S9xUnfreezeGame (const char *filename);
|
|
|
|
extern unsigned char savebuffer[];
|
|
|
|
|
|
|
|
static int bufoffset;
|
|
|
|
static char membuffer[MEMBUFFER];
|
|
|
|
|
2008-08-22 04:47:08 +02:00
|
|
|
char freezecomment[2][32];
|
2008-08-06 03:39:43 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GetMem
|
|
|
|
*
|
|
|
|
* Return x bytes from memory buffer
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
GetMem (char *buffer, int len)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
memcpy (buffer, membuffer + bufoffset, len);
|
|
|
|
bufoffset += len;
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
return len;
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PutMem
|
|
|
|
*
|
|
|
|
* Put some values in memory buffer
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
PutMem (char *buffer, int len)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
memcpy (membuffer + bufoffset, buffer, len);
|
|
|
|
bufoffset += len;
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NGCFreezeBlock (char *name, uint8 * block, int size)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
char buffer[512];
|
|
|
|
sprintf (buffer, "%s:%06d:", name, size);
|
|
|
|
PutMem (buffer, strlen (buffer));
|
|
|
|
PutMem ((char *) block, size);
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NGCFreezeMembuffer
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
NGCFreezeMemBuffer ()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char buffer[1024];
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
bufoffset = 0;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
S9xUpdateRTC ();
|
|
|
|
S9xSRTCPreSaveState ();
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
SoundData.channels[i].previous16[0] =
|
|
|
|
(int16) SoundData.channels[i].previous[0];
|
|
|
|
SoundData.channels[i].previous16[1] =
|
|
|
|
(int16) SoundData.channels[i].previous[1];
|
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
sprintf (buffer, "%s:%04d\n", SNAPSHOT_MAGIC, SNAPSHOT_VERSION);
|
|
|
|
PutMem (buffer, strlen (buffer));
|
|
|
|
sprintf (buffer, "NAM:%06d:%s%c", (int) strlen (Memory.ROMFilename) + 1,
|
|
|
|
Memory.ROMFilename, 0);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
PutMem (buffer, strlen (buffer) + 1);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
NGCFreezeStruct ();
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NGCFreezeGame
|
|
|
|
*
|
|
|
|
* Do freeze game for Nintendo Gamecube
|
|
|
|
*/
|
|
|
|
int
|
2008-08-07 05:25:02 +02:00
|
|
|
NGCFreezeGame (int method, bool8 silent)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
ShowAction ((char*) "Saving...");
|
|
|
|
|
2008-08-07 05:25:02 +02:00
|
|
|
if(method == METHOD_AUTO)
|
|
|
|
method = autoSaveMethod();
|
|
|
|
|
|
|
|
char filename[1024];
|
2008-08-10 10:09:22 +02:00
|
|
|
int offset = 0;
|
2008-08-07 05:25:02 +02:00
|
|
|
char msg[100];
|
|
|
|
|
|
|
|
S9xSetSoundMute (TRUE);
|
|
|
|
S9xPrepareSoundForSnapshotSave (FALSE);
|
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
NGCFreezeMemBuffer (); // copy freeze mem into membuffer
|
2008-08-13 07:47:04 +02:00
|
|
|
|
|
|
|
ClearSaveBuffer ();
|
2008-08-12 05:25:16 +02:00
|
|
|
memcpy (savebuffer, membuffer, bufoffset);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
S9xPrepareSoundForSnapshotSave (TRUE);
|
|
|
|
S9xSetSoundMute (FALSE);
|
|
|
|
|
|
|
|
if (method == METHOD_SD || method == METHOD_USB) // FAT devices
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
if(ChangeFATInterface(method, NOTSILENT))
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
sprintf (filename, "%s/%s/%s.frz", ROOTFATDIR, GCSettings.SaveFolder, Memory.ROMFilename);
|
|
|
|
offset = SaveBufferToFAT (filename, bufoffset, silent);
|
2008-08-07 05:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-12 05:25:16 +02:00
|
|
|
else if (method == METHOD_SMB) // SMB
|
|
|
|
{
|
|
|
|
sprintf (filename, "%s/%s.frz", GCSettings.SaveFolder, Memory.ROMFilename);
|
|
|
|
offset = SaveBufferToSMB (filename, bufoffset, silent);
|
|
|
|
}
|
2008-08-08 09:57:01 +02:00
|
|
|
else if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB) // MC Slot A or B
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
2008-08-17 07:25:33 +02:00
|
|
|
sprintf (filename, "%s.snz", Memory.ROMName);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
ClearSaveBuffer ();
|
|
|
|
|
|
|
|
/*** Copy in save icon ***/
|
2008-08-13 07:47:04 +02:00
|
|
|
int woffset = sizeof (saveicon);
|
|
|
|
memcpy (savebuffer, saveicon, woffset);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
/*** And the freezecomment ***/
|
2008-08-22 04:47:08 +02:00
|
|
|
sprintf (freezecomment[0], "%s Freeze", VERSIONSTR);
|
|
|
|
sprintf (freezecomment[1], Memory.ROMName);
|
2008-08-13 07:47:04 +02:00
|
|
|
memcpy (savebuffer + woffset, freezecomment, 64);
|
|
|
|
woffset += 64;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
/*** Zip and copy in the freeze ***/
|
|
|
|
uLongf DestBuffSize = (uLongf) SAVEBUFFERSIZE;
|
2008-08-13 07:47:04 +02:00
|
|
|
int err= compress2((Bytef*)(savebuffer+woffset+8), (uLongf*)&DestBuffSize, (const Bytef*)membuffer, (uLongf)bufoffset, Z_BEST_COMPRESSION);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
if(err!=Z_OK)
|
|
|
|
{
|
|
|
|
sprintf (msg, "zip error %s ",zError(err));
|
|
|
|
WaitPrompt (msg);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
|
|
|
|
int zippedsize = (int)DestBuffSize;
|
2008-08-13 07:47:04 +02:00
|
|
|
memcpy (savebuffer + woffset, &zippedsize, 4);
|
|
|
|
woffset += 4;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
int decompressedsize = (int)bufoffset;
|
2008-08-13 07:47:04 +02:00
|
|
|
memcpy (savebuffer + woffset, &decompressedsize, 4);
|
|
|
|
woffset += 4;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-13 07:47:04 +02:00
|
|
|
woffset += zippedsize;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
if(method == METHOD_MC_SLOTA)
|
2008-08-13 07:47:04 +02:00
|
|
|
offset = SaveBufferToMC ( savebuffer, CARD_SLOTA, filename, woffset, SILENT );
|
2008-08-07 05:25:02 +02:00
|
|
|
else
|
2008-08-13 07:47:04 +02:00
|
|
|
offset = SaveBufferToMC ( savebuffer, CARD_SLOTB, filename, woffset, SILENT );
|
2008-08-07 05:25:02 +02:00
|
|
|
}
|
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if(offset > 0) // save successful!
|
|
|
|
{
|
|
|
|
if(!silent)
|
|
|
|
WaitPrompt((char*) "Save successful");
|
|
|
|
return 1;
|
2008-08-07 05:25:02 +02:00
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NGCUnFreezeBlock
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NGCUnFreezeBlock (char *name, uint8 * block, int size)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
char buffer[20], *e;
|
|
|
|
int len = 0;
|
|
|
|
int rem = 0;
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
GetMem (buffer, 11);
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if (strncmp (buffer, name, 3) != 0 || buffer[3] != ':' ||
|
|
|
|
buffer[10] != ':' || (len = strtol (&buffer[4], &e, 10)) == 0 ||
|
|
|
|
e != buffer + 10)
|
|
|
|
{
|
|
|
|
return WRONG_FORMAT;
|
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if (len > size)
|
|
|
|
{
|
|
|
|
rem = len - size;
|
|
|
|
len = size;
|
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
ZeroMemory (block, size);
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
GetMem ((char *) block, len);
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if (rem)
|
|
|
|
{
|
|
|
|
bufoffset += rem;
|
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
return SUCCESS;
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NGCUnfreezeGame
|
|
|
|
*/
|
|
|
|
int
|
2008-08-07 05:25:02 +02:00
|
|
|
NGCUnfreezeGame (int method, bool8 silent)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
ShowAction ((char*) "Loading...");
|
|
|
|
char filename[1024];
|
|
|
|
int offset = 0;
|
|
|
|
char msg[80];
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
bufoffset = 0;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
if(method == METHOD_AUTO)
|
2008-08-17 07:25:33 +02:00
|
|
|
method = autoSaveMethod(); // we use 'Save' because snapshot needs R/W
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
if (method == METHOD_SD || method == METHOD_USB) // SD & USB
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
if(ChangeFATInterface(method, NOTSILENT))
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
sprintf (filename, "%s/%s/%s.frz", ROOTFATDIR, GCSettings.SaveFolder, Memory.ROMFilename);
|
|
|
|
offset = LoadBufferFromFAT (filename, silent);
|
2008-08-07 05:25:02 +02:00
|
|
|
}
|
2008-08-12 05:25:16 +02:00
|
|
|
}
|
|
|
|
else if (method == METHOD_SMB) // Network (SMB)
|
|
|
|
{
|
|
|
|
sprintf (filename, "%s/%s.frz", GCSettings.SaveFolder, Memory.ROMFilename);
|
|
|
|
offset = LoadBufferFromSMB (filename, silent);
|
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
else if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB) // MC in slot A or slot B
|
|
|
|
{
|
2008-08-17 07:25:33 +02:00
|
|
|
sprintf (filename, "%s.snz", Memory.ROMName);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if(method == METHOD_MC_SLOTA)
|
2008-08-13 07:47:04 +02:00
|
|
|
ret = LoadBufferFromMC ( savebuffer, CARD_SLOTA, filename, silent );
|
2008-08-07 05:25:02 +02:00
|
|
|
else
|
2008-08-13 07:47:04 +02:00
|
|
|
ret = LoadBufferFromMC ( savebuffer, CARD_SLOTB, filename, silent );
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-13 07:47:04 +02:00
|
|
|
if (ret)
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
2008-08-13 07:47:04 +02:00
|
|
|
char zipbuffer[MEMBUFFER];
|
|
|
|
|
2008-08-07 05:25:02 +02:00
|
|
|
// skip the saveicon and comment
|
|
|
|
offset = (sizeof(saveicon) + 64);
|
|
|
|
uLongf zipsize = 0;
|
|
|
|
uLongf decompressedsize = 0;
|
|
|
|
|
|
|
|
memcpy (&zipsize, savebuffer+offset, 4);
|
|
|
|
offset += 4;
|
|
|
|
|
|
|
|
memcpy (&decompressedsize, savebuffer+offset, 4);
|
|
|
|
offset += 4;
|
|
|
|
|
|
|
|
memset(membuffer, 0, MEMBUFFER);
|
|
|
|
|
|
|
|
uLongf DestBuffSize = MEMBUFFER;
|
2008-08-13 07:47:04 +02:00
|
|
|
int err= uncompress((Bytef*)zipbuffer, (uLongf*)&DestBuffSize, (const Bytef*)(savebuffer + offset), zipsize);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
if ( err!=Z_OK )
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
sprintf (msg, "Unzip error %s ",zError(err));
|
2008-08-07 05:25:02 +02:00
|
|
|
WaitPrompt (msg);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-13 07:47:04 +02:00
|
|
|
else if ( DestBuffSize != decompressedsize )
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
|
|
|
WaitPrompt((char*) "Unzipped size doesn't match expected size!");
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-13 07:47:04 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
offset = MEMBUFFER;
|
|
|
|
memcpy (savebuffer, zipbuffer, MEMBUFFER);
|
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if(offset > 0)
|
|
|
|
{
|
|
|
|
memcpy (membuffer, savebuffer, offset);
|
2008-08-13 07:47:04 +02:00
|
|
|
ClearSaveBuffer ();
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if (S9xUnfreezeGame ("AGAME") == SUCCESS)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
WaitPrompt((char*) "Error thawing");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!silent)
|
2008-08-10 05:14:39 +02:00
|
|
|
WaitPrompt((char*) "Freeze file not found");
|
2008-08-12 05:25:16 +02:00
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
return 0; // if we reached here, nothing was done!
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|