2008-08-07 14:26:07 +02:00
|
|
|
/***************************************************************************************
|
2009-05-13 16:26:55 +02:00
|
|
|
* Genesis Plus
|
|
|
|
* Savestate support
|
2007-08-10 22:34:06 +02:00
|
|
|
*
|
2011-04-01 00:11:05 +02:00
|
|
|
* Copyright (C) 2007-2011 Eke-Eke (GCN/Wii port)
|
2007-08-10 22:34:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
2008-08-07 14:26:07 +02:00
|
|
|
****************************************************************************************/
|
|
|
|
|
2007-08-10 22:34:06 +02:00
|
|
|
#include "shared.h"
|
|
|
|
|
2009-05-27 16:07:14 +02:00
|
|
|
int state_load(unsigned char *buffer)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2009-02-25 17:53:28 +01:00
|
|
|
/* buffer size */
|
|
|
|
int bufferptr = 0;
|
2007-08-10 22:34:06 +02:00
|
|
|
|
2010-05-28 14:08:00 +02:00
|
|
|
/* first allocate state buffer */
|
|
|
|
unsigned char *state = (unsigned char *)malloc(STATE_SIZE);
|
|
|
|
if (!state) return 0;
|
|
|
|
|
2008-12-04 20:32:22 +01:00
|
|
|
/* uncompress savestate */
|
|
|
|
unsigned long inbytes, outbytes;
|
2008-12-10 19:16:30 +01:00
|
|
|
memcpy(&inbytes, buffer, 4);
|
2009-02-25 17:53:28 +01:00
|
|
|
outbytes = STATE_SIZE;
|
2008-08-07 14:26:07 +02:00
|
|
|
uncompress ((Bytef *)state, &outbytes, (Bytef *)(buffer + 4), inbytes);
|
2007-08-10 22:34:06 +02:00
|
|
|
|
2011-04-01 00:11:05 +02:00
|
|
|
/* signature check (GENPLUS-GX x.x.x) */
|
2010-12-04 18:13:55 +01:00
|
|
|
char version[17];
|
2009-05-27 16:07:14 +02:00
|
|
|
load_param(version,16);
|
2010-12-04 18:13:55 +01:00
|
|
|
version[16] = 0;
|
2011-04-01 00:11:05 +02:00
|
|
|
if (strncmp(version,STATE_VERSION,11))
|
|
|
|
{
|
|
|
|
free(state);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* version check (1.4.0 and above) */
|
|
|
|
if ((version[11] < 0x31) || ((version[11] == 0x31) && (version[13] < 0x34)))
|
2009-05-27 16:07:14 +02:00
|
|
|
{
|
2010-05-28 14:08:00 +02:00
|
|
|
free(state);
|
|
|
|
return -1;
|
2009-05-27 16:07:14 +02:00
|
|
|
}
|
|
|
|
|
2008-12-10 19:16:30 +01:00
|
|
|
/* reset system */
|
|
|
|
system_reset();
|
|
|
|
|
|
|
|
// GENESIS
|
2011-04-01 00:11:05 +02:00
|
|
|
if (system_hw == SYSTEM_PBC)
|
2010-12-04 18:13:55 +01:00
|
|
|
{
|
2011-04-01 00:11:05 +02:00
|
|
|
load_param(work_ram, 0x2000);
|
2010-12-04 18:13:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-01 00:11:05 +02:00
|
|
|
load_param(work_ram, sizeof(work_ram));
|
|
|
|
load_param(zram, sizeof(zram));
|
|
|
|
load_param(&zstate, sizeof(zstate));
|
|
|
|
load_param(&zbank, sizeof(zbank));
|
|
|
|
if (zstate == 3)
|
|
|
|
{
|
|
|
|
m68k_memory_map[0xa0].read8 = z80_read_byte;
|
|
|
|
m68k_memory_map[0xa0].read16 = z80_read_word;
|
|
|
|
m68k_memory_map[0xa0].write8 = z80_write_byte;
|
|
|
|
m68k_memory_map[0xa0].write16 = z80_write_word;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m68k_memory_map[0xa0].read8 = m68k_read_bus_8;
|
|
|
|
m68k_memory_map[0xa0].read16 = m68k_read_bus_16;
|
|
|
|
m68k_memory_map[0xa0].write8 = m68k_unused_8_w;
|
|
|
|
m68k_memory_map[0xa0].write16 = m68k_unused_16_w;
|
|
|
|
}
|
2010-12-04 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
2011-04-01 00:11:05 +02:00
|
|
|
/* 1.4.1 and above */
|
|
|
|
if ((version[11] > 0x31) || (version[13] > 0x34) || (version[15] > 0x30))
|
2010-12-04 18:13:55 +01:00
|
|
|
{
|
|
|
|
/* extended state */
|
|
|
|
load_param(&mcycles_68k, sizeof(mcycles_68k));
|
|
|
|
load_param(&mcycles_z80, sizeof(mcycles_z80));
|
|
|
|
}
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
// IO
|
2011-04-01 00:11:05 +02:00
|
|
|
if (system_hw == SYSTEM_PBC)
|
|
|
|
{
|
|
|
|
load_param(&io_reg[0], 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
load_param(io_reg, sizeof(io_reg));
|
|
|
|
io_reg[0] = region_code | 0x20 | (config.tmss & 1);
|
|
|
|
}
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
// VDP
|
2010-12-04 18:13:55 +01:00
|
|
|
bufferptr += vdp_context_load(&state[bufferptr], version);
|
|
|
|
|
|
|
|
// SOUND
|
|
|
|
bufferptr += sound_context_load(&state[bufferptr], version);
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
// 68000
|
2011-04-01 00:11:05 +02:00
|
|
|
if (system_hw != SYSTEM_PBC)
|
|
|
|
{
|
|
|
|
uint16 tmp16;
|
|
|
|
uint32 tmp32;
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_D0, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_D1, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_D2, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_D3, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_D4, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_D5, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_D6, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_D7, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_A0, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_A1, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_A2, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_A3, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_A4, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_A5, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_A6, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_A7, tmp32);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_PC, tmp32);
|
|
|
|
load_param(&tmp16, 2); m68k_set_reg(M68K_REG_SR, tmp16);
|
|
|
|
load_param(&tmp32, 4); m68k_set_reg(M68K_REG_USP,tmp32);
|
|
|
|
}
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
// Z80
|
|
|
|
load_param(&Z80, sizeof(Z80_Regs));
|
2011-02-23 20:14:55 +01:00
|
|
|
Z80.irq_callback = z80_irq_callback;
|
2009-05-27 16:07:14 +02:00
|
|
|
|
2010-12-04 18:13:55 +01:00
|
|
|
// Cartridge HW
|
2011-04-01 00:11:05 +02:00
|
|
|
if (system_hw == SYSTEM_PBC)
|
|
|
|
{
|
|
|
|
bufferptr += sms_cart_context_load(&state[bufferptr], version);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bufferptr += md_cart_context_load(&state[bufferptr], version);
|
|
|
|
}
|
2010-12-04 18:13:55 +01:00
|
|
|
|
2010-05-28 14:08:00 +02:00
|
|
|
free(state);
|
2009-05-27 16:07:14 +02:00
|
|
|
return 1;
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int state_save(unsigned char *buffer)
|
|
|
|
{
|
2009-02-25 17:53:28 +01:00
|
|
|
/* buffer size */
|
2009-07-22 19:25:41 +02:00
|
|
|
int bufferptr = 0;
|
2009-05-27 16:07:14 +02:00
|
|
|
|
2010-05-28 14:08:00 +02:00
|
|
|
/* first allocate state buffer */
|
|
|
|
unsigned char *state = (unsigned char *)malloc(STATE_SIZE);
|
|
|
|
if (!state) return 0;
|
|
|
|
|
2009-05-27 16:07:14 +02:00
|
|
|
/* version string */
|
|
|
|
char version[16];
|
|
|
|
strncpy(version,STATE_VERSION,16);
|
|
|
|
save_param(version, 16);
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
// GENESIS
|
2011-04-01 00:11:05 +02:00
|
|
|
if (system_hw == SYSTEM_PBC)
|
|
|
|
{
|
|
|
|
save_param(work_ram, 0x2000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
save_param(work_ram, sizeof(work_ram));
|
|
|
|
save_param(zram, sizeof(zram));
|
|
|
|
save_param(&zstate, sizeof(zstate));
|
|
|
|
save_param(&zbank, sizeof(zbank));
|
|
|
|
}
|
2010-12-04 18:13:55 +01:00
|
|
|
save_param(&mcycles_68k, sizeof(mcycles_68k));
|
|
|
|
save_param(&mcycles_z80, sizeof(mcycles_z80));
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
// IO
|
2011-04-01 00:11:05 +02:00
|
|
|
if (system_hw == SYSTEM_PBC)
|
|
|
|
{
|
|
|
|
save_param(&io_reg[0], 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
save_param(io_reg, sizeof(io_reg));
|
|
|
|
}
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
// VDP
|
2010-12-04 18:13:55 +01:00
|
|
|
bufferptr += vdp_context_save(&state[bufferptr]);
|
|
|
|
|
|
|
|
// SOUND
|
|
|
|
bufferptr += sound_context_save(&state[bufferptr]);
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
// 68000
|
2011-04-01 00:11:05 +02:00
|
|
|
if (system_hw != SYSTEM_PBC)
|
|
|
|
{
|
|
|
|
uint16 tmp16;
|
|
|
|
uint32 tmp32;
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_D0); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_D1); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_D2); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_D3); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_D4); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_D5); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_D6); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_D7); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_A0); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_A1); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_A2); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_A3); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_A4); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_A5); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_A6); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_A7); save_param(&tmp32, 4);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_PC); save_param(&tmp32, 4);
|
|
|
|
tmp16 = m68k_get_reg(NULL, M68K_REG_SR); save_param(&tmp16, 2);
|
|
|
|
tmp32 = m68k_get_reg(NULL, M68K_REG_USP); save_param(&tmp32, 4);
|
|
|
|
}
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
// Z80
|
|
|
|
save_param(&Z80, sizeof(Z80_Regs));
|
|
|
|
|
2010-12-04 18:13:55 +01:00
|
|
|
// Cartridge HW
|
2011-04-01 00:11:05 +02:00
|
|
|
if (system_hw == SYSTEM_PBC)
|
|
|
|
{
|
|
|
|
bufferptr += sms_cart_context_save(&state[bufferptr]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bufferptr += md_cart_context_save(&state[bufferptr]);
|
|
|
|
}
|
2010-12-04 18:13:55 +01:00
|
|
|
|
2008-12-10 19:16:30 +01:00
|
|
|
/* compress state file */
|
|
|
|
unsigned long inbytes = bufferptr;
|
2009-02-25 17:53:28 +01:00
|
|
|
unsigned long outbytes = STATE_SIZE;
|
2008-08-07 14:26:07 +02:00
|
|
|
compress2 ((Bytef *)(buffer + 4), &outbytes, (Bytef *)state, inbytes, 9);
|
2008-12-10 19:16:30 +01:00
|
|
|
memcpy(buffer, &outbytes, 4);
|
2010-05-28 14:08:00 +02:00
|
|
|
free(state);
|
2007-08-10 22:34:06 +02:00
|
|
|
|
2008-12-10 19:16:30 +01:00
|
|
|
/* return total size */
|
|
|
|
return (outbytes + 4);
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|