src/gba/gba.c (view raw)
1/* Copyright (c) 2013-2014 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 "gba-bios.h"
9#include "gba-io.h"
10#include "gba-rr.h"
11#include "gba-serialize.h"
12#include "gba-sio.h"
13#include "gba-thread.h"
14
15#include "isa-inlines.h"
16
17#include "util/crc32.h"
18#include "util/memory.h"
19#include "util/patch.h"
20#include "util/vfs.h"
21
22const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000;
23const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
24
25static const size_t GBA_ROM_MAGIC_OFFSET = 3;
26static const uint8_t GBA_ROM_MAGIC[] = { 0xEA };
27
28static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component);
29static void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh);
30static void GBAProcessEvents(struct ARMCore* cpu);
31static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles);
32static void GBAHitStub(struct ARMCore* cpu, uint32_t opcode);
33static void GBAIllegal(struct ARMCore* cpu, uint32_t opcode);
34
35void GBACreate(struct GBA* gba) {
36 gba->d.id = GBA_COMPONENT_MAGIC;
37 gba->d.init = GBAInit;
38 gba->d.deinit = 0;
39}
40
41static void GBAInit(struct ARMCore* cpu, struct ARMComponent* component) {
42 struct GBA* gba = (struct GBA*) component;
43 gba->cpu = cpu;
44 gba->debugger = 0;
45
46 GBAInterruptHandlerInit(&cpu->irqh);
47 GBAMemoryInit(gba);
48 GBASavedataInit(&gba->memory.savedata, 0);
49
50 gba->video.p = gba;
51 GBAVideoInit(&gba->video);
52
53 gba->audio.p = gba;
54 GBAAudioInit(&gba->audio, GBA_AUDIO_SAMPLES);
55
56 GBAIOInit(gba);
57
58 gba->sio.p = gba;
59 GBASIOInit(&gba->sio);
60
61 gba->timersEnabled = 0;
62 memset(gba->timers, 0, sizeof(gba->timers));
63
64 gba->springIRQ = 0;
65 gba->keySource = 0;
66 gba->rotationSource = 0;
67 gba->luminanceSource = 0;
68 gba->rtcSource = 0;
69 gba->rumble = 0;
70 gba->rr = 0;
71
72 gba->romVf = 0;
73 gba->biosVf = 0;
74
75 gba->logLevel = GBA_LOG_INFO | GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL;
76
77 gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
78
79 gba->busyLoop = -1;
80 gba->performingDMA = false;
81}
82
83void GBADestroy(struct GBA* gba) {
84 if (gba->pristineRom == gba->memory.rom) {
85 gba->memory.rom = 0;
86 }
87
88 if (gba->romVf) {
89 gba->romVf->unmap(gba->romVf, gba->pristineRom, gba->pristineRomSize);
90 }
91
92 if (gba->biosVf) {
93 gba->biosVf->unmap(gba->biosVf, gba->memory.bios, SIZE_BIOS);
94 }
95
96 GBAMemoryDeinit(gba);
97 GBAVideoDeinit(&gba->video);
98 GBAAudioDeinit(&gba->audio);
99 GBARRContextDestroy(gba);
100}
101
102void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {
103 irqh->reset = GBAReset;
104 irqh->processEvents = GBAProcessEvents;
105 irqh->swi16 = GBASwi16;
106 irqh->swi32 = GBASwi32;
107 irqh->hitIllegal = GBAIllegal;
108 irqh->readCPSR = GBATestIRQ;
109 irqh->hitStub = GBAHitStub;
110}
111
112void GBAReset(struct ARMCore* cpu) {
113 ARMSetPrivilegeMode(cpu, MODE_IRQ);
114 cpu->gprs[ARM_SP] = SP_BASE_IRQ;
115 ARMSetPrivilegeMode(cpu, MODE_SUPERVISOR);
116 cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR;
117 ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
118 cpu->gprs[ARM_SP] = SP_BASE_SYSTEM;
119
120 struct GBA* gba = (struct GBA*) cpu->master;
121 if (!GBARRIsPlaying(gba->rr) && !GBARRIsRecording(gba->rr)) {
122 GBASavedataUnmask(&gba->memory.savedata);
123 }
124 GBAMemoryReset(gba);
125 GBAVideoReset(&gba->video);
126 GBAAudioReset(&gba->audio);
127 GBAIOInit(gba);
128
129 GBASIODeinit(&gba->sio);
130 GBASIOInit(&gba->sio);
131
132 gba->timersEnabled = 0;
133 memset(gba->timers, 0, sizeof(gba->timers));
134}
135
136void GBASkipBIOS(struct ARMCore* cpu) {
137 if (cpu->gprs[ARM_PC] == BASE_RESET + WORD_SIZE_ARM) {
138 cpu->gprs[ARM_PC] = BASE_CART0;
139 int currentCycles = 0;
140 ARM_WRITE_PC;
141 }
142}
143
144static void GBAProcessEvents(struct ARMCore* cpu) {
145 do {
146 struct GBA* gba = (struct GBA*) cpu->master;
147 int32_t cycles = cpu->nextEvent;
148 int32_t nextEvent = INT_MAX;
149 int32_t testEvent;
150
151 gba->bus = cpu->prefetch[1];
152 if (cpu->executionMode == MODE_THUMB) {
153 gba->bus |= cpu->prefetch[1] << 16;
154 }
155
156 if (gba->springIRQ) {
157 ARMRaiseIRQ(cpu);
158 gba->springIRQ = 0;
159 }
160
161 testEvent = GBAVideoProcessEvents(&gba->video, cycles);
162 if (testEvent < nextEvent) {
163 nextEvent = testEvent;
164 }
165
166 testEvent = GBAAudioProcessEvents(&gba->audio, cycles);
167 if (testEvent < nextEvent) {
168 nextEvent = testEvent;
169 }
170
171 testEvent = GBATimersProcessEvents(gba, cycles);
172 if (testEvent < nextEvent) {
173 nextEvent = testEvent;
174 }
175
176 testEvent = GBAMemoryRunDMAs(gba, cycles);
177 if (testEvent < nextEvent) {
178 nextEvent = testEvent;
179 }
180
181 testEvent = GBASIOProcessEvents(&gba->sio, cycles);
182 if (testEvent < nextEvent) {
183 nextEvent = testEvent;
184 }
185
186 cpu->cycles -= cycles;
187 cpu->nextEvent = nextEvent;
188
189 if (cpu->halted) {
190 cpu->cycles = cpu->nextEvent;
191 }
192 } while (cpu->cycles >= cpu->nextEvent);
193}
194
195static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles) {
196 int32_t nextEvent = INT_MAX;
197 if (gba->timersEnabled) {
198 struct GBATimer* timer;
199 struct GBATimer* nextTimer;
200
201 timer = &gba->timers[0];
202 if (timer->enable) {
203 timer->nextEvent -= cycles;
204 timer->lastEvent -= cycles;
205 if (timer->nextEvent <= 0) {
206 timer->lastEvent = timer->nextEvent;
207 timer->nextEvent += timer->overflowInterval;
208 gba->memory.io[REG_TM0CNT_LO >> 1] = timer->reload;
209 timer->oldReload = timer->reload;
210
211 if (timer->doIrq) {
212 GBARaiseIRQ(gba, IRQ_TIMER0);
213 }
214
215 if (gba->audio.enable) {
216 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 0) {
217 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
218 }
219
220 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 0) {
221 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
222 }
223 }
224
225 nextTimer = &gba->timers[1];
226 if (nextTimer->countUp) {
227 ++gba->memory.io[REG_TM1CNT_LO >> 1];
228 if (!gba->memory.io[REG_TM1CNT_LO >> 1]) {
229 nextTimer->nextEvent = 0;
230 }
231 }
232 }
233 nextEvent = timer->nextEvent;
234 }
235
236 timer = &gba->timers[1];
237 if (timer->enable) {
238 timer->nextEvent -= cycles;
239 timer->lastEvent -= cycles;
240 if (timer->nextEvent <= 0) {
241 timer->lastEvent = timer->nextEvent;
242 timer->nextEvent += timer->overflowInterval;
243 gba->memory.io[REG_TM1CNT_LO >> 1] = timer->reload;
244 timer->oldReload = timer->reload;
245
246 if (timer->doIrq) {
247 GBARaiseIRQ(gba, IRQ_TIMER1);
248 }
249
250 if (gba->audio.enable) {
251 if ((gba->audio.chALeft || gba->audio.chARight) && gba->audio.chATimer == 1) {
252 GBAAudioSampleFIFO(&gba->audio, 0, timer->lastEvent);
253 }
254
255 if ((gba->audio.chBLeft || gba->audio.chBRight) && gba->audio.chBTimer == 1) {
256 GBAAudioSampleFIFO(&gba->audio, 1, timer->lastEvent);
257 }
258 }
259
260 if (timer->countUp) {
261 timer->nextEvent = INT_MAX;
262 }
263
264 nextTimer = &gba->timers[2];
265 if (nextTimer->countUp) {
266 ++gba->memory.io[REG_TM2CNT_LO >> 1];
267 if (!gba->memory.io[REG_TM2CNT_LO >> 1]) {
268 nextTimer->nextEvent = 0;
269 }
270 }
271 }
272 if (timer->nextEvent < nextEvent) {
273 nextEvent = timer->nextEvent;
274 }
275 }
276
277 timer = &gba->timers[2];
278 if (timer->enable) {
279 timer->nextEvent -= cycles;
280 timer->lastEvent -= cycles;
281 if (timer->nextEvent <= 0) {
282 timer->lastEvent = timer->nextEvent;
283 timer->nextEvent += timer->overflowInterval;
284 gba->memory.io[REG_TM2CNT_LO >> 1] = timer->reload;
285 timer->oldReload = timer->reload;
286
287 if (timer->doIrq) {
288 GBARaiseIRQ(gba, IRQ_TIMER2);
289 }
290
291 if (timer->countUp) {
292 timer->nextEvent = INT_MAX;
293 }
294
295 nextTimer = &gba->timers[3];
296 if (nextTimer->countUp) {
297 ++gba->memory.io[REG_TM3CNT_LO >> 1];
298 if (!gba->memory.io[REG_TM3CNT_LO >> 1]) {
299 nextTimer->nextEvent = 0;
300 }
301 }
302 }
303 if (timer->nextEvent < nextEvent) {
304 nextEvent = timer->nextEvent;
305 }
306 }
307
308 timer = &gba->timers[3];
309 if (timer->enable) {
310 timer->nextEvent -= cycles;
311 timer->lastEvent -= cycles;
312 if (timer->nextEvent <= 0) {
313 timer->lastEvent = timer->nextEvent;
314 timer->nextEvent += timer->overflowInterval;
315 gba->memory.io[REG_TM3CNT_LO >> 1] = timer->reload;
316 timer->oldReload = timer->reload;
317
318 if (timer->doIrq) {
319 GBARaiseIRQ(gba, IRQ_TIMER3);
320 }
321
322 if (timer->countUp) {
323 timer->nextEvent = INT_MAX;
324 }
325 }
326 if (timer->nextEvent < nextEvent) {
327 nextEvent = timer->nextEvent;
328 }
329 }
330 }
331 return nextEvent;
332}
333
334void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) {
335 gba->debugger = debugger;
336 gba->cpu->components[GBA_COMPONENT_DEBUGGER] = &debugger->d;
337 ARMHotplugAttach(gba->cpu, GBA_COMPONENT_DEBUGGER);
338}
339
340void GBADetachDebugger(struct GBA* gba) {
341 gba->debugger = 0;
342 ARMHotplugDetach(gba->cpu, GBA_COMPONENT_DEBUGGER);
343 gba->cpu->components[GBA_COMPONENT_DEBUGGER] = 0;
344}
345
346void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
347 gba->romVf = vf;
348 gba->pristineRomSize = vf->size(vf);
349 vf->seek(vf, 0, SEEK_SET);
350 if (gba->pristineRomSize > SIZE_CART0) {
351 gba->pristineRomSize = SIZE_CART0;
352 }
353 gba->pristineRom = vf->map(vf, gba->pristineRomSize, MAP_READ);
354 if (!gba->pristineRom) {
355 GBALog(gba, GBA_LOG_WARN, "Couldn't map ROM");
356 return;
357 }
358 gba->memory.rom = gba->pristineRom;
359 gba->activeFile = fname;
360 gba->memory.romSize = gba->pristineRomSize;
361 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
362 GBASavedataInit(&gba->memory.savedata, sav);
363 GBAGPIOInit(&gba->memory.gpio, &((uint16_t*) gba->memory.rom)[GPIO_REG_DATA >> 1]);
364 // TODO: error check
365}
366
367void GBALoadBIOS(struct GBA* gba, struct VFile* vf) {
368 gba->biosVf = vf;
369 uint32_t* bios = vf->map(vf, SIZE_BIOS, MAP_READ);
370 if (!bios) {
371 GBALog(gba, GBA_LOG_WARN, "Couldn't map BIOS");
372 return;
373 }
374 gba->memory.bios = bios;
375 gba->memory.fullBios = 1;
376 uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);
377 GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
378 if (checksum == GBA_BIOS_CHECKSUM) {
379 GBALog(gba, GBA_LOG_INFO, "Official GBA BIOS detected");
380 } else if (checksum == GBA_DS_BIOS_CHECKSUM) {
381 GBALog(gba, GBA_LOG_INFO, "Official GBA (DS) BIOS detected");
382 } else {
383 GBALog(gba, GBA_LOG_WARN, "BIOS checksum incorrect");
384 }
385 gba->biosChecksum = checksum;
386 if (gba->memory.activeRegion == REGION_BIOS) {
387 gba->cpu->memory.activeRegion = gba->memory.bios;
388 }
389 // TODO: error check
390}
391
392void GBAApplyPatch(struct GBA* gba, struct Patch* patch) {
393 size_t patchedSize = patch->outputSize(patch, gba->memory.romSize);
394 if (!patchedSize) {
395 return;
396 }
397 gba->memory.rom = anonymousMemoryMap(patchedSize);
398 if (!patch->applyPatch(patch, gba->pristineRom, gba->pristineRomSize, gba->memory.rom, patchedSize)) {
399 mappedMemoryFree(gba->memory.rom, patchedSize);
400 gba->memory.rom = gba->pristineRom;
401 return;
402 }
403 gba->memory.romSize = patchedSize;
404 gba->romCrc32 = doCrc32(gba->memory.rom, gba->memory.romSize);
405}
406
407void GBATimerUpdateRegister(struct GBA* gba, int timer) {
408 struct GBATimer* currentTimer = &gba->timers[timer];
409 if (currentTimer->enable && !currentTimer->countUp) {
410 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> currentTimer->prescaleBits);
411 }
412}
413
414void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t reload) {
415 gba->timers[timer].reload = reload;
416}
417
418void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t control) {
419 struct GBATimer* currentTimer = &gba->timers[timer];
420 GBATimerUpdateRegister(gba, timer);
421
422 int oldPrescale = currentTimer->prescaleBits;
423 switch (control & 0x0003) {
424 case 0x0000:
425 currentTimer->prescaleBits = 0;
426 break;
427 case 0x0001:
428 currentTimer->prescaleBits = 6;
429 break;
430 case 0x0002:
431 currentTimer->prescaleBits = 8;
432 break;
433 case 0x0003:
434 currentTimer->prescaleBits = 10;
435 break;
436 }
437 currentTimer->countUp = !!(control & 0x0004);
438 currentTimer->doIrq = !!(control & 0x0040);
439 currentTimer->overflowInterval = (0x10000 - currentTimer->reload) << currentTimer->prescaleBits;
440 int wasEnabled = currentTimer->enable;
441 currentTimer->enable = !!(control & 0x0080);
442 if (!wasEnabled && currentTimer->enable) {
443 if (!currentTimer->countUp) {
444 currentTimer->nextEvent = gba->cpu->cycles + currentTimer->overflowInterval;
445 } else {
446 currentTimer->nextEvent = INT_MAX;
447 }
448 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->reload;
449 currentTimer->oldReload = currentTimer->reload;
450 currentTimer->lastEvent = 0;
451 gba->timersEnabled |= 1 << timer;
452 } else if (wasEnabled && !currentTimer->enable) {
453 if (!currentTimer->countUp) {
454 gba->memory.io[(REG_TM0CNT_LO + (timer << 2)) >> 1] = currentTimer->oldReload + ((gba->cpu->cycles - currentTimer->lastEvent) >> oldPrescale);
455 }
456 gba->timersEnabled &= ~(1 << timer);
457 } else if (currentTimer->prescaleBits != oldPrescale && !currentTimer->countUp) {
458 // FIXME: this might be before present
459 currentTimer->nextEvent = currentTimer->lastEvent + currentTimer->overflowInterval;
460 }
461
462 if (currentTimer->nextEvent < gba->cpu->nextEvent) {
463 gba->cpu->nextEvent = currentTimer->nextEvent;
464 }
465};
466
467void GBAWriteIE(struct GBA* gba, uint16_t value) {
468 if (value & (1 << IRQ_KEYPAD)) {
469 GBALog(gba, GBA_LOG_STUB, "Keypad interrupts not implemented");
470 }
471
472 if (value & (1 << IRQ_GAMEPAK)) {
473 GBALog(gba, GBA_LOG_STUB, "Gamepak interrupts not implemented");
474 }
475
476 if (gba->memory.io[REG_IME >> 1] && value & gba->memory.io[REG_IF >> 1]) {
477 ARMRaiseIRQ(gba->cpu);
478 }
479}
480
481void GBAWriteIME(struct GBA* gba, uint16_t value) {
482 if (value && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
483 ARMRaiseIRQ(gba->cpu);
484 }
485}
486
487void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq) {
488 gba->memory.io[REG_IF >> 1] |= 1 << irq;
489 gba->cpu->halted = 0;
490
491 if (gba->memory.io[REG_IME >> 1] && (gba->memory.io[REG_IE >> 1] & 1 << irq)) {
492 ARMRaiseIRQ(gba->cpu);
493 }
494}
495
496void GBATestIRQ(struct ARMCore* cpu) {
497 struct GBA* gba = (struct GBA*) cpu->master;
498 if (gba->memory.io[REG_IME >> 1] && gba->memory.io[REG_IE >> 1] & gba->memory.io[REG_IF >> 1]) {
499 gba->springIRQ = 1;
500 gba->cpu->nextEvent = 0;
501 }
502}
503
504void GBAHalt(struct GBA* gba) {
505 gba->cpu->nextEvent = 0;
506 gba->cpu->halted = 1;
507}
508
509static void _GBAVLog(struct GBA* gba, enum GBALogLevel level, const char* format, va_list args) {
510 struct GBAThread* threadContext = GBAThreadGetContext();
511 enum GBALogLevel logLevel = -1;
512
513 if (gba) {
514 logLevel = gba->logLevel;
515 }
516
517 if (threadContext) {
518 logLevel = threadContext->logLevel;
519 gba = threadContext->gba;
520 }
521
522 if (!(level & logLevel) && level != GBA_LOG_FATAL) {
523 return;
524 }
525
526 if (level == GBA_LOG_FATAL && gba) {
527 gba->cpu->nextEvent = 0;
528 }
529
530 if (threadContext) {
531 if (level == GBA_LOG_FATAL) {
532 MutexLock(&threadContext->stateMutex);
533 threadContext->state = THREAD_CRASHED;
534 MutexUnlock(&threadContext->stateMutex);
535 }
536 if (threadContext->logHandler) {
537 threadContext->logHandler(threadContext, level, format, args);
538 return;
539 }
540 }
541
542 vprintf(format, args);
543 printf("\n");
544
545 if (level == GBA_LOG_FATAL && !threadContext) {
546 abort();
547 }
548}
549
550void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...) {
551 va_list args;
552 va_start(args, format);
553 _GBAVLog(gba, level, format, args);
554 va_end(args);
555}
556
557void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...) {
558 struct GBA* gba = 0;
559 if (debugger->cpu) {
560 gba = (struct GBA*) debugger->cpu->master;
561 }
562
563 enum GBALogLevel gbaLevel;
564 switch (level) {
565 default: // Avoids compiler warning
566 case DEBUGGER_LOG_DEBUG:
567 gbaLevel = GBA_LOG_DEBUG;
568 break;
569 case DEBUGGER_LOG_INFO:
570 gbaLevel = GBA_LOG_INFO;
571 break;
572 case DEBUGGER_LOG_WARN:
573 gbaLevel = GBA_LOG_WARN;
574 break;
575 case DEBUGGER_LOG_ERROR:
576 gbaLevel = GBA_LOG_ERROR;
577 break;
578 }
579 va_list args;
580 va_start(args, format);
581 _GBAVLog(gba, gbaLevel, format, args);
582 va_end(args);
583}
584
585bool GBAIsROM(struct VFile* vf) {
586 if (vf->seek(vf, GBA_ROM_MAGIC_OFFSET, SEEK_SET) < 0) {
587 return false;
588 }
589 uint8_t signature[sizeof(GBA_ROM_MAGIC)];
590 if (vf->read(vf, &signature, sizeof(signature)) != sizeof(signature)) {
591 return false;
592 }
593 return memcmp(signature, GBA_ROM_MAGIC, sizeof(signature)) == 0;
594}
595
596bool GBAIsBIOS(struct VFile* vf) {
597 if (vf->seek(vf, 0, SEEK_SET) < 0) {
598 return false;
599 }
600 uint32_t interruptTable[7];
601 if (vf->read(vf, &interruptTable, sizeof(interruptTable)) != sizeof(interruptTable)) {
602 return false;
603 }
604 int i;
605 for (i = 0; i < 7; ++i) {
606 if ((interruptTable[i] & 0xFFFF0000) != 0xEA000000) {
607 return false;
608 }
609 }
610 return true;
611}
612
613void GBAGetGameCode(struct GBA* gba, char* out) {
614 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->id, 4);
615}
616
617void GBAGetGameTitle(struct GBA* gba, char* out) {
618 memcpy(out, &((struct GBACartridge*) gba->memory.rom)->title, 12);
619}
620
621void GBAHitStub(struct ARMCore* cpu, uint32_t opcode) {
622 struct GBA* gba = (struct GBA*) cpu->master;
623 enum GBALogLevel level = GBA_LOG_FATAL;
624 if (gba->debugger) {
625 level = GBA_LOG_STUB;
626 struct DebuggerEntryInfo info = {
627 .address = cpu->gprs[ARM_PC],
628 .opcode = opcode
629 };
630 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
631 }
632 GBALog(gba, level, "Stub opcode: %08x", opcode);
633}
634
635void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
636 struct GBA* gba = (struct GBA*) cpu->master;
637 GBALog(gba, GBA_LOG_WARN, "Illegal opcode: %08x", opcode);
638 if (gba->debugger) {
639 struct DebuggerEntryInfo info = {
640 .address = cpu->gprs[ARM_PC],
641 .opcode = opcode
642 };
643 ARMDebuggerEnter(gba->debugger, DEBUGGER_ENTER_ILLEGAL_OP, &info);
644 }
645}
646
647void GBAFrameStarted(struct GBA* gba) {
648 UNUSED(gba);
649
650 struct GBAThread* thread = GBAThreadGetContext();
651 if (!thread) {
652 return;
653 }
654
655 if (thread->rewindBuffer) {
656 --thread->rewindBufferNext;
657 if (thread->rewindBufferNext <= 0) {
658 thread->rewindBufferNext = thread->rewindBufferInterval;
659 GBARecordFrame(thread);
660 }
661 }
662}
663
664void GBAFrameEnded(struct GBA* gba) {
665 if (gba->rr) {
666 GBARRNextFrame(gba->rr);
667 }
668
669 struct GBAThread* thread = GBAThreadGetContext();
670 if (!thread) {
671 return;
672 }
673
674 if (thread->stream) {
675 thread->stream->postVideoFrame(thread->stream, thread->renderer);
676 }
677
678 if (thread->frameCallback) {
679 thread->frameCallback(thread);
680 }
681}