all repos — mgba @ 5ea104844d58095e3dc002d93074d3302976cae6

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