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 bool maskWriteback;
78 struct VFile* realVf;
79
80 int32_t readBitsRemaining;
81 uint32_t readAddress;
82 uint32_t writeAddress;
83
84 uint8_t* currentBank;
85
86 bool realisticTiming;
87 unsigned settling;
88 int dust;
89
90 enum SavedataDirty dirty;
91 uint32_t dirtAge;
92
93 enum FlashStateMachine flashState;
94};
95
96void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf);
97void GBASavedataDeinit(struct GBASavedata* savedata);
98
99void GBASavedataMask(struct GBASavedata* savedata, struct VFile* vf, bool writeback);
100void GBASavedataUnmask(struct GBASavedata* savedata);
101size_t GBASavedataSize(struct GBASavedata* savedata);
102bool GBASavedataClone(struct GBASavedata* savedata, struct VFile* out);
103bool GBASavedataLoad(struct GBASavedata* savedata, struct VFile* in);
104void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type, bool realisticTiming);
105
106void GBASavedataInitFlash(struct GBASavedata* savedata, bool realisticTiming);
107void GBASavedataInitEEPROM(struct GBASavedata* savedata, bool realisticTiming);
108void GBASavedataInitSRAM(struct GBASavedata* savedata);
109
110uint8_t GBASavedataReadFlash(struct GBASavedata* savedata, uint16_t address);
111void GBASavedataWriteFlash(struct GBASavedata* savedata, uint16_t address, uint8_t value);
112
113uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata);
114void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize);
115
116void GBASavedataClean(struct GBASavedata* savedata, uint32_t frameCount);
117
118struct GBASerializedState;
119void GBASavedataSerialize(const struct GBASavedata* savedata, struct GBASerializedState* state);
120void GBASavedataDeserialize(struct GBASavedata* savedata, const struct GBASerializedState* state);
121
122#endif