Genesis-Plus-GX/source/state.c

190 lines
6.5 KiB
C
Raw Normal View History

2008-08-07 14:26:07 +02:00
/***************************************************************************************
2007-08-10 22:34:06 +02:00
* Genesis Plus 1.2a
2008-08-07 14:26:07 +02:00
* FreezeState support
2007-08-10 22:34:06 +02:00
*
2008-08-07 14:26:07 +02:00
* coded by Eke-Eke, GC/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"
2008-08-07 14:26:07 +02:00
static unsigned char state[0x24000];
static unsigned int bufferptr;
2007-08-10 22:34:06 +02:00
2008-12-04 20:32:22 +01:00
static inline void load_param(void *param, unsigned int size)
2007-08-10 22:34:06 +02:00
{
memcpy(param, &state[bufferptr], size);
bufferptr+= size;
}
2008-12-04 20:32:22 +01:00
static inline void save_param(void *param, unsigned int size)
2007-08-10 22:34:06 +02:00
{
memcpy(&state[bufferptr], param, size);
bufferptr+= size;
}
void state_load(unsigned char *buffer)
{
2008-12-04 20:32:22 +01:00
/* reset buffer pointer */
2008-08-07 14:26:07 +02:00
bufferptr = 0;
2007-08-10 22:34:06 +02:00
2008-12-04 20:32:22 +01:00
/* uncompress savestate */
unsigned long inbytes, outbytes;
memcpy(&inbytes, buffer, 4);
2008-08-07 14:26:07 +02:00
outbytes = 0x24000;
uncompress ((Bytef *)state, &outbytes, (Bytef *)(buffer + 4), inbytes);
2007-08-10 22:34:06 +02:00
2008-12-04 20:32:22 +01:00
/* reset system */
2007-08-10 22:34:06 +02:00
system_reset();
2008-12-04 20:32:22 +01:00
m68k_memory_map[0].base = default_rom;
2008-08-07 14:26:07 +02:00
// GENESIS
2007-08-10 22:34:06 +02:00
load_param(work_ram, sizeof(work_ram));
load_param(zram, sizeof(zram));
load_param(&zbusreq, sizeof(zbusreq));
load_param(&zreset, sizeof(zreset));
load_param(&zbank, sizeof(zbank));
2008-08-07 14:26:07 +02:00
zbusack = 1 ^(zbusreq & zreset);
2007-08-10 22:34:06 +02:00
2008-08-07 14:26:07 +02:00
// IO
2007-08-10 22:34:06 +02:00
load_param(io_reg, sizeof(io_reg));
2008-08-07 14:26:07 +02:00
// VDP
2008-12-04 20:32:22 +01:00
uint8 temp_reg[0x20];
2007-08-10 22:34:06 +02:00
load_param(sat, sizeof(sat));
load_param(vram, sizeof(vram));
load_param(cram, sizeof(cram));
load_param(vsram, sizeof(vsram));
2008-08-07 14:26:07 +02:00
load_param(temp_reg, sizeof(temp_reg));
2007-08-10 22:34:06 +02:00
load_param(&addr, sizeof(addr));
load_param(&addr_latch, sizeof(addr_latch));
load_param(&code, sizeof(code));
load_param(&pending, sizeof(pending));
load_param(&status, sizeof(status));
2008-08-07 14:26:07 +02:00
load_param(&dmafill, sizeof(dmafill));
2007-08-10 22:34:06 +02:00
load_param(&hint_pending, sizeof(hint_pending));
load_param(&vint_pending, sizeof(vint_pending));
2008-12-04 20:32:22 +01:00
load_param(&irq_status, sizeof(irq_status));
2008-08-07 14:26:07 +02:00
vdp_restore(temp_reg);
2007-08-10 22:34:06 +02:00
2008-08-07 14:26:07 +02:00
// FM
2007-08-10 22:34:06 +02:00
load_param(fm_reg,sizeof(fm_reg));
fm_restore();
2008-08-07 14:26:07 +02:00
// PSG
load_param(SN76489_GetContextPtr (0),SN76489_GetContextSize ());
2007-08-10 22:34:06 +02:00
2008-08-07 14:26:07 +02:00
// 68000
2008-12-04 20:32:22 +01:00
uint16 tmp16;
uint32 tmp32;
2007-08-10 22:34:06 +02:00
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-08-07 14:26:07 +02:00
// Z80
load_param(&Z80, sizeof(Z80_Regs));
2007-08-10 22:34:06 +02:00
}
int state_save(unsigned char *buffer)
{
2008-12-04 20:32:22 +01:00
/* reset buffer pointer */
2007-08-10 22:34:06 +02:00
bufferptr = 0;
2008-08-07 14:26:07 +02:00
// GENESIS
2007-08-10 22:34:06 +02:00
save_param(work_ram, sizeof(work_ram));
save_param(zram, sizeof(zram));
save_param(&zbusreq, sizeof(zbusreq));
save_param(&zreset, sizeof(zreset));
save_param(&zbank, sizeof(zbank));
2008-08-07 14:26:07 +02:00
// IO
2007-08-10 22:34:06 +02:00
save_param(io_reg, sizeof(io_reg));
2008-08-07 14:26:07 +02:00
// VDP
2007-08-10 22:34:06 +02:00
save_param(sat, sizeof(sat));
save_param(vram, sizeof(vram));
save_param(cram, sizeof(cram));
save_param(vsram, sizeof(vsram));
save_param(reg, sizeof(reg));
save_param(&addr, sizeof(addr));
save_param(&addr_latch, sizeof(addr_latch));
save_param(&code, sizeof(code));
save_param(&pending, sizeof(pending));
save_param(&status, sizeof(status));
2008-08-07 14:26:07 +02:00
save_param(&dmafill, sizeof(dmafill));
2007-08-10 22:34:06 +02:00
save_param(&hint_pending, sizeof(hint_pending));
save_param(&vint_pending, sizeof(vint_pending));
2008-12-04 20:32:22 +01:00
save_param(&irq_status, sizeof(irq_status));
2007-08-10 22:34:06 +02:00
2008-08-07 14:26:07 +02:00
// FM
2007-08-10 22:34:06 +02:00
save_param(fm_reg,sizeof(fm_reg));
2008-08-07 14:26:07 +02:00
// PSG
save_param(SN76489_GetContextPtr (0),SN76489_GetContextSize ());
// 68000
2008-12-04 20:32:22 +01:00
uint16 tmp16;
uint32 tmp32;
2008-08-07 14:26:07 +02:00
tmp32 = m68k_get_reg(NULL, M68K_REG_D0); save_param(&tmp32, 4);
2007-08-10 22:34:06 +02:00
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-08-07 14:26:07 +02:00
// Z80
save_param(&Z80, sizeof(Z80_Regs));
2007-08-10 22:34:06 +02:00
/* compress state file */
2008-12-04 20:32:22 +01:00
unsigned long inbytes = bufferptr;
unsigned long outbytes = 0x26000;
2008-08-07 14:26:07 +02:00
compress2 ((Bytef *)(buffer + 4), &outbytes, (Bytef *)state, inbytes, 9);
memcpy(buffer, &outbytes, 4);
2007-08-10 22:34:06 +02:00
/* return total size */
2008-08-07 14:26:07 +02:00
return (outbytes + 4);
2007-08-10 22:34:06 +02:00
}