all repos — mgba @ 340d3ce6a7387431e6defcacb63c263f84ab5340

mGBA Game Boy Advance Emulator

src/gba.c (view raw)

  1#include "gba.h"
  2
  3#include <sys/mman.h>
  4
  5static const char* GBA_CANNOT_MMAP = "Could not map memory";
  6
  7void GBAInit(struct GBA* gba) {
  8	gba->errno = GBA_NO_ERROR;
  9	gba->errstr = 0;
 10
 11	ARMInit(&gba->cpu);
 12
 13	gba->memory.p = gba;
 14	GBAMemoryInit(&gba->memory);
 15}
 16
 17void GBADeinit(struct GBA* gba) {
 18	GBAMemoryDeinit(&gba->memory);
 19}
 20
 21void GBAMemoryInit(struct GBAMemory* memory) {
 22	memory->d.load32 = GBALoad32;
 23	memory->d.load16 = GBALoad16;
 24	memory->d.loadU16 = GBALoadU16;
 25	memory->d.load8 = GBALoad8;
 26	memory->d.loadU8 = GBALoadU8;
 27	memory->d.store32 = GBAStore32;
 28	memory->d.store16 = GBAStore16;
 29	memory->d.store8 = GBAStore8;
 30
 31	memory->bios = 0;
 32	memory->wram = mmap(0, SIZE_WORKING_RAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
 33	memory->iwram = mmap(0, SIZE_WORKING_IRAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
 34	memory->rom = 0;
 35
 36	if (!memory->wram || !memory->iwram) {
 37		GBAMemoryDeinit(memory);
 38		memory->p->errno = GBA_OUT_OF_MEMORY;
 39		memory->p->errstr = GBA_CANNOT_MMAP;
 40	}
 41}
 42
 43void GBAMemoryDeinit(struct GBAMemory* memory) {
 44	munmap(memory->wram, SIZE_WORKING_RAM);
 45	munmap(memory->iwram, SIZE_WORKING_IRAM);
 46}
 47
 48int32_t GBALoad32(struct ARMMemory* memory, uint32_t address) {
 49	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
 50
 51	switch (address & OFFSET_MASK) {
 52	case REGION_BIOS:
 53		break;
 54	case REGION_WORKING_RAM:
 55		break;
 56	case REGION_WORKING_IRAM:
 57		break;
 58	case REGION_IO:
 59		break;
 60	case REGION_PALETTE_RAM:
 61		break;
 62	case REGION_VRAM:
 63		break;
 64	case REGION_OAM:
 65		break;
 66	case REGION_CART0:
 67	case REGION_CART0_EX:
 68	case REGION_CART1:
 69	case REGION_CART1_EX:
 70	case REGION_CART2:
 71	case REGION_CART2_EX:
 72		break;
 73	case REGION_CART_SRAM:
 74		break;
 75	default:
 76		break;
 77	}
 78
 79	return 0;
 80}
 81
 82int16_t GBALoad16(struct ARMMemory* memory, uint32_t address) {
 83	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
 84
 85	switch (address & OFFSET_MASK) {
 86	case REGION_BIOS:
 87		break;
 88	case REGION_WORKING_RAM:
 89		break;
 90	case REGION_WORKING_IRAM:
 91		break;
 92	case REGION_IO:
 93		break;
 94	case REGION_PALETTE_RAM:
 95		break;
 96	case REGION_VRAM:
 97		break;
 98	case REGION_OAM:
 99		break;
100	case REGION_CART0:
101	case REGION_CART0_EX:
102	case REGION_CART1:
103	case REGION_CART1_EX:
104	case REGION_CART2:
105	case REGION_CART2_EX:
106		break;
107	case REGION_CART_SRAM:
108		break;
109	default:
110		break;
111	}
112
113	return 0;
114}
115
116uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address) {
117	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
118
119	switch (address & OFFSET_MASK) {
120	case REGION_BIOS:
121		break;
122	case REGION_WORKING_RAM:
123		break;
124	case REGION_WORKING_IRAM:
125		break;
126	case REGION_IO:
127		break;
128	case REGION_PALETTE_RAM:
129		break;
130	case REGION_VRAM:
131		break;
132	case REGION_OAM:
133		break;
134	case REGION_CART0:
135	case REGION_CART0_EX:
136	case REGION_CART1:
137	case REGION_CART1_EX:
138	case REGION_CART2:
139	case REGION_CART2_EX:
140		break;
141	case REGION_CART_SRAM:
142		break;
143	default:
144		break;
145	}
146
147	return 0;
148}
149
150int8_t GBALoad8(struct ARMMemory* memory, uint32_t address) {
151	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
152
153	switch (address & OFFSET_MASK) {
154	case REGION_BIOS:
155		break;
156	case REGION_WORKING_RAM:
157		break;
158	case REGION_WORKING_IRAM:
159		break;
160	case REGION_IO:
161		break;
162	case REGION_PALETTE_RAM:
163		break;
164	case REGION_VRAM:
165		break;
166	case REGION_OAM:
167		break;
168	case REGION_CART0:
169	case REGION_CART0_EX:
170	case REGION_CART1:
171	case REGION_CART1_EX:
172	case REGION_CART2:
173	case REGION_CART2_EX:
174		break;
175	case REGION_CART_SRAM:
176		break;
177	default:
178		break;
179	}
180
181	return 0;
182}
183
184uint8_t GBALoadU8(struct ARMMemory* memory, uint32_t address) {
185	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
186
187	switch (address & OFFSET_MASK) {
188	case REGION_BIOS:
189		break;
190	case REGION_WORKING_RAM:
191		break;
192	case REGION_WORKING_IRAM:
193		break;
194	case REGION_IO:
195		break;
196	case REGION_PALETTE_RAM:
197		break;
198	case REGION_VRAM:
199		break;
200	case REGION_OAM:
201		break;
202	case REGION_CART0:
203	case REGION_CART0_EX:
204	case REGION_CART1:
205	case REGION_CART1_EX:
206	case REGION_CART2:
207	case REGION_CART2_EX:
208		break;
209	case REGION_CART_SRAM:
210		break;
211	default:
212		break;
213	}
214
215	return 0;
216}
217
218void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value) {
219	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
220
221	switch (address & OFFSET_MASK) {
222	case REGION_WORKING_RAM:
223		break;
224	case REGION_WORKING_IRAM:
225		break;
226	case REGION_IO:
227		break;
228	case REGION_PALETTE_RAM:
229		break;
230	case REGION_VRAM:
231		break;
232	case REGION_OAM:
233		break;
234	case REGION_CART0:
235		break;
236	case REGION_CART2_EX:
237		break;
238	case REGION_CART_SRAM:
239		break;
240	default:
241		break;
242	}
243}
244
245void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value) {
246	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
247
248	switch (address & OFFSET_MASK) {
249	case REGION_WORKING_RAM:
250		break;
251	case REGION_WORKING_IRAM:
252		break;
253	case REGION_IO:
254		break;
255	case REGION_PALETTE_RAM:
256		break;
257	case REGION_VRAM:
258		break;
259	case REGION_OAM:
260		break;
261	case REGION_CART0:
262		break;
263	case REGION_CART2_EX:
264		break;
265	case REGION_CART_SRAM:
266		break;
267	default:
268		break;
269	}
270}
271
272void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value) {
273	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
274
275	switch (address & OFFSET_MASK) {
276	case REGION_WORKING_RAM:
277		break;
278	case REGION_WORKING_IRAM:
279		break;
280	case REGION_IO:
281		break;
282	case REGION_PALETTE_RAM:
283		break;
284	case REGION_VRAM:
285		break;
286	case REGION_OAM:
287		break;
288	case REGION_CART0:
289		break;
290	case REGION_CART2_EX:
291		break;
292	case REGION_CART_SRAM:
293		break;
294	default:
295		break;
296	}
297}