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 bool realisticTiming;
91 unsigned settling;
92 struct mTimingEvent dust;
93
94 enum SavedataDirty dirty;
95 uint32_t dirtAge;
96
97 enum FlashStateMachine flashState;
98};
99
100void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf);
101void GBASavedataDeinit(struct GBASavedata* savedata);
102
103void GBASavedataMask(struct GBASavedata* savedata, struct VFile* vf, bool writeback);
104void GBASavedataUnmask(struct GBASavedata* savedata);
105size_t GBASavedataSize(struct GBASavedata* savedata);
106bool GBASavedataClone(struct GBASavedata* savedata, struct VFile* out);
107bool GBASavedataLoad(struct GBASavedata* savedata, struct VFile* in);
108void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type, bool realisticTiming);
109
110void GBASavedataInitFlash(struct GBASavedata* savedata, bool realisticTiming);
111void GBASavedataInitEEPROM(struct GBASavedata* savedata, bool realisticTiming);
112void GBASavedataInitSRAM(struct GBASavedata* savedata);
113
114uint8_t GBASavedataReadFlash(struct GBASavedata* savedata, uint16_t address);
115void GBASavedataWriteFlash(struct GBASavedata* savedata, uint16_t address, uint8_t value);
116
117uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata);
118void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize);
119
120void GBASavedataClean(struct GBASavedata* savedata, uint32_t frameCount);
121
122struct GBASerializedState;
123void GBASavedataSerialize(const struct GBASavedata* savedata, struct GBASerializedState* state);
124void GBASavedataDeserialize(struct GBASavedata* savedata, const struct GBASerializedState* state);
125
126CXX_GUARD_END
127
128#endif