all repos — mgba @ 3341cc386567c7713aaa3ccab67677635293ba29

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 <mgba/internal/ds/ds.h>
  7
  8#include <mgba/core/interface.h>
  9#include <mgba/internal/arm/decoder.h>
 10#include <mgba/internal/arm/debugger/debugger.h>
 11#include <mgba/internal/arm/isa-inlines.h>
 12#include <mgba/internal/ds/bios.h>
 13
 14#include <mgba-util/crc32.h>
 15#include <mgba-util/memory.h>
 16#include <mgba-util/math.h>
 17#include <mgba-util/vfs.h>
 18
 19#define SLICE_CYCLES 2048
 20
 21mLOG_DEFINE_CATEGORY(DS, "DS");
 22
 23const uint32_t DS_ARM946ES_FREQUENCY = 0x1FF61FE;
 24const uint32_t DS_ARM7TDMI_FREQUENCY = 0xFFB0FF;
 25const uint32_t DS_COMPONENT_MAGIC = 0x1FF61FE;
 26
 27static const size_t DS_ROM_MAGIC_OFFSET = 0x15C;
 28static const uint8_t DS_ROM_MAGIC[] = { 0x56, 0xCF };
 29static const uint8_t DS_ROM_MAGIC_2[] = { 0x1A, 0x9E };
 30
 31enum {
 32	DS7_SP_BASE = 0x380FD80,
 33	DS7_SP_BASE_IRQ = 0x380FF80,
 34	DS7_SP_BASE_SVC = 0x380FFC0,
 35
 36	DS9_SP_BASE = 0x3002F7C,
 37	DS9_SP_BASE_IRQ = 0x3003F80,
 38	DS9_SP_BASE_SVC = 0x3003FC0,
 39};
 40
 41static void DSInit(void* cpu, struct mCPUComponent* component);
 42
 43static void DS7Reset(struct ARMCore* cpu);
 44static void DS7TestIRQ(struct ARMCore* cpu);
 45static void DS7InterruptHandlerInit(struct ARMInterruptHandler* irqh);
 46static void DS7ProcessEvents(struct ARMCore* cpu);
 47
 48static void DS9Reset(struct ARMCore* cpu);
 49static void DS9TestIRQ(struct ARMCore* cpu);
 50static void DS9WriteCP15(struct ARMCore* cpu, int crn, int crm, int opcode1, int opcode2, uint32_t value);
 51static uint32_t DS9ReadCP15(struct ARMCore* cpu, int crn, int crm, int opcode1, int opcode2);
 52static void DS9InterruptHandlerInit(struct ARMInterruptHandler* irqh);
 53static void DS9ProcessEvents(struct ARMCore* cpu);
 54
 55static void DSProcessEvents(struct DSCommon* dscore);
 56static void DSHitStub(struct ARMCore* cpu, uint32_t opcode);
 57static void DSIllegal(struct ARMCore* cpu, uint32_t opcode);
 58static void DSBreakpoint(struct ARMCore* cpu, int immediate);
 59
 60static void _slice(struct mTiming* timing, void* context, uint32_t cyclesLate) {
 61	UNUSED(cyclesLate);
 62	struct DS* ds = context;
 63	uint32_t cycles = mTimingCurrentTime(timing) - ds->sliceStart;
 64	if (ds->activeCpu == ds->ds9.cpu) {
 65		ds->activeCpu = ds->ds7.cpu;
 66		ds->cycleDrift += cycles;
 67		cycles = ds->cycleDrift >> 1;
 68		timing = &ds->ds7.timing;
 69	} else {
 70		ds->activeCpu = ds->ds9.cpu;
 71		ds->cycleDrift -= cycles << 1;
 72		cycles = ds->cycleDrift + SLICE_CYCLES;
 73		timing = &ds->ds9.timing;
 74	}
 75	mTimingSchedule(timing, &ds->slice, cycles);
 76	ds->sliceStart = mTimingCurrentTime(timing);
 77	ds->earlyExit = true;
 78}
 79
 80static void _divide(struct mTiming* timing, void* context, uint32_t cyclesLate) {
 81	UNUSED(timing);
 82	UNUSED(cyclesLate);
 83	struct DS* ds = context;
 84	ds->memory.io9[DS9_REG_DIVCNT >> 1] &= ~0x8000;
 85	int64_t numerator;
 86	int64_t denominator;
 87	LOAD_64LE(numerator, DS9_REG_DIV_NUMER_0, ds->memory.io9);
 88	LOAD_64LE(denominator, DS9_REG_DIV_DENOM_0, ds->memory.io9);
 89	bool max = false;
 90	switch (ds->memory.io9[DS9_REG_DIVCNT >> 1] & 0x3) {
 91	case 0:
 92		numerator = (int64_t)(int32_t) numerator;
 93	case 1:
 94	case 3:
 95		denominator = (int64_t)(int32_t) denominator;
 96		break;
 97	}
 98	if (numerator == INT64_MIN) {
 99		max = true;
100	}
101	if (!denominator) {
102		ds->memory.io9[DS9_REG_DIVCNT >> 1] |= 0x4000;
103		STORE_64LE(numerator, DS9_REG_DIVREM_RESULT_0, ds->memory.io9);
104		numerator >>= 63LL;
105		numerator = -numerator;
106		STORE_64LE(numerator, DS9_REG_DIV_RESULT_0, ds->memory.io9);
107		return;
108	}
109	if (denominator == -1LL && max) {
110		ds->memory.io9[DS9_REG_DIVCNT >> 1] |= 0x4000;
111		STORE_64LE(numerator, DS9_REG_DIV_RESULT_0, ds->memory.io9);
112		return;
113	}
114	ds->memory.io9[DS9_REG_DIVCNT >> 1] &= ~0x4000;
115	int64_t result = numerator / denominator;
116	int64_t remainder = numerator % denominator; // TODO: defined behavior for negative denominator?
117	STORE_64LE(result, DS9_REG_DIV_RESULT_0, ds->memory.io9);
118	STORE_64LE(remainder, DS9_REG_DIVREM_RESULT_0, ds->memory.io9);
119}
120
121	static void _sqrt(struct mTiming* timing, void* context, uint32_t cyclesLate) {
122	UNUSED(timing);
123	UNUSED(cyclesLate);
124	struct DS* ds = context;
125	ds->memory.io9[DS9_REG_SQRTCNT >> 1] &= ~0x8000;
126	uint64_t param;
127	LOAD_64LE(param, DS9_REG_SQRT_PARAM_0, ds->memory.io9);
128	if (!(ds->memory.io9[DS9_REG_SQRTCNT >> 1] & 1)) {
129		param &= 0xFFFFFFFFULL;
130	}
131
132	uint64_t result = 0;
133	uint64_t bit = 0x4000000000000000ULL; // The second-to-top bit is set: 1 << 30 for 32 bits
134
135	// "bit" starts at the highest power of four <= the argument.
136	while (bit > param) {
137		bit >>= 2;
138	}
139
140	while (bit != 0) {
141		if (param >= param + bit) {
142			param -= param + bit;
143			param = (result >> 1) + bit;
144		} else {
145			param >>= 1;
146		}
147		bit >>= 2;
148	}
149	STORE_32LE(result, DS9_REG_SQRT_RESULT_LO, ds->memory.io9);
150}
151
152void DSCreate(struct DS* ds) {
153	ds->d.id = DS_COMPONENT_MAGIC;
154	ds->d.init = DSInit;
155	ds->d.deinit = NULL;
156	ds->ds7.p = ds;
157	ds->ds9.p = ds;
158	ds->ds7.cpu = NULL;
159	ds->ds9.cpu = NULL;
160	ds->ds7.ipc = &ds->ds9;
161	ds->ds9.ipc = &ds->ds7;
162}
163
164static void DSInit(void* cpu, struct mCPUComponent* component) {
165	struct DS* ds = (struct DS*) component;
166	struct ARMCore* core = cpu;
167	if (!ds->ds7.cpu) {
168		// The ARM7 must get initialized first
169		ds->ds7.cpu = core;
170		ds->debugger = 0;
171		ds->sync = 0;
172		return;
173	}
174	ds->ds9.cpu = cpu;
175	ds->activeCpu = NULL;
176
177	ds->ds9.cpu->cp15.r1.c0 = ARMControlRegFillVE(0);
178
179	ds->slice.name = "DS CPU Time Slicing";
180	ds->slice.callback = _slice;
181	ds->slice.context = ds;
182	ds->slice.priority = UINT_MAX;
183
184	CircleBufferInit(&ds->ds7.fifo, 64);
185	CircleBufferInit(&ds->ds9.fifo, 64);
186
187	DS7InterruptHandlerInit(&ds->ds7.cpu->irqh);
188	DS9InterruptHandlerInit(&ds->ds9.cpu->irqh);
189	DSMemoryInit(ds);
190	DSDMAInit(ds);
191
192	DSVideoInit(&ds->video);
193	ds->video.p = ds;
194
195	ds->ds7.springIRQ = 0;
196	ds->ds9.springIRQ = 0;
197	DSTimerInit(ds);
198	ds->keySource = NULL;
199	ds->rtcSource = NULL;
200	ds->rumble = NULL;
201
202	ds->romVf = NULL;
203
204	ds->keyCallback = NULL;
205
206	ds->divEvent.name = "DS Hardware Divide";
207	ds->divEvent.callback = _divide;
208	ds->divEvent.context = ds;
209	ds->divEvent.priority = 0x50;
210
211	ds->sqrtEvent.name = "DS Hardware Sqrt";
212	ds->sqrtEvent.callback = _sqrt;
213	ds->sqrtEvent.context = ds;
214	ds->sqrtEvent.priority = 0x51;
215
216	mTimingInit(&ds->ds7.timing, &ds->ds7.cpu->cycles, &ds->ds7.cpu->nextEvent);
217	mTimingInit(&ds->ds9.timing, &ds->ds9.cpu->cycles, &ds->ds9.cpu->nextEvent);
218}
219
220void DSUnloadROM(struct DS* ds) {
221	if (ds->romVf) {
222		ds->romVf->close(ds->romVf);
223		ds->romVf = NULL;
224	}
225}
226
227void DSDestroy(struct DS* ds) {
228	CircleBufferDeinit(&ds->ds7.fifo);
229	CircleBufferDeinit(&ds->ds9.fifo);
230	DSUnloadROM(ds);
231	DSMemoryDeinit(ds);
232	mTimingDeinit(&ds->ds7.timing);
233	mTimingDeinit(&ds->ds9.timing);
234}
235
236void DS7InterruptHandlerInit(struct ARMInterruptHandler* irqh) {
237	irqh->reset = DS7Reset;
238	irqh->processEvents = DS7ProcessEvents;
239	irqh->swi16 = DS7Swi16;
240	irqh->swi32 = DS7Swi32;
241	irqh->hitIllegal = DSIllegal;
242	irqh->readCPSR = DS7TestIRQ;
243	irqh->writeCP15 = NULL;
244	irqh->readCP15 = NULL;
245	irqh->hitStub = DSHitStub;
246	irqh->bkpt16 = DSBreakpoint;
247	irqh->bkpt32 = DSBreakpoint;
248}
249
250void DS9InterruptHandlerInit(struct ARMInterruptHandler* irqh) {
251	irqh->reset = DS9Reset;
252	irqh->processEvents = DS9ProcessEvents;
253	irqh->swi16 = DS9Swi16;
254	irqh->swi32 = DS9Swi32;
255	irqh->hitIllegal = DSIllegal;
256	irqh->readCPSR = DS9TestIRQ;
257	irqh->writeCP15 = DS9WriteCP15;
258	irqh->readCP15 = DS9ReadCP15;
259	irqh->hitStub = DSHitStub;
260	irqh->bkpt16 = DSBreakpoint;
261	irqh->bkpt32 = DSBreakpoint;
262}
263
264void DS7Reset(struct ARMCore* cpu) {
265	ARMSetPrivilegeMode(cpu, MODE_IRQ);
266	cpu->gprs[ARM_SP] = DS7_SP_BASE_IRQ;
267	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
268	cpu->gprs[ARM_SP] = DS7_SP_BASE_SVC;
269	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
270	cpu->gprs[ARM_SP] = DS7_SP_BASE;
271
272	struct DS* ds = (struct DS*) cpu->master;
273	mTimingClear(&ds->ds7.timing);
274	CircleBufferInit(&ds->ds7.fifo, 64);
275	DSMemoryReset(ds);
276	DSDMAReset(&ds->ds7);
277	DS7IOInit(ds);
278
279	struct DSCartridge* header = ds->romVf->map(ds->romVf, sizeof(*header), MAP_READ);
280	if (header) {
281		memcpy(&ds->memory.ram[0x3FFE00 >> 2], header, 0x170);
282		DS7IOWrite32(ds, DS_REG_ROMCNT_LO, header->busTiming | 0x2700000);
283		// TODO: Error check
284		ds->romVf->seek(ds->romVf, header->arm7Offset, SEEK_SET);
285		uint32_t base = header->arm7Base - DS_BASE_RAM;
286		uint32_t* basePointer = &ds->memory.ram[base >> 2];
287		if (base < DS_SIZE_RAM && base + header->arm7Size <= DS_SIZE_RAM) {
288			ds->romVf->read(ds->romVf, basePointer, header->arm7Size);
289		}
290		cpu->gprs[12] = header->arm7Entry;
291		cpu->gprs[ARM_LR] = header->arm7Entry;
292		cpu->gprs[ARM_PC] = header->arm7Entry;
293		int currentCycles = 0;
294		ARM_WRITE_PC;
295
296		ds->romVf->unmap(ds->romVf, header, sizeof(*header));
297	}
298}
299
300void DS9Reset(struct ARMCore* cpu) {
301	ARMSetPrivilegeMode(cpu, MODE_IRQ);
302	cpu->gprs[ARM_SP] = DS9_SP_BASE_IRQ;
303	ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
304	cpu->gprs[ARM_SP] = DS9_SP_BASE_SVC;
305	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
306	cpu->gprs[ARM_SP] = DS9_SP_BASE;
307
308	struct DS* ds = (struct DS*) cpu->master;
309	mTimingClear(&ds->ds9.timing);
310	CircleBufferInit(&ds->ds9.fifo, 64);
311	DSVideoReset(&ds->video);
312	DSDMAReset(&ds->ds9);
313	DS9IOInit(ds);
314
315	ds->activeCpu = cpu;
316	mTimingSchedule(&ds->ds9.timing, &ds->slice, SLICE_CYCLES);
317	ds->cycleDrift = 0;
318	ds->sliceStart = mTimingCurrentTime(&ds->ds9.timing);
319
320	struct DSCartridge* header = ds->romVf->map(ds->romVf, sizeof(*header), MAP_READ);
321	if (header) {
322		// TODO: Error check
323		ds->romVf->seek(ds->romVf, header->arm9Offset, SEEK_SET);
324		uint32_t base = header->arm9Base - DS_BASE_RAM;
325		uint32_t* basePointer = &ds->memory.ram[base >> 2];
326		if (base < DS_SIZE_RAM && base + header->arm9Size <= DS_SIZE_RAM) {
327			ds->romVf->read(ds->romVf, basePointer, header->arm9Size);
328		}
329		cpu->gprs[12] = header->arm9Entry;
330		cpu->gprs[ARM_LR] = header->arm9Entry;
331		cpu->gprs[ARM_PC] = header->arm9Entry;
332		int currentCycles = 0;
333		ARM_WRITE_PC;
334
335		ds->romVf->unmap(ds->romVf, header, sizeof(*header));
336	}
337}
338
339static void DS7ProcessEvents(struct ARMCore* cpu) {
340	struct DS* ds = (struct DS*) cpu->master;
341	DSProcessEvents(&ds->ds7);
342}
343
344static void DS9ProcessEvents(struct ARMCore* cpu) {
345	struct DS* ds = (struct DS*) cpu->master;
346	DSProcessEvents(&ds->ds9);
347}
348
349static void DSProcessEvents(struct DSCommon* dscore) {
350	struct ARMCore* cpu = dscore->cpu;
351	struct DS* ds = dscore->p;
352	if (dscore->springIRQ && !cpu->cpsr.i) {
353		ARMRaiseIRQ(cpu);
354		dscore->springIRQ = 0;
355	}
356
357	int32_t nextEvent = cpu->nextEvent;
358	while (cpu->cycles >= nextEvent) {
359		int32_t cycles = cpu->cycles;
360
361		cpu->cycles = 0;
362		cpu->nextEvent = 0;
363
364#ifndef NDEBUG
365		if (cycles < 0) {
366			mLOG(DS, FATAL, "Negative cycles passed: %i", cycles);
367		}
368#endif
369		nextEvent = cycles;
370		do {
371			nextEvent = mTimingTick(&dscore->timing, nextEvent);
372		} while (ds->cpuBlocked);
373
374		if (ds->earlyExit) {
375			ds->earlyExit = false;
376			break;
377		}
378
379		cpu->nextEvent = nextEvent;
380		if (cpu->halted) {
381			cpu->cycles = nextEvent;
382		}
383#ifndef NDEBUG
384		else if (nextEvent < 0) {
385			mLOG(DS, FATAL, "Negative cycles will pass: %i", nextEvent);
386		}
387#endif
388	}
389}
390
391void DSRunLoop(struct DS* ds) {
392	if (ds->activeCpu == ds->ds9.cpu) {
393		ARMv5RunLoop(ds->ds9.cpu);
394	} else {
395		ARMv4RunLoop(ds->ds7.cpu);
396	}
397}
398
399void DS7Step(struct DS* ds) {
400	int32_t pc = ds->ds7.cpu->gprs[ARM_PC];
401	do {
402		while (ds->activeCpu == ds->ds9.cpu) {
403			ARMv5RunLoop(ds->ds9.cpu);
404		}
405		ARMv4Run(ds->ds7.cpu);
406	} while (ds->ds7.cpu->halted || ds->ds7.cpu->gprs[ARM_PC] == pc);
407}
408
409void DS9Step(struct DS* ds) {
410	int32_t pc = ds->ds9.cpu->gprs[ARM_PC];
411	do {
412		while (ds->activeCpu == ds->ds7.cpu) {
413			ARMv4RunLoop(ds->ds7.cpu);
414		}
415		ARMv5Run(ds->ds9.cpu);
416	} while (ds->ds9.cpu->halted || ds->ds9.cpu->gprs[ARM_PC] == pc);
417}
418
419void DSAttachDebugger(struct DS* ds, struct mDebugger* debugger) {
420	ds->debugger = (struct ARMDebugger*) debugger->platform;
421	ds->ds7.cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
422	ds->ds9.cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
423	ARMHotplugAttach(ds->ds7.cpu, CPU_COMPONENT_DEBUGGER);
424	ARMHotplugAttach(ds->ds9.cpu, CPU_COMPONENT_DEBUGGER);
425}
426
427void DSDetachDebugger(struct DS* ds) {
428	ds->debugger = NULL;
429	ARMHotplugDetach(ds->ds7.cpu, CPU_COMPONENT_DEBUGGER);
430	ARMHotplugDetach(ds->ds9.cpu, CPU_COMPONENT_DEBUGGER);
431	ds->ds7.cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
432	ds->ds9.cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
433}
434
435bool DSLoadROM(struct DS* ds, struct VFile* vf) {
436	DSUnloadROM(ds);
437	ds->romVf = vf;
438	// TODO: error check
439	return true;
440}
441
442bool DSIsROM(struct VFile* vf) {
443	if (vf->seek(vf, DS_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
444		return false;
445	}
446	uint8_t signature[sizeof(DS_ROM_MAGIC)];
447	if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
448		return false;
449	}
450	return memcmp(signature, DS_ROM_MAGIC, sizeof(signature)) == 0 || memcmp(signature, DS_ROM_MAGIC_2, sizeof(signature)) == 0;
451}
452
453bool DSIsBIOS7(struct VFile* vf) {
454	size_t size = vf->size(vf);
455	void* data = NULL;
456	uint32_t crc;
457	if (size == DS7_SIZE_BIOS) {
458		data = vf->map(vf, size, MAP_READ);
459	}
460	if (!data) {
461		return false;
462	}
463	crc = doCrc32(data, size);
464	vf->unmap(vf, data, size);
465	return crc == DS7_BIOS_CHECKSUM;
466}
467
468bool DSIsBIOS9(struct VFile* vf) {
469	size_t size = vf->size(vf);
470	void* data = NULL;
471	uint32_t crc;
472	if (size == DS9_SIZE_BIOS) {
473		data = vf->map(vf, 0x1000, MAP_READ);
474	} else if (size == 0x1000) {
475		data = vf->map(vf, 0x1000, MAP_READ);
476	}
477	if (!data) {
478		return false;
479	}
480	crc = doCrc32(data, 0x1000);
481	vf->unmap(vf, data, 0x1000);
482	return crc == DS9_BIOS_CHECKSUM;
483}
484
485bool DSLoadBIOS(struct DS* ds, struct VFile* vf) {
486	size_t size = vf->size(vf);
487	void* data = NULL;
488	uint32_t crc;
489	if (size == DS7_SIZE_BIOS) {
490		data = vf->map(vf, size, MAP_READ);
491	} else if (size == 0x1000) {
492		data = calloc(DS9_SIZE_BIOS, 1);
493		vf->read(vf, data, size);
494	} else if (size == DS9_SIZE_BIOS) {
495		data = vf->map(vf, size, MAP_READ);
496	}
497	if (!data) {
498		return false;
499	}
500	crc = doCrc32(data, size);
501	if (crc == DS7_BIOS_CHECKSUM) {
502		ds->bios7Vf = vf;
503		ds->memory.bios7 = data;
504		mLOG(DS, INFO, "Official DS ARM7 BIOS detected");
505	} else if (crc == DS9_BIOS_CHECKSUM) {
506		ds->bios9Vf = vf;
507		ds->memory.bios9 = data;
508		mLOG(DS, INFO, "Official DS ARM9 BIOS detected");
509	} else {
510		mLOG(DS, WARN, "BIOS checksum incorrect");
511		vf->unmap(vf, data, size);
512		return false;
513	}
514	return true;
515}
516
517void DSGetGameCode(struct DS* ds, char* out) {
518	memset(out, 0, 8);
519	if (!ds->romVf) {
520		return;
521	}
522
523	struct DSCartridge* cart = ds->romVf->map(ds->romVf, sizeof(*cart), MAP_READ);
524	// TODO: TWL-?
525	memcpy(out, "NTR-", 4);
526	memcpy(&out[4], &cart->id, 4);
527	ds->romVf->unmap(ds->romVf, cart, sizeof(*cart));
528}
529
530void DSGetGameTitle(struct DS* ds, char* out) {
531	memset(out, 0, 12);
532	if (!ds->romVf) {
533		return;
534	}
535
536	struct DSCartridge* cart = ds->romVf->map(ds->romVf, sizeof(*cart), MAP_READ);
537	memcpy(out, &cart->title, 12);
538	ds->romVf->unmap(ds->romVf, cart, sizeof(*cart));
539}
540
541void DSHitStub(struct ARMCore* cpu, uint32_t opcode) {
542	struct DS* ds = (struct DS*) cpu->master;
543	if (ds->debugger) {
544		struct mDebuggerEntryInfo info = {
545			.address = _ARMPCAddress(cpu),
546			.opcode = opcode
547		};
548		mDebuggerEnter(ds->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
549	}
550	// TODO: More sensible category?
551	mLOG(DS, ERROR, "Stub opcode: %08x", opcode);
552}
553
554void DSIllegal(struct ARMCore* cpu, uint32_t opcode) {
555	struct DS* ds = (struct DS*) cpu->master;
556	if (ds->debugger) {
557		struct mDebuggerEntryInfo info = {
558			.address = _ARMPCAddress(cpu),
559			.opcode = opcode
560		};
561		mDebuggerEnter(ds->debugger->d.p, DEBUGGER_ENTER_ILLEGAL_OP, &info);
562	} else {
563		ARMRaiseUndefined(cpu);
564	}
565}
566
567void DSBreakpoint(struct ARMCore* cpu, int immediate) {
568	struct DS* ds = (struct DS*) cpu->master;
569	if (immediate >= CPU_COMPONENT_MAX) {
570		return;
571	}
572	switch (immediate) {
573	case CPU_COMPONENT_DEBUGGER:
574		if (ds->debugger) {
575			struct mDebuggerEntryInfo info = {
576				.address = _ARMPCAddress(cpu)
577			};
578			mDebuggerEnter(ds->debugger->d.p, DEBUGGER_ENTER_BREAKPOINT, &info);
579		}
580		break;
581	default:
582		break;
583	}
584}
585
586void DS7TestIRQ(struct ARMCore* cpu) {
587	struct DS* ds = (struct DS*) cpu->master;
588	if (!ds->memory.io7[DS_REG_IME >> 1]) {
589		return;
590	}
591	uint32_t test = (ds->memory.io7[DS_REG_IE_LO >> 1] & ds->memory.io7[DS_REG_IF_LO >> 1]);
592	test |= (ds->memory.io7[DS_REG_IE_HI >> 1] & ds->memory.io7[DS_REG_IF_HI >> 1]) << 16;
593	if (test) {
594		ds->ds7.springIRQ = test;
595		cpu->nextEvent = cpu->cycles;
596	}
597}
598
599void DS9TestIRQ(struct ARMCore* cpu) {
600	struct DS* ds = (struct DS*) cpu->master;
601	if (!ds->memory.io9[DS_REG_IME >> 1]) {
602		return;
603	}
604	uint32_t test = (ds->memory.io9[DS_REG_IE_LO >> 1] & ds->memory.io9[DS_REG_IF_LO >> 1]);
605	test |= (ds->memory.io9[DS_REG_IE_HI >> 1] & ds->memory.io9[DS_REG_IF_HI >> 1]) << 16;
606	if (test) {
607		ds->ds9.springIRQ = test;
608		cpu->nextEvent = cpu->cycles;
609	}
610}
611
612static void _writeSysControl(struct ARMCore* cpu, int crm, int opcode2, uint32_t value) {
613	mLOG(DS, STUB, "CP15 system control write: CRm: %i, Op2: %i, Value: 0x%08X", crm, opcode2, value);
614}
615
616static void _writeCacheControl(struct ARMCore* cpu, int crm, int opcode2, uint32_t value) {
617	mLOG(DS, STUB, "CP15 cache control control write: CRm: %i, Op2: %i, Value: 0x%08X", crm, opcode2, value);
618	switch (opcode2) {
619	case 0:
620		cpu->cp15.r2.d = value;
621		break;
622	case 1:
623		cpu->cp15.r2.i = value;
624		break;
625	default:
626		mLOG(DS, GAME_ERROR, "CP15 cache control control bad op2: %i", opcode2);
627		break;
628	}
629}
630
631static void _writeWriteBufferControl(struct ARMCore* cpu, int crm, int opcode2, uint32_t value) {
632	mLOG(DS, STUB, "CP15 write buffer control write: CRm: %i, Op2: %i, Value: 0x%08X", crm, opcode2, value);
633	switch (opcode2) {
634	case 0:
635		cpu->cp15.r3.d = value;
636		break;
637	default:
638		mLOG(DS, GAME_ERROR, "CP15 cache control control bad op2: %i", opcode2);
639		break;
640	}
641}
642
643static void _writeAccessControl(struct ARMCore* cpu, int crm, int opcode2, uint32_t value) {
644	mLOG(DS, STUB, "CP15 access control write: CRm: %i, Op2: %i, Value: 0x%08X", crm, opcode2, value);
645}
646
647static void _writeRegionConfiguration(struct ARMCore* cpu, int crm, int opcode2, uint32_t value) {
648	cpu->cp15.r6.region[crm] = value;
649	uint32_t base = ARMProtectionGetBase(value) << 12;
650	uint32_t size = 2 << ARMProtectionGetSize(value);
651	mLOG(DS, STUB, "CP15 region configuration write: Region: %i, Insn: %i, Base: %08X, Size: %08X", crm, opcode2, base, size);
652}
653
654static void _writeCache(struct ARMCore* cpu, int crm, int opcode2, uint32_t value) {
655	switch (crm) {
656	case 0:
657		if (opcode2 == 4) {
658			ARMHalt(cpu);
659			return;
660		}
661		break;
662	}
663	mLOG(DS, STUB, "CP15 cache write: CRm: %i, Op2: %i, Value: 0x%08X", crm, opcode2, value);
664}
665
666static void _writeTCMControl(struct ARMCore* cpu, int crm, int opcode2, uint32_t value) {
667	uint32_t base = ARMTCMControlGetBase(value) << 12;
668	uint32_t size = 512 << ARMTCMControlGetVirtualSize(value);
669	struct DS* ds = (struct DS*) cpu->master;
670	mLOG(DS, DEBUG, "CP15 TCM control write: CRm: %i, Op2: %i, Base: %08X, Size: %08X", crm, opcode2, base, size);
671	switch (opcode2) {
672	case 0:
673		cpu->cp15.r9.d = value;
674		ds->memory.dtcmBase = base;
675		ds->memory.dtcmSize = size;
676		break;
677	case 1:
678		cpu->cp15.r9.i = value;
679		ds->memory.itcmSize = size;
680		break;
681	default:
682		mLOG(DS, GAME_ERROR, "CP15 TCM control bad op2: %i", opcode2);
683		break;
684	}
685}
686
687void DS9WriteCP15(struct ARMCore* cpu, int crn, int crm, int opcode1, int opcode2, uint32_t value) {
688	switch (crn) {
689	default:
690		mLOG(DS, STUB, "CP15 unknown write: CRn: %i, CRm: %i, Op1: %i, Op2: %i, Value: 0x%08X", crn, crm, opcode1, opcode2, value);
691		break;
692	case 0:
693		mLOG(DS, GAME_ERROR, "Attempted to write to read-only cp15 register");
694		ARMRaiseUndefined(cpu);
695		break;
696	case 1:
697		_writeSysControl(cpu, crm, opcode2, value);
698		break;
699	case 2:
700		_writeCacheControl(cpu, crm, opcode2, value);
701		break;
702	case 3:
703		_writeWriteBufferControl(cpu, crm, opcode2, value);
704		break;
705	case 5:
706		_writeAccessControl(cpu, crm, opcode2, value);
707		break;
708	case 6:
709		_writeRegionConfiguration(cpu, crm, opcode2, value);
710		break;
711	case 7:
712		_writeCache(cpu, crm, opcode2, value);
713		break;
714	case 9:
715		_writeTCMControl(cpu, crm, opcode2, value);
716		break;
717	}
718}
719
720static uint32_t _readTCMControl(struct ARMCore* cpu, int crm, int opcode2) {
721	switch (opcode2) {
722	case 0:
723		return cpu->cp15.r9.d;
724	case 1:
725		return cpu->cp15.r9.i;
726	default:
727		mLOG(DS, GAME_ERROR, "CP15 TCM control bad op2: %i", opcode2);
728		return 0;
729	}
730}
731
732uint32_t DS9ReadCP15(struct ARMCore* cpu, int crn, int crm, int opcode1, int opcode2) {
733	switch (crn) {
734	default:
735		mLOG(DS, STUB, "CP15 unknown read: CRn: %i, CRm: %i, Op1: %i, Op2: %i", crn, crm, opcode1, opcode2);
736		return 0;
737	case 9:
738		return _readTCMControl(cpu, crm, opcode2);
739	}
740}
741
742void DSWriteIE(struct ARMCore* cpu, uint16_t* io, uint32_t value) {
743	if (io[DS_REG_IME >> 1] && (value & io[DS_REG_IF_LO >> 1] || (value >> 16) & io[DS_REG_IF_HI >> 1])) {
744		ARMRaiseIRQ(cpu);
745	}
746}
747void DSWriteIME(struct ARMCore* cpu, uint16_t* io, uint16_t value) {
748	if (value && (io[DS_REG_IE_LO >> 1] & io[DS_REG_IF_LO >> 1] || io[DS_REG_IE_HI >> 1] & io[DS_REG_IF_HI >> 1])) {
749		ARMRaiseIRQ(cpu);
750	}
751}
752
753void DSRaiseIRQ(struct ARMCore* cpu, uint16_t* io, enum DSIRQ irq) {
754	if (irq < 16) {
755		io[DS_REG_IF_LO >> 1] |= 1 << irq;
756	} else {
757		io[DS_REG_IF_HI >> 1] |= 1 << (irq - 16);
758	}
759
760	if ((irq < 16 && (io[DS_REG_IE_LO >> 1] & 1 << irq)) || (io[DS_REG_IE_HI >> 1] & 1 << (irq - 16))) {
761		cpu->halted = 0;
762		if (io[DS_REG_IME >> 1]) {
763			ARMRaiseIRQ(cpu);
764		}
765	}
766}
767
768void DSFrameStarted(struct DS* ds) {
769	struct mCoreCallbacks* callbacks = ds->coreCallbacks;
770	if (callbacks && callbacks->videoFrameStarted) {
771		callbacks->videoFrameStarted(callbacks->context);
772	}
773}
774
775void DSFrameEnded(struct DS* ds) {
776	struct mCoreCallbacks* callbacks = ds->coreCallbacks;
777	if (callbacks && callbacks->videoFrameEnded) {
778		callbacks->videoFrameEnded(callbacks->context);
779	}
780}