all repos — mgba @ fec4c064475615dcf5593fa8fa8d342913997936

mGBA Game Boy Advance Emulator

include/mgba/internal/gba/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 GBA_MEMORY_H
  7#define GBA_MEMORY_H
  8
  9#include <mgba-util/common.h>
 10
 11CXX_GUARD_START
 12
 13#include <mgba/core/timing.h>
 14
 15#include <mgba/internal/arm/arm.h>
 16#include <mgba/internal/gba/hardware.h>
 17#include <mgba/internal/gba/savedata.h>
 18#include <mgba/internal/gba/vfame.h>
 19
 20enum GBAMemoryRegion {
 21	REGION_BIOS = 0x0,
 22	REGION_WORKING_RAM = 0x2,
 23	REGION_WORKING_IRAM = 0x3,
 24	REGION_IO = 0x4,
 25	REGION_PALETTE_RAM = 0x5,
 26	REGION_VRAM = 0x6,
 27	REGION_OAM = 0x7,
 28	REGION_CART0 = 0x8,
 29	REGION_CART0_EX = 0x9,
 30	REGION_CART1 = 0xA,
 31	REGION_CART1_EX = 0xB,
 32	REGION_CART2 = 0xC,
 33	REGION_CART2_EX = 0xD,
 34	REGION_CART_SRAM = 0xE,
 35	REGION_CART_SRAM_MIRROR = 0xF
 36};
 37
 38enum GBAMemoryBase {
 39	BASE_BIOS = 0x00000000,
 40	BASE_WORKING_RAM = 0x02000000,
 41	BASE_WORKING_IRAM = 0x03000000,
 42	BASE_IO = 0x04000000,
 43	BASE_PALETTE_RAM = 0x05000000,
 44	BASE_VRAM = 0x06000000,
 45	BASE_OAM = 0x07000000,
 46	BASE_CART0 = 0x08000000,
 47	BASE_CART0_EX = 0x09000000,
 48	BASE_CART1 = 0x0A000000,
 49	BASE_CART1_EX = 0x0B000000,
 50	BASE_CART2 = 0x0C000000,
 51	BASE_CART2_EX = 0x0D000000,
 52	BASE_CART_SRAM = 0x0E000000,
 53	BASE_CART_SRAM_MIRROR = 0x0F000000
 54};
 55
 56enum {
 57	SIZE_BIOS = 0x00004000,
 58	SIZE_WORKING_RAM = 0x00040000,
 59	SIZE_WORKING_IRAM = 0x00008000,
 60	SIZE_IO = 0x00000400,
 61	SIZE_PALETTE_RAM = 0x00000400,
 62	SIZE_VRAM = 0x00018000,
 63	SIZE_OAM = 0x00000400,
 64	SIZE_CART0 = 0x02000000,
 65	SIZE_CART1 = 0x02000000,
 66	SIZE_CART2 = 0x02000000,
 67	SIZE_CART_SRAM = 0x00010000,
 68	SIZE_CART_FLASH512 = 0x00010000,
 69	SIZE_CART_FLASH1M = 0x00020000,
 70	SIZE_CART_EEPROM = 0x00002000
 71};
 72
 73enum {
 74	OFFSET_MASK = 0x00FFFFFF,
 75	BASE_OFFSET = 24
 76};
 77
 78enum DMAControl {
 79	DMA_INCREMENT = 0,
 80	DMA_DECREMENT = 1,
 81	DMA_FIXED = 2,
 82	DMA_INCREMENT_RELOAD = 3
 83};
 84
 85enum DMATiming {
 86	DMA_TIMING_NOW = 0,
 87	DMA_TIMING_VBLANK = 1,
 88	DMA_TIMING_HBLANK = 2,
 89	DMA_TIMING_CUSTOM = 3
 90};
 91
 92mLOG_DECLARE_CATEGORY(GBA_MEM);
 93
 94DECL_BITFIELD(GBADMARegister, uint16_t);
 95DECL_BITS(GBADMARegister, DestControl, 5, 2);
 96DECL_BITS(GBADMARegister, SrcControl, 7, 2);
 97DECL_BIT(GBADMARegister, Repeat, 9);
 98DECL_BIT(GBADMARegister, Width, 10);
 99DECL_BIT(GBADMARegister, DRQ, 11);
100DECL_BITS(GBADMARegister, Timing, 12, 2);
101DECL_BIT(GBADMARegister, DoIRQ, 14);
102DECL_BIT(GBADMARegister, Enable, 15);
103
104struct GBADMA {
105	GBADMARegister reg;
106
107	uint32_t source;
108	uint32_t dest;
109	int32_t count;
110	uint32_t nextSource;
111	uint32_t nextDest;
112	int32_t nextCount;
113	uint32_t when;
114};
115
116struct GBAMemory {
117	uint32_t* bios;
118	uint32_t* wram;
119	uint32_t* iwram;
120	uint32_t* rom;
121	uint16_t io[512];
122
123	struct GBACartridgeHardware hw;
124	struct GBASavedata savedata;
125	struct GBAVFameCart vfame;
126	size_t romSize;
127	uint32_t romMask;
128	uint16_t romID;
129	int fullBios;
130
131	char waitstatesSeq32[256];
132	char waitstatesSeq16[256];
133	char waitstatesNonseq32[256];
134	char waitstatesNonseq16[256];
135	char waitstatesPrefetchSeq32[16];
136	char waitstatesPrefetchSeq16[16];
137	char waitstatesPrefetchNonseq32[16];
138	char waitstatesPrefetchNonseq16[16];
139	int activeRegion;
140	bool prefetch;
141	uint32_t lastPrefetchedPc;
142	uint32_t biosPrefetch;
143
144	struct GBADMA dma[4];
145	struct mTimingEvent dmaEvent;
146	int activeDMA;
147
148	bool mirroring;
149};
150
151struct GBA;
152void GBAMemoryInit(struct GBA* gba);
153void GBAMemoryDeinit(struct GBA* gba);
154
155void GBAMemoryReset(struct GBA* gba);
156
157uint32_t GBALoad32(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
158uint32_t GBALoad16(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
159uint32_t GBALoad8(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
160
161uint32_t GBALoadBad(struct ARMCore* cpu);
162
163void GBAStore32(struct ARMCore* cpu, uint32_t address, int32_t value, int* cycleCounter);
164void GBAStore16(struct ARMCore* cpu, uint32_t address, int16_t value, int* cycleCounter);
165void GBAStore8(struct ARMCore* cpu, uint32_t address, int8_t value, int* cycleCounter);
166
167uint32_t GBAView32(struct ARMCore* cpu, uint32_t address);
168uint16_t GBAView16(struct ARMCore* cpu, uint32_t address);
169uint8_t GBAView8(struct ARMCore* cpu, uint32_t address);
170
171void GBAPatch32(struct ARMCore* cpu, uint32_t address, int32_t value, int32_t* old);
172void GBAPatch16(struct ARMCore* cpu, uint32_t address, int16_t value, int16_t* old);
173void GBAPatch8(struct ARMCore* cpu, uint32_t address, int8_t value, int8_t* old);
174
175uint32_t GBALoadMultiple(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
176                         int* cycleCounter);
177uint32_t GBAStoreMultiple(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
178                          int* cycleCounter);
179
180void GBAAdjustWaitstates(struct GBA* gba, uint16_t parameters);
181
182struct GBASerializedState;
183void GBAMemorySerialize(const struct GBAMemory* memory, struct GBASerializedState* state);
184void GBAMemoryDeserialize(struct GBAMemory* memory, const struct GBASerializedState* state);
185
186CXX_GUARD_END
187
188#endif