all repos — mgba @ d7d8dacaa86c8d39cd63fc728f7dd13f9fe5e4aa

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