all repos — mgba @ 752b4faaff506092ed6017f14d6adb6463337243

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