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