all repos — mgba @ 4b3df31e4970c2db065e3ed801ef179ce4b9955a

mGBA Game Boy Advance Emulator

src/gba/savedata.h (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#ifndef GBA_SAVEDATA_H
  7#define GBA_SAVEDATA_H
  8
  9#include "util/common.h"
 10
 11struct VFile;
 12
 13enum SavedataType {
 14	SAVEDATA_AUTODETECT = -1,
 15	SAVEDATA_FORCE_NONE = 0,
 16	SAVEDATA_SRAM = 1,
 17	SAVEDATA_FLASH512 = 2,
 18	SAVEDATA_FLASH1M = 3,
 19	SAVEDATA_EEPROM = 4
 20};
 21
 22enum SavedataCommand {
 23	EEPROM_COMMAND_NULL = 0,
 24	EEPROM_COMMAND_PENDING = 1,
 25	EEPROM_COMMAND_WRITE = 2,
 26	EEPROM_COMMAND_READ_PENDING = 3,
 27	EEPROM_COMMAND_READ = 4,
 28
 29	FLASH_COMMAND_START = 0xAA,
 30	FLASH_COMMAND_CONTINUE = 0x55,
 31
 32	FLASH_COMMAND_ERASE_CHIP = 0x10,
 33	FLASH_COMMAND_ERASE_SECTOR = 0x30,
 34
 35	FLASH_COMMAND_NONE = 0,
 36	FLASH_COMMAND_ERASE = 0x80,
 37	FLASH_COMMAND_ID = 0x90,
 38	FLASH_COMMAND_PROGRAM = 0xA0,
 39	FLASH_COMMAND_SWITCH_BANK = 0xB0,
 40	FLASH_COMMAND_TERMINATE = 0xF0
 41};
 42
 43enum FlashStateMachine {
 44	FLASH_STATE_RAW = 0,
 45	FLASH_STATE_START = 1,
 46	FLASH_STATE_CONTINUE = 2,
 47};
 48
 49enum FlashManufacturer {
 50	FLASH_MFG_PANASONIC = 0x1B32,
 51	FLASH_MFG_SANYO = 0x1362
 52};
 53
 54enum SavedataDirty {
 55	SAVEDATA_DIRT_NEW = 1,
 56	SAVEDATA_DIRT_SEEN = 2
 57};
 58
 59enum {
 60	SAVEDATA_FLASH_BASE = 0x0E005555,
 61
 62	FLASH_BASE_HI = 0x5555,
 63	FLASH_BASE_LO = 0x2AAA
 64};
 65
 66struct GBASavedata {
 67	enum SavedataType type;
 68	uint8_t* data;
 69	enum SavedataCommand command;
 70	struct VFile* vf;
 71
 72	int mapMode;
 73	struct VFile* realVf;
 74
 75	int32_t readBitsRemaining;
 76	uint32_t readAddress;
 77	uint32_t writeAddress;
 78
 79	uint8_t* currentBank;
 80
 81	bool realisticTiming;
 82	unsigned settling;
 83	int dust;
 84
 85	enum SavedataDirty dirty;
 86	uint32_t dirtAge;
 87
 88	enum FlashStateMachine flashState;
 89};
 90
 91void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf);
 92void GBASavedataDeinit(struct GBASavedata* savedata);
 93
 94void GBASavedataMask(struct GBASavedata* savedata, struct VFile* vf);
 95void GBASavedataUnmask(struct GBASavedata* savedata);
 96bool GBASavedataClone(struct GBASavedata* savedata, struct VFile* out);
 97void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type, bool realisticTiming);
 98
 99void GBASavedataInitFlash(struct GBASavedata* savedata, bool realisticTiming);
100void GBASavedataInitEEPROM(struct GBASavedata* savedata);
101void GBASavedataInitSRAM(struct GBASavedata* savedata);
102
103uint8_t GBASavedataReadFlash(struct GBASavedata* savedata, uint16_t address);
104void GBASavedataWriteFlash(struct GBASavedata* savedata, uint16_t address, uint8_t value);
105
106uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata);
107void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize);
108
109void GBASavedataClean(struct GBASavedata* savedata, uint32_t frameCount);
110
111struct GBASerializedState;
112void GBASavedataSerialize(const struct GBASavedata* savedata, struct GBASerializedState* state, bool includeData);
113void GBASavedataDeserialize(struct GBASavedata* savedata, const struct GBASerializedState* state, bool includeData);
114
115#endif