changes for save states. although there's still save corruption :(

This commit is contained in:
dborth 2009-12-02 22:26:44 +00:00
parent a7b1ba9ae3
commit 6bb71834d3
9 changed files with 417 additions and 517 deletions

View File

@ -394,42 +394,7 @@ bool SaveBatteryOrState(char * filepath, int action, bool silent)
}
else
{
bool written = emulator.emuWriteMemState((char *)savebuffer, SAVEBUFFERSIZE);
// we need to set datasize to the exact memory size written
// but emuWriteMemState doesn't return that for us
// so instead we'll find the end of the save the old fashioned way
if(written)
{
datasize = (1024*192); // we'll start at 192K - no save should be larger
char check = savebuffer[datasize];
while(check == 0)
{
datasize -= 16384;
check = savebuffer[datasize];
}
datasize += 16384;
check = savebuffer[datasize];
while(check == 0)
{
datasize -= 1024;
check = savebuffer[datasize];
}
datasize += 1024;
check = savebuffer[datasize];
while(check == 0)
{
datasize -= 64;
check = savebuffer[datasize];
}
datasize += 64;
check = savebuffer[datasize];
while(check == 0)
{
datasize -= 1;
check = savebuffer[datasize];
}
datasize += 2; // include last byte AND a null byte
}
datasize = emulator.emuWriteMemState((char *)savebuffer, SAVEBUFFERSIZE);
}
// write savebuffer into file

View File

@ -25,7 +25,7 @@ struct EmulatedSystem {
// load memory state (rewind)
bool (*emuReadMemState)(char *, int);
// write memory state (rewind)
bool (*emuWriteMemState)(char *, int);
int (*emuWriteMemState)(char *, int);
// write PNG file
bool (*emuWritePNG)(const char *);
// write BMP file

View File

@ -163,7 +163,7 @@ void utilWriteData(gzFile gzFile, variable_desc *data)
gzFile utilGzOpen(const char *file, const char *mode)
{
utilGzWriteFunc = (int (*)(void *,void * const, unsigned int))gzwrite;
utilGzWriteFunc = (int (ZEXPORT *)(void *,void * const, unsigned int))gzwrite;
utilGzReadFunc = gzread;
utilGzCloseFunc = gzclose;
utilGzSeekFunc = gzseek;

View File

@ -9,15 +9,33 @@
* Adapted from original gzio.c from zlib library by Forgotten
*/
/* @(#) $Id: memgzio.c,v 1.3 2004/01/17 23:07:32 kxu Exp $ */
/* @(#) $Id: memgzio.c,v 1.5 2006/06/06 21:04:20 spacy51 Exp $ */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "memgzio.h"
#ifndef local
#define local static
#endif
#ifndef DEF_MEM_LEVEL
# define DEF_MEM_LEVEL 8
#endif
#ifndef OS_CODE
#define OS_CODE 3
#endif
#ifndef zmemcpy
#define zmemcpy memcpy
#endif
/*struct internal_state {int dummy;};*/ /* for buggy compilers */
#ifndef Z_BUFSIZE
@ -44,18 +62,15 @@ static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
#define COMMENT 0x10 /* bit 4 set: file comment present */
#define RESERVED 0xE0 /* bits 5..7: reserved */
typedef struct _MemFile
{
typedef struct _MemFile {
char *memory;
char *next;
int available;
int error;
char mode;
}
MEMFILE;
} MEMFILE;
typedef struct mem_stream
{
typedef struct mem_stream {
z_stream stream;
int z_err; /* error code for last stream operation */
int z_eof; /* set if end of input file */
@ -67,8 +82,7 @@ typedef struct mem_stream
int transparent; /* 1 if input file is not a .gz file */
char mode; /* 'w' or 'r' */
long startpos; /* start of compressed data in file (header skipped) */
}
mem_stream;
} mem_stream;
local gzFile gz_open OF((char *memory, const int available, const char *mode));
@ -95,8 +109,7 @@ local MEMFILE *memOpen(char *memory, int available, char mode)
f->mode = mode;
f->error = 0;
if(mode == 'w')
{
if(mode == 'w') {
f->available = available - 8;
f->next = memory + 8;
memory[0] = 'V';
@ -104,12 +117,9 @@ local MEMFILE *memOpen(char *memory, int available, char mode)
memory[2] = 'A';
memory[3] = ' ';
*((int *)(memory+4)) = 0;
}
else
{
} else {
if(memory[0] != 'V' || memory[1] != 'B' || memory[2] != 'A' ||
memory[3] != ' ')
{
memory[3] != ' ') {
free(f);
return NULL;
}
@ -125,18 +135,16 @@ local size_t memWrite(const void *buffer, size_t size, size_t count,
{
size_t total = size*count;
if(file->mode != 'w')
{
if(file->mode != 'w') {
file->error = 1;
return 0;
}
if(total > (size_t)file->available)
{
if(total > (size_t)file->available) {
total = file->available;
}
memcpy(file->next, buffer, total);
file->available -= total;
file->available -= (int)total;
file->next += total;
return total;
}
@ -146,8 +154,7 @@ local size_t memRead(void *buffer, size_t size, size_t count,
{
size_t total = size*count;
if(file->mode != 'r')
{
if(file->mode != 'r') {
file->error = 1;
return 0;
}
@ -155,30 +162,26 @@ local size_t memRead(void *buffer, size_t size, size_t count,
if(file->available == 0)
return -1;
if(total > (size_t)file->available)
{
if(total > (size_t)file->available) {
total = file->available;
}
memcpy(buffer, file->next, total);
file->available -= total;
file->available -= (int)total;
file->next += total;
return total;
}
local int memPutc(int c, MEMFILE *file)
{
if(file->mode != 'w')
{
if(file->mode != 'w') {
file->error = 1;
return -1;
}
if(file->available >= 1)
{
if(file->available >= 1) {
*file->next++ = c;
file->available--;
}
else
} else
return -1;
return c;
@ -186,7 +189,7 @@ local int memPutc(int c, MEMFILE *file)
local long memTell(MEMFILE *f)
{
return (f->next - f->memory) - 8;
return (long)(f->next - f->memory) - 8;
}
local int memError(MEMFILE *f)
@ -196,8 +199,7 @@ local int memError(MEMFILE *f)
local int memClose(MEMFILE *f)
{
if(f->mode == 'w')
{
if(f->mode == 'w') {
*((int *)(f->memory+4)) = memTell(f);
}
free(f);
@ -214,7 +216,7 @@ local int memPrintf(MEMFILE *f, const char *format, ...)
len = vsprintf(buffer, format, list);
va_end(list);
return memWrite(buffer, 1, len, f);
return (int)memWrite(buffer, 1, len, f);
}
/* ===========================================================================
@ -227,9 +229,9 @@ local int memPrintf(MEMFILE *f, const char *format, ...)
zlib error is Z_MEM_ERROR).
*/
local gzFile gz_open (memory, available, mode)
char *memory;
const int available;
const char *mode;
char *memory;
const int available;
const char *mode;
{
int err;
int level = Z_DEFAULT_COMPRESSION; /* compression level */
@ -256,32 +258,22 @@ const char *mode;
s->file = NULL;
s->mode = '\0';
do
{
do {
if (*p == 'r') s->mode = 'r';
if (*p == 'w' || *p == 'a') s->mode = 'w';
if (*p >= '0' && *p <= '9')
{
if (*p >= '0' && *p <= '9') {
level = *p - '0';
}
else if (*p == 'f')
{
} else if (*p == 'f') {
strategy = Z_FILTERED;
}
else if (*p == 'h')
{
} else if (*p == 'h') {
strategy = Z_HUFFMAN_ONLY;
}
else
{
} else {
*m++ = *p; /* copy the mode */
}
}
while (*p++ && m != fmode + sizeof(fmode));
} while (*p++ && m != fmode + sizeof(fmode));
if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
if (s->mode == 'w')
{
if (s->mode == 'w') {
#ifdef NO_DEFLATE
err = Z_STREAM_ERROR;
#else
@ -291,13 +283,10 @@ const char *mode;
s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
#endif
if (err != Z_OK || s->outbuf == Z_NULL)
{
if (err != Z_OK || s->outbuf == Z_NULL) {
return destroy(s), (gzFile)Z_NULL;
}
}
else
{
} else {
s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
err = inflateInit2(&(s->stream), -MAX_WBITS);
@ -307,8 +296,7 @@ const char *mode;
* return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
* present after the compressed stream.
*/
if (err != Z_OK || s->inbuf == Z_NULL)
{
if (err != Z_OK || s->inbuf == Z_NULL) {
return destroy(s), (gzFile)Z_NULL;
}
}
@ -317,13 +305,11 @@ const char *mode;
errno = 0;
s->file = memOpen(memory, available, s->mode);
if (s->file == NULL)
{
if (s->file == NULL) {
return destroy(s), (gzFile)Z_NULL;
}
if (s->mode == 'w')
{
if (s->mode == 'w') {
/* Write a very simple .gz header:
*/
memPrintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
@ -334,9 +320,7 @@ const char *mode;
* startpos anyway in write mode, so this initialization is not
* necessary.
*/
}
else
{
} else {
check_header(s); /* skip the .gz header */
s->startpos = (memTell(s->file) - s->stream.avail_in);
}
@ -348,9 +332,9 @@ const char *mode;
Opens a gzip (.gz) file for reading or writing.
*/
gzFile ZEXPORT memgzopen (memory, available, mode)
char *memory;
int available;
const char *mode;
char *memory;
int available;
const char *mode;
{
return gz_open (memory, available, mode);
}
@ -361,15 +345,13 @@ const char *mode;
IN assertion: the stream s has been sucessfully opened for reading.
*/
local int get_byte(s)
mem_stream *s;
mem_stream *s;
{
if (s->z_eof) return EOF;
if (s->stream.avail_in == 0)
{
if (s->stream.avail_in == 0) {
errno = 0;
s->stream.avail_in = memRead(s->inbuf, 1, Z_BUFSIZE, s->file);
if (s->stream.avail_in == 0)
{
s->stream.avail_in = (uInt)memRead(s->inbuf, 1, Z_BUFSIZE, s->file);
if (s->stream.avail_in == 0) {
s->z_eof = 1;
if (memError(s->file)) s->z_err = Z_ERRNO;
return EOF;
@ -390,7 +372,7 @@ mem_stream *s;
for concatenated .gz files.
*/
local void check_header(s)
mem_stream *s;
mem_stream *s;
{
int method; /* method byte */
int flags; /* flags byte */
@ -398,14 +380,11 @@ mem_stream *s;
int c;
/* Check the gzip magic header */
for (len = 0; len < 2; len++)
{
for (len = 0; len < 2; len++) {
c = get_byte(s);
if (c != gz_magic[len])
{
if (c != gz_magic[len]) {
if (len != 0) s->stream.avail_in++, s->stream.next_in--;
if (c != EOF)
{
if (c != EOF) {
s->stream.avail_in++, s->stream.next_in--;
s->transparent = 1;
}
@ -415,8 +394,7 @@ mem_stream *s;
}
method = get_byte(s);
flags = get_byte(s);
if (method != Z_DEFLATED || (flags & RESERVED) != 0)
{
if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
s->z_err = Z_DATA_ERROR;
return;
}
@ -424,34 +402,30 @@ mem_stream *s;
/* Discard time, xflags and OS code: */
for (len = 0; len < 6; len++) (void)get_byte(s);
if ((flags & EXTRA_FIELD) != 0)
{ /* skip the extra field */
if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
len = (uInt)get_byte(s);
len += ((uInt)get_byte(s))<<8;
/* len is garbage if EOF but the loop below will quit anyway */
while (len-- != 0 && get_byte(s) != EOF) ;
}
if ((flags & ORIG_NAME) != 0)
{ /* skip the original file name */
if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
while ((c = get_byte(s)) != 0 && c != EOF) ;
}
if ((flags & COMMENT) != 0)
{ /* skip the .gz file comment */
if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
while ((c = get_byte(s)) != 0 && c != EOF) ;
}
if ((flags & HEAD_CRC) != 0)
{ /* skip the header crc */
if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
for (len = 0; len < 2; len++) (void)get_byte(s);
}
s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
}
/* ===========================================================================
* Cleanup then free the given mem_stream. Return a zlib error code.
/* ===========================================================================
* Cleanup then free the given mem_stream. Return a zlib error code.
Try freeing in the reverse order of allocations.
*/
*/
local int destroy (s)
mem_stream *s;
mem_stream *s;
{
int err = Z_OK;
@ -459,23 +433,18 @@ mem_stream *s;
TRYFREE(s->msg);
if (s->stream.state != NULL)
{
if (s->mode == 'w')
{
if (s->stream.state != NULL) {
if (s->mode == 'w') {
#ifdef NO_DEFLATE
err = Z_STREAM_ERROR;
#else
err = deflateEnd(&(s->stream));
#endif
}
else if (s->mode == 'r')
{
} else if (s->mode == 'r') {
err = inflateEnd(&(s->stream));
}
}
if (s->file != NULL && memClose(s->file))
{
if (s->file != NULL && memClose(s->file)) {
#ifdef ESPIPE
if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
#endif
@ -494,9 +463,9 @@ mem_stream *s;
gzread returns the number of bytes actually read (0 for end of file).
*/
int ZEXPORT memgzread (file, buf, len)
gzFile file;
voidp buf;
unsigned len;
gzFile file;
voidp buf;
unsigned len;
{
mem_stream *s = (mem_stream*)file;
Bytef *start = (Bytef*)buf; /* starting point for crc computation */
@ -511,16 +480,13 @@ unsigned len;
s->stream.next_out = (Bytef*)buf;
s->stream.avail_out = len;
while (s->stream.avail_out != 0)
{
while (s->stream.avail_out != 0) {
if (s->transparent)
{
if (s->transparent) {
/* Copy first the lookahead bytes: */
uInt n = s->stream.avail_in;
if (n > s->stream.avail_out) n = s->stream.avail_out;
if (n > 0)
{
if (n > 0) {
zmemcpy(s->stream.next_out, s->stream.next_in, n);
next_out += n;
s->stream.next_out = next_out;
@ -528,10 +494,8 @@ unsigned len;
s->stream.avail_out -= n;
s->stream.avail_in -= n;
}
if (s->stream.avail_out > 0)
{
s->stream.avail_out -= memRead(next_out, 1, s->stream.avail_out,
s->file);
if (s->stream.avail_out > 0) {
s->stream.avail_out -= (uInt)memRead(next_out, 1, s->stream.avail_out, s->file);
}
len -= s->stream.avail_out;
s->stream.total_in += (uLong)len;
@ -539,16 +503,13 @@ unsigned len;
if (len == 0) s->z_eof = 1;
return (int)len;
}
if (s->stream.avail_in == 0 && !s->z_eof)
{
if (s->stream.avail_in == 0 && !s->z_eof) {
errno = 0;
s->stream.avail_in = memRead(s->inbuf, 1, Z_BUFSIZE, s->file);
if (s->stream.avail_in == 0)
{
s->stream.avail_in = (uInt)memRead(s->inbuf, 1, Z_BUFSIZE, s->file);
if (s->stream.avail_in == 0) {
s->z_eof = 1;
if (memError(s->file))
{
if (memError(s->file)) {
s->z_err = Z_ERRNO;
break;
}
@ -557,26 +518,21 @@ unsigned len;
}
s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
if (s->z_err == Z_STREAM_END)
{
if (s->z_err == Z_STREAM_END) {
/* Check CRC and original size */
s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
start = s->stream.next_out;
if (getLong(s) != s->crc)
{
if (getLong(s) != s->crc) {
s->z_err = Z_DATA_ERROR;
}
else
{
} else {
(void)getLong(s);
/* The uncompressed length returned by above getlong() may
* be different from s->stream.total_out) in case of
* concatenated .gz files. Check for such files:
*/
check_header(s);
if (s->z_err == Z_OK)
{
if (s->z_err == Z_OK) {
uLong total_in = s->stream.total_in;
uLong total_out = s->stream.total_out;
@ -601,9 +557,9 @@ unsigned len;
gzwrite returns the number of bytes actually written (0 in case of error).
*/
int ZEXPORT memgzwrite (file, buf, len)
gzFile file;
const voidp buf;
unsigned len;
gzFile file;
const voidp buf;
unsigned len;
{
mem_stream *s = (mem_stream*)file;
@ -612,15 +568,12 @@ unsigned len;
s->stream.next_in = (Bytef*)buf;
s->stream.avail_in = len;
while (s->stream.avail_in != 0)
{
while (s->stream.avail_in != 0) {
if (s->stream.avail_out == 0)
{
if (s->stream.avail_out == 0) {
s->stream.next_out = s->outbuf;
if (memWrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE)
{
if (memWrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
s->z_err = Z_ERRNO;
break;
}
@ -639,8 +592,8 @@ unsigned len;
flush is as in the deflate() function.
*/
local int do_flush (file, flush)
gzFile file;
int flush;
gzFile file;
int flush;
{
uInt len;
int done = 0;
@ -650,14 +603,11 @@ int flush;
s->stream.avail_in = 0; /* should be zero already anyway */
for (;;)
{
for (;;) {
len = Z_BUFSIZE - s->stream.avail_out;
if (len != 0)
{
if ((uInt)memWrite(s->outbuf, 1, len, s->file) != len)
{
if (len != 0) {
if ((uInt)memWrite(s->outbuf, 1, len, s->file) != len) {
s->z_err = Z_ERRNO;
return Z_ERRNO;
}
@ -684,12 +634,11 @@ int flush;
Outputs a long in LSB order to the given file
*/
local void putLong (file, x)
MEMFILE *file;
uLong x;
MEMFILE *file;
uLong x;
{
int n;
for (n = 0; n < 4; n++)
{
for (n = 0; n < 4; n++) {
memPutc((int)(x & 0xff), file);
x >>= 8;
}
@ -700,7 +649,7 @@ uLong x;
of error.
*/
local uLong getLong (s)
mem_stream *s;
mem_stream *s;
{
uLong x = (uLong)get_byte(s);
int c;
@ -718,15 +667,14 @@ mem_stream *s;
and deallocates all the (de)compression state.
*/
int ZEXPORT memgzclose (file)
gzFile file;
gzFile file;
{
int err;
mem_stream *s = (mem_stream*)file;
if (s == NULL) return Z_STREAM_ERROR;
if (s->mode == 'w')
{
if (s->mode == 'w') {
#ifdef NO_DEFLATE
return Z_STREAM_ERROR;
#else
@ -741,7 +689,7 @@ gzFile file;
}
long ZEXPORT memtell(file)
gzFile file;
gzFile file;
{
mem_stream *s = (mem_stream*)file;

View File

@ -1,3 +1,6 @@
#ifndef MEMGZIO_H
#define MEMGZIO_H
/* gzio.c -- IO on .gz files
* Copyright (C) 1995-2002 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
@ -8,27 +11,13 @@
/* memgzio.c - IO on .gz files in memory
* Adapted from original gzio.c from zlib library by Forgotten
*/
#ifndef HAVE_ZUTIL_H
#include "../win32/include/zlib/zutil.h"
#else
#include <zlib.h>
#endif
#ifndef local
# define local static
#endif
gzFile ZEXPORT memgzopen(char *memory, int available, const char *mode);
int ZEXPORT memgzread(gzFile file, voidp buf, unsigned len);
int ZEXPORT memgzwrite(gzFile file, const voidp buf, unsigned len);
int ZEXPORT memgzclose(gzFile file);
long ZEXPORT memtell(gzFile file);
#if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
#else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
#endif
#define OS_CODE 0x03 /* assume Unix */
#define zmemcpy memcpy
gzFile ZEXPORT memgzopen(char *memory, int, const char *);
int ZEXPORT memgzread(gzFile, voidp, unsigned);
int ZEXPORT memgzwrite(gzFile, const voidp, unsigned);
int ZEXPORT memgzclose(gzFile);
long ZEXPORT memtell(gzFile);
#endif // MEMGZIO_H

View File

@ -3550,24 +3550,23 @@ static bool gbWriteSaveState(gzFile gzFile)
return true;
}
bool gbWriteMemSaveState(char *memory, int available)
int gbWriteMemSaveState(char *memory, int available)
{
int pos = 0;
gzFile gzFile = utilMemGzOpen(memory, available, "w");
if(gzFile == NULL) {
return false;
}
if(gzFile == NULL)
return 0;
bool res = gbWriteSaveState(gzFile);
long pos = utilGzMemTell(gzFile)+8;
if(gbWriteSaveState(gzFile))
{
pos = utilGzMemTell(gzFile)+8;
if(pos >= (available))
res = false;
pos = 0;
}
utilGzClose(gzFile);
return res;
return pos;
}
bool gbWriteSaveState(const char *name)

View File

@ -30,7 +30,7 @@ bool gbWriteBatteryFile(const char *);
bool gbWriteBatteryFile(const char *, bool);
bool gbReadBatteryFile(const char *);
bool gbWriteSaveState(const char *);
bool gbWriteMemSaveState(char *, int);
int gbWriteMemSaveState(char *, int);
bool gbReadSaveState(const char *);
bool gbReadMemSaveState(char *, int);
void gbSgbRenderBorder();

View File

@ -634,24 +634,23 @@ bool CPUWriteState(const char *file)
return res;
}
bool CPUWriteMemState(char *memory, int available)
int CPUWriteMemState(char *memory, int available)
{
int pos = 0;
gzFile gzFile = utilMemGzOpen(memory, available, "w");
if(gzFile == NULL) {
return false;
}
if(gzFile == NULL)
return 0;
bool res = CPUWriteState(gzFile);
long pos = utilGzMemTell(gzFile)+8;
if(CPUWriteState(gzFile))
{
pos = utilGzMemTell(gzFile)+8;
if(pos >= (available))
res = false;
pos = 0;
}
utilGzClose(gzFile);
return res;
return pos;
}
static bool CPUReadState(gzFile gzFile)

View File

@ -90,7 +90,7 @@ extern void CPUUpdateRender();
extern void CPUUpdateRenderBuffers(bool);
extern bool CPUReadMemState(char *, int);
extern bool CPUReadState(const char *);
extern bool CPUWriteMemState(char *, int);
extern int CPUWriteMemState(char *, int);
extern bool CPUWriteState(const char *);
extern int CPULoadRom(const char *);
extern void doMirroring(bool);