all repos — mgba @ c0eb7c81f70d94b21b931b6a1ed44e7b9c9f5dd3

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