all repos — mgba @ 4b43a88ebd9deba76421e7741a2abe808a2fa897

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	cpu->memory.setActiveRegion(cpu, cpu->pc);
199
200	if (gb->yankedRomSize) {
201		gb->memory.romSize = gb->yankedRomSize;
202		gb->yankedRomSize = 0;
203	}
204	GBMemoryReset(gb);
205	GBVideoReset(&gb->video);
206	GBTimerReset(&gb->timer);
207	GBIOReset(gb);
208	GBAudioReset(&gb->audio);
209}
210
211void GBUpdateIRQs(struct GB* gb) {
212	int irqs = gb->memory.ie & gb->memory.io[REG_IF];
213	if (!irqs) {
214		return;
215	}
216	gb->cpu->halted = false;
217
218	if (!gb->memory.ime) {
219		return;
220	}
221
222	if (irqs & (1 << GB_IRQ_VBLANK)) {
223		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
224		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
225		return;
226	}
227	if (irqs & (1 << GB_IRQ_LCDSTAT)) {
228		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
229		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
230		return;
231	}
232	if (irqs & (1 << GB_IRQ_TIMER)) {
233		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
234		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
235		return;
236	}
237	if (irqs & (1 << GB_IRQ_SIO)) {
238		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
239		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
240		return;
241	}
242	if (irqs & (1 << GB_IRQ_KEYPAD)) {
243		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
244		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
245	}
246}
247
248void GBProcessEvents(struct LR35902Core* cpu) {
249	struct GB* gb = (struct GB*) cpu->master;
250	do {
251		int32_t cycles = cpu->nextEvent;
252		int32_t nextEvent = INT_MAX;
253		int32_t testEvent;
254
255		if (gb->eiPending) {
256			gb->eiPending -= cycles;
257			if (gb->eiPending <= 0) {
258				gb->memory.ime = true;
259				GBUpdateIRQs(gb);
260				gb->eiPending = 0;
261			}
262		}
263
264		testEvent = GBVideoProcessEvents(&gb->video, cycles >> gb->doubleSpeed);
265		if (testEvent != INT_MAX) {
266			testEvent <<= gb->doubleSpeed;
267			if (testEvent < nextEvent) {
268				nextEvent = testEvent;
269			}
270		}
271
272		testEvent = GBAudioProcessEvents(&gb->audio, cycles >> gb->doubleSpeed);
273		if (testEvent != INT_MAX) {
274			testEvent <<= gb->doubleSpeed;
275			if (testEvent < nextEvent) {
276				nextEvent = testEvent;
277			}
278		}
279
280		testEvent = GBTimerProcessEvents(&gb->timer, cycles);
281		if (testEvent < nextEvent) {
282			nextEvent = testEvent;
283		}
284
285		testEvent = GBMemoryProcessEvents(gb, cycles);
286		if (testEvent < nextEvent) {
287			nextEvent = testEvent;
288		}
289
290		cpu->cycles -= cycles;
291		cpu->nextEvent = nextEvent;
292
293		if (cpu->halted) {
294			cpu->cycles = cpu->nextEvent;
295		}
296	} while (cpu->cycles >= cpu->nextEvent);
297}
298
299void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
300	struct GB* gb = (struct GB*) cpu->master;
301	if (!enable) {
302		gb->memory.ime = enable;
303		gb->eiPending = 0;
304		GBUpdateIRQs(gb);
305	} else {
306		if (cpu->nextEvent > cpu->cycles + 4) {
307			cpu->nextEvent = cpu->cycles + 4;
308		}
309		gb->eiPending = cpu->cycles + 4;
310	}
311}
312
313void GBHalt(struct LR35902Core* cpu) {
314	cpu->cycles = cpu->nextEvent;
315	cpu->halted = true;
316}
317
318void GBStop(struct LR35902Core* cpu) {
319	struct GB* gb = (struct GB*) cpu->master;
320	if (gb->memory.io[REG_KEY1] & 1) {
321		gb->doubleSpeed ^= 1;
322		gb->memory.io[REG_KEY1] &= 1;
323		gb->memory.io[REG_KEY1] |= gb->doubleSpeed << 7;
324	}
325	// TODO: Actually stop
326}
327
328void GBIllegal(struct LR35902Core* cpu) {
329	// TODO
330	mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
331}
332
333bool GBIsROM(struct VFile* vf) {
334	vf->seek(vf, 0x104, SEEK_SET);
335	uint8_t header[4];
336	static const uint8_t knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
337
338	if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
339		return false;
340	}
341	if (memcmp(header, knownHeader, sizeof(header))) {
342		return false;
343	}
344	return true;
345}
346
347void GBGetGameTitle(struct GB* gb, char* out) {
348	const struct GBCartridge* cart = NULL;
349	if (gb->memory.rom) {
350		cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
351	}
352	if (gb->pristineRom) {
353		cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
354	}
355	if (!cart) {
356		return;
357	}
358	if (cart->oldLicensee != 0x33) {
359		memcpy(out, cart->titleLong, 16);
360	} else {
361		memcpy(out, cart->titleShort, 11);
362	}
363}
364
365void GBGetGameCode(struct GB* gb, char* out) {
366	memset(out, 0, 8);
367	const struct GBCartridge* cart = NULL;
368	if (gb->memory.rom) {
369		cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
370	}
371	if (gb->pristineRom) {
372		cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
373	}
374	if (!cart) {
375		return;
376	}
377	if (cart->cgb == 0xC0) {
378		memcpy(out, "CGB-????", 8);
379	} else {
380		memcpy(out, "DMG-????", 8);
381	}
382	if (cart->oldLicensee == 0x33) {
383		memcpy(&out[4], cart->maker, 4);
384	}
385}