all repos — mgba @ af96ee70879f65255c11e1cc28a88f2afd23894c

mGBA Game Boy Advance Emulator

src/gb/gb.c (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#include "gb.h"
  7
  8#include "gb/io.h"
  9
 10#include "core/core.h"
 11#include "util/crc32.h"
 12#include "util/memory.h"
 13#include "util/math.h"
 14#include "util/patch.h"
 15#include "util/vfs.h"
 16
 17const uint32_t CGB_LR35902_FREQUENCY = 0x800000;
 18const uint32_t SGB_LR35902_FREQUENCY = 0x418B1E;
 19
 20const uint32_t GB_COMPONENT_MAGIC = 0x400000;
 21
 22mLOG_DEFINE_CATEGORY(GB, "GB");
 23
 24static void GBInit(void* cpu, struct mCPUComponent* component);
 25static void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh);
 26static void GBProcessEvents(struct LR35902Core* cpu);
 27static void GBSetInterrupts(struct LR35902Core* cpu, bool enable);
 28static void GBIllegal(struct LR35902Core* cpu);
 29static void GBStop(struct LR35902Core* cpu);
 30
 31#ifdef _3DS
 32extern uint32_t* romBuffer;
 33extern size_t romBufferSize;
 34#endif
 35
 36void GBCreate(struct GB* gb) {
 37	gb->d.id = GB_COMPONENT_MAGIC;
 38	gb->d.init = GBInit;
 39	gb->d.deinit = 0;
 40}
 41
 42static void GBInit(void* cpu, struct mCPUComponent* component) {
 43	struct GB* gb = (struct GB*) component;
 44	gb->cpu = cpu;
 45	gb->sync = NULL;
 46
 47	GBInterruptHandlerInit(&gb->cpu->irqh);
 48	GBMemoryInit(gb);
 49
 50	gb->video.p = gb;
 51	GBVideoInit(&gb->video);
 52
 53	gb->audio.p = gb;
 54	GBAudioInit(&gb->audio, 2048, &gb->memory.io[REG_NR52], GB_AUDIO_DMG); // TODO: Remove magic constant
 55
 56	gb->timer.p = gb;
 57
 58	gb->romVf = 0;
 59	gb->sramVf = 0;
 60
 61	gb->pristineRom = 0;
 62	gb->pristineRomSize = 0;
 63	gb->yankedRomSize = 0;
 64
 65	gb->stream = NULL;
 66
 67	gb->eiPending = false;
 68	gb->doubleSpeed = 0;
 69}
 70
 71bool GBLoadROM(struct GB* gb, struct VFile* vf) {
 72	GBUnloadROM(gb);
 73	gb->romVf = vf;
 74	gb->pristineRomSize = vf->size(vf);
 75	vf->seek(vf, 0, SEEK_SET);
 76#ifdef _3DS
 77	gb->pristineRom = 0;
 78	if (gb->pristineRomSize <= romBufferSize) {
 79		gb->pristineRom = romBuffer;
 80		vf->read(vf, romBuffer, gb->pristineRomSize);
 81	}
 82#else
 83	gb->pristineRom = vf->map(vf, gb->pristineRomSize, MAP_READ);
 84#endif
 85	if (!gb->pristineRom) {
 86		return false;
 87	}
 88	gb->yankedRomSize = 0;
 89	gb->memory.rom = gb->pristineRom;
 90	gb->memory.romSize = gb->pristineRomSize;
 91	gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
 92
 93	// TODO: error check
 94	return true;
 95}
 96
 97bool GBLoadSave(struct GB* gb, struct VFile* vf) {
 98	gb->sramVf = vf;
 99	if (vf) {
100		// TODO: Do this in bank-switching code
101		if (vf->size(vf) < 0x20000) {
102			vf->truncate(vf, 0x20000);
103		}
104		gb->memory.sram = vf->map(vf, 0x20000, MAP_WRITE);
105	}
106	return gb->memory.sram;
107}
108
109void GBUnloadROM(struct GB* gb) {
110	// TODO: Share with GBAUnloadROM
111	if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
112		if (gb->yankedRomSize) {
113			gb->yankedRomSize = 0;
114		}
115		mappedMemoryFree(gb->memory.rom, GB_SIZE_CART_MAX);
116	}
117	gb->memory.rom = 0;
118
119	if (gb->romVf) {
120#ifndef _3DS
121		gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
122#endif
123		gb->romVf->close(gb->romVf);
124		gb->pristineRom = 0;
125		gb->romVf = 0;
126	}
127
128	if (gb->sramVf) {
129		gb->sramVf->unmap(gb->sramVf, gb->memory.sram, 0x8000);
130		gb->sramVf = 0;
131	} else if (gb->memory.sram) {
132		mappedMemoryFree(gb->memory.sram, 0x8000);
133	}
134	gb->memory.sram = 0;
135}
136
137void GBApplyPatch(struct GB* gb, struct Patch* patch) {
138	size_t patchedSize = patch->outputSize(patch, gb->memory.romSize);
139	if (!patchedSize) {
140		return;
141	}
142	if (patchedSize > GB_SIZE_CART_MAX) {
143		patchedSize = GB_SIZE_CART_MAX;
144	}
145	gb->memory.rom = anonymousMemoryMap(GB_SIZE_CART_MAX);
146	if (!patch->applyPatch(patch, gb->pristineRom, gb->pristineRomSize, gb->memory.rom, patchedSize)) {
147		mappedMemoryFree(gb->memory.rom, patchedSize);
148		gb->memory.rom = gb->pristineRom;
149		return;
150	}
151	gb->memory.romSize = patchedSize;
152	gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
153}
154
155void GBDestroy(struct GB* gb) {
156	GBUnloadROM(gb);
157
158	GBMemoryDeinit(gb);
159}
160
161void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
162	irqh->reset = GBReset;
163	irqh->processEvents = GBProcessEvents;
164	irqh->setInterrupts = GBSetInterrupts;
165	irqh->hitIllegal = GBIllegal;
166	irqh->stop = GBStop;
167	irqh->halt = GBHalt;
168}
169
170void GBReset(struct LR35902Core* cpu) {
171	struct GB* gb = (struct GB*) cpu->master;
172
173	const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
174	if (cart->cgb & 0x80) {
175		gb->model = GB_MODEL_CGB;
176		gb->audio.style = GB_AUDIO_CGB;
177		cpu->a = 0x11;
178		cpu->f.packed = 0x80;
179		cpu->c = 0;
180		cpu->e = 0x08;
181		cpu->h = 0;
182		cpu->l = 0x7C;
183	} else {
184		// TODO: SGB
185		gb->model = GB_MODEL_DMG;
186		gb->audio.style = GB_AUDIO_DMG;
187		cpu->a = 1;
188		cpu->f.packed = 0xB0;
189		cpu->c = 0x13;
190		cpu->e = 0xD8;
191		cpu->h = 1;
192		cpu->l = 0x4D;
193	}
194	cpu->b = 0;
195	cpu->d = 0;
196	cpu->sp = 0xFFFE;
197	cpu->pc = 0x100;
198
199	if (gb->yankedRomSize) {
200		gb->memory.romSize = gb->yankedRomSize;
201		gb->yankedRomSize = 0;
202	}
203	GBMemoryReset(gb);
204	GBVideoReset(&gb->video);
205	GBTimerReset(&gb->timer);
206	GBIOReset(gb);
207	GBAudioReset(&gb->audio);
208}
209
210void GBUpdateIRQs(struct GB* gb) {
211	int irqs = gb->memory.ie & gb->memory.io[REG_IF];
212	if (!irqs) {
213		return;
214	}
215	gb->cpu->halted = false;
216
217	if (!gb->memory.ime) {
218		return;
219	}
220
221	if (irqs & (1 << GB_IRQ_VBLANK)) {
222		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
223		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
224		return;
225	}
226	if (irqs & (1 << GB_IRQ_LCDSTAT)) {
227		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
228		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
229		return;
230	}
231	if (irqs & (1 << GB_IRQ_TIMER)) {
232		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
233		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
234		return;
235	}
236	if (irqs & (1 << GB_IRQ_SIO)) {
237		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
238		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
239		return;
240	}
241	if (irqs & (1 << GB_IRQ_KEYPAD)) {
242		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
243		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
244	}
245}
246
247void GBProcessEvents(struct LR35902Core* cpu) {
248	struct GB* gb = (struct GB*) cpu->master;
249	do {
250		int32_t cycles = cpu->nextEvent;
251		int32_t nextEvent = INT_MAX;
252		int32_t testEvent;
253
254		if (gb->eiPending) {
255			gb->eiPending -= cycles;
256			if (gb->eiPending <= 0) {
257				gb->memory.ime = true;
258				GBUpdateIRQs(gb);
259				gb->eiPending = 0;
260			}
261		}
262
263		testEvent = GBVideoProcessEvents(&gb->video, cycles >> gb->doubleSpeed);
264		if (testEvent != INT_MAX) {
265			testEvent <<= gb->doubleSpeed;
266			if (testEvent < nextEvent) {
267				nextEvent = testEvent;
268			}
269		}
270
271		testEvent = GBAudioProcessEvents(&gb->audio, cycles >> gb->doubleSpeed);
272		if (testEvent != INT_MAX) {
273			testEvent <<= gb->doubleSpeed;
274			if (testEvent < nextEvent) {
275				nextEvent = testEvent;
276			}
277		}
278
279		testEvent = GBTimerProcessEvents(&gb->timer, cycles);
280		if (testEvent < nextEvent) {
281			nextEvent = testEvent;
282		}
283
284		testEvent = GBMemoryProcessEvents(gb, cycles);
285		if (testEvent < nextEvent) {
286			nextEvent = testEvent;
287		}
288
289		cpu->cycles -= cycles;
290		cpu->nextEvent = nextEvent;
291
292		if (cpu->halted) {
293			cpu->cycles = cpu->nextEvent;
294		}
295	} while (cpu->cycles >= cpu->nextEvent);
296}
297
298void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
299	struct GB* gb = (struct GB*) cpu->master;
300	if (!enable) {
301		gb->memory.ime = enable;
302		gb->eiPending = 0;
303		GBUpdateIRQs(gb);
304	} else {
305		if (cpu->nextEvent > cpu->cycles + 4) {
306			cpu->nextEvent = cpu->cycles + 4;
307		}
308		gb->eiPending = cpu->cycles + 4;
309	}
310}
311
312void GBHalt(struct LR35902Core* cpu) {
313	cpu->cycles = cpu->nextEvent;
314	cpu->halted = true;
315}
316
317void GBStop(struct LR35902Core* cpu) {
318	struct GB* gb = (struct GB*) cpu->master;
319	if (gb->memory.io[REG_KEY1] & 1) {
320		gb->doubleSpeed ^= 1;
321		gb->memory.io[REG_KEY1] &= 1;
322		gb->memory.io[REG_KEY1] |= gb->doubleSpeed << 7;
323	}
324	// TODO: Actually stop
325}
326
327void GBIllegal(struct LR35902Core* cpu) {
328	// TODO
329	mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
330}
331
332bool GBIsROM(struct VFile* vf) {
333	vf->seek(vf, 0x104, SEEK_SET);
334	uint8_t header[4];
335	static const uint8_t knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
336
337	if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
338		return false;
339	}
340	if (memcmp(header, knownHeader, sizeof(header))) {
341		return false;
342	}
343	return true;
344}
345
346void GBGetGameTitle(struct GB* gb, char* out) {
347	const struct GBCartridge* cart = NULL;
348	if (gb->memory.rom) {
349		cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
350	}
351	if (gb->pristineRom) {
352		cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
353	}
354	if (!cart) {
355		return;
356	}
357	if (cart->oldLicensee != 0x33) {
358		memcpy(out, cart->titleLong, 16);
359	} else {
360		memcpy(out, cart->titleShort, 11);
361	}
362}
363
364void GBGetGameCode(struct GB* gb, char* out) {
365	memset(out, 0, 8);
366	const struct GBCartridge* cart = NULL;
367	if (gb->memory.rom) {
368		cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
369	}
370	if (gb->pristineRom) {
371		cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
372	}
373	if (!cart) {
374		return;
375	}
376	if (cart->cgb == 0xC0) {
377		memcpy(out, "CGB-????", 8);
378	} else {
379		memcpy(out, "DMG-????", 8);
380	}
381	if (cart->oldLicensee == 0x33) {
382		memcpy(&out[4], cart->maker, 4);
383	}
384}