| 1 | #ifndef _STATE_H |
| 2 | #define _STATE_H |
| 3 | |
| 4 | #include "mednafen-types.h" |
| 5 | |
| 6 | typedef struct |
| 7 | { |
| 8 | uint8 *data; |
| 9 | uint32 loc; |
| 10 | uint32 len; |
| 11 | uint32 malloced; |
| 12 | uint32 initial_malloc; // A setting! |
| 13 | } StateMem; |
| 14 | |
| 15 | // Eh, we abuse the smem_* in-memory stream code |
| 16 | // in a few other places. :) |
| 17 | int32 smem_read(StateMem *st, void *buffer, uint32 len); |
| 18 | int32 smem_write(StateMem *st, void *buffer, uint32 len); |
| 19 | int32 smem_putc(StateMem *st, int value); |
| 20 | int32 smem_tell(StateMem *st); |
| 21 | int32 smem_seek(StateMem *st, uint32 offset, int whence); |
| 22 | int smem_write32le(StateMem *st, uint32 b); |
| 23 | int smem_read32le(StateMem *st, uint32 *b); |
| 24 | |
| 25 | int MDFNSS_SaveSM(void *st, int, int, const void*, const void*, const void*); |
| 26 | int MDFNSS_LoadSM(void *st, int, int); |
| 27 | |
| 28 | // Flag for a single, >= 1 byte native-endian variable |
| 29 | #define MDFNSTATE_RLSB 0x80000000 |
| 30 | |
| 31 | // 32-bit native-endian elements |
| 32 | #define MDFNSTATE_RLSB32 0x40000000 |
| 33 | |
| 34 | // 16-bit native-endian elements |
| 35 | #define MDFNSTATE_RLSB16 0x20000000 |
| 36 | |
| 37 | // 64-bit native-endian elements |
| 38 | #define MDFNSTATE_RLSB64 0x10000000 |
| 39 | |
| 40 | #define MDFNSTATE_BOOL 0x08000000 |
| 41 | |
| 42 | typedef struct { |
| 43 | void *v; // Pointer to the variable/array |
| 44 | uint32 size; // Length, in bytes, of the data to be saved EXCEPT: |
| 45 | // In the case of MDFNSTATE_BOOL, it is the number of bool elements to save(bool is not always 1-byte). |
| 46 | // If 0, the subchunk isn't saved. |
| 47 | uint32 flags; // Flags |
| 48 | const char *name; // Name |
| 49 | //uint32 struct_size; // Only used for MDFNSTATE_ARRAYOFS, sizeof(struct) that members of the linked SFORMAT struct are in. |
| 50 | } SFORMAT; |
| 51 | |
| 52 | INLINE bool SF_IS_BOOL(bool *) { return(1); } |
| 53 | INLINE bool SF_IS_BOOL(void *) { return(0); } |
| 54 | |
| 55 | INLINE uint32 SF_FORCE_AB(bool *) { return(0); } |
| 56 | |
| 57 | INLINE uint32 SF_FORCE_A8(int8 *) { return(0); } |
| 58 | INLINE uint32 SF_FORCE_A8(uint8 *) { return(0); } |
| 59 | |
| 60 | INLINE uint32 SF_FORCE_A16(int16 *) { return(0); } |
| 61 | INLINE uint32 SF_FORCE_A16(uint16 *) { return(0); } |
| 62 | |
| 63 | INLINE uint32 SF_FORCE_A32(int32 *) { return(0); } |
| 64 | INLINE uint32 SF_FORCE_A32(uint32 *) { return(0); } |
| 65 | |
| 66 | INLINE uint32 SF_FORCE_A64(int64 *) { return(0); } |
| 67 | INLINE uint32 SF_FORCE_A64(uint64 *) { return(0); } |
| 68 | |
| 69 | INLINE uint32 SF_FORCE_D(double *) { return(0); } |
| 70 | |
| 71 | #define SFVARN(x, n) { &(x), SF_IS_BOOL(&(x)) ? 1 : (uint32_t)sizeof(x), MDFNSTATE_RLSB | (SF_IS_BOOL(&(x)) ? MDFNSTATE_BOOL : 0), n } |
| 72 | #define SFVAR(x) SFVARN((x), #x) |
| 73 | |
| 74 | #define SFARRAYN(x, l, n) { (x), (uint32)(l), 0 | SF_FORCE_A8(x), n } |
| 75 | #define SFARRAY(x, l) SFARRAYN((x), (l), #x) |
| 76 | |
| 77 | #define SFARRAYBN(x, l, n) { (x), (uint32)(l), MDFNSTATE_BOOL | SF_FORCE_AB(x), n } |
| 78 | #define SFARRAYB(x, l) SFARRAYBN((x), (l), #x) |
| 79 | |
| 80 | #define SFARRAY16N(x, l, n) { (x), (uint32)((l) * sizeof(uint16)), MDFNSTATE_RLSB16 | SF_FORCE_A16(x), n } |
| 81 | #define SFARRAY16(x, l) SFARRAY16N((x), (l), #x) |
| 82 | |
| 83 | #define SFARRAY32N(x, l, n) { (x), (uint32)((l) * sizeof(uint32)), MDFNSTATE_RLSB32 | SF_FORCE_A32(x), n } |
| 84 | #define SFARRAY32(x, l) SFARRAY32N((x), (l), #x) |
| 85 | |
| 86 | #define SFARRAY64N(x, l, n) { (x), (uint32)((l) * sizeof(uint64)), MDFNSTATE_RLSB64 | SF_FORCE_A64(x), n } |
| 87 | #define SFARRAY64(x, l) SFARRAY64N((x), (l), #x) |
| 88 | |
| 89 | #if SIZEOF_DOUBLE != 8 |
| 90 | #error "sizeof(double) != 8" |
| 91 | #endif |
| 92 | |
| 93 | #define SFARRAYDN(x, l, n) { (x), (uint32)((l) * 8), MDFNSTATE_RLSB64 | SF_FORCE_D(x), n } |
| 94 | #define SFARRAYD(x, l) SFARRAYDN((x), (l), #x) |
| 95 | |
| 96 | #define SFEND { 0, 0, 0, 0 } |
| 97 | |
| 98 | #include <vector> |
| 99 | |
| 100 | // State-Section Descriptor |
| 101 | class SSDescriptor |
| 102 | { |
| 103 | public: |
| 104 | SSDescriptor(SFORMAT *n_sf, const char *n_name, bool n_optional = 0) |
| 105 | { |
| 106 | sf = n_sf; |
| 107 | name = n_name; |
| 108 | optional = n_optional; |
| 109 | } |
| 110 | ~SSDescriptor(void) |
| 111 | { |
| 112 | |
| 113 | } |
| 114 | |
| 115 | SFORMAT *sf; |
| 116 | const char *name; |
| 117 | bool optional; |
| 118 | }; |
| 119 | |
| 120 | int MDFNSS_StateAction(void *st, int load, int data_only, std::vector <SSDescriptor> §ions); |
| 121 | int MDFNSS_StateAction(void *st, int load, int data_only, SFORMAT *sf, const char *name, bool optional = 0); |
| 122 | |
| 123 | #endif |