all repos — mgba @ 17264e2af984d2034e94c36a6bbf0d3a818001ed

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	} else {
106		gb->memory.sram = anonymousMemoryMap(0x20000);
107	}
108	return gb->memory.sram;
109}
110
111void GBUnloadROM(struct GB* gb) {
112	// TODO: Share with GBAUnloadROM
113	if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
114		if (gb->yankedRomSize) {
115			gb->yankedRomSize = 0;
116		}
117		mappedMemoryFree(gb->memory.rom, 0x400000);
118	}
119	gb->memory.rom = 0;
120
121	if (gb->romVf) {
122#ifndef _3DS
123		gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
124#endif
125		gb->pristineRom = 0;
126		gb->romVf = 0;
127	}
128
129	if (gb->sramVf) {
130		gb->sramVf->unmap(gb->sramVf, gb->memory.sram, 0x8000);
131		gb->sramVf = 0;
132	} else if (gb->memory.sram) {
133		mappedMemoryFree(gb->memory.sram, 0x8000);
134	}
135	gb->memory.sram = 0;
136}
137
138void GBApplyPatch(struct GB* gb, struct Patch* patch) {
139	size_t patchedSize = patch->outputSize(patch, gb->memory.romSize);
140	if (!patchedSize) {
141		return;
142	}
143	if (patchedSize > 0x400000) {
144		patchedSize = 0x400000;
145	}
146	gb->memory.rom = anonymousMemoryMap(0x400000);
147	if (!patch->applyPatch(patch, gb->pristineRom, gb->pristineRomSize, gb->memory.rom, patchedSize)) {
148		mappedMemoryFree(gb->memory.rom, patchedSize);
149		gb->memory.rom = gb->pristineRom;
150		return;
151	}
152	gb->memory.romSize = patchedSize;
153	gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
154}
155
156void GBDestroy(struct GB* gb) {
157	GBUnloadROM(gb);
158
159	GBMemoryDeinit(gb);
160}
161
162void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
163	irqh->reset = GBReset;
164	irqh->processEvents = GBProcessEvents;
165	irqh->setInterrupts = GBSetInterrupts;
166	irqh->hitIllegal = GBIllegal;
167	irqh->stop = GBStop;
168	irqh->halt = GBHalt;
169}
170
171void GBReset(struct LR35902Core* cpu) {
172	struct GB* gb = (struct GB*) cpu->master;
173
174	const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
175	if (cart->cgb & 0x80) {
176		gb->model = GB_MODEL_CGB;
177		gb->audio.style = GB_AUDIO_CGB;
178		cpu->a = 0x11;
179	} else {
180		// TODO: SGB
181		gb->model = GB_MODEL_DMG;
182		gb->audio.style = GB_AUDIO_DMG;
183		cpu->a = 1;
184	}
185
186	cpu->f.packed = 0xB0;
187	cpu->b = 0;
188	cpu->c = 0x13;
189	cpu->d = 0;
190	cpu->e = 0xD8;
191	cpu->h = 1;
192	cpu->l = 0x4D;
193	cpu->sp = 0xFFFE;
194	cpu->pc = 0x100;
195
196	if (gb->yankedRomSize) {
197		gb->memory.romSize = gb->yankedRomSize;
198		gb->yankedRomSize = 0;
199	}
200	GBMemoryReset(gb);
201	GBVideoReset(&gb->video);
202	GBTimerReset(&gb->timer);
203	GBIOReset(gb);
204	GBAudioReset(&gb->audio);
205}
206
207void GBUpdateIRQs(struct GB* gb) {
208	int irqs = gb->memory.ie & gb->memory.io[REG_IF];
209	if (!irqs) {
210		return;
211	}
212	gb->cpu->halted = false;
213
214	if (!gb->memory.ime) {
215		return;
216	}
217
218	if (irqs & (1 << GB_IRQ_VBLANK)) {
219		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
220		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
221		return;
222	}
223	if (irqs & (1 << GB_IRQ_LCDSTAT)) {
224		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
225		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
226		return;
227	}
228	if (irqs & (1 << GB_IRQ_TIMER)) {
229		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
230		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
231		return;
232	}
233	if (irqs & (1 << GB_IRQ_SIO)) {
234		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
235		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
236		return;
237	}
238	if (irqs & (1 << GB_IRQ_KEYPAD)) {
239		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
240		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
241	}
242}
243
244void GBProcessEvents(struct LR35902Core* cpu) {
245	struct GB* gb = (struct GB*) cpu->master;
246	do {
247		int32_t cycles = cpu->nextEvent;
248		int32_t nextEvent = INT_MAX;
249		int32_t testEvent;
250
251		if (gb->eiPending) {
252			gb->eiPending -= cycles;
253			if (gb->eiPending <= 0) {
254				gb->memory.ime = true;
255				GBUpdateIRQs(gb);
256				gb->eiPending = 0;
257			}
258		}
259
260		testEvent = GBVideoProcessEvents(&gb->video, cycles >> gb->doubleSpeed);
261		if (testEvent != INT_MAX) {
262			testEvent <<= gb->doubleSpeed;
263			if (testEvent < nextEvent) {
264				nextEvent = testEvent;
265			}
266		}
267
268		testEvent = GBAudioProcessEvents(&gb->audio, cycles >> gb->doubleSpeed);
269		if (testEvent != INT_MAX) {
270			testEvent <<= gb->doubleSpeed;
271			if (testEvent < nextEvent) {
272				nextEvent = testEvent;
273			}
274		}
275
276		testEvent = GBTimerProcessEvents(&gb->timer, cycles);
277		if (testEvent < nextEvent) {
278			nextEvent = testEvent;
279		}
280
281		testEvent = GBMemoryProcessEvents(gb, cycles);
282		if (testEvent < nextEvent) {
283			nextEvent = testEvent;
284		}
285
286		cpu->cycles -= cycles;
287		cpu->nextEvent = nextEvent;
288
289		if (cpu->halted) {
290			cpu->cycles = cpu->nextEvent;
291		}
292	} while (cpu->cycles >= cpu->nextEvent);
293}
294
295void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
296	struct GB* gb = (struct GB*) cpu->master;
297	if (!enable) {
298		gb->memory.ime = enable;
299		gb->eiPending = 0;
300		GBUpdateIRQs(gb);
301	} else {
302		if (cpu->nextEvent > cpu->cycles + 4) {
303			cpu->nextEvent = cpu->cycles + 4;
304		}
305		gb->eiPending = cpu->cycles + 4;
306	}
307}
308
309void GBHalt(struct LR35902Core* cpu) {
310	cpu->cycles = cpu->nextEvent;
311	cpu->halted = true;
312}
313
314void GBStop(struct LR35902Core* cpu) {
315	struct GB* gb = (struct GB*) cpu->master;
316	if (gb->memory.io[REG_KEY1] & 1) {
317		gb->doubleSpeed ^= 1;
318		gb->memory.io[REG_KEY1] &= 1;
319		gb->memory.io[REG_KEY1] |= gb->doubleSpeed << 7;
320	}
321	// TODO: Actually stop
322}
323
324void GBIllegal(struct LR35902Core* cpu) {
325	// TODO
326	mLOG(GB, GAME_ERROR, "Hit illegal opcode at address %04X:%02X\n", cpu->pc, cpu->bus);
327}
328
329bool GBIsROM(struct VFile* vf) {
330	vf->seek(vf, 0x104, SEEK_SET);
331	uint8_t header[4];
332	static const uint8_t knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
333
334	if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
335		return false;
336	}
337	if (memcmp(header, knownHeader, sizeof(header))) {
338		return false;
339	}
340	return true;
341}
342
343void GBGetGameTitle(struct GB* gb, char* out) {
344	const struct GBCartridge* cart = NULL;
345	if (gb->memory.rom) {
346		cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
347	}
348	if (gb->pristineRom) {
349		cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
350	}
351	if (!cart) {
352		return;
353	}
354	if (cart->oldLicensee != 0x33) {
355		memcpy(out, cart->titleLong, 16);
356	} else {
357		memcpy(out, cart->titleShort, 11);
358	}
359}
360
361void GBGetGameCode(struct GB* gb, char* out) {
362	memset(out, 0, 4);
363	const struct GBCartridge* cart = NULL;
364	if (gb->memory.rom) {
365		cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
366	}
367	if (gb->pristineRom) {
368		cart = (const struct GBCartridge*) &((uint8_t*) gb->pristineRom)[0x100];
369	}
370	if (!cart) {
371		return;
372	}
373	if (cart->oldLicensee == 0x33) {
374		memcpy(out, cart->maker, 11);
375	}
376}