all repos — mgba @ 228b6aaa016e7e249ac54504f9c42b2732261cd3

mGBA Game Boy Advance Emulator

src/gba/gba-memory.h (view raw)

  1#ifndef GBA_MEMORY_H
  2#define GBA_MEMORY_H
  3
  4#include "arm.h"
  5
  6#include "gba-savedata.h"
  7
  8#include <string.h>
  9
 10enum GBAMemoryRegion {
 11	REGION_BIOS = 0x0,
 12	REGION_WORKING_RAM = 0x2,
 13	REGION_WORKING_IRAM = 0x3,
 14	REGION_IO = 0x4,
 15	REGION_PALETTE_RAM = 0x5,
 16	REGION_VRAM = 0x6,
 17	REGION_OAM = 0x7,
 18	REGION_CART0 = 0x8,
 19	REGION_CART0_EX = 0x9,
 20	REGION_CART1 = 0xA,
 21	REGION_CART1_EX = 0xB,
 22	REGION_CART2 = 0xC,
 23	REGION_CART2_EX = 0xD,
 24	REGION_CART_SRAM = 0xE
 25};
 26
 27enum GBAMemoryBase {
 28	BASE_BIOS = 0x00000000,
 29	BASE_WORKING_RAM = 0x02000000,
 30	BASE_WORKING_IRAM = 0x03000000,
 31	BASE_IO = 0x04000000,
 32	BASE_PALETTE_RAM = 0x05000000,
 33	BASE_VRAM = 0x06000000,
 34	BASE_OAM = 0x07000000,
 35	BASE_CART0 = 0x08000000,
 36	BASE_CART0_EX = 0x09000000,
 37	BASE_CART1 = 0x0A000000,
 38	BASE_CART1_EX = 0x0B000000,
 39	BASE_CART2 = 0x0C000000,
 40	BASE_CART2_EX = 0x0D000000,
 41	BASE_CART_SRAM = 0x0E000000
 42};
 43
 44enum {
 45	SIZE_BIOS = 0x00004000,
 46	SIZE_WORKING_RAM = 0x00040000,
 47	SIZE_WORKING_IRAM = 0x00008000,
 48	SIZE_IO = 0x00000400,
 49	SIZE_PALETTE_RAM = 0x00000400,
 50	SIZE_VRAM = 0x00018000,
 51	SIZE_OAM = 0x00000400,
 52	SIZE_CART0 = 0x02000000,
 53	SIZE_CART1 = 0x02000000,
 54	SIZE_CART2 = 0x02000000,
 55	SIZE_CART_SRAM = 0x00008000,
 56	SIZE_CART_FLASH512 = 0x00010000,
 57	SIZE_CART_FLASH1M = 0x00020000,
 58	SIZE_CART_EEPROM = 0x00002000
 59};
 60
 61enum {
 62	OFFSET_MASK = 0x00FFFFFF,
 63	BASE_OFFSET = 24
 64};
 65
 66enum DMAControl {
 67	DMA_INCREMENT = 0,
 68	DMA_DECREMENT = 1,
 69	DMA_FIXED = 2,
 70	DMA_INCREMENT_RELOAD = 3
 71};
 72
 73enum DMATiming {
 74	DMA_TIMING_NOW = 0,
 75	DMA_TIMING_VBLANK = 1,
 76	DMA_TIMING_HBLANK = 2,
 77	DMA_TIMING_CUSTOM = 3
 78};
 79
 80struct GBADMA {
 81	union {
 82		struct {
 83			int : 5;
 84			enum DMAControl dstControl : 2;
 85			enum DMAControl srcControl : 2;
 86			unsigned repeat : 1;
 87			unsigned width : 1;
 88			unsigned drq : 1;
 89			enum DMATiming timing : 2;
 90			unsigned doIrq : 1;
 91			unsigned enable : 1;
 92		};
 93		uint16_t packed;
 94	};
 95
 96	uint32_t source;
 97	uint32_t dest;
 98	int32_t count;
 99	uint32_t nextSource;
100	uint32_t nextDest;
101	int32_t nextCount;
102	int32_t nextIRQ;
103};
104
105struct GBAMemory {
106	struct ARMMemory d;
107	struct GBA* p;
108
109	uint32_t* bios;
110	uint32_t* wram;
111	uint32_t* iwram;
112	uint32_t* rom;
113	uint16_t io[SIZE_IO >> 1];
114
115	struct GBASavedata savedata;
116	size_t romSize;
117	uint16_t romID;
118	int fullBios;
119
120	char waitstates32[256];
121	char waitstates16[256];
122	char waitstatesSeq32[256];
123	char waitstatesSeq16[256];
124	char waitstatesPrefetch32[256];
125	char waitstatesPrefetch16[256];
126	int activeRegion;
127	uint32_t biosPrefetch;
128
129	struct GBADMA dma[4];
130};
131
132int32_t GBAMemoryProcessEvents(struct GBAMemory* memory, int32_t cycles);
133
134int32_t GBALoad32(struct ARMMemory* memory, uint32_t address, int* cycleCounter);
135int16_t GBALoad16(struct ARMMemory* memory, uint32_t address, int* cycleCounter);
136uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address, int* cycleCounter);
137int8_t GBALoad8(struct ARMMemory* memory, uint32_t address, int* cycleCounter);
138uint8_t GBALoadU8(struct ARMMemory* memory, uint32_t address, int* cycleCounter);
139
140void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value, int* cycleCounter);
141void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value, int* cycleCounter);
142void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value, int* cycleCounter);
143
144void GBAAdjustWaitstates(struct GBAMemory* memory, uint16_t parameters);
145
146void GBAMemoryWriteDMASAD(struct GBAMemory* memory, int dma, uint32_t address);
147void GBAMemoryWriteDMADAD(struct GBAMemory* memory, int dma, uint32_t address);
148void GBAMemoryWriteDMACNT_LO(struct GBAMemory* memory, int dma, uint16_t count);
149uint16_t GBAMemoryWriteDMACNT_HI(struct GBAMemory* memory, int dma, uint16_t control);
150
151void GBAMemoryScheduleDMA(struct GBAMemory* memory, int number, struct GBADMA* info);
152void GBAMemoryServiceDMA(struct GBAMemory* memory, int number, struct GBADMA* info);
153void GBAMemoryRunHblankDMAs(struct GBAMemory* memory);
154void GBAMemoryRunVblankDMAs(struct GBAMemory* memory);
155
156#endif