Refactor state.cpp to state.c

This commit is contained in:
twinaphex 2020-07-05 20:13:04 +02:00
parent 1c404cc613
commit 406be2a4f0
13 changed files with 189 additions and 144 deletions

View File

@ -202,13 +202,13 @@ ifneq ($(HAVE_GRIFFIN), 1)
$(MEDNAFEN_DIR)/FileStream.cpp \
$(MEDNAFEN_DIR)/MemoryStream.cpp \
$(MEDNAFEN_DIR)/Stream.cpp \
$(MEDNAFEN_DIR)/state.cpp \
$(MEDNAFEN_DIR)/mempatcher.cpp \
$(MEDNAFEN_DIR)/okiadpcm.cpp \
$(CORE_DIR)/libretro.cpp
SOURCES_C += \
$(MEDNAFEN_DIR)/file.c \
$(MEDNAFEN_DIR)/state.c \
$(MEDNAFEN_DIR)/mednafen-endian.c
endif

View File

@ -10,7 +10,7 @@
#include "mednafen/mednafen.h"
#include "mednafen/git.h"
#include "mednafen/general.h"
#include "mednafen/state_helpers.h"
#include "mednafen/pce_fast/pce.h"
#include "mednafen/pce_fast/vdc.h"
@ -395,33 +395,33 @@ static void Emulate(EmulateSpecStruct *espec)
PCECD_ResetTS();
}
int StateAction(StateMem *sm, int load, int data_only)
extern "C" int StateAction(StateMem *sm, int load, int data_only)
{
SFORMAT StateRegs[] =
{
SFARRAY(BaseRAM, 8192),
SFVAR(PCEIODataBuffer),
SFEND
};
SFORMAT StateRegs[] =
{
SFARRAY(BaseRAM, 8192),
SFVAR(PCEIODataBuffer),
SFEND
};
//for(int i = 8192; i < 32768; i++)
// if(BaseRAM[i] != 0xFF)
// printf("%d %02x\n", i, BaseRAM[i]);
//for(int i = 8192; i < 32768; i++)
// if(BaseRAM[i] != 0xFF)
// printf("%d %02x\n", i, BaseRAM[i]);
int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "MAIN", false);
int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "MAIN", false);
ret &= HuC6280_StateAction(sm, load, data_only);
ret &= VDC_StateAction(sm, load, data_only);
ret &= psg->StateAction(sm, load, data_only);
ret &= INPUT_StateAction(sm, load, data_only);
ret &= HuC_StateAction(sm, load, data_only);
ret &= HuC6280_StateAction(sm, load, data_only);
ret &= VDC_StateAction(sm, load, data_only);
ret &= psg->StateAction(sm, load, data_only);
ret &= INPUT_StateAction(sm, load, data_only);
ret &= HuC_StateAction(sm, load, data_only);
if(load)
{
if(load)
{
}
}
return(ret);
return(ret);
}
void PCE_Power(void)

View File

@ -351,7 +351,13 @@ static INLINE uint32 round_up_pow2(uint32 v)
return(v);
}
#ifdef __cplusplus
extern "C" {
#endif
int StateAction(StateMem *sm, int load, int data_only);
#ifdef __cplusplus
}
#endif
uint8 MemRead(uint32 addr);
extern retro_log_printf_t log_cb;

View File

@ -18,15 +18,15 @@
Arcade Card emulation based on information provided by Ki and David Shadoff
*/
#include "../../mednafen.h"
#include <errno.h>
#include <string.h>
//#include "pce.h"
//#include "huc.h"
#include "../../mednafen.h"
#include "../../state_helpers.h"
#include "arcade_card.h"
#include <errno.h>
#include <string.h>
static INLINE void ACAutoIncrement(ACPort_t *port)
{
if(port->control & 0x1)

View File

@ -17,6 +17,7 @@
#include "pce.h"
#include "vdc.h"
#include "../state_helpers.h"
static uint8 dummy_bank[8192 + 8192]; // + 8192 for PC-as-ptr safety padding
HuC6280 HuCPU;

View File

@ -19,6 +19,7 @@
#include "input.h"
#include "huc.h"
#include "../mednafen-endian.h"
#include "../state_helpers.h"
static int InputTypes[5];
static uint8 *data_ptr[5];

View File

@ -30,6 +30,7 @@
#include "pcecd.h"
#include "../cdrom/SimpleFIFO.h"
#include "../state_helpers.h"
//#define PCECD_DEBUG

View File

@ -21,6 +21,7 @@
#include "../cdrom/cdromif.h"
#include "../cdrom/SimpleFIFO.h"
#include "../msvc_compat.h"
#include "../state_helpers.h"
static inline void SCSIDBG(const char *format, ...)
{

View File

@ -15,11 +15,12 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <math.h>
#include "../mednafen.h"
#include "psg.h"
#include <math.h>
#include <string.h>
#include "../state_helpers.h"
void PCEFast_PSG::SetVolume(double new_volume)
{

View File

@ -25,13 +25,15 @@ of samples between the ~60Hz peaks(just to verify that the math shown below is r
The spectrum peaked at 15734 Hz. 21477272.727272... / 3 / 15734 = 455.00(CPU cycles per scanline)"
*/
#include <math.h>
#include "pce.h"
#include "../video.h"
#include "vdc.h"
#include "huc.h"
#include "pcecd.h"
#include "../FileStream.h"
#include <math.h>
#include "../state_helpers.h"
static uint32 userle; // User layer enable.
static uint32 disabled_layer_color;

View File

@ -15,21 +15,22 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <boolean.h>
#include <compat/strl.h>
#include "mednafen.h"
#include "driver.h"
#include "general.h"
#include "state.h"
#include "video.h"
#include <compat/msvc.h>
#define RLSB MDFNSTATE_RLSB //0x80000000
#include "mednafen-endian.h"
#include "state.h"
#define RLSB MDFNSTATE_RLSB /* 0x80000000 */
/* Forward declaration */
int StateAction(StateMem *sm, int load, int data_only);
int32_t smem_read(StateMem *st, void *buffer, uint32_t len)
{
@ -74,9 +75,15 @@ int32_t smem_seek(StateMem *st, uint32_t offset, int whence)
{
switch(whence)
{
case SEEK_SET: st->loc = offset; break;
case SEEK_END: st->loc = st->len - offset; break;
case SEEK_CUR: st->loc += offset; break;
case SEEK_SET:
st->loc = offset;
break;
case SEEK_END:
st->loc = st->len - offset;
break;
case SEEK_CUR:
st->loc += offset;
break;
}
if(st->loc > st->len)
@ -118,7 +125,10 @@ int smem_read32le(StateMem *st, uint32_t *b)
static bool SubWrite(StateMem *st, SFORMAT *sf, const char *name_prefix)
{
while(sf->size || sf->name) // Size can sometimes be zero, so also check for the text name. These two should both be zero only at the end of a struct.
/* Size can sometimes be zero, so also check for the
* text name. These two should both be zero only at
* the end of a struct. */
while(sf->size || sf->name)
{
if(!sf->size || !sf->v)
{
@ -169,14 +179,14 @@ static bool SubWrite(StateMem *st, SFORMAT *sf, const char *name_prefix)
Endian_V_NE_to_LE(sf->v, bytesize);
#endif
// Special case for the evil bool type, to convert bool to 1-byte elements.
// Don't do it if we're only saving the raw data.
/* Special case for the evil bool type,
* to convert bool to 1-byte elements.
* Don't do it if we're only saving the raw data. */
if(sf->flags & MDFNSTATE_BOOL)
{
for(int32_t bool_monster = 0; bool_monster < bytesize; bool_monster++)
{
uint8_t tmp_bool = ((bool *)sf->v)[bool_monster];
//printf("Bool write: %.31s\n", sf->name);
smem_write(st, &tmp_bool, 1);
}
}
@ -222,7 +232,8 @@ static int WriteStateChunk(StateMem *st, const char *sname, SFORMAT *sf)
smem_write(st, sname_tmp, 32);
smem_write32le(st, 0); // We'll come back and write this later.
/* We'll come back and write this later. */
smem_write32le(st, 0);
data_start_pos = st->loc;
@ -249,7 +260,7 @@ static SFORMAT *FindSF(const char *name, SFORMAT *sf)
continue;
}
if (sf->size == (uint32)~0) /* Link to another SFORMAT structure. */
if (sf->size == (uint32_t)~0) /* Link to another SFORMAT structure. */
{
SFORMAT *temp_sf = FindSF(name, (SFORMAT*)sf->v);
if (temp_sf)
@ -267,11 +278,12 @@ static SFORMAT *FindSF(const char *name, SFORMAT *sf)
return NULL;
}
// Fast raw chunk reader
/* Fast raw chunk reader */
static void DOReadChunk(StateMem *st, SFORMAT *sf)
{
while(sf->size || sf->name) // Size can sometimes be zero, so also check for the text name.
// These two should both be zero only at the end of a struct.
/* Size can sometimes be zero, so also check for the text name.
* These two should both be zero only at the end of a struct. */
while(sf->size || sf->name)
{
if(!sf->size || !sf->v)
{
@ -279,7 +291,7 @@ static void DOReadChunk(StateMem *st, SFORMAT *sf)
continue;
}
if(sf->size == (uint32_t) ~0) // Link to another SFORMAT struct
if(sf->size == (uint32_t) ~0) /* Link to another SFORMAT struct */
{
DOReadChunk(st, (SFORMAT *)sf->v);
sf++;
@ -288,9 +300,12 @@ static void DOReadChunk(StateMem *st, SFORMAT *sf)
int32_t bytesize = sf->size;
// Loading raw data, bool types are stored as they appear in memory, not as single bytes in the full state format.
// In the SFORMAT structure, the size member for bool entries is the number of bool elements, not the total in-memory size,
// so we adjust it here.
/* Loading raw data, bool types are stored as they
* appear in memory, not as single bytes in the full state format.
* In the SFORMAT structure, the size member for
* bool entries is the number of bool elements, not the
* total in-memory size,
* so we adjust it here. */
if(sf->flags & MDFNSTATE_BOOL)
bytesize *= sizeof(bool);
@ -305,8 +320,10 @@ static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size)
while (st->loc < (temp + size))
{
uint32_t recorded_size; // In bytes
uint8_t toa[1 + 256]; // Don't change to char unless cast toa[0] to unsigned to smem_read() and other places.
uint32_t recorded_size; /* In bytes */
uint8_t toa[1 + 256]; /* Don't change to char unless
cast toa[0] to unsigned to smem_read()
and other places. */
if(smem_read(st, toa, 1) != 1)
{
@ -328,11 +345,10 @@ static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size)
if(tmp)
{
uint32_t expected_size = tmp->size; // In bytes
uint32_t expected_size = tmp->size; /* In bytes */
if(recorded_size != expected_size)
{
printf("Variable in save state wrong size: %s. Need: %d, got: %d\n", toa + 1, expected_size, recorded_size);
if(smem_seek(st, recorded_size, SEEK_CUR) < 0)
{
puts("Seek error");
@ -345,7 +361,8 @@ static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size)
if(tmp->flags & MDFNSTATE_BOOL)
{
// Converting downwards is necessary for the case of sizeof(bool) > 1
/* Converting downwards is necessary for the
* case of sizeof(bool) > 1 */
for(int32_t bool_monster = expected_size - 1; bool_monster >= 0; bool_monster--)
{
((bool *)tmp->v)[bool_monster] = ((uint8_t *)tmp->v)[bool_monster];
@ -372,13 +389,13 @@ static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size)
return(0);
}
}
} // while(...)
}
return 1;
}
/* This function is called by the game driver(NES, GB, GBA) to save a state. */
static int MDFNSS_StateAction_internal(void *st_p, int load, int data_only, SSDescriptor *section)
static int MDFNSS_StateAction_internal(void *st_p, int load, int data_only, struct SSDescriptor *section)
{
StateMem *st = (StateMem*)st_p;
@ -397,7 +414,7 @@ static int MDFNSS_StateAction_internal(void *st_p, int load, int data_only, SSDe
total += tmp_size + 32 + 4;
// Yay, we found the section
/* Yay, we found the section */
if(!strncmp(sname, section->name, 32))
{
if(!ReadStateChunk(st, section->sf, tmp_size))
@ -422,7 +439,7 @@ static int MDFNSS_StateAction_internal(void *st_p, int load, int data_only, SSDe
puts("Reverse seek error");
return(0);
}
if(!found && !section->optional) // Not found. We are sad!
if(!found && !section->optional) /* Not found. We are sad! */
{
printf("Section missing: %.32s\n", section->name);
return(0);
@ -439,7 +456,7 @@ static int MDFNSS_StateAction_internal(void *st_p, int load, int data_only, SSDe
int MDFNSS_StateAction(void *st_p, int load, int data_only, SFORMAT *sf, const char *name, bool optional)
{
SSDescriptor love;
struct SSDescriptor love;
StateMem *st = (StateMem*)st_p;
love.sf = sf;
@ -449,7 +466,7 @@ int MDFNSS_StateAction(void *st_p, int load, int data_only, SFORMAT *sf, const c
return(MDFNSS_StateAction_internal(st, load, 0, &love));
}
int MDFNSS_SaveSM(void *st_p, int, int, const void*, const void*, const void*)
int MDFNSS_SaveSM(void *st_p, int a, int b, const void *c, const void *d, const void *e)
{
uint8_t header[32];
StateMem *st = (StateMem*)st_p;
@ -474,7 +491,7 @@ int MDFNSS_SaveSM(void *st_p, int, int, const void*, const void*, const void*)
return(1);
}
int MDFNSS_LoadSM(void *st_p, int, int)
int MDFNSS_LoadSM(void *st_p, int a, int b)
{
uint8_t header[32];
uint32_t stateversion;

View File

@ -1,7 +1,18 @@
#ifndef _STATE_H
#define _STATE_H
#include <retro_inline.h>
#include <stdint.h.>
/* Flag for a single, >= 1 byte native-endian variable */
#define MDFNSTATE_RLSB 0x80000000
/* 32-bit native-endian elements */
#define MDFNSTATE_RLSB32 0x40000000
/* 16-bit native-endian elements */
#define MDFNSTATE_RLSB16 0x20000000
/* 64-bit native-endian elements */
#define MDFNSTATE_RLSB64 0x10000000
#define MDFNSTATE_BOOL 0x08000000
typedef struct
{
@ -9,87 +20,20 @@ typedef struct
uint32_t loc;
uint32_t len;
uint32_t malloced;
uint32_t initial_malloc; // A setting!
uint32_t initial_malloc; /* A setting! */
} StateMem;
// Eh, we abuse the smem_* in-memory stream code
// in a few other places. :)
int32_t smem_read(StateMem *st, void *buffer, uint32_t len);
int32_t smem_write(StateMem *st, void *buffer, uint32_t len);
int32_t smem_putc(StateMem *st, int value);
int32_t smem_seek(StateMem *st, uint32_t offset, int whence);
int smem_write32le(StateMem *st, uint32_t b);
int smem_read32le(StateMem *st, uint32_t *b);
int MDFNSS_SaveSM(void *st, int, int, const void*, const void*, const void*);
int MDFNSS_LoadSM(void *st, int, int);
// Flag for a single, >= 1 byte native-endian variable
#define MDFNSTATE_RLSB 0x80000000
// 32-bit native-endian elements
#define MDFNSTATE_RLSB32 0x40000000
// 16-bit native-endian elements
#define MDFNSTATE_RLSB16 0x20000000
// 64-bit native-endian elements
#define MDFNSTATE_RLSB64 0x10000000
#define MDFNSTATE_BOOL 0x08000000
typedef struct {
void *v; // Pointer to the variable/array
uint32_t size; // Length, in bytes, of the data to be saved EXCEPT:
// In the case of MDFNSTATE_BOOL, it is the number of bool elements to save(bool is not always 1-byte).
// If 0, the subchunk isn't saved.
uint32_t flags; // Flags
const char *name; // Name
typedef struct
{
void *v; /* Pointer to the variable/array */
uint32_t size; /* Length, in bytes, of the data to be saved EXCEPT:
* In the case of MDFNSTATE_BOOL, it is the number of bool elements to save(bool is not always 1-byte).
* If 0, the subchunk isn't saved. */
uint32_t flags; /* Flags */
const char *name; /* Name */
} SFORMAT;
INLINE bool SF_IS_BOOL(bool *) { return(1); }
INLINE bool SF_IS_BOOL(void *) { return(0); }
INLINE uint32_t SF_FORCE_AB(bool *) { return(0); }
INLINE uint32_t SF_FORCE_A8(int8_t *) { return(0); }
INLINE uint32_t SF_FORCE_A8(uint8_t *) { return(0); }
INLINE uint32_t SF_FORCE_A16(int16_t *) { return(0); }
INLINE uint32_t SF_FORCE_A16(uint16_t *) { return(0); }
INLINE uint32_t SF_FORCE_A32(int32_t *) { return(0); }
INLINE uint32_t SF_FORCE_A32(uint32_t *) { return(0); }
INLINE uint32_t SF_FORCE_A64(int64_t *) { return(0); }
INLINE uint32_t SF_FORCE_A64(uint64_t *) { return(0); }
INLINE uint32_t SF_FORCE_D(double *) { return(0); }
#define SFVARN(x, n) { &(x), SF_IS_BOOL(&(x)) ? 1 : (uint32_t)sizeof(x), MDFNSTATE_RLSB | (SF_IS_BOOL(&(x)) ? MDFNSTATE_BOOL : 0), n }
#define SFVAR(x) SFVARN((x), #x)
#define SFARRAYN(x, l, n) { (x), (uint32_t)(l), 0 | SF_FORCE_A8(x), n }
#define SFARRAY(x, l) SFARRAYN((x), (l), #x)
#define SFARRAYBN(x, l, n) { (x), (uint32_t)(l), MDFNSTATE_BOOL | SF_FORCE_AB(x), n }
#define SFARRAYB(x, l) SFARRAYBN((x), (l), #x)
#define SFARRAY16N(x, l, n) { (x), (uint32_t)((l) * sizeof(uint16_t)), MDFNSTATE_RLSB16 | SF_FORCE_A16(x), n }
#define SFARRAY16(x, l) SFARRAY16N((x), (l), #x)
#define SFARRAY32N(x, l, n) { (x), (uint32_t)((l) * sizeof(uint32_t)), MDFNSTATE_RLSB32 | SF_FORCE_A32(x), n }
#define SFARRAY32(x, l) SFARRAY32N((x), (l), #x)
#define SFARRAY64N(x, l, n) { (x), (uint32_t)((l) * sizeof(uint64_t)), MDFNSTATE_RLSB64 | SF_FORCE_A64(x), n }
#define SFARRAY64(x, l) SFARRAY64N((x), (l), #x)
#define SFARRAYDN(x, l, n) { (x), (uint32_t)((l) * 8), MDFNSTATE_RLSB64 | SF_FORCE_D(x), n }
#define SFARRAYD(x, l) SFARRAYDN((x), (l), #x)
#define SFEND { 0, 0, 0, 0 }
// State-Section Descriptor
/* State-Section Descriptor */
struct SSDescriptor
{
SFORMAT *sf;
@ -97,6 +41,27 @@ struct SSDescriptor
bool optional;
};
#ifdef __cplusplus
extern "C" {
#endif
/* Eh, we abuse the smem_* in-memory stream code
* in a few other places. :) */
int32_t smem_read(StateMem *st, void *buffer, uint32_t len);
int32_t smem_write(StateMem *st, void *buffer, uint32_t len);
int32_t smem_putc(StateMem *st, int value);
int32_t smem_seek(StateMem *st, uint32_t offset, int whence);
int smem_write32le(StateMem *st, uint32_t b);
int smem_read32le(StateMem *st, uint32_t *b);
int MDFNSS_SaveSM(void *st, int a, int b, const void *c, const void *d, const void *e);
int MDFNSS_LoadSM(void *st, int a, int b);
int MDFNSS_StateAction(void *st, int load, int data_only, SFORMAT *sf, const char *name, bool optional);
#ifdef __cplusplus
}
#endif
#endif

50
mednafen/state_helpers.h Normal file
View File

@ -0,0 +1,50 @@
#ifndef _STATE_HELPERS_H
#define _STATE_HELPERS_H
#include <stdint.h>
#include <retro_inline.h>
INLINE bool SF_IS_BOOL(bool *) { return(1); }
INLINE bool SF_IS_BOOL(void *) { return(0); }
INLINE uint32_t SF_FORCE_AB(bool *) { return(0); }
INLINE uint32_t SF_FORCE_A8(int8_t *) { return(0); }
INLINE uint32_t SF_FORCE_A8(uint8_t *) { return(0); }
INLINE uint32_t SF_FORCE_A16(int16_t *) { return(0); }
INLINE uint32_t SF_FORCE_A16(uint16_t *) { return(0); }
INLINE uint32_t SF_FORCE_A32(int32_t *) { return(0); }
INLINE uint32_t SF_FORCE_A32(uint32_t *) { return(0); }
INLINE uint32_t SF_FORCE_A64(int64_t *) { return(0); }
INLINE uint32_t SF_FORCE_A64(uint64_t *) { return(0); }
INLINE uint32_t SF_FORCE_D(double *) { return(0); }
#define SFVARN(x, n) { &(x), SF_IS_BOOL(&(x)) ? 1 : (uint32_t)sizeof(x), MDFNSTATE_RLSB | (SF_IS_BOOL(&(x)) ? MDFNSTATE_BOOL : 0), n }
#define SFVAR(x) SFVARN((x), #x)
#define SFARRAYN(x, l, n) { (x), (uint32_t)(l), 0 | SF_FORCE_A8(x), n }
#define SFARRAY(x, l) SFARRAYN((x), (l), #x)
#define SFARRAYBN(x, l, n) { (x), (uint32_t)(l), MDFNSTATE_BOOL | SF_FORCE_AB(x), n }
#define SFARRAYB(x, l) SFARRAYBN((x), (l), #x)
#define SFARRAY16N(x, l, n) { (x), (uint32_t)((l) * sizeof(uint16_t)), MDFNSTATE_RLSB16 | SF_FORCE_A16(x), n }
#define SFARRAY16(x, l) SFARRAY16N((x), (l), #x)
#define SFARRAY32N(x, l, n) { (x), (uint32_t)((l) * sizeof(uint32_t)), MDFNSTATE_RLSB32 | SF_FORCE_A32(x), n }
#define SFARRAY32(x, l) SFARRAY32N((x), (l), #x)
#define SFARRAY64N(x, l, n) { (x), (uint32_t)((l) * sizeof(uint64_t)), MDFNSTATE_RLSB64 | SF_FORCE_A64(x), n }
#define SFARRAY64(x, l) SFARRAY64N((x), (l), #x)
#define SFARRAYDN(x, l, n) { (x), (uint32_t)((l) * 8), MDFNSTATE_RLSB64 | SF_FORCE_D(x), n }
#define SFARRAYD(x, l) SFARRAYDN((x), (l), #x)
#define SFEND { 0, 0, 0, 0 }
#endif