all repos — mgba @ 9de2189b2ef52ddb51dfde14befda9e4dff4e436

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