all repos — mgba @ 3062a6a2d740593e14c99feb102169c83926c2e6

mGBA Game Boy Advance Emulator

include/mgba/internal/gb/memory.h (view raw)

  1/* Copyright (c) 2013-2016 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 GB_MEMORY_H
  7#define GB_MEMORY_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#include <mgba/gb/interface.h>
 16
 17mLOG_DECLARE_CATEGORY(GB_MBC);
 18mLOG_DECLARE_CATEGORY(GB_MEM);
 19
 20struct GB;
 21
 22enum {
 23	GB_BASE_CART_BANK0 = 0x0000,
 24	GB_BASE_CART_BANK1 = 0x4000,
 25	GB_BASE_CART_HALFBANK1 = 0x4000,
 26	GB_BASE_CART_HALFBANK2 = 0x6000,
 27	GB_BASE_VRAM = 0x8000,
 28	GB_BASE_EXTERNAL_RAM = 0xA000,
 29	GB_BASE_EXTERNAL_RAM_HALFBANK0 = 0xA000,
 30	GB_BASE_EXTERNAL_RAM_HALFBANK1 = 0xB000,
 31	GB_BASE_WORKING_RAM_BANK0 = 0xC000,
 32	GB_BASE_WORKING_RAM_BANK1 = 0xD000,
 33	GB_BASE_OAM = 0xFE00,
 34	GB_BASE_UNUSABLE = 0xFEA0,
 35	GB_BASE_IO = 0xFF00,
 36	GB_BASE_HRAM = 0xFF80,
 37	GB_BASE_IE = 0xFFFF
 38};
 39
 40enum {
 41	GB_REGION_CART_BANK0 = 0x0,
 42	GB_REGION_CART_BANK1 = 0x4,
 43	GB_REGION_VRAM = 0x8,
 44	GB_REGION_EXTERNAL_RAM = 0xA,
 45	GB_REGION_WORKING_RAM_BANK0 = 0xC,
 46	GB_REGION_WORKING_RAM_BANK1 = 0xD,
 47	GB_REGION_WORKING_RAM_BANK1_MIRROR = 0xE,
 48	GB_REGION_OTHER = 0xF,
 49};
 50
 51enum {
 52	GB_SIZE_CART_BANK0 = 0x4000,
 53	GB_SIZE_CART_HALFBANK = 0x2000,
 54	GB_SIZE_CART_MAX = 0x800000,
 55	GB_SIZE_VRAM = 0x4000,
 56	GB_SIZE_VRAM_BANK0 = 0x2000,
 57	GB_SIZE_EXTERNAL_RAM = 0x2000,
 58	GB_SIZE_EXTERNAL_RAM_HALFBANK = 0x1000,
 59	GB_SIZE_WORKING_RAM = 0x8000,
 60	GB_SIZE_WORKING_RAM_BANK0 = 0x1000,
 61	GB_SIZE_OAM = 0xA0,
 62	GB_SIZE_IO = 0x80,
 63	GB_SIZE_HRAM = 0x7F,
 64};
 65
 66enum {
 67	GB_SRAM_DIRT_NEW = 1,
 68	GB_SRAM_DIRT_SEEN = 2
 69};
 70
 71struct GBMemory;
 72typedef void (*GBMemoryBankControllerWrite)(struct GB*, uint16_t address, uint8_t value);
 73typedef uint8_t (*GBMemoryBankControllerRead)(struct GBMemory*, uint16_t address);
 74
 75DECL_BITFIELD(GBMBC7Field, uint8_t);
 76DECL_BIT(GBMBC7Field, CS, 7);
 77DECL_BIT(GBMBC7Field, CLK, 6);
 78DECL_BIT(GBMBC7Field, DI, 1);
 79DECL_BIT(GBMBC7Field, DO, 0);
 80
 81enum GBMBC7MachineState {
 82	GBMBC7_STATE_IDLE = 0,
 83	GBMBC7_STATE_READ_COMMAND = 1,
 84	GBMBC7_STATE_DO = 2,
 85
 86	GBMBC7_STATE_EEPROM_EWDS = 0x10,
 87	GBMBC7_STATE_EEPROM_WRAL = 0x11,
 88	GBMBC7_STATE_EEPROM_ERAL = 0x12,
 89	GBMBC7_STATE_EEPROM_EWEN = 0x13,
 90	GBMBC7_STATE_EEPROM_WRITE = 0x14,
 91	GBMBC7_STATE_EEPROM_READ = 0x18,
 92	GBMBC7_STATE_EEPROM_ERASE = 0x1C,
 93};
 94
 95enum GBTAMA5Register {
 96	GBTAMA5_BANK_LO = 0x0,
 97	GBTAMA5_BANK_HI = 0x1,
 98	GBTAMA5_WRITE_LO = 0x4,
 99	GBTAMA5_WRITE_HI = 0x5,
100	GBTAMA5_CS = 0x6,
101	GBTAMA5_ADDR_LO = 0x7,
102	GBTAMA5_MAX = 0x8,
103	GBTAMA5_ACTIVE = 0xA,
104	GBTAMA5_READ_LO = 0xC,
105	GBTAMA5_READ_HI = 0xD,
106};
107
108struct GBMBC1State {
109	int mode;
110	int multicartStride;
111	uint8_t bankLo;
112	uint8_t bankHi;
113};
114
115struct GBMBC6State {
116	int currentBank1;
117	uint8_t* romBank1;
118	bool sramAccess;
119	int currentSramBank1;
120	uint8_t* sramBank1;
121};
122
123struct GBMBC7State {
124	enum GBMBC7MachineState state;
125	uint16_t sr;
126	uint8_t address;
127	bool writable;
128	int srBits;
129	uint8_t access;
130	uint8_t latch;
131	GBMBC7Field eeprom;
132};
133
134struct GBMMM01State {
135	bool locked;
136	int currentBank0;
137};
138
139struct GBPocketCamState {
140	bool registersActive;
141	uint8_t registers[0x36];
142};
143
144struct GBTAMA5State {
145	uint8_t reg;
146	uint8_t registers[GBTAMA5_MAX];
147};
148
149union GBMBCState {
150	struct GBMBC1State mbc1;
151	struct GBMBC6State mbc6;
152	struct GBMBC7State mbc7;
153	struct GBMMM01State mmm01;
154	struct GBPocketCamState pocketCam;
155	struct GBTAMA5State tama5;
156};
157
158struct mRotationSource;
159struct GBMemory {
160	uint8_t* rom;
161	uint8_t* romBase;
162	uint8_t* romBank;
163	enum GBMemoryBankControllerType mbcType;
164	GBMemoryBankControllerWrite mbcWrite;
165	GBMemoryBankControllerRead mbcRead;
166	union GBMBCState mbcState;
167	int currentBank;
168
169	uint8_t* wram;
170	uint8_t* wramBank;
171	int wramCurrentBank;
172
173	bool sramAccess;
174	uint8_t* sram;
175	uint8_t* sramBank;
176	int sramCurrentBank;
177
178	uint8_t io[GB_SIZE_IO];
179	bool ime;
180	uint8_t ie;
181
182	uint8_t hram[GB_SIZE_HRAM];
183
184	uint16_t dmaSource;
185	uint16_t dmaDest;
186	int dmaRemaining;
187
188	uint16_t hdmaSource;
189	uint16_t hdmaDest;
190	int hdmaRemaining;
191	bool isHdma;
192
193	struct mTimingEvent dmaEvent;
194	struct mTimingEvent hdmaEvent;
195
196	size_t romSize;
197
198	bool rtcAccess;
199	int activeRtcReg;
200	bool rtcLatched;
201	uint8_t rtcRegs[5];
202	time_t rtcLastLatch;
203	struct mRTCSource* rtc;
204	struct mRotationSource* rotation;
205	struct mRumble* rumble;
206	struct mImageSource* cam;
207};
208
209struct SM83Core;
210void GBMemoryInit(struct GB* gb);
211void GBMemoryDeinit(struct GB* gb);
212
213void GBMemoryReset(struct GB* gb);
214void GBMemorySwitchWramBank(struct GBMemory* memory, int bank);
215
216uint8_t GBLoad8(struct SM83Core* cpu, uint16_t address);
217void GBStore8(struct SM83Core* cpu, uint16_t address, int8_t value);
218
219int GBCurrentSegment(struct SM83Core* cpu, uint16_t address);
220
221uint8_t GBView8(struct SM83Core* cpu, uint16_t address, int segment);
222
223void GBMemoryDMA(struct GB* gb, uint16_t base);
224uint8_t GBMemoryWriteHDMA5(struct GB* gb, uint8_t value);
225
226void GBPatch8(struct SM83Core* cpu, uint16_t address, int8_t value, int8_t* old, int segment);
227
228struct GBSerializedState;
229void GBMemorySerialize(const struct GB* gb, struct GBSerializedState* state);
230void GBMemoryDeserialize(struct GB* gb, const struct GBSerializedState* state);
231
232CXX_GUARD_END
233
234#endif