src/gba/gba-savedata.h (view raw)
1/* Copyright (c) 2013-2014 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
11struct VFile;
12
13enum SavedataType {
14 SAVEDATA_NONE = 0,
15 SAVEDATA_SRAM,
16 SAVEDATA_FLASH512,
17 SAVEDATA_FLASH1M,
18 SAVEDATA_EEPROM
19};
20
21enum SavedataCommand {
22 EEPROM_COMMAND_NULL = 0,
23 EEPROM_COMMAND_PENDING = 1,
24 EEPROM_COMMAND_WRITE = 2,
25 EEPROM_COMMAND_READ_PENDING = 3,
26 EEPROM_COMMAND_READ = 4,
27
28 FLASH_COMMAND_START = 0xAA,
29 FLASH_COMMAND_CONTINUE = 0x55,
30
31 FLASH_COMMAND_ERASE_CHIP = 0x10,
32 FLASH_COMMAND_ERASE_SECTOR = 0x30,
33
34 FLASH_COMMAND_NONE = 0,
35 FLASH_COMMAND_ERASE = 0x80,
36 FLASH_COMMAND_ID = 0x90,
37 FLASH_COMMAND_PROGRAM = 0xA0,
38 FLASH_COMMAND_SWITCH_BANK = 0xB0,
39 FLASH_COMMAND_TERMINATE = 0xF0
40};
41
42enum FlashStateMachine {
43 FLASH_STATE_RAW = 0,
44 FLASH_STATE_START,
45 FLASH_STATE_CONTINUE
46};
47
48enum FlashManufacturer {
49 FLASH_MFG_PANASONIC = 0x1B32,
50 FLASH_MFG_SANYO = 0x1362
51};
52
53enum {
54 SAVEDATA_FLASH_BASE = 0x0E005555,
55
56 FLASH_BASE_HI = 0x5555,
57 FLASH_BASE_LO = 0x2AAA
58};
59
60struct GBASavedata {
61 enum SavedataType type;
62 uint8_t* data;
63 enum SavedataCommand command;
64 struct VFile* vf;
65
66 int mapMode;
67 struct VFile* realVf;
68
69 int readBitsRemaining;
70 int readAddress;
71 int writeAddress;
72 int writePending;
73 int addressBits;
74
75 uint8_t* currentBank;
76
77 enum FlashStateMachine flashState;
78};
79
80void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf);
81void GBASavedataDeinit(struct GBASavedata* savedata);
82
83void GBASavedataMask(struct GBASavedata* savedata, struct VFile* vf);
84void GBASavedataUnmask(struct GBASavedata* savedata);
85bool GBASavedataClone(struct GBASavedata* savedata, struct VFile* out);
86void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type);
87
88void GBASavedataInitFlash(struct GBASavedata* savedata);
89void GBASavedataInitEEPROM(struct GBASavedata* savedata);
90void GBASavedataInitSRAM(struct GBASavedata* savedata);
91
92uint8_t GBASavedataReadFlash(struct GBASavedata* savedata, uint16_t address);
93void GBASavedataWriteFlash(struct GBASavedata* savedata, uint16_t address, uint8_t value);
94
95uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata);
96void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize);
97
98#endif