src/gba/supervisor/thread.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 "thread.h"
7
8#include "arm.h"
9#include "core/config.h"
10#include "gba/gba.h"
11#include "gba/cheats.h"
12#include "gba/serialize.h"
13#include "gba/rr/mgm.h"
14#include "gba/rr/vbm.h"
15
16#include "debugger/debugger.h"
17
18#include "util/patch.h"
19#include "util/vfs.h"
20
21#include "platform/commandline.h"
22
23#include <signal.h>
24
25static const float _defaultFPSTarget = 60.f;
26
27#ifndef DISABLE_THREADING
28
29static bool _reloadDirectories(struct GBAThread* threadContext) {
30 mDirectorySetDetachBase(&threadContext->dirs);
31
32 char basename[PATH_MAX];
33 if (threadContext->fname) {
34 char dirname[PATH_MAX];
35 separatePath(threadContext->fname, dirname, basename, 0);
36 mDirectorySetAttachBase(&threadContext->dirs, VDirOpen(dirname));
37 } else {
38 return false;
39 }
40
41 char path[PATH_MAX];
42 snprintf(path, sizeof(path), "%s.sav", basename);
43 threadContext->save = threadContext->dirs.save->openFile(threadContext->dirs.save, path, O_CREAT | O_RDWR);
44
45 if (!threadContext->patch) {
46 snprintf(path, sizeof(path), "%s.ups", basename);
47 threadContext->patch = threadContext->dirs.patch->openFile(threadContext->dirs.patch, path, O_RDONLY);
48 }
49 if (!threadContext->patch) {
50 snprintf(path, sizeof(path), "%s.ips", basename);
51 threadContext->patch = threadContext->dirs.patch->openFile(threadContext->dirs.patch, path, O_RDONLY);
52 }
53 if (!threadContext->patch) {
54 snprintf(path, sizeof(path), "%s.bps", basename);
55 threadContext->patch = threadContext->dirs.patch->openFile(threadContext->dirs.patch, path, O_RDONLY);
56 }
57 return true;
58}
59
60#ifdef USE_PTHREADS
61static pthread_key_t _contextKey;
62static pthread_once_t _contextOnce = PTHREAD_ONCE_INIT;
63
64static void _createTLS(void) {
65 pthread_key_create(&_contextKey, 0);
66}
67#elif _WIN32
68static DWORD _contextKey;
69static INIT_ONCE _contextOnce = INIT_ONCE_STATIC_INIT;
70
71static BOOL CALLBACK _createTLS(PINIT_ONCE once, PVOID param, PVOID* context) {
72 UNUSED(once);
73 UNUSED(param);
74 UNUSED(context);
75 _contextKey = TlsAlloc();
76 return TRUE;
77}
78#endif
79
80static void _changeState(struct GBAThread* threadContext, enum mCoreThreadState newState, bool broadcast) {
81 MutexLock(&threadContext->stateMutex);
82 threadContext->state = newState;
83 if (broadcast) {
84 ConditionWake(&threadContext->stateCond);
85 }
86 MutexUnlock(&threadContext->stateMutex);
87}
88
89static void _waitOnInterrupt(struct GBAThread* threadContext) {
90 while (threadContext->state == THREAD_INTERRUPTED) {
91 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
92 }
93}
94
95static void _waitUntilNotState(struct GBAThread* threadContext, enum mCoreThreadState oldState) {
96 MutexLock(&threadContext->sync.videoFrameMutex);
97 bool videoFrameWait = threadContext->sync.videoFrameWait;
98 threadContext->sync.videoFrameWait = false;
99 MutexUnlock(&threadContext->sync.videoFrameMutex);
100
101 while (threadContext->state == oldState) {
102 MutexUnlock(&threadContext->stateMutex);
103
104 if (!MutexTryLock(&threadContext->sync.videoFrameMutex)) {
105 ConditionWake(&threadContext->sync.videoFrameRequiredCond);
106 MutexUnlock(&threadContext->sync.videoFrameMutex);
107 }
108
109 if (!MutexTryLock(&threadContext->sync.audioBufferMutex)) {
110 ConditionWake(&threadContext->sync.audioRequiredCond);
111 MutexUnlock(&threadContext->sync.audioBufferMutex);
112 }
113
114 MutexLock(&threadContext->stateMutex);
115 ConditionWake(&threadContext->stateCond);
116 }
117
118 MutexLock(&threadContext->sync.videoFrameMutex);
119 threadContext->sync.videoFrameWait = videoFrameWait;
120 MutexUnlock(&threadContext->sync.videoFrameMutex);
121}
122
123static void _pauseThread(struct GBAThread* threadContext, bool onThread) {
124 threadContext->state = THREAD_PAUSING;
125 if (!onThread) {
126 _waitUntilNotState(threadContext, THREAD_PAUSING);
127 }
128}
129
130struct GBAThreadStop {
131 struct mStopCallback d;
132 struct GBAThread* p;
133};
134
135static void _stopCallback(struct mStopCallback* stop) {
136 struct GBAThreadStop* callback = (struct GBAThreadStop*) stop;
137 if (callback->p->stopCallback(callback->p)) {
138 _changeState(callback->p, THREAD_EXITING, false);
139 }
140}
141
142static THREAD_ENTRY _GBAThreadRun(void* context) {
143#ifdef USE_PTHREADS
144 pthread_once(&_contextOnce, _createTLS);
145#elif _WIN32
146 InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
147#endif
148
149 struct GBA gba;
150 struct ARMCore cpu;
151 struct Patch patch;
152 struct GBACheatDevice cheatDevice;
153 struct GBAThread* threadContext = context;
154 struct mCPUComponent* components[GBA_COMPONENT_MAX] = { 0 };
155 struct GBARRContext* movie = 0;
156 int numComponents = GBA_COMPONENT_MAX;
157
158 ThreadSetName("CPU Thread");
159
160#if !defined(_WIN32) && defined(USE_PTHREADS)
161 sigset_t signals;
162 sigemptyset(&signals);
163 pthread_sigmask(SIG_SETMASK, &signals, 0);
164#endif
165
166 GBACreate(&gba);
167 ARMSetComponents(&cpu, &gba.d, numComponents, components);
168 ARMInit(&cpu);
169 gba.sync = &threadContext->sync;
170 threadContext->gba = &gba;
171 threadContext->cpu = &cpu;
172 gba.logLevel = threadContext->logLevel;
173 gba.logHandler = threadContext->logHandler;
174 gba.stream = threadContext->stream;
175 gba.video.frameskip = threadContext->frameskip;
176
177 struct GBAThreadStop stop;
178 if (threadContext->stopCallback) {
179 stop.d.stop = _stopCallback;
180 stop.p = threadContext;
181 gba.stopCallback = &stop.d;
182 }
183
184 gba.idleOptimization = threadContext->idleOptimization;
185#ifdef USE_PTHREADS
186 pthread_setspecific(_contextKey, threadContext);
187#elif _WIN32
188 TlsSetValue(_contextKey, threadContext);
189#endif
190
191 if (threadContext->audioBuffers) {
192 GBAAudioResizeBuffer(&gba.audio, threadContext->audioBuffers);
193 } else {
194 threadContext->audioBuffers = GBA_AUDIO_SAMPLES;
195 }
196
197 if (threadContext->renderer) {
198 GBAVideoAssociateRenderer(&gba.video, threadContext->renderer);
199 }
200
201 if (threadContext->rom) {
202 if (GBAIsMB(threadContext->rom)) {
203 GBALoadMB(&gba, threadContext->rom, threadContext->fname);
204 } else {
205 GBALoadROM(&gba, threadContext->rom, threadContext->save, threadContext->fname);
206 }
207
208 struct GBACartridgeOverride override;
209 const struct GBACartridge* cart = (const struct GBACartridge*) gba.pristineRom;
210 memcpy(override.id, &cart->id, sizeof(override.id));
211 if (GBAOverrideFind(threadContext->overrides, &override)) {
212 GBAOverrideApply(&gba, &override);
213 }
214 if (threadContext->hasOverride) {
215 GBAOverrideApply(&gba, &threadContext->override);
216 }
217
218 if (threadContext->patch && loadPatch(threadContext->patch, &patch)) {
219 GBAApplyPatch(&gba, &patch);
220 }
221 }
222
223 if (threadContext->bios && GBAIsBIOS(threadContext->bios)) {
224 GBALoadBIOS(&gba, threadContext->bios);
225 }
226
227 if (threadContext->movie) {
228 struct VDir* movieDir = VDirOpen(threadContext->movie);
229 if (!movieDir) {
230 movieDir = VDirOpenArchive(threadContext->movie);
231 }
232 if (movieDir) {
233 struct GBAMGMContext* mgm = malloc(sizeof(*mgm));
234 GBAMGMContextCreate(mgm);
235 if (!GBAMGMSetStream(mgm, movieDir)) {
236 mgm->d.destroy(&mgm->d);
237 } else {
238 movie = &mgm->d;
239 }
240 } else {
241 struct VFile* movieFile = VFileOpen(threadContext->movie, O_RDONLY);
242 if (movieFile) {
243 struct GBAVBMContext* vbm = malloc(sizeof(*vbm));
244 GBAVBMContextCreate(vbm);
245 if (!GBAVBMSetStream(vbm, movieFile)) {
246 vbm->d.destroy(&vbm->d);
247 } else {
248 movie = &vbm->d;
249 }
250 }
251 }
252 }
253
254 ARMReset(&cpu);
255
256 if (movie) {
257 gba.rr = movie;
258 movie->startPlaying(movie, false);
259 GBARRInitPlay(&gba);
260 }
261
262 if (threadContext->skipBios && gba.pristineRom) {
263 GBASkipBIOS(&gba);
264 }
265
266 if (!threadContext->cheats) {
267 GBACheatDeviceCreate(&cheatDevice);
268 threadContext->cheats = &cheatDevice;
269 }
270 if (threadContext->cheatsFile) {
271 GBACheatParseFile(threadContext->cheats, threadContext->cheatsFile);
272 }
273 GBACheatAttachDevice(&gba, threadContext->cheats);
274
275 if (threadContext->debugger) {
276 threadContext->debugger->log = GBADebuggerLogShim;
277 GBAAttachDebugger(&gba, threadContext->debugger);
278 DebuggerEnter(threadContext->debugger, DEBUGGER_ENTER_ATTACHED, 0);
279 }
280
281 GBASIOSetDriverSet(&gba.sio, &threadContext->sioDrivers);
282
283 if (threadContext->volume == 0) {
284 threadContext->volume = GBA_AUDIO_VOLUME_MAX;
285 }
286 if (threadContext->mute) {
287 gba.audio.masterVolume = 0;
288 } else {
289 gba.audio.masterVolume = threadContext->volume;
290 }
291
292 gba.keySource = &threadContext->activeKeys;
293
294 if (threadContext->startCallback) {
295 threadContext->startCallback(threadContext);
296 }
297
298 _changeState(threadContext, THREAD_RUNNING, true);
299
300 while (threadContext->state < THREAD_EXITING) {
301 if (threadContext->debugger) {
302 struct Debugger* debugger = threadContext->debugger;
303 DebuggerRun(debugger);
304 if (debugger->state == DEBUGGER_SHUTDOWN) {
305 _changeState(threadContext, THREAD_EXITING, false);
306 }
307 } else {
308 while (threadContext->state == THREAD_RUNNING) {
309 ARMRunLoop(&cpu);
310 }
311 }
312
313 int resetScheduled = 0;
314 MutexLock(&threadContext->stateMutex);
315 while (threadContext->state > THREAD_RUNNING && threadContext->state < THREAD_EXITING) {
316 if (threadContext->state == THREAD_PAUSING) {
317 threadContext->state = THREAD_PAUSED;
318 ConditionWake(&threadContext->stateCond);
319 }
320 if (threadContext->state == THREAD_INTERRUPTING) {
321 threadContext->state = THREAD_INTERRUPTED;
322 ConditionWake(&threadContext->stateCond);
323 }
324 if (threadContext->state == THREAD_RUN_ON) {
325 if (threadContext->run) {
326 threadContext->run(threadContext);
327 }
328 threadContext->state = threadContext->savedState;
329 ConditionWake(&threadContext->stateCond);
330 }
331 if (threadContext->state == THREAD_RESETING) {
332 threadContext->state = THREAD_RUNNING;
333 resetScheduled = 1;
334 }
335 while (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_INTERRUPTED) {
336 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
337 }
338 }
339 MutexUnlock(&threadContext->stateMutex);
340 if (resetScheduled) {
341 ARMReset(&cpu);
342 if (threadContext->skipBios && gba.pristineRom) {
343 GBASkipBIOS(&gba);
344 }
345 }
346 }
347
348 while (threadContext->state < THREAD_SHUTDOWN) {
349 _changeState(threadContext, THREAD_SHUTDOWN, false);
350 }
351
352 if (threadContext->cleanCallback) {
353 threadContext->cleanCallback(threadContext);
354 }
355
356 threadContext->gba = 0;
357 ARMDeinit(&cpu);
358 GBADestroy(&gba);
359 if (&cheatDevice == threadContext->cheats) {
360 GBACheatDeviceDestroy(&cheatDevice);
361 }
362
363 if (movie) {
364 movie->destroy(movie);
365 free(movie);
366 }
367
368 threadContext->sync.videoFrameOn = false;
369 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
370 ConditionWake(&threadContext->sync.audioRequiredCond);
371
372 return 0;
373}
374
375void GBAMapOptionsToContext(const struct mCoreOptions* opts, struct GBAThread* threadContext) {
376 if (opts->useBios) {
377 threadContext->bios = VFileOpen(opts->bios, O_RDONLY);
378 } else {
379 threadContext->bios = 0;
380 }
381 threadContext->frameskip = opts->frameskip;
382 threadContext->volume = opts->volume;
383 threadContext->mute = opts->mute;
384 threadContext->logLevel = opts->logLevel;
385 if (opts->rewindEnable) {
386 threadContext->rewindBufferCapacity = opts->rewindBufferCapacity;
387 threadContext->rewindBufferInterval = opts->rewindBufferInterval;
388 } else {
389 threadContext->rewindBufferCapacity = 0;
390 }
391 threadContext->skipBios = opts->skipBios;
392 threadContext->sync.audioWait = opts->audioSync;
393 threadContext->sync.videoFrameWait = opts->videoSync;
394
395 if (opts->fpsTarget) {
396 threadContext->fpsTarget = opts->fpsTarget;
397 }
398
399 if (opts->audioBuffers) {
400 threadContext->audioBuffers = opts->audioBuffers;
401 }
402
403 mDirectorySetMapOptions(&threadContext->dirs, opts);
404}
405
406void GBAMapArgumentsToContext(const struct mArguments* args, struct GBAThread* threadContext) {
407 GBAThreadLoadROM(threadContext, args->fname);
408 threadContext->fname = args->fname;
409 threadContext->patch = VFileOpen(args->patch, O_RDONLY);
410 threadContext->cheatsFile = VFileOpen(args->cheatsFile, O_RDONLY);
411 threadContext->movie = args->movie;
412}
413
414bool GBAThreadStart(struct GBAThread* threadContext) {
415 // TODO: error check
416 threadContext->activeKeys = 0;
417 threadContext->state = THREAD_INITIALIZED;
418 threadContext->sync.videoFrameOn = true;
419
420 threadContext->rewindBuffer = 0;
421 threadContext->rewindScreenBuffer = 0;
422 int newCapacity = threadContext->rewindBufferCapacity;
423 int newInterval = threadContext->rewindBufferInterval;
424 threadContext->rewindBufferCapacity = 0;
425 threadContext->rewindBufferInterval = 0;
426 GBARewindSettingsChanged(threadContext, newCapacity, newInterval);
427
428 if (!threadContext->fpsTarget) {
429 threadContext->fpsTarget = _defaultFPSTarget;
430 }
431
432 bool bootBios = threadContext->bootBios && threadContext->bios;
433
434 if (threadContext->rom && (!GBAIsROM(threadContext->rom) || bootBios)) {
435 threadContext->rom->close(threadContext->rom);
436 threadContext->rom = 0;
437 }
438
439 if (!threadContext->rom && !bootBios) {
440 threadContext->state = THREAD_SHUTDOWN;
441 return false;
442 }
443
444 _reloadDirectories(threadContext);
445
446 MutexInit(&threadContext->stateMutex);
447 ConditionInit(&threadContext->stateCond);
448
449 MutexInit(&threadContext->sync.videoFrameMutex);
450 ConditionInit(&threadContext->sync.videoFrameAvailableCond);
451 ConditionInit(&threadContext->sync.videoFrameRequiredCond);
452 MutexInit(&threadContext->sync.audioBufferMutex);
453 ConditionInit(&threadContext->sync.audioRequiredCond);
454
455 threadContext->interruptDepth = 0;
456
457#ifdef USE_PTHREADS
458 sigset_t signals;
459 sigemptyset(&signals);
460 sigaddset(&signals, SIGINT);
461 sigaddset(&signals, SIGTRAP);
462 pthread_sigmask(SIG_BLOCK, &signals, 0);
463#endif
464
465 MutexLock(&threadContext->stateMutex);
466 ThreadCreate(&threadContext->thread, _GBAThreadRun, threadContext);
467 while (threadContext->state < THREAD_RUNNING) {
468 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
469 }
470 MutexUnlock(&threadContext->stateMutex);
471
472 return true;
473}
474
475bool GBAThreadHasStarted(struct GBAThread* threadContext) {
476 bool hasStarted;
477 MutexLock(&threadContext->stateMutex);
478 hasStarted = threadContext->state > THREAD_INITIALIZED;
479 MutexUnlock(&threadContext->stateMutex);
480 return hasStarted;
481}
482
483bool GBAThreadHasExited(struct GBAThread* threadContext) {
484 bool hasExited;
485 MutexLock(&threadContext->stateMutex);
486 hasExited = threadContext->state > THREAD_EXITING;
487 MutexUnlock(&threadContext->stateMutex);
488 return hasExited;
489}
490
491bool GBAThreadHasCrashed(struct GBAThread* threadContext) {
492 bool hasExited;
493 MutexLock(&threadContext->stateMutex);
494 hasExited = threadContext->state == THREAD_CRASHED;
495 MutexUnlock(&threadContext->stateMutex);
496 return hasExited;
497}
498
499void GBAThreadEnd(struct GBAThread* threadContext) {
500 MutexLock(&threadContext->stateMutex);
501 _waitOnInterrupt(threadContext);
502 threadContext->state = THREAD_EXITING;
503 if (threadContext->gba) {
504 threadContext->gba->cpu->halted = false;
505 }
506 ConditionWake(&threadContext->stateCond);
507 MutexUnlock(&threadContext->stateMutex);
508 MutexLock(&threadContext->sync.audioBufferMutex);
509 threadContext->sync.audioWait = 0;
510 ConditionWake(&threadContext->sync.audioRequiredCond);
511 MutexUnlock(&threadContext->sync.audioBufferMutex);
512
513 MutexLock(&threadContext->sync.videoFrameMutex);
514 threadContext->sync.videoFrameWait = false;
515 threadContext->sync.videoFrameOn = false;
516 ConditionWake(&threadContext->sync.videoFrameRequiredCond);
517 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
518 MutexUnlock(&threadContext->sync.videoFrameMutex);
519}
520
521void GBAThreadReset(struct GBAThread* threadContext) {
522 MutexLock(&threadContext->stateMutex);
523 _waitOnInterrupt(threadContext);
524 threadContext->state = THREAD_RESETING;
525 ConditionWake(&threadContext->stateCond);
526 MutexUnlock(&threadContext->stateMutex);
527}
528
529void GBAThreadJoin(struct GBAThread* threadContext) {
530 ThreadJoin(threadContext->thread);
531
532 MutexDeinit(&threadContext->stateMutex);
533 ConditionDeinit(&threadContext->stateCond);
534
535 MutexDeinit(&threadContext->sync.videoFrameMutex);
536 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
537 ConditionDeinit(&threadContext->sync.videoFrameAvailableCond);
538 ConditionWake(&threadContext->sync.videoFrameRequiredCond);
539 ConditionDeinit(&threadContext->sync.videoFrameRequiredCond);
540
541 ConditionWake(&threadContext->sync.audioRequiredCond);
542 ConditionDeinit(&threadContext->sync.audioRequiredCond);
543 MutexDeinit(&threadContext->sync.audioBufferMutex);
544
545 int i;
546 for (i = 0; i < threadContext->rewindBufferCapacity; ++i) {
547 if (threadContext->rewindBuffer[i]) {
548 GBADeallocateState(threadContext->rewindBuffer[i]);
549 }
550 }
551 free(threadContext->rewindBuffer);
552 free(threadContext->rewindScreenBuffer);
553
554 if (threadContext->rom) {
555 threadContext->rom->close(threadContext->rom);
556 threadContext->rom = 0;
557 }
558
559 if (threadContext->save) {
560 threadContext->save->close(threadContext->save);
561 threadContext->save = 0;
562 }
563
564 if (threadContext->bios) {
565 threadContext->bios->close(threadContext->bios);
566 threadContext->bios = 0;
567 }
568
569 if (threadContext->patch) {
570 threadContext->patch->close(threadContext->patch);
571 threadContext->patch = 0;
572 }
573}
574
575bool GBAThreadIsActive(struct GBAThread* threadContext) {
576 return threadContext->state >= THREAD_RUNNING && threadContext->state < THREAD_EXITING;
577}
578
579void GBAThreadInterrupt(struct GBAThread* threadContext) {
580 MutexLock(&threadContext->stateMutex);
581 ++threadContext->interruptDepth;
582 if (threadContext->interruptDepth > 1 || !GBAThreadIsActive(threadContext)) {
583 MutexUnlock(&threadContext->stateMutex);
584 return;
585 }
586 threadContext->savedState = threadContext->state;
587 _waitOnInterrupt(threadContext);
588 threadContext->state = THREAD_INTERRUPTING;
589 threadContext->gba->cpu->nextEvent = 0;
590 ConditionWake(&threadContext->stateCond);
591 _waitUntilNotState(threadContext, THREAD_INTERRUPTING);
592 MutexUnlock(&threadContext->stateMutex);
593}
594
595void GBAThreadContinue(struct GBAThread* threadContext) {
596 MutexLock(&threadContext->stateMutex);
597 --threadContext->interruptDepth;
598 if (threadContext->interruptDepth < 1 && GBAThreadIsActive(threadContext)) {
599 threadContext->state = threadContext->savedState;
600 ConditionWake(&threadContext->stateCond);
601 }
602 MutexUnlock(&threadContext->stateMutex);
603}
604
605void GBARunOnThread(struct GBAThread* threadContext, void (*run)(struct GBAThread*)) {
606 MutexLock(&threadContext->stateMutex);
607 threadContext->run = run;
608 _waitOnInterrupt(threadContext);
609 threadContext->savedState = threadContext->state;
610 threadContext->state = THREAD_RUN_ON;
611 threadContext->gba->cpu->nextEvent = 0;
612 ConditionWake(&threadContext->stateCond);
613 _waitUntilNotState(threadContext, THREAD_RUN_ON);
614 MutexUnlock(&threadContext->stateMutex);
615}
616
617void GBAThreadPause(struct GBAThread* threadContext) {
618 bool frameOn = threadContext->sync.videoFrameOn;
619 MutexLock(&threadContext->stateMutex);
620 _waitOnInterrupt(threadContext);
621 if (threadContext->state == THREAD_RUNNING) {
622 _pauseThread(threadContext, false);
623 threadContext->frameWasOn = frameOn;
624 frameOn = false;
625 }
626 MutexUnlock(&threadContext->stateMutex);
627
628 mCoreSyncSetVideoSync(&threadContext->sync, frameOn);
629}
630
631void GBAThreadUnpause(struct GBAThread* threadContext) {
632 bool frameOn = threadContext->sync.videoFrameOn;
633 MutexLock(&threadContext->stateMutex);
634 _waitOnInterrupt(threadContext);
635 if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
636 threadContext->state = THREAD_RUNNING;
637 ConditionWake(&threadContext->stateCond);
638 frameOn = threadContext->frameWasOn;
639 }
640 MutexUnlock(&threadContext->stateMutex);
641
642 mCoreSyncSetVideoSync(&threadContext->sync, frameOn);
643}
644
645bool GBAThreadIsPaused(struct GBAThread* threadContext) {
646 bool isPaused;
647 MutexLock(&threadContext->stateMutex);
648 _waitOnInterrupt(threadContext);
649 isPaused = threadContext->state == THREAD_PAUSED;
650 MutexUnlock(&threadContext->stateMutex);
651 return isPaused;
652}
653
654void GBAThreadTogglePause(struct GBAThread* threadContext) {
655 bool frameOn = threadContext->sync.videoFrameOn;
656 MutexLock(&threadContext->stateMutex);
657 _waitOnInterrupt(threadContext);
658 if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
659 threadContext->state = THREAD_RUNNING;
660 ConditionWake(&threadContext->stateCond);
661 frameOn = threadContext->frameWasOn;
662 } else if (threadContext->state == THREAD_RUNNING) {
663 _pauseThread(threadContext, false);
664 threadContext->frameWasOn = frameOn;
665 frameOn = false;
666 }
667 MutexUnlock(&threadContext->stateMutex);
668
669 mCoreSyncSetVideoSync(&threadContext->sync, frameOn);
670}
671
672void GBAThreadPauseFromThread(struct GBAThread* threadContext) {
673 bool frameOn = true;
674 MutexLock(&threadContext->stateMutex);
675 _waitOnInterrupt(threadContext);
676 if (threadContext->state == THREAD_RUNNING) {
677 _pauseThread(threadContext, true);
678 frameOn = false;
679 }
680 MutexUnlock(&threadContext->stateMutex);
681
682 mCoreSyncSetVideoSync(&threadContext->sync, frameOn);
683}
684
685void GBAThreadLoadROM(struct GBAThread* threadContext, const char* fname) {
686 threadContext->rom = mDirectorySetOpenPath(&threadContext->dirs, fname, GBAIsROM);
687}
688
689void GBAThreadReplaceROM(struct GBAThread* threadContext, const char* fname) {
690 GBAUnloadROM(threadContext->gba);
691
692 if (threadContext->rom) {
693 threadContext->rom->close(threadContext->rom);
694 threadContext->rom = 0;
695 }
696
697 if (threadContext->save) {
698 threadContext->save->close(threadContext->save);
699 threadContext->save = 0;
700 }
701
702 if (threadContext->dirs.archive) {
703 threadContext->dirs.archive->close(threadContext->dirs.archive);
704 threadContext->dirs.archive = 0;
705 }
706
707 GBAThreadLoadROM(threadContext, fname);
708
709 threadContext->fname = fname;
710 _reloadDirectories(threadContext);
711
712 GBARaiseIRQ(threadContext->gba, IRQ_GAMEPAK);
713 GBALoadROM(threadContext->gba, threadContext->rom, threadContext->save, threadContext->fname);
714}
715
716#ifdef USE_PTHREADS
717struct GBAThread* GBAThreadGetContext(void) {
718 pthread_once(&_contextOnce, _createTLS);
719 return pthread_getspecific(_contextKey);
720}
721#elif _WIN32
722struct GBAThread* GBAThreadGetContext(void) {
723 InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
724 return TlsGetValue(_contextKey);
725}
726#endif
727
728void GBAThreadTakeScreenshot(struct GBAThread* threadContext) {
729 GBATakeScreenshot(threadContext->gba, threadContext->dirs.screenshot);
730}
731
732#else
733struct GBAThread* GBAThreadGetContext(void) {
734 return 0;
735}
736#endif