all repos — mgba @ 8bfad3f7beb823a9560e017fff526cf18d04b5d4

mGBA Game Boy Advance Emulator

src/ds/ds.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 "ds.h"
  7
  8#include "arm/decoder.h"
  9#include "arm/debugger/debugger.h"
 10#include "arm/isa-inlines.h"
 11#include "ds/bios.h"
 12
 13#include "util/crc32.h"
 14#include "util/memory.h"
 15#include "util/math.h"
 16#include "util/vfs.h"
 17
 18mLOG_DEFINE_CATEGORY(DS, "DS");
 19
 20const uint32_t DS_ARM946ES_FREQUENCY = 0x1FF61FE;
 21const uint32_t DS_ARM7TDMI_FREQUENCY = 0xFFB0FF;
 22const uint32_t DS_COMPONENT_MAGIC = 0x1FF61FE;
 23
 24static const size_t DS_ROM_MAGIC_OFFSET = 0x15C;
 25static const uint8_t DS_ROM_MAGIC[] = { 0x56, 0xCF };
 26
 27enum {
 28	DS7_SP_BASE = 0x380FD80,
 29	DS7_SP_BASE_IRQ = 0x380FF80,
 30	DS7_SP_BASE_SVC = 0x380FFC0,
 31
 32	DS9_SP_BASE = 0x3002F7C,
 33	DS9_SP_BASE_IRQ = 0x3003F80,
 34	DS9_SP_BASE_SVC = 0x3003FC0,
 35};
 36
 37static void DSInit(void* cpu, struct mCPUComponent* component);
 38
 39static void DS7Reset(struct ARMCore* cpu);
 40static void DS7TestIRQ(struct ARMCore* cpu);
 41static void DS7InterruptHandlerInit(struct ARMInterruptHandler* irqh);
 42
 43static void DS9Reset(struct ARMCore* cpu);
 44static void DS9TestIRQ(struct ARMCore* cpu);
 45static void DS9InterruptHandlerInit(struct ARMInterruptHandler* irqh);
 46
 47
 48static void DSProcessEvents(struct ARMCore* cpu);
 49static void DSHitStub(struct ARMCore* cpu, uint32_t opcode);
 50static void DSIllegal(struct ARMCore* cpu, uint32_t opcode);
 51static void DSBreakpoint(struct ARMCore* cpu, int immediate);
 52
 53void DSCreate(struct DS* ds) {
 54	ds->d.id = DS_COMPONENT_MAGIC;
 55	ds->d.init = DSInit;
 56	ds->d.deinit = NULL;
 57	ds->arm7 = NULL;
 58	ds->arm9 = NULL;
 59}
 60
 61static void DSInit(void* cpu, struct mCPUComponent* component) {
 62	struct DS* ds = (struct DS*) component;
 63	struct ARMCore* core = cpu;
 64	if (!ds->arm7) {
 65		// The ARM7 must get initialized first
 66		ds->arm7 = core;
 67		ds->debugger = 0;
 68		ds->sync = 0;
 69		return;
 70	}
 71	ds->arm9 = cpu;
 72
 73	ds->arm9->cp15.r1.c0 = ARMControlRegFillVE(0);
 74
 75	DS7InterruptHandlerInit(&ds->arm7->irqh);
 76	DS9InterruptHandlerInit(&ds->arm9->irqh);
 77	DSMemoryInit(ds);
 78
 79	ds->video.p = ds;
 80
 81	ds->springIRQ7 = 0;
 82	ds->springIRQ9 = 0;
 83	ds->keySource = NULL;
 84	ds->rtcSource = NULL;
 85	ds->rumble = NULL;
 86
 87	ds->romVf = NULL;
 88
 89	ds->keyCallback = NULL;
 90}
 91
 92void DSUnloadROM(struct DS* ds) {
 93	if (ds->romVf) {
 94		ds->romVf->close(ds->romVf);
 95		ds->romVf = NULL;
 96	}
 97}
 98
 99void DSDestroy(struct DS* ds) {
100	DSUnloadROM(ds);
101	DSMemoryDeinit(ds);
102}
103
104void DS7InterruptHandlerInit(struct ARMInterruptHandler* irqh) {
105	irqh->reset = DS7Reset;
106	irqh->processEvents = DSProcessEvents;
107	irqh->swi16 = NULL;
108	irqh->swi32 = NULL;
109	irqh->hitIllegal = DSIllegal;
110	irqh->readCPSR = DS7TestIRQ;
111	irqh->hitStub = DSHitStub;
112	irqh->bkpt16 = DSBreakpoint;
113	irqh->bkpt32 = DSBreakpoint;
114}
115
116void DS9InterruptHandlerInit(struct ARMInterruptHandler* irqh) {
117	irqh->reset = DS9Reset;
118	irqh->processEvents = DSProcessEvents;
119	irqh->swi16 = NULL;
120	irqh->swi32 = NULL;
121	irqh->hitIllegal = DSIllegal;
122	irqh->readCPSR = DS9TestIRQ;
123	irqh->hitStub = DSHitStub;
124	irqh->bkpt16 = DSBreakpoint;
125	irqh->bkpt32 = DSBreakpoint;
126}
127
128void DS7Reset(struct ARMCore* cpu) {
129	ARMSetPrivilegeMode(cpu, MODE_IRQ);
130	cpu->gprs[ARM_SP] = DS7_SP_BASE_IRQ;
131	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
132	cpu->gprs[ARM_SP] = DS7_SP_BASE_SVC;
133	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
134	cpu->gprs[ARM_SP] = DS7_SP_BASE;
135
136	struct DS* ds = (struct DS*) cpu->master;
137	DSMemoryReset(ds);
138
139	struct DSCartridge* header = ds->romVf->map(ds->romVf, sizeof(*header), MAP_READ);
140	if (header) {
141		// TODO: Error check
142		ds->romVf->seek(ds->romVf, header->arm7Offset, SEEK_SET);
143		uint32_t base = header->arm7Base - DS_BASE_RAM;
144		uint32_t* basePointer = &ds->memory.ram[base >> 2];
145		if (base < DS_SIZE_RAM && base + header->arm7Size <= DS_SIZE_RAM) {
146			ds->romVf->read(ds->romVf, basePointer, header->arm7Size);
147		}
148		cpu->gprs[12] = header->arm7Entry;
149		cpu->gprs[ARM_LR] = header->arm7Entry;
150		cpu->gprs[ARM_PC] = header->arm7Entry;
151		int currentCycles = 0;
152		ARM_WRITE_PC;
153
154		ds->romVf->unmap(ds->romVf, header, sizeof(*header));
155	}
156}
157
158void DS9Reset(struct ARMCore* cpu) {
159	ARMSetPrivilegeMode(cpu, MODE_IRQ);
160	cpu->gprs[ARM_SP] = DS9_SP_BASE_IRQ;
161	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
162	cpu->gprs[ARM_SP] = DS9_SP_BASE_SVC;
163	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
164	cpu->gprs[ARM_SP] = DS9_SP_BASE;
165
166	struct DS* ds = (struct DS*) cpu->master;
167	struct DSCartridge* header = ds->romVf->map(ds->romVf, sizeof(*header), MAP_READ);
168	if (header) {
169		// TODO: Error check
170		ds->romVf->seek(ds->romVf, header->arm9Offset, SEEK_SET);
171		uint32_t base = header->arm9Base - DS_BASE_RAM;
172		uint32_t* basePointer = &ds->memory.ram[base >> 2];
173		if (base < DS_SIZE_RAM && base + header->arm9Size <= DS_SIZE_RAM) {
174			ds->romVf->read(ds->romVf, basePointer, header->arm9Size);
175		}
176		cpu->gprs[12] = header->arm9Entry;
177		cpu->gprs[ARM_LR] = header->arm9Entry;
178		cpu->gprs[ARM_PC] = header->arm9Entry;
179		int currentCycles = 0;
180		ARM_WRITE_PC;
181
182		ds->romVf->unmap(ds->romVf, header, sizeof(*header));
183	}
184}
185
186static void DSProcessEvents(struct ARMCore* cpu) {
187	struct DS* ds = (struct DS*) cpu->master;
188
189	if (ds->springIRQ7) {
190		ARMRaiseIRQ(cpu);
191		ds->springIRQ7 = 0;
192	}
193
194	do {
195		int32_t cycles = cpu->nextEvent;
196		int32_t nextEvent = INT_MAX;
197#ifndef NDEBUG
198		if (cycles < 0) {
199			mLOG(DS, FATAL, "Negative cycles passed: %i", cycles);
200		}
201#endif
202
203		cpu->cycles -= cycles;
204		cpu->nextEvent = nextEvent;
205
206		if (cpu->halted) {
207			cpu->cycles = cpu->nextEvent;
208		}
209	} while (cpu->cycles >= cpu->nextEvent);
210}
211
212void DSAttachDebugger(struct DS* ds, struct mDebugger* debugger) {
213	ds->debugger = (struct ARMDebugger*) debugger->platform;
214	ds->arm7->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
215	ds->arm9->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
216	ARMHotplugAttach(ds->arm7, CPU_COMPONENT_DEBUGGER);
217	ARMHotplugAttach(ds->arm9, CPU_COMPONENT_DEBUGGER);
218}
219
220
221void DSDetachDebugger(struct DS* ds) {
222	ds->debugger = NULL;
223	ARMHotplugDetach(ds->arm7, CPU_COMPONENT_DEBUGGER);
224	ARMHotplugDetach(ds->arm9, CPU_COMPONENT_DEBUGGER);
225	ds->arm7->components[CPU_COMPONENT_DEBUGGER] = NULL;
226	ds->arm9->components[CPU_COMPONENT_DEBUGGER] = NULL;
227}
228
229bool DSLoadROM(struct DS* ds, struct VFile* vf) {
230	DSUnloadROM(ds);
231	ds->romVf = vf;
232	// TODO: error check
233	return true;
234}
235
236bool DSIsROM(struct VFile* vf) {
237	if (vf->seek(vf, DS_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
238		return false;
239	}
240	uint8_t signature[sizeof(DS_ROM_MAGIC)];
241	if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
242		return false;
243	}
244	return memcmp(signature, DS_ROM_MAGIC, sizeof(signature)) == 0;
245}
246
247bool DSLoadBIOS(struct DS* ds, struct VFile* vf) {
248	size_t size = vf->size(vf);
249	void* data = NULL;
250	uint32_t crc;
251	if (size == DS7_SIZE_BIOS) {
252		data = vf->map(vf, size, MAP_READ);
253	} else if (size == 0x1000) {
254		data = vf->map(vf, size, MAP_READ);
255	}
256	if (!data) {
257		return false;
258	}
259	crc = doCrc32(data, size);
260	if (crc == DS7_BIOS_CHECKSUM) {
261		ds->bios7Vf = vf;
262		ds->memory.bios7 = data;
263		mLOG(DS, INFO, "Official DS ARM7 BIOS detected");
264	} else if (crc == DS9_BIOS_CHECKSUM) {
265		ds->bios9Vf = vf;
266		ds->memory.bios9 = data;
267		mLOG(DS, INFO, "Official DS ARM9 BIOS detected");
268	} else {
269		mLOG(DS, WARN, "BIOS checksum incorrect");
270		vf->unmap(vf, data, size);
271		return false;
272	}
273	return true;
274}
275
276void DSGetGameCode(struct DS* ds, char* out) {
277	memset(out, 0, 8);
278	if (!ds->romVf) {
279		return;
280	}
281
282	struct DSCartridge* cart = ds->romVf->map(ds->romVf, sizeof(*cart), MAP_READ);
283	memcpy(out, "NTR-", 4);
284	memcpy(&out[4], &cart->id, 4);
285	ds->romVf->unmap(ds->romVf, cart, sizeof(*cart));
286}
287
288void DSGetGameTitle(struct DS* ds, char* out) {
289	memset(out, 0, 12);
290	if (!ds->romVf) {
291		return;
292	}
293
294	struct DSCartridge* cart = ds->romVf->map(ds->romVf, sizeof(*cart), MAP_READ);
295	memcpy(out, &cart->title, 4);
296	ds->romVf->unmap(ds->romVf, cart, sizeof(*cart));
297}
298
299void DSHitStub(struct ARMCore* cpu, uint32_t opcode) {
300	struct DS* ds = (struct DS*) cpu->master;
301	if (ds->debugger) {
302		struct mDebuggerEntryInfo info = {
303			.address = _ARMPCAddress(cpu),
304			.opcode = opcode
305		};
306		mDebuggerEnter(ds->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
307	}
308	// TODO: More sensible category?
309	mLOG(DS, ERROR, "Stub opcode: %08x", opcode);
310}
311
312void DSIllegal(struct ARMCore* cpu, uint32_t opcode) {
313	struct DS* ds = (struct DS*) cpu->master;
314	if (ds->debugger) {
315		struct mDebuggerEntryInfo info = {
316			.address = _ARMPCAddress(cpu),
317			.opcode = opcode
318		};
319		mDebuggerEnter(ds->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
320	} else {
321		ARMRaiseUndefined(cpu);
322	}
323}
324
325void DSBreakpoint(struct ARMCore* cpu, int immediate) {
326	struct DS* ds = (struct DS*) cpu->master;
327	if (immediate >= CPU_COMPONENT_MAX) {
328		return;
329	}
330	switch (immediate) {
331	case CPU_COMPONENT_DEBUGGER:
332		if (ds->debugger) {
333			struct mDebuggerEntryInfo info = {
334				.address = _ARMPCAddress(cpu)
335			};
336			mDebuggerEnter(ds->debugger->d.p, DEBUGGER_ENTER_BREAKPOINT, &info);
337		}
338		break;
339	default:
340		break;
341	}
342}
343
344void DS7TestIRQ(struct ARMCore* cpu) {
345	struct DS* ds = (struct DS*) cpu->master;
346	if (0) {
347		ds->springIRQ7 = 1;
348		cpu->nextEvent = cpu->cycles;
349	}
350}
351
352void DS9TestIRQ(struct ARMCore* cpu) {
353	struct DS* ds = (struct DS*) cpu->master;
354	if (0) {
355		ds->springIRQ9 = 1;
356		cpu->nextEvent = cpu->cycles;
357	}
358}