all repos — mgba @ c3e02f9c92f7f834a8baef8fc597ff6c0f5eb6b7

mGBA Game Boy Advance Emulator

include/mgba/internal/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 <mgba-util/common.h>
 10
 11CXX_GUARD_START
 12
 13#include <mgba/core/log.h>
 14#include <mgba/core/timing.h>
 15
 16mLOG_DECLARE_CATEGORY(GBA_SAVE);
 17
 18struct VFile;
 19
 20enum SavedataType {
 21	SAVEDATA_AUTODETECT = -1,
 22	SAVEDATA_FORCE_NONE = 0,
 23	SAVEDATA_SRAM = 1,
 24	SAVEDATA_FLASH512 = 2,
 25	SAVEDATA_FLASH1M = 3,
 26	SAVEDATA_EEPROM = 4
 27};
 28
 29enum SavedataCommand {
 30	EEPROM_COMMAND_NULL = 0,
 31	EEPROM_COMMAND_PENDING = 1,
 32	EEPROM_COMMAND_WRITE = 2,
 33	EEPROM_COMMAND_READ_PENDING = 3,
 34	EEPROM_COMMAND_READ = 4,
 35
 36	FLASH_COMMAND_START = 0xAA,
 37	FLASH_COMMAND_CONTINUE = 0x55,
 38
 39	FLASH_COMMAND_ERASE_CHIP = 0x10,
 40	FLASH_COMMAND_ERASE_SECTOR = 0x30,
 41
 42	FLASH_COMMAND_NONE = 0,
 43	FLASH_COMMAND_ERASE = 0x80,
 44	FLASH_COMMAND_ID = 0x90,
 45	FLASH_COMMAND_PROGRAM = 0xA0,
 46	FLASH_COMMAND_SWITCH_BANK = 0xB0,
 47	FLASH_COMMAND_TERMINATE = 0xF0
 48};
 49
 50enum FlashStateMachine {
 51	FLASH_STATE_RAW = 0,
 52	FLASH_STATE_START = 1,
 53	FLASH_STATE_CONTINUE = 2,
 54};
 55
 56enum FlashManufacturer {
 57	FLASH_MFG_PANASONIC = 0x1B32,
 58	FLASH_MFG_SANYO = 0x1362
 59};
 60
 61enum SavedataDirty {
 62	SAVEDATA_DIRT_NEW = 1,
 63	SAVEDATA_DIRT_SEEN = 2
 64};
 65
 66enum {
 67	SAVEDATA_FLASH_BASE = 0x0E005555,
 68
 69	FLASH_BASE_HI = 0x5555,
 70	FLASH_BASE_LO = 0x2AAA
 71};
 72
 73struct GBASavedata {
 74	enum SavedataType type;
 75	uint8_t* data;
 76	enum SavedataCommand command;
 77	struct VFile* vf;
 78
 79	int mapMode;
 80	bool maskWriteback;
 81	struct VFile* realVf;
 82
 83	int8_t readBitsRemaining;
 84	uint32_t readAddress;
 85	uint32_t writeAddress;
 86
 87	uint8_t* currentBank;
 88
 89	struct mTiming* timing;
 90	unsigned settling;
 91	struct mTimingEvent dust;
 92
 93	enum SavedataDirty dirty;
 94	uint32_t dirtAge;
 95
 96	enum FlashStateMachine flashState;
 97};
 98
 99void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf);
100void GBASavedataDeinit(struct GBASavedata* savedata);
101
102void GBASavedataMask(struct GBASavedata* savedata, struct VFile* vf, bool writeback);
103void GBASavedataUnmask(struct GBASavedata* savedata);
104size_t GBASavedataSize(const struct GBASavedata* savedata);
105bool GBASavedataClone(struct GBASavedata* savedata, struct VFile* out);
106bool GBASavedataLoad(struct GBASavedata* savedata, struct VFile* in);
107void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type);
108
109void GBASavedataInitFlash(struct GBASavedata* savedata);
110void GBASavedataInitEEPROM(struct GBASavedata* savedata);
111void GBASavedataInitSRAM(struct GBASavedata* savedata);
112
113uint8_t GBASavedataReadFlash(struct GBASavedata* savedata, uint16_t address);
114void GBASavedataWriteFlash(struct GBASavedata* savedata, uint16_t address, uint8_t value);
115
116uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata);
117void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize);
118
119void GBASavedataClean(struct GBASavedata* savedata, uint32_t frameCount);
120
121struct GBASerializedState;
122void GBASavedataSerialize(const struct GBASavedata* savedata, struct GBASerializedState* state);
123void GBASavedataDeserialize(struct GBASavedata* savedata, const struct GBASerializedState* state);
124
125CXX_GUARD_END
126
127#endif