all repos — mgba @ 61e7cc9556eaa118efa209c2b9cef2a550539bb7

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 "util/crc32.h"
 11#include "util/memory.h"
 12#include "util/math.h"
 13#include "util/patch.h"
 14#include "util/vfs.h"
 15
 16const uint32_t DMG_LR35902_FREQUENCY = 0x400000;
 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);
 23
 24static void GBInit(struct LR35902Core* cpu, struct LR35902Component* component);
 25static void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh);
 26static void GBProcessEvents(struct LR35902Core* cpu);
 27static void GBSetInterrupts(struct LR35902Core* cpu, bool enable);
 28static void GBHitStub(struct LR35902Core* cpu);
 29
 30void GBCreate(struct GB* gb) {
 31	gb->d.id = GB_COMPONENT_MAGIC;
 32	gb->d.init = GBInit;
 33	gb->d.deinit = 0;
 34}
 35
 36static void GBInit(struct LR35902Core* cpu, struct LR35902Component* component) {
 37	struct GB* gb = (struct GB*) component;
 38	gb->cpu = cpu;
 39
 40	GBInterruptHandlerInit(&cpu->irqh);
 41	GBMemoryInit(gb);
 42
 43	gb->video.p = gb;
 44	GBVideoInit(&gb->video);
 45
 46	gb->timer.p = gb;
 47
 48	gb->romVf = 0;
 49	gb->sramVf = 0;
 50
 51	gb->pristineRom = 0;
 52	gb->pristineRomSize = 0;
 53	gb->yankedRomSize = 0;
 54}
 55
 56bool GBLoadROM(struct GB* gb, struct VFile* vf, struct VFile* sav, const char* fname) {
 57	GBUnloadROM(gb);
 58	gb->romVf = vf;
 59	gb->pristineRomSize = vf->size(vf);
 60	vf->seek(vf, 0, SEEK_SET);
 61#ifdef _3DS
 62	gb->pristineRom = 0;
 63	if (gb->pristineRomSize <= romBufferSize) {
 64		gb->pristineRom = romBuffer;
 65		vf->read(vf, romBuffer, gb->pristineRomSize);
 66	}
 67#else
 68	gb->pristineRom = vf->map(vf, gb->pristineRomSize, MAP_READ);
 69#endif
 70	if (!gb->pristineRom) {
 71		return false;
 72	}
 73	gb->yankedRomSize = 0;
 74	gb->memory.rom = gb->pristineRom;
 75	gb->activeFile = fname;
 76	gb->memory.romSize = gb->pristineRomSize;
 77	gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize);
 78	gb->sramVf = sav;
 79	if (sav) {
 80		gb->memory.sram = sav->map(sav, 0x8000, MAP_WRITE);
 81	} else {
 82		gb->memory.sram = anonymousMemoryMap(0x8000);
 83	}
 84	return true;
 85	// TODO: error check
 86}
 87
 88void GBUnloadROM(struct GB* gb) {
 89	// TODO: Share with GBAUnloadROM
 90	if (gb->memory.rom && gb->pristineRom != gb->memory.rom) {
 91		if (gb->yankedRomSize) {
 92			gb->yankedRomSize = 0;
 93		}
 94		mappedMemoryFree(gb->memory.rom, 0x400000);
 95	}
 96	gb->memory.rom = 0;
 97
 98	if (gb->romVf) {
 99#ifndef _3DS
100		gb->romVf->unmap(gb->romVf, gb->pristineRom, gb->pristineRomSize);
101#endif
102		gb->pristineRom = 0;
103		gb->romVf = 0;
104	}
105
106	if (gb->sramVf) {
107		gb->sramVf->unmap(gb->sramVf, gb->memory.sram, 0x8000);
108	} else if (gb->memory.sram) {
109		mappedMemoryFree(gb->memory.sram, 0x8000);
110	}
111	gb->memory.sram = 0;
112}
113
114void GBDestroy(struct GB* gb) {
115	GBUnloadROM(gb);
116
117	GBMemoryDeinit(gb);
118}
119
120void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
121	irqh->reset = GBReset;
122	irqh->processEvents = GBProcessEvents;
123	irqh->setInterrupts = GBSetInterrupts;
124	irqh->hitStub = GBHitStub;
125	irqh->halt = GBHalt;
126}
127
128void GBReset(struct LR35902Core* cpu) {
129	cpu->a = 1;
130	cpu->f.packed = 0xB0;
131	cpu->b = 0;
132	cpu->c = 0x13;
133	cpu->d = 0;
134	cpu->e = 0xD8;
135	cpu->h = 1;
136	cpu->l = 0x4D;
137	cpu->sp = 0xFFFE;
138	cpu->pc = 0x100;
139
140	struct GB* gb = (struct GB*) cpu->master;
141
142	if (gb->yankedRomSize) {
143		gb->memory.romSize = gb->yankedRomSize;
144		gb->yankedRomSize = 0;
145	}
146	GBMemoryReset(gb);
147	GBVideoReset(&gb->video);
148	GBTimerReset(&gb->timer);
149	GBIOReset(gb);
150}
151
152void GBUpdateIRQs(struct GB* gb) {
153	int irqs = gb->memory.ie & gb->memory.io[REG_IF];
154	if (!irqs) {
155		return;
156	}
157	gb->cpu->halted = false;
158
159	if (!gb->memory.ime) {
160		return;
161	}
162
163	gb->cpu->irqh.setInterrupts(gb->cpu, false);
164	if (irqs & (1 << GB_IRQ_VBLANK)) {
165		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_VBLANK);
166		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_VBLANK);
167		return;
168	}
169	if (irqs & (1 << GB_IRQ_LCDSTAT)) {
170		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_LCDSTAT);
171		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_LCDSTAT);
172		return;
173	}
174	if (irqs & (1 << GB_IRQ_TIMER)) {
175		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_TIMER);
176		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_TIMER);
177		return;
178	}
179	if (irqs & (1 << GB_IRQ_SIO)) {
180		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_SIO);
181		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_SIO);
182		return;
183	}
184	if (irqs & (1 << GB_IRQ_KEYPAD)) {
185		LR35902RaiseIRQ(gb->cpu, GB_VECTOR_KEYPAD);
186		gb->memory.io[REG_IF] &= ~(1 << GB_IRQ_KEYPAD);
187	}
188}
189
190void GBProcessEvents(struct LR35902Core* cpu) {
191	struct GB* gb = (struct GB*) cpu->master;
192	do {
193		int32_t cycles = cpu->nextEvent;
194		int32_t nextEvent = INT_MAX;
195		int32_t testEvent;
196
197		testEvent = GBVideoProcessEvents(&gb->video, cycles);
198		if (testEvent < nextEvent) {
199			nextEvent = testEvent;
200		}
201
202		testEvent = GBTimerProcessEvents(&gb->timer, cycles);
203		if (testEvent < nextEvent) {
204			nextEvent = testEvent;
205		}
206
207		testEvent = GBMemoryProcessEvents(gb, cycles);
208		if (testEvent < nextEvent) {
209			nextEvent = testEvent;
210		}
211
212		cpu->cycles -= cycles;
213		cpu->nextEvent = nextEvent;
214
215		if (cpu->halted) {
216			cpu->cycles = cpu->nextEvent;
217		}
218	} while (cpu->cycles >= cpu->nextEvent);
219}
220
221void GBSetInterrupts(struct LR35902Core* cpu, bool enable) {
222	struct GB* gb = (struct GB*) cpu->master;
223	gb->memory.ime = enable;
224	GBUpdateIRQs(gb);
225}
226
227void GBHalt(struct LR35902Core* cpu) {
228	cpu->cycles = cpu->nextEvent;
229	cpu->halted = true;
230}
231
232void GBHitStub(struct LR35902Core* cpu) {
233	// TODO
234	mLOG(GB, STUB, "Hit stub at address %04X:%02X\n", cpu->pc, cpu->bus);
235}
236
237bool GBIsROM(struct VFile* vf) {
238	vf->seek(vf, 0x104, SEEK_SET);
239	uint8_t header[4];
240	static const uint8_t knownHeader[4] = { 0xCE, 0xED, 0x66, 0x66};
241
242	if (vf->read(vf, &header, sizeof(header)) < (ssize_t) sizeof(header)) {
243		return false;
244	}
245	if (memcmp(header, knownHeader, sizeof(header))) {
246		return false;
247	}
248	return true;
249}