src/gba/gba.c (view raw)
1/* Copyright (c) 2013-2015 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 "gba.h"
7
8#include "core/thread.h"
9
10#include "arm/decoder.h"
11#include "arm/isa-inlines.h"
12
13#include "gba/bios.h"
14#include "gba/cheats.h"
15#include "gba/context/overrides.h"
16#include "gba/io.h"
17#include "gba/rr/rr.h"
18#include "gba/serialize.h"
19#include "gba/sio.h"
20
21#include "util/crc32.h"
22#include "util/memory.h"
23#include "util/math.h"
24#include "util/patch.h"
25#include "util/vfs.h"
26
27mLOG_DEFINE_CATEGORY(GBA, "GBA");
28
29const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
30const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
31
32static const size_t GBA_ROM_MAGIC_OFFSET = 3;
33static const uint8_t GBA_ROM_MAGIC[] = { 0xEA };
34
35static const size_t GBA_MB_MAGIC_OFFSET = 0xC0;
36
37static void GBAInit(void* cpu, struct mCPUComponent* component);
38static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
39static void GBAProcessEvents(struct ARMCore* cpu);
40static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
41static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
42static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
43static void GBABreakpoint(struct ARMCore* cpu, int immediate);
44
45static bool _setSoftwareBreakpoint(struct Debugger*, uint32_t address, enum ExecutionMode mode, uint32_t* opcode);
46static bool _clearSoftwareBreakpoint(struct Debugger*, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
47
48
49#ifdef _3DS
50extern uint32_t* romBuffer;
51extern size_t romBufferSize;
52#endif
53
54void GBACreate(struct GBA* gba) {
55 gba->d.id = GBA_COMPONENT_MAGIC;
56 gba->d.init = GBAInit;
57 gba->d.deinit = 0;
58}
59
60static void GBAInit(void* cpu, struct mCPUComponent* component) {
61 struct GBA* gba = (struct GBA*) component;
62 gba->cpu = cpu;
63 gba->debugger = 0;
64 gba->sync = 0;
65
66 GBAInterruptHandlerInit(&gba->cpu->irqh);
67 GBAMemoryInit(gba);
68 GBASavedataInit(&gba->memory.savedata, 0);
69
70 gba->video.p = gba;
71 GBAVideoInit(&gba->video);
72
73 gba->audio.p = gba;
74 GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
75
76 GBAIOInit(gba);
77
78 gba->sio.p = gba;
79 GBASIOInit(&gba->sio);
80
81 gba->timersEnabled = 0;
82 memset(gba->timers, 0, sizeof(gba->timers));
83
84 gba->springIRQ = 0;
85 gba->keySource = 0;
86 gba->rotationSource = 0;
87 gba->luminanceSource = 0;
88 gba->rtcSource = 0;
89 gba->rumble = 0;
90 gba->rr = 0;
91
92 gba->romVf = 0;
93 gba->biosVf = 0;
94
95 gba->stream = 0;
96 gba->keyCallback = 0;
97 gba->stopCallback = 0;
98
99 gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
100
101 gba->idleOptimization = IDLE_LOOP_REMOVE;
102 gba->idleLoop = IDLE_LOOP_NONE;
103
104 gba->realisticTiming = true;
105 gba->hardCrash = true;
106 gba->allowOpposingDirections = true;
107
108 gba->performingDMA = false;
109
110 gba->pristineRom = 0;
111 gba->pristineRomSize = 0;
112 gba->yankedRomSize = 0;
113}
114
115void GBAUnloadROM(struct GBA* gba) {
116 if (gba->memory.rom && gba->pristineRom != gba->memory.rom) {
117 if (gba->yankedRomSize) {
118 gba->yankedRomSize = 0;
119 }
120 mappedMemoryFree(gba->memory.rom, SIZE_CART0);
121 }
122 gba->memory.rom = 0;
123
124 if (gba->romVf) {
125#ifndef _3DS
126 gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
127#endif
128 gba->pristineRom = 0;
129 gba->romVf = 0;
130 }
131
132 GBASavedataDeinit(&gba->memory.savedata);
133 gba->idleLoop = IDLE_LOOP_NONE;
134}
135
136void GBADestroy(struct GBA* gba) {
137 GBAUnloadROM(gba);
138
139 if (gba->biosVf) {
140 gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
141 }
142
143 GBAMemoryDeinit(gba);
144 GBAVideoDeinit(&gba->video);
145 GBAAudioDeinit(&gba->audio);
146 GBASIODeinit(&gba->sio);
147 gba->rr = 0;
148}
149
150void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
151 irqh->reset = GBAReset;
152 irqh->processEvents = GBAProcessEvents;
153 irqh->swi16 = GBASwi16;
154 irqh->swi32 = GBASwi32;
155 irqh->hitIllegal = GBAIllegal;
156 irqh->readCPSR = GBATestIRQ;
157 irqh->hitStub = GBAHitStub;
158 irqh->bkpt16 = GBABreakpoint;
159 irqh->bkpt32 = GBABreakpoint;
160}
161
162void GBAReset(struct ARMCore* cpu) {
163 ARMSetPrivilegeMode(cpu, MODE_IRQ);
164 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
165 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
166 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
167 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
168 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
169
170 struct GBA* gba = (struct GBA*) cpu->master;
171 if (!gba->rr || (!gba->rr->isPlaying(gba->rr) && !gba->rr->isRecording(gba->rr))) {
172 GBASavedataUnmask(&gba->memory.savedata);
173 }
174
175 if (gba->yankedRomSize) {
176 gba->memory.romSize = gba->yankedRomSize;
177 gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
178 gba->yankedRomSize = 0;
179 }
180 GBAMemoryReset(gba);
181 GBAVideoReset(&gba->video);
182 GBAAudioReset(&gba->audio);
183 GBAIOInit(gba);
184
185 GBASIOReset(&gba->sio);
186
187 gba->timersEnabled = 0;
188 memset(gba->timers, 0, sizeof(gba->timers));
189
190 gba->lastJump = 0;
191 gba->haltPending = false;
192 gba->idleDetectionStep = 0;
193 gba->idleDetectionFailures = 0;
194}
195
196void GBASkipBIOS(struct GBA* gba) {
197 struct ARMCore* cpu = gba->cpu;
198 if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
199 if (gba->memory.rom) {
200 cpu->gprs[ARM_PC] = BASE_CART0;
201 } else {
202 cpu->gprs[ARM_PC] = BASE_WORKING_RAM;
203 }
204 int currentCycles = 0;
205 ARM_WRITE_PC;
206 }
207}
208
209static void GBAProcessEvents(struct ARMCore* cpu) {
210 struct GBA* gba = (struct GBA*) cpu->master;
211
212 gba->bus = cpu->prefetch[1];
213 if (cpu->executionMode == MODE_THUMB) {
214 gba->bus |= cpu->prefetch[1] << 16;
215 }
216
217 if (gba->springIRQ) {
218 ARMRaiseIRQ(cpu);
219 gba->springIRQ = 0;
220 }
221
222 do {
223 int32_t cycles = cpu->nextEvent;
224 int32_t nextEvent = INT_MAX;
225 int32_t testEvent;
226#ifndef NDEBUG
227 if (cycles < 0) {
228 GBALog(gba, GBA_LOG_FATAL, "Negative cycles passed: %i", cycles);
229 }
230#endif
231
232 testEvent = GBAVideoProcessEvents(&gba->video, cycles);
233 if (testEvent < nextEvent) {
234 nextEvent = testEvent;
235 }
236
237 testEvent = GBAAudioProcessEvents(&gba->audio, cycles);
238 if (testEvent < nextEvent) {
239 nextEvent = testEvent;
240 }
241
242 testEvent = GBATimersProcessEvents(gba, cycles);
243 if (testEvent < nextEvent) {
244 nextEvent = testEvent;
245 }
246
247 testEvent = GBAMemoryRunDMAs(gba, cycles);
248 if (testEvent < nextEvent) {
249 nextEvent = testEvent;
250 }
251
252 testEvent = GBASIOProcessEvents(&gba->sio, cycles);
253 if (testEvent < nextEvent) {
254 nextEvent = testEvent;
255 }
256
257 cpu->cycles -= cycles;
258 cpu->nextEvent = nextEvent;
259
260 if (cpu->halted) {
261 cpu->cycles = cpu->nextEvent;
262 }
263 } while (cpu->cycles >= cpu->nextEvent);
264}
265
266static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
267 int32_t nextEvent = INT_MAX;
268 if (gba->timersEnabled) {
269 struct GBATimer* timer;
270 struct GBATimer* nextTimer;
271
272 timer = &gba->timers[0];
273 if (GBATimerFlagsIsEnable(timer->flags)) {
274 timer->nextEvent -= cycles;
275 timer->lastEvent -= cycles;
276 while (timer->nextEvent <= 0) {
277 timer->lastEvent = timer->nextEvent;
278 timer->nextEvent += timer->overflowInterval;
279 gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
280 timer->oldReload = timer->reload;
281
282 if (GBATimerFlagsIsDoIrq(timer->flags)) {
283 GBARaiseIRQ(gba, IRQ_TIMER0);
284 }
285
286 if (gba->audio.enable) {
287 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
288 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
289 }
290
291 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
292 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
293 }
294 }
295
296 nextTimer = &gba->timers[1];
297 if (GBATimerFlagsIsCountUp(nextTimer->flags)) {
298 ++gba->memory.io[REG_TM1CNT_LO >> 1];
299 if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
300 nextTimer->nextEvent = 0;
301 }
302 }
303 }
304 nextEvent = timer->nextEvent;
305 }
306
307 timer = &gba->timers[1];
308 if (GBATimerFlagsIsEnable(timer->flags)) {
309 timer->nextEvent -= cycles;
310 timer->lastEvent -= cycles;
311 if (timer->nextEvent <= 0) {
312 timer->lastEvent = timer->nextEvent;
313 timer->nextEvent += timer->overflowInterval;
314 gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
315 timer->oldReload = timer->reload;
316
317 if (GBATimerFlagsIsDoIrq(timer->flags)) {
318 GBARaiseIRQ(gba, IRQ_TIMER1);
319 }
320
321 if (gba->audio.enable) {
322 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
323 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
324 }
325
326 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
327 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
328 }
329 }
330
331 if (GBATimerFlagsIsCountUp(timer->flags)) {
332 timer->nextEvent = INT_MAX;
333 }
334
335 nextTimer = &gba->timers[2];
336 if (GBATimerFlagsIsCountUp(nextTimer->flags)) {
337 ++gba->memory.io[REG_TM2CNT_LO >> 1];
338 if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
339 nextTimer->nextEvent = 0;
340 }
341 }
342 }
343 if (timer->nextEvent < nextEvent) {
344 nextEvent = timer->nextEvent;
345 }
346 }
347
348 timer = &gba->timers[2];
349 if (GBATimerFlagsIsEnable(timer->flags)) {
350 timer->nextEvent -= cycles;
351 timer->lastEvent -= cycles;
352 if (timer->nextEvent <= 0) {
353 timer->lastEvent = timer->nextEvent;
354 timer->nextEvent += timer->overflowInterval;
355 gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
356 timer->oldReload = timer->reload;
357
358 if (GBATimerFlagsIsDoIrq(timer->flags)) {
359 GBARaiseIRQ(gba, IRQ_TIMER2);
360 }
361
362 if (GBATimerFlagsIsCountUp(timer->flags)) {
363 timer->nextEvent = INT_MAX;
364 }
365
366 nextTimer = &gba->timers[3];
367 if (GBATimerFlagsIsCountUp(nextTimer->flags)) {
368 ++gba->memory.io[REG_TM3CNT_LO >> 1];
369 if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
370 nextTimer->nextEvent = 0;
371 }
372 }
373 }
374 if (timer->nextEvent < nextEvent) {
375 nextEvent = timer->nextEvent;
376 }
377 }
378
379 timer = &gba->timers[3];
380 if (GBATimerFlagsIsEnable(timer->flags)) {
381 timer->nextEvent -= cycles;
382 timer->lastEvent -= cycles;
383 if (timer->nextEvent <= 0) {
384 timer->lastEvent = timer->nextEvent;
385 timer->nextEvent += timer->overflowInterval;
386 gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
387 timer->oldReload = timer->reload;
388
389 if (GBATimerFlagsIsDoIrq(timer->flags)) {
390 GBARaiseIRQ(gba, IRQ_TIMER3);
391 }
392
393 if (GBATimerFlagsIsCountUp(timer->flags)) {
394 timer->nextEvent = INT_MAX;
395 }
396 }
397 if (timer->nextEvent < nextEvent) {
398 nextEvent = timer->nextEvent;
399 }
400 }
401 }
402 return nextEvent;
403}
404
405void GBAAttachDebugger(struct GBA* gba, struct Debugger* debugger) {
406 debugger->setSoftwareBreakpoint = _setSoftwareBreakpoint;
407 debugger->clearSoftwareBreakpoint = _clearSoftwareBreakpoint;
408 gba->debugger = debugger;
409 gba->cpu->components[GBA_COMPONENT_DEBUGGER] = &debugger->d;
410 ARMHotplugAttach(gba->cpu, GBA_COMPONENT_DEBUGGER);
411}
412
413void GBADetachDebugger(struct GBA* gba) {
414 gba->debugger = 0;
415 ARMHotplugDetach(gba->cpu, GBA_COMPONENT_DEBUGGER);
416 gba->cpu->components[GBA_COMPONENT_DEBUGGER] = 0;
417}
418
419bool GBALoadMB(struct GBA* gba, struct VFile* vf, const char* fname) {
420 GBAUnloadROM(gba);
421 gba->romVf = vf;
422 gba->pristineRomSize = vf->size(vf);
423 vf->seek(vf, 0, SEEK_SET);
424 if (gba->pristineRomSize > SIZE_WORKING_RAM) {
425 gba->pristineRomSize = SIZE_WORKING_RAM;
426 }
427#ifdef _3DS
428 gba->pristineRom = 0;
429 if (gba->pristineRomSize <= romBufferSize) {
430 gba->pristineRom = romBuffer;
431 vf->read(vf, romBuffer, gba->pristineRomSize);
432 }
433#else
434 gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
435#endif
436 if (!gba->pristineRom) {
437 GBALog(gba, GBA_LOG_WARN, "Couldn't map ROM");
438 return false;
439 }
440 gba->yankedRomSize = 0;
441 gba->activeFile = fname;
442 gba->memory.romSize = 0;
443 gba->memory.romMask = 0;
444 gba->romCrc32 = doCrc32(gba->pristineRom, gba->pristineRomSize);
445 return true;
446}
447
448bool GBALoadROM2(struct GBA* gba, struct VFile* vf) {
449 GBAUnloadROM(gba);
450 gba->romVf = vf;
451 gba->pristineRomSize = vf->size(vf);
452 vf->seek(vf, 0, SEEK_SET);
453 if (gba->pristineRomSize > SIZE_CART0) {
454 gba->pristineRomSize = SIZE_CART0;
455 }
456#ifdef _3DS
457 gba->pristineRom = 0;
458 if (gba->pristineRomSize <= romBufferSize) {
459 gba->pristineRom = romBuffer;
460 vf->read(vf, romBuffer, gba->pristineRomSize);
461 }
462#else
463 gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
464#endif
465 if (!gba->pristineRom) {
466 GBALog(gba, GBA_LOG_WARN, "Couldn't map ROM");
467 return false;
468 }
469 gba->yankedRomSize = 0;
470 gba->memory.rom = gba->pristineRom;
471 gba->memory.romSize = gba->pristineRomSize;
472 gba->memory.romMask = toPow2(gba->memory.romSize) - 1;
473 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
474 GBAHardwareInit(&gba->memory.hw, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
475 // TODO: error check
476 return true;
477}
478
479bool GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
480 if (!GBALoadROM2(gba, vf)) {
481 return false;
482 }
483 gba->activeFile = fname;
484 GBALoadSave(gba, sav);
485 return true;
486}
487
488bool GBALoadSave(struct GBA* gba, struct VFile* sav) {
489 GBASavedataInit(&gba->memory.savedata, sav);
490 return true;
491}
492
493void GBAYankROM(struct GBA* gba) {
494 gba->yankedRomSize = gba->memory.romSize;
495 gba->memory.romSize = 0;
496 gba->memory.romMask = 0;
497 GBARaiseIRQ(gba, IRQ_GAMEPAK);
498}
499
500void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
501 gba->biosVf = vf;
502 uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
503 if (!bios) {
504 GBALog(gba, GBA_LOG_WARN, "Couldn't map BIOS");
505 return;
506 }
507 gba->memory.bios = bios;
508 gba->memory.fullBios = 1;
509 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
510 GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
511 if (checksum == GBA_BIOS_CHECKSUM) {
512 GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
513 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
514 GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
515 } else {
516 GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
517 }
518 gba->biosChecksum = checksum;
519 if (gba->memory.activeRegion == REGION_BIOS) {
520 gba->cpu->memory.activeRegion = gba->memory.bios;
521 }
522 // TODO: error check
523}
524
525void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
526 size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
527 if (!patchedSize || patchedSize > SIZE_CART0) {
528 return;
529 }
530 gba->memory.rom = anonymousMemoryMap(SIZE_CART0);
531 if (!patch->applyPatch(patch, gba->pristineRom, gba->pristineRomSize, gba->memory.rom, patchedSize)) {
532 mappedMemoryFree(gba->memory.rom, patchedSize);
533 gba->memory.rom = gba->pristineRom;
534 return;
535 }
536 gba->memory.romSize = patchedSize;
537 gba->memory.romMask = SIZE_CART0 - 1;
538 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
539}
540
541void GBATimerUpdateRegister(struct GBA* gba, int timer) {
542 struct GBATimer* currentTimer = &gba->timers[timer];
543 if (GBATimerFlagsIsEnable(currentTimer->flags) && !GBATimerFlagsIsCountUp(currentTimer->flags)) {
544 int32_t prefetchSkew = 0;
545 if (gba->memory.lastPrefetchedPc - gba->memory.lastPrefetchedLoads * WORD_SIZE_THUMB >= (uint32_t) gba->cpu->gprs[ARM_PC]) {
546 prefetchSkew = (gba->memory.lastPrefetchedPc - gba->cpu->gprs[ARM_PC]) * (gba->cpu->memory.activeSeqCycles16 + 1) / WORD_SIZE_THUMB;
547 }
548 // Reading this takes two cycles (1N+1I), so let's remove them preemptively
549 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent - 2 + prefetchSkew) >> GBATimerFlagsGetPrescaleBits(currentTimer->flags));
550 }
551}
552
553void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
554 gba->timers[timer].reload = reload;
555 gba->timers[timer].overflowInterval = (0x10000 - gba->timers[timer].reload) << GBATimerFlagsGetPrescaleBits(gba->timers[timer].flags);
556}
557
558void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
559 struct GBATimer* currentTimer = &gba->timers[timer];
560 GBATimerUpdateRegister(gba, timer);
561
562 unsigned oldPrescale = GBATimerFlagsGetPrescaleBits(currentTimer->flags);
563 switch (control & 0x0003) {
564 case 0x0000:
565 currentTimer->flags = GBATimerFlagsSetPrescaleBits(currentTimer->flags, 0);
566 break;
567 case 0x0001:
568 currentTimer->flags = GBATimerFlagsSetPrescaleBits(currentTimer->flags, 6);
569 break;
570 case 0x0002:
571 currentTimer->flags = GBATimerFlagsSetPrescaleBits(currentTimer->flags, 8);
572 break;
573 case 0x0003:
574 currentTimer->flags = GBATimerFlagsSetPrescaleBits(currentTimer->flags, 10);
575 break;
576 }
577 currentTimer->flags = GBATimerFlagsTestFillCountUp(currentTimer->flags, control & 0x0004);
578 currentTimer->flags = GBATimerFlagsTestFillDoIrq(currentTimer->flags, control & 0x0040);
579 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << GBATimerFlagsGetPrescaleBits(currentTimer->flags);
580 bool wasEnabled = GBATimerFlagsIsEnable(currentTimer->flags);
581 currentTimer->flags = GBATimerFlagsTestFillEnable(currentTimer->flags, control & 0x0080);
582 if (!wasEnabled && GBATimerFlagsIsEnable(currentTimer->flags)) {
583 if (!GBATimerFlagsIsCountUp(currentTimer->flags)) {
584 currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
585 } else {
586 currentTimer->nextEvent = INT_MAX;
587 }
588 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
589 currentTimer->oldReload = currentTimer->reload;
590 currentTimer->lastEvent = gba->cpu->cycles;
591 gba->timersEnabled |= 1 << timer;
592 } else if (wasEnabled && !GBATimerFlagsIsEnable(currentTimer->flags)) {
593 if (!GBATimerFlagsIsCountUp(currentTimer->flags)) {
594 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
595 }
596 gba->timersEnabled &= ~(1 << timer);
597 } else if (GBATimerFlagsGetPrescaleBits(currentTimer->flags) != oldPrescale && !GBATimerFlagsIsCountUp(currentTimer->flags)) {
598 // FIXME: this might be before present
599 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
600 }
601
602 if (currentTimer->nextEvent < gba->cpu->nextEvent) {
603 gba->cpu->nextEvent = currentTimer->nextEvent;
604 }
605};
606
607void GBAWriteIE(struct GBA* gba, uint16_t value) {
608 if (value & (1 << IRQ_KEYPAD)) {
609 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
610 }
611
612 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
613 ARMRaiseIRQ(gba->cpu);
614 }
615}
616
617void GBAWriteIME(struct GBA* gba, uint16_t value) {
618 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
619 ARMRaiseIRQ(gba->cpu);
620 }
621}
622
623void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
624 gba->memory.io[REG_IF >> 1] |= 1 << irq;
625 gba->cpu->halted = 0;
626
627 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
628 ARMRaiseIRQ(gba->cpu);
629 }
630}
631
632void GBATestIRQ(struct ARMCore* cpu) {
633 struct GBA* gba = (struct GBA*) cpu->master;
634 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
635 gba->springIRQ = 1;
636 gba->cpu->nextEvent = gba->cpu->cycles;
637 }
638}
639
640void GBAHalt(struct GBA* gba) {
641 gba->cpu->nextEvent = gba->cpu->cycles;
642 gba->cpu->halted = 1;
643}
644
645void GBAStop(struct GBA* gba) {
646 if (!gba->stopCallback) {
647 return;
648 }
649 gba->cpu->nextEvent = gba->cpu->cycles;
650 gba->stopCallback->stop(gba->stopCallback);
651}
652
653void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
654 // TODO: Kill GBALog
655}
656
657void GBADebuggerLogShim(struct Debugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
658 // TODO: Kill GBADebuggerLogShim
659}
660
661bool GBAIsROM(struct VFile* vf) {
662 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
663 return false;
664 }
665 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
666 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
667 return false;
668 }
669 if (GBAIsBIOS(vf)) {
670 return false;
671 }
672 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
673}
674
675bool GBAIsMB(struct VFile* vf) {
676 if (!GBAIsROM(vf)) {
677 return false;
678 }
679 if (vf->size(vf) > SIZE_WORKING_RAM) {
680 return false;
681 }
682 if (vf->seek(vf, GBA_MB_MAGIC_OFFSET, SEEK_SET) < 0) {
683 return false;
684 }
685 uint32_t signature;
686 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
687 return false;
688 }
689 uint32_t opcode;
690 LOAD_32(opcode, 0, &signature);
691 struct ARMInstructionInfo info;
692 ARMDecodeARM(opcode, &info);
693 if (info.branchType != ARM_BRANCH) {
694 return false;
695 }
696 if (info.op1.immediate <= 0) {
697 return false;
698 } else if (info.op1.immediate == 28) {
699 // Ancient toolchain that is known to throw MB detection for a loop
700 return false;
701 } else if (info.op1.immediate != 24) {
702 return true;
703 }
704 // Found a libgba-linked cart...these are a bit harder to detect.
705 return false;
706}
707
708bool GBAIsBIOS(struct VFile* vf) {
709 if (vf->seek(vf, 0, SEEK_SET) < 0) {
710 return false;
711 }
712 uint8_t interruptTable[7 * 4];
713 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
714 return false;
715 }
716 int i;
717 for (i = 0; i < 7; ++i) {
718 if (interruptTable[4 * i + 3] != 0xEA || interruptTable[4 * i + 2]) {
719 return false;
720 }
721 }
722 return true;
723}
724
725void GBAGetGameCode(struct GBA* gba, char* out) {
726 if (!gba->memory.rom) {
727 out[0] = '\0';
728 return;
729 }
730 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->id, 4);
731}
732
733void GBAGetGameTitle(struct GBA* gba, char* out) {
734 if (gba->memory.rom) {
735 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
736 return;
737 }
738 if (gba->pristineRom) {
739 memcpy(out, &((struct GBACartridge*) gba->pristineRom)->title, 12);
740 return;
741 }
742 strncpy(out, "(BIOS)", 12);
743}
744
745void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
746 struct GBA* gba = (struct GBA*) cpu->master;
747 enum GBALogLevel level = GBA_LOG_ERROR;
748 if (gba->debugger) {
749 level = GBA_LOG_STUB;
750 struct DebuggerEntryInfo info = {
751 .address = _ARMPCAddress(cpu),
752 .opcode = opcode
753 };
754 DebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
755 }
756 GBALog(gba, level, "Stub opcode: %08x", opcode);
757}
758
759void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
760 struct GBA* gba = (struct GBA*) cpu->master;
761 if (!gba->yankedRomSize) {
762 GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
763 }
764 if (gba->debugger) {
765 struct DebuggerEntryInfo info = {
766 .address = _ARMPCAddress(cpu),
767 .opcode = opcode
768 };
769 DebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
770 } else {
771 ARMRaiseUndefined(cpu);
772 }
773}
774
775void GBABreakpoint(struct ARMCore* cpu, int immediate) {
776 struct GBA* gba = (struct GBA*) cpu->master;
777 if (immediate >= GBA_COMPONENT_MAX) {
778 return;
779 }
780 switch (immediate) {
781 case GBA_COMPONENT_DEBUGGER:
782 if (gba->debugger) {
783 struct DebuggerEntryInfo info = {
784 .address = _ARMPCAddress(cpu)
785 };
786 DebuggerEnter(gba->debugger, DEBUGGER_ENTER_BREAKPOINT, &info);
787 }
788 break;
789 case GBA_COMPONENT_CHEAT_DEVICE:
790 if (gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE]) {
791 struct GBACheatDevice* device = (struct GBACheatDevice*) gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE];
792 struct GBACheatHook* hook = 0;
793 size_t i;
794 for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
795 struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
796 if (cheats->hook && cheats->hook->address == _ARMPCAddress(cpu)) {
797 GBACheatRefresh(device, cheats);
798 hook = cheats->hook;
799 }
800 }
801 if (hook) {
802 ARMRunFake(cpu, hook->patchedOpcode);
803 }
804 }
805 break;
806 default:
807 break;
808 }
809}
810
811void GBAFrameStarted(struct GBA* gba) {
812 UNUSED(gba);
813
814 // TODO: Put back rewind
815}
816
817void GBAFrameEnded(struct GBA* gba) {
818 GBASavedataClean(&gba->memory.savedata, gba->video.frameCounter);
819
820 if (gba->rr) {
821 gba->rr->nextFrame(gba->rr);
822 }
823
824 if (gba->cpu->components && gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE]) {
825 struct GBACheatDevice* device = (struct GBACheatDevice*) gba->cpu->components[GBA_COMPONENT_CHEAT_DEVICE];
826 size_t i;
827 for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) {
828 struct GBACheatSet* cheats = *GBACheatSetsGetPointer(&device->cheats, i);
829 if (!cheats->hook) {
830 GBACheatRefresh(device, cheats);
831 }
832 }
833 }
834
835 if (gba->stream && gba->stream->postVideoFrame) {
836 const color_t* pixels;
837 unsigned stride;
838 gba->video.renderer->getPixels(gba->video.renderer, &stride, (const void**) &pixels);
839 gba->stream->postVideoFrame(gba->stream, pixels, stride);
840 }
841
842 if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {
843 GBAHardwarePlayerUpdate(gba);
844 }
845
846 struct mCoreThread* thread = mCoreThreadGet();
847 if (!thread) {
848 return;
849 }
850
851 if (thread->frameCallback) {
852 thread->frameCallback(thread);
853 }
854
855 // TODO: Put back RR
856}
857
858void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
859 size_t immediate;
860 for (immediate = 0; immediate < gba->cpu->numComponents; ++immediate) {
861 if (gba->cpu->components[immediate] == component) {
862 break;
863 }
864 }
865 if (immediate == gba->cpu->numComponents) {
866 return;
867 }
868 if (mode == MODE_ARM) {
869 int32_t value;
870 int32_t old;
871 value = 0xE1200070;
872 value |= immediate & 0xF;
873 value |= (immediate & 0xFFF0) << 4;
874 GBAPatch32(gba->cpu, address, value, &old);
875 *opcode = old;
876 } else {
877 int16_t value;
878 int16_t old;
879 value = 0xBE00;
880 value |= immediate & 0xFF;
881 GBAPatch16(gba->cpu, address, value, &old);
882 *opcode = (uint16_t) old;
883 }
884}
885
886void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
887 if (mode == MODE_ARM) {
888 GBAPatch32(gba->cpu, address, opcode, 0);
889 } else {
890 GBAPatch16(gba->cpu, address, opcode, 0);
891 }
892}
893
894static bool _setSoftwareBreakpoint(struct Debugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t* opcode) {
895 GBASetBreakpoint((struct GBA*) debugger->cpu->master, &debugger->d, address, mode, opcode);
896 return true;
897}
898
899static bool _clearSoftwareBreakpoint(struct Debugger* debugger, uint32_t address, enum ExecutionMode mode, uint32_t opcode) {
900 GBAClearBreakpoint((struct GBA*) debugger->cpu->master, address, mode, opcode);
901 return true;
902}