src/gba/gba-thread.c (view raw)
1#include "gba-thread.h"
2
3#include "arm.h"
4#include "gba.h"
5#include "gba-serialize.h"
6
7#include "debugger/debugger.h"
8
9#include "util/patch.h"
10#include "util/vfs.h"
11
12#include <signal.h>
13
14#ifdef USE_PTHREADS
15static pthread_key_t _contextKey;
16static pthread_once_t _contextOnce = PTHREAD_ONCE_INIT;
17
18static void _createTLS(void) {
19 pthread_key_create(&_contextKey, 0);
20}
21#else
22static DWORD _contextKey;
23static INIT_ONCE _contextOnce = INIT_ONCE_STATIC_INIT;
24
25static BOOL CALLBACK _createTLS(PINIT_ONCE once, PVOID param, PVOID* context) {
26 UNUSED(once);
27 UNUSED(param);
28 UNUSED(context);
29 _contextKey = TlsAlloc();
30 return TRUE;
31}
32#endif
33
34static void _changeState(struct GBAThread* threadContext, enum ThreadState newState, int broadcast) {
35 MutexLock(&threadContext->stateMutex);
36 threadContext->state = newState;
37 if (broadcast) {
38 ConditionWake(&threadContext->stateCond);
39 }
40 MutexUnlock(&threadContext->stateMutex);
41}
42
43static void _waitOnInterrupt(struct GBAThread* threadContext) {
44 while (threadContext->state == THREAD_INTERRUPTED) {
45 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
46 }
47}
48
49static THREAD_ENTRY _GBAThreadRun(void* context) {
50#ifdef USE_PTHREADS
51 pthread_once(&_contextOnce, _createTLS);
52#else
53 InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
54#endif
55
56 struct GBA gba;
57 struct ARMCore cpu;
58 struct Patch patch;
59 struct GBAThread* threadContext = context;
60 struct ARMComponent* components[1] = {};
61 int numComponents = 0;
62
63 if (threadContext->debugger) {
64 components[numComponents] = &threadContext->debugger->d;
65 ++numComponents;
66 }
67
68#if !defined(_WIN32) && defined(USE_PTHREADS)
69 sigset_t signals;
70 sigemptyset(&signals);
71 pthread_sigmask(SIG_SETMASK, &signals, 0);
72#endif
73
74 gba.logHandler = threadContext->logHandler;
75 GBACreate(&gba);
76 ARMSetComponents(&cpu, &gba.d, numComponents, components);
77 ARMInit(&cpu);
78 ARMReset(&cpu);
79 threadContext->gba = &gba;
80 gba.sync = &threadContext->sync;
81 gba.logLevel = threadContext->logLevel;
82#ifdef USE_PTHREADS
83 pthread_setspecific(_contextKey, threadContext);
84#else
85 TlsSetValue(_contextKey, threadContext);
86#endif
87 if (threadContext->renderer) {
88 GBAVideoAssociateRenderer(&gba.video, threadContext->renderer);
89 }
90
91 if (threadContext->rom) {
92 GBALoadROM(&gba, threadContext->rom, threadContext->save, threadContext->fname);
93 if (threadContext->bios) {
94 GBALoadBIOS(&gba, threadContext->bios);
95 }
96
97 if (threadContext->patch && loadPatch(threadContext->patch, &patch)) {
98 GBAApplyPatch(&gba, &patch);
99 }
100 }
101
102 if (threadContext->debugger) {
103 GBAAttachDebugger(&gba, threadContext->debugger);
104 ARMDebuggerEnter(threadContext->debugger, DEBUGGER_ENTER_ATTACHED);
105 }
106
107 GBASIOSetDriverSet(&gba.sio, &threadContext->sioDrivers);
108
109 gba.keySource = &threadContext->activeKeys;
110
111 if (threadContext->startCallback) {
112 threadContext->startCallback(threadContext);
113 }
114
115 _changeState(threadContext, THREAD_RUNNING, 1);
116
117 while (threadContext->state < THREAD_EXITING) {
118 if (threadContext->debugger) {
119 struct ARMDebugger* debugger = threadContext->debugger;
120 ARMDebuggerRun(debugger);
121 if (debugger->state == DEBUGGER_SHUTDOWN) {
122 _changeState(threadContext, THREAD_EXITING, 0);
123 }
124 } else {
125 while (threadContext->state == THREAD_RUNNING) {
126 ARMRun(&cpu);
127 }
128 }
129
130 int resetScheduled = 0;
131 MutexLock(&threadContext->stateMutex);
132 if (threadContext->state == THREAD_PAUSING) {
133 threadContext->state = THREAD_PAUSED;
134 ConditionWake(&threadContext->stateCond);
135 }
136 if (threadContext->state == THREAD_INTERRUPTING) {
137 threadContext->state = THREAD_INTERRUPTED;
138 ConditionWake(&threadContext->stateCond);
139 }
140 if (threadContext->state == THREAD_RESETING) {
141 threadContext->state = THREAD_RUNNING;
142 resetScheduled = 1;
143 }
144 while (threadContext->state == THREAD_PAUSED) {
145 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
146 }
147 MutexUnlock(&threadContext->stateMutex);
148 if (resetScheduled) {
149 ARMReset(&cpu);
150 }
151 }
152
153 while (threadContext->state != THREAD_SHUTDOWN) {
154 _changeState(threadContext, THREAD_SHUTDOWN, 0);
155 }
156
157 if (threadContext->cleanCallback) {
158 threadContext->cleanCallback(threadContext);
159 }
160
161 threadContext->gba = 0;
162 ARMDeinit(&cpu);
163 GBADestroy(&gba);
164
165 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
166 ConditionWake(&threadContext->sync.audioRequiredCond);
167
168 return 0;
169}
170
171void GBAMapOptionsToContext(struct StartupOptions* opts, struct GBAThread* threadContext) {
172 if (opts->dirmode) {
173 threadContext->gamedir = VDirOpen(opts->fname);
174 } else {
175 threadContext->rom = VFileOpen(opts->fname, O_RDONLY);
176#if ENABLE_LIBZIP
177 threadContext->gamedir = VDirOpenZip(opts->fname, 0);
178#endif
179 }
180 threadContext->fname = opts->fname;
181 threadContext->bios = VFileOpen(opts->bios, O_RDONLY);
182 threadContext->patch = VFileOpen(opts->patch, O_RDONLY);
183 threadContext->frameskip = opts->frameskip;
184 threadContext->logLevel = opts->logLevel;
185 threadContext->rewindBufferCapacity = opts->rewindBufferCapacity;
186 threadContext->rewindBufferInterval = opts->rewindBufferInterval;
187}
188
189bool GBAThreadStart(struct GBAThread* threadContext) {
190 // TODO: error check
191 threadContext->activeKeys = 0;
192 threadContext->state = THREAD_INITIALIZED;
193 threadContext->sync.videoFrameOn = 1;
194 threadContext->sync.videoFrameSkip = 0;
195
196 threadContext->rewindBufferNext = threadContext->rewindBufferInterval;
197 threadContext->rewindBufferSize = 0;
198 if (threadContext->rewindBufferCapacity) {
199 threadContext->rewindBuffer = calloc(threadContext->rewindBufferCapacity, sizeof(void*));
200 } else {
201 threadContext->rewindBuffer = 0;
202 }
203
204 if (threadContext->rom && !GBAIsROM(threadContext->rom)) {
205 threadContext->rom->close(threadContext->rom);
206 threadContext->rom = 0;
207 }
208
209 if (threadContext->gamedir) {
210 threadContext->gamedir->rewind(threadContext->gamedir);
211 struct VDirEntry* dirent = threadContext->gamedir->listNext(threadContext->gamedir);
212 while (dirent) {
213 struct Patch patchTemp;
214 struct VFile* vf = threadContext->gamedir->openFile(threadContext->gamedir, dirent->name(dirent), O_RDONLY);
215 if (!vf) {
216 continue;
217 }
218 if (!threadContext->rom && GBAIsROM(vf)) {
219 threadContext->rom = vf;
220 } else if (!threadContext->patch && loadPatch(vf, &patchTemp)) {
221 threadContext->patch = vf;
222 } else {
223 vf->close(vf);
224 }
225 dirent = threadContext->gamedir->listNext(threadContext->gamedir);
226 }
227 }
228
229 if (!threadContext->rom) {
230 return false;
231 }
232
233 if (threadContext->fname && !threadContext->save) {
234 char* savedata = 0;
235 char* dotPoint = strrchr(threadContext->fname, '.');
236 if (dotPoint > strrchr(threadContext->fname, '/') && dotPoint[1] && dotPoint[2] && dotPoint[3]) {
237 savedata = strdup(threadContext->fname);
238 dotPoint = strrchr(savedata, '.');
239 dotPoint[1] = 's';
240 dotPoint[2] = 'a';
241 dotPoint[3] = 'v';
242 dotPoint[4] = '\0';
243 } else if (dotPoint) {
244 savedata = malloc((dotPoint - threadContext->fname + 5) * sizeof(char));
245 strncpy(savedata, threadContext->fname, dotPoint - threadContext->fname + 1);
246 strcat(savedata, "sav");
247 } else {
248 savedata = malloc(strlen(threadContext->fname + 5));
249 strcpy(savedata, threadContext->fname);
250 strcat(savedata, "sav");
251 }
252 threadContext->save = VFileOpen(savedata, O_RDWR | O_CREAT);
253 free(savedata);
254 }
255
256 MutexInit(&threadContext->stateMutex);
257 ConditionInit(&threadContext->stateCond);
258
259 MutexInit(&threadContext->sync.videoFrameMutex);
260 ConditionInit(&threadContext->sync.videoFrameAvailableCond);
261 ConditionInit(&threadContext->sync.videoFrameRequiredCond);
262 MutexInit(&threadContext->sync.audioBufferMutex);
263 ConditionInit(&threadContext->sync.audioRequiredCond);
264
265#ifndef _WIN32
266 sigset_t signals;
267 sigemptyset(&signals);
268 sigaddset(&signals, SIGINT);
269 sigaddset(&signals, SIGTRAP);
270 pthread_sigmask(SIG_BLOCK, &signals, 0);
271#endif
272
273 MutexLock(&threadContext->stateMutex);
274 ThreadCreate(&threadContext->thread, _GBAThreadRun, threadContext);
275 while (threadContext->state < THREAD_RUNNING) {
276 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
277 }
278 MutexUnlock(&threadContext->stateMutex);
279
280 return true;
281}
282
283bool GBAThreadHasStarted(struct GBAThread* threadContext) {
284 bool hasStarted;
285 MutexLock(&threadContext->stateMutex);
286 hasStarted = threadContext->state > THREAD_INITIALIZED;
287 MutexUnlock(&threadContext->stateMutex);
288 return hasStarted;
289}
290
291void GBAThreadEnd(struct GBAThread* threadContext) {
292 MutexLock(&threadContext->stateMutex);
293 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
294 threadContext->debugger->state = DEBUGGER_EXITING;
295 }
296 threadContext->state = THREAD_EXITING;
297 MutexUnlock(&threadContext->stateMutex);
298 MutexLock(&threadContext->sync.audioBufferMutex);
299 threadContext->sync.audioWait = 0;
300 ConditionWake(&threadContext->sync.audioRequiredCond);
301 MutexUnlock(&threadContext->sync.audioBufferMutex);
302}
303
304void GBAThreadReset(struct GBAThread* threadContext) {
305 MutexLock(&threadContext->stateMutex);
306 _waitOnInterrupt(threadContext);
307 threadContext->state = THREAD_RESETING;
308 ConditionWake(&threadContext->stateCond);
309 MutexUnlock(&threadContext->stateMutex);
310}
311
312void GBAThreadJoin(struct GBAThread* threadContext) {
313 MutexLock(&threadContext->sync.videoFrameMutex);
314 threadContext->sync.videoFrameWait = 0;
315 ConditionWake(&threadContext->sync.videoFrameRequiredCond);
316 MutexUnlock(&threadContext->sync.videoFrameMutex);
317
318 ThreadJoin(threadContext->thread);
319
320 MutexDeinit(&threadContext->stateMutex);
321 ConditionDeinit(&threadContext->stateCond);
322
323 MutexDeinit(&threadContext->sync.videoFrameMutex);
324 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
325 ConditionDeinit(&threadContext->sync.videoFrameAvailableCond);
326 ConditionWake(&threadContext->sync.videoFrameRequiredCond);
327 ConditionDeinit(&threadContext->sync.videoFrameRequiredCond);
328
329 ConditionWake(&threadContext->sync.audioRequiredCond);
330 ConditionDeinit(&threadContext->sync.audioRequiredCond);
331 MutexDeinit(&threadContext->sync.audioBufferMutex);
332
333 int i;
334 for (i = 0; i < threadContext->rewindBufferCapacity; ++i) {
335 if (threadContext->rewindBuffer[i]) {
336 GBADeallocateState(threadContext->rewindBuffer[i]);
337 }
338 }
339 free(threadContext->rewindBuffer);
340
341 if (threadContext->rom) {
342 threadContext->rom->close(threadContext->rom);
343 threadContext->rom = 0;
344 }
345
346 if (threadContext->save) {
347 threadContext->save->close(threadContext->save);
348 threadContext->save = 0;
349 }
350
351 if (threadContext->bios) {
352 threadContext->bios->close(threadContext->bios);
353 threadContext->bios = 0;
354 }
355
356 if (threadContext->patch) {
357 threadContext->patch->close(threadContext->patch);
358 threadContext->patch = 0;
359 }
360}
361
362void GBAThreadInterrupt(struct GBAThread* threadContext) {
363 MutexLock(&threadContext->stateMutex);
364 threadContext->savedState = threadContext->state;
365 _waitOnInterrupt(threadContext);
366 threadContext->state = THREAD_INTERRUPTING;
367 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
368 threadContext->debugger->state = DEBUGGER_EXITING;
369 }
370 ConditionWake(&threadContext->stateCond);
371 while (threadContext->state == THREAD_INTERRUPTING) {
372 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
373 }
374 MutexUnlock(&threadContext->stateMutex);
375}
376
377void GBAThreadContinue(struct GBAThread* threadContext) {
378 _changeState(threadContext, threadContext->savedState, 1);
379}
380
381void GBAThreadPause(struct GBAThread* threadContext) {
382 int frameOn = 1;
383 MutexLock(&threadContext->stateMutex);
384 _waitOnInterrupt(threadContext);
385 if (threadContext->state == THREAD_RUNNING) {
386 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
387 threadContext->debugger->state = DEBUGGER_EXITING;
388 }
389 threadContext->state = THREAD_PAUSING;
390 frameOn = 0;
391 }
392 MutexUnlock(&threadContext->stateMutex);
393 MutexLock(&threadContext->sync.videoFrameMutex);
394 if (frameOn != threadContext->sync.videoFrameOn) {
395 threadContext->sync.videoFrameOn = frameOn;
396 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
397 }
398 MutexUnlock(&threadContext->sync.videoFrameMutex);
399}
400
401void GBAThreadUnpause(struct GBAThread* threadContext) {
402 int frameOn = 1;
403 MutexLock(&threadContext->stateMutex);
404 _waitOnInterrupt(threadContext);
405 if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
406 threadContext->state = THREAD_RUNNING;
407 ConditionWake(&threadContext->stateCond);
408 }
409 MutexUnlock(&threadContext->stateMutex);
410 MutexLock(&threadContext->sync.videoFrameMutex);
411 if (frameOn != threadContext->sync.videoFrameOn) {
412 threadContext->sync.videoFrameOn = frameOn;
413 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
414 }
415 MutexUnlock(&threadContext->sync.videoFrameMutex);
416}
417
418bool GBAThreadIsPaused(struct GBAThread* threadContext) {
419 bool isPaused;
420 MutexLock(&threadContext->stateMutex);
421 _waitOnInterrupt(threadContext);
422 isPaused = threadContext->state == THREAD_PAUSED;
423 MutexUnlock(&threadContext->stateMutex);
424 return isPaused;
425}
426
427void GBAThreadTogglePause(struct GBAThread* threadContext) {
428 bool frameOn = true;
429 MutexLock(&threadContext->stateMutex);
430 _waitOnInterrupt(threadContext);
431 if (threadContext->state == THREAD_PAUSED) {
432 threadContext->state = THREAD_RUNNING;
433 ConditionWake(&threadContext->stateCond);
434 } else if (threadContext->state == THREAD_RUNNING) {
435 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
436 threadContext->debugger->state = DEBUGGER_EXITING;
437 }
438 threadContext->state = THREAD_PAUSED;
439 frameOn = false;
440 }
441 MutexUnlock(&threadContext->stateMutex);
442 MutexLock(&threadContext->sync.videoFrameMutex);
443 if (frameOn != threadContext->sync.videoFrameOn) {
444 threadContext->sync.videoFrameOn = frameOn;
445 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
446 }
447 MutexUnlock(&threadContext->sync.videoFrameMutex);
448}
449
450#ifdef USE_PTHREADS
451struct GBAThread* GBAThreadGetContext(void) {
452 pthread_once(&_contextOnce, _createTLS);
453 return pthread_getspecific(_contextKey);
454}
455#else
456struct GBAThread* GBAThreadGetContext(void) {
457 InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
458 return TlsGetValue(_contextKey);
459}
460#endif
461
462void GBASyncPostFrame(struct GBASync* sync) {
463 if (!sync) {
464 return;
465 }
466
467 MutexLock(&sync->videoFrameMutex);
468 ++sync->videoFramePending;
469 --sync->videoFrameSkip;
470 if (sync->videoFrameSkip < 0) {
471 ConditionWake(&sync->videoFrameAvailableCond);
472 while (sync->videoFrameWait && sync->videoFramePending) {
473 ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex);
474 }
475 }
476 MutexUnlock(&sync->videoFrameMutex);
477
478 struct GBAThread* thread = GBAThreadGetContext();
479 if (thread->rewindBuffer) {
480 --thread->rewindBufferNext;
481 if (thread->rewindBufferNext <= 0) {
482 thread->rewindBufferNext = thread->rewindBufferInterval;
483 GBARecordFrame(thread);
484 }
485 }
486 if (thread->frameCallback) {
487 thread->frameCallback(thread);
488 }
489}
490
491bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) {
492 if (!sync) {
493 return true;
494 }
495
496 MutexLock(&sync->videoFrameMutex);
497 ConditionWake(&sync->videoFrameRequiredCond);
498 if (!sync->videoFrameOn) {
499 return false;
500 }
501 ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex);
502 sync->videoFramePending = 0;
503 sync->videoFrameSkip = frameskip;
504 return true;
505}
506
507void GBASyncWaitFrameEnd(struct GBASync* sync) {
508 if (!sync) {
509 return;
510 }
511
512 MutexUnlock(&sync->videoFrameMutex);
513}
514
515bool GBASyncDrawingFrame(struct GBASync* sync) {
516 return sync->videoFrameSkip <= 0;
517}
518
519void GBASyncProduceAudio(struct GBASync* sync, int wait) {
520 if (sync->audioWait && wait) {
521 // TODO loop properly in event of spurious wakeups
522 ConditionWait(&sync->audioRequiredCond, &sync->audioBufferMutex);
523 }
524 MutexUnlock(&sync->audioBufferMutex);
525}
526
527void GBASyncLockAudio(struct GBASync* sync) {
528 MutexLock(&sync->audioBufferMutex);
529}
530
531void GBASyncConsumeAudio(struct GBASync* sync) {
532 ConditionWake(&sync->audioRequiredCond);
533 MutexUnlock(&sync->audioBufferMutex);
534}