all repos — mgba @ e9a2b2a57c4f478fb3600fe9d87e1b97804d73de

mGBA Game Boy Advance Emulator

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