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 (!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->fname && !threadContext->save) {
230 char* savedata = 0;
231 char* dotPoint = strrchr(threadContext->fname, '.');
232 if (dotPoint > strrchr(threadContext->fname, '/') && dotPoint[1] && dotPoint[2] && dotPoint[3]) {
233 savedata = strdup(threadContext->fname);
234 dotPoint = strrchr(savedata, '.');
235 dotPoint[1] = 's';
236 dotPoint[2] = 'a';
237 dotPoint[3] = 'v';
238 dotPoint[4] = '\0';
239 } else if (dotPoint) {
240 savedata = malloc((dotPoint - threadContext->fname + 5) * sizeof(char));
241 strncpy(savedata, threadContext->fname, dotPoint - threadContext->fname + 1);
242 strcat(savedata, "sav");
243 } else {
244 savedata = malloc(strlen(threadContext->fname + 5));
245 strcpy(savedata, threadContext->fname);
246 strcat(savedata, "sav");
247 }
248 threadContext->save = VFileOpen(savedata, O_RDWR | O_CREAT);
249 free(savedata);
250 }
251
252 MutexInit(&threadContext->stateMutex);
253 ConditionInit(&threadContext->stateCond);
254
255 MutexInit(&threadContext->sync.videoFrameMutex);
256 ConditionInit(&threadContext->sync.videoFrameAvailableCond);
257 ConditionInit(&threadContext->sync.videoFrameRequiredCond);
258 MutexInit(&threadContext->sync.audioBufferMutex);
259 ConditionInit(&threadContext->sync.audioRequiredCond);
260
261#ifndef _WIN32
262 sigset_t signals;
263 sigemptyset(&signals);
264 sigaddset(&signals, SIGINT);
265 sigaddset(&signals, SIGTRAP);
266 pthread_sigmask(SIG_BLOCK, &signals, 0);
267#endif
268
269 MutexLock(&threadContext->stateMutex);
270 ThreadCreate(&threadContext->thread, _GBAThreadRun, threadContext);
271 while (threadContext->state < THREAD_RUNNING) {
272 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
273 }
274 MutexUnlock(&threadContext->stateMutex);
275
276 return true;
277}
278
279bool GBAThreadHasStarted(struct GBAThread* threadContext) {
280 bool hasStarted;
281 MutexLock(&threadContext->stateMutex);
282 hasStarted = threadContext->state > THREAD_INITIALIZED;
283 MutexUnlock(&threadContext->stateMutex);
284 return hasStarted;
285}
286
287void GBAThreadEnd(struct GBAThread* threadContext) {
288 MutexLock(&threadContext->stateMutex);
289 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
290 threadContext->debugger->state = DEBUGGER_EXITING;
291 }
292 threadContext->state = THREAD_EXITING;
293 MutexUnlock(&threadContext->stateMutex);
294 MutexLock(&threadContext->sync.audioBufferMutex);
295 threadContext->sync.audioWait = 0;
296 ConditionWake(&threadContext->sync.audioRequiredCond);
297 MutexUnlock(&threadContext->sync.audioBufferMutex);
298}
299
300void GBAThreadReset(struct GBAThread* threadContext) {
301 MutexLock(&threadContext->stateMutex);
302 _waitOnInterrupt(threadContext);
303 threadContext->state = THREAD_RESETING;
304 ConditionWake(&threadContext->stateCond);
305 MutexUnlock(&threadContext->stateMutex);
306}
307
308void GBAThreadJoin(struct GBAThread* threadContext) {
309 MutexLock(&threadContext->sync.videoFrameMutex);
310 threadContext->sync.videoFrameWait = 0;
311 ConditionWake(&threadContext->sync.videoFrameRequiredCond);
312 MutexUnlock(&threadContext->sync.videoFrameMutex);
313
314 ThreadJoin(threadContext->thread);
315
316 MutexDeinit(&threadContext->stateMutex);
317 ConditionDeinit(&threadContext->stateCond);
318
319 MutexDeinit(&threadContext->sync.videoFrameMutex);
320 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
321 ConditionDeinit(&threadContext->sync.videoFrameAvailableCond);
322 ConditionWake(&threadContext->sync.videoFrameRequiredCond);
323 ConditionDeinit(&threadContext->sync.videoFrameRequiredCond);
324
325 ConditionWake(&threadContext->sync.audioRequiredCond);
326 ConditionDeinit(&threadContext->sync.audioRequiredCond);
327 MutexDeinit(&threadContext->sync.audioBufferMutex);
328
329 int i;
330 for (i = 0; i < threadContext->rewindBufferCapacity; ++i) {
331 if (threadContext->rewindBuffer[i]) {
332 GBADeallocateState(threadContext->rewindBuffer[i]);
333 }
334 }
335 free(threadContext->rewindBuffer);
336
337 if (threadContext->rom) {
338 threadContext->rom->close(threadContext->rom);
339 threadContext->rom = 0;
340 }
341
342 if (threadContext->save) {
343 threadContext->save->close(threadContext->save);
344 threadContext->save = 0;
345 }
346
347 if (threadContext->bios) {
348 threadContext->bios->close(threadContext->bios);
349 threadContext->bios = 0;
350 }
351
352 if (threadContext->patch) {
353 threadContext->patch->close(threadContext->patch);
354 threadContext->patch = 0;
355 }
356}
357
358void GBAThreadInterrupt(struct GBAThread* threadContext) {
359 MutexLock(&threadContext->stateMutex);
360 threadContext->savedState = threadContext->state;
361 _waitOnInterrupt(threadContext);
362 threadContext->state = THREAD_INTERRUPTING;
363 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
364 threadContext->debugger->state = DEBUGGER_EXITING;
365 }
366 ConditionWake(&threadContext->stateCond);
367 while (threadContext->state == THREAD_INTERRUPTING) {
368 ConditionWait(&threadContext->stateCond, &threadContext->stateMutex);
369 }
370 MutexUnlock(&threadContext->stateMutex);
371}
372
373void GBAThreadContinue(struct GBAThread* threadContext) {
374 _changeState(threadContext, threadContext->savedState, 1);
375}
376
377void GBAThreadPause(struct GBAThread* threadContext) {
378 int frameOn = 1;
379 MutexLock(&threadContext->stateMutex);
380 _waitOnInterrupt(threadContext);
381 if (threadContext->state == THREAD_RUNNING) {
382 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
383 threadContext->debugger->state = DEBUGGER_EXITING;
384 }
385 threadContext->state = THREAD_PAUSING;
386 frameOn = 0;
387 }
388 MutexUnlock(&threadContext->stateMutex);
389 MutexLock(&threadContext->sync.videoFrameMutex);
390 if (frameOn != threadContext->sync.videoFrameOn) {
391 threadContext->sync.videoFrameOn = frameOn;
392 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
393 }
394 MutexUnlock(&threadContext->sync.videoFrameMutex);
395}
396
397void GBAThreadUnpause(struct GBAThread* threadContext) {
398 int frameOn = 1;
399 MutexLock(&threadContext->stateMutex);
400 _waitOnInterrupt(threadContext);
401 if (threadContext->state == THREAD_PAUSED || threadContext->state == THREAD_PAUSING) {
402 threadContext->state = THREAD_RUNNING;
403 ConditionWake(&threadContext->stateCond);
404 }
405 MutexUnlock(&threadContext->stateMutex);
406 MutexLock(&threadContext->sync.videoFrameMutex);
407 if (frameOn != threadContext->sync.videoFrameOn) {
408 threadContext->sync.videoFrameOn = frameOn;
409 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
410 }
411 MutexUnlock(&threadContext->sync.videoFrameMutex);
412}
413
414bool GBAThreadIsPaused(struct GBAThread* threadContext) {
415 bool isPaused;
416 MutexLock(&threadContext->stateMutex);
417 _waitOnInterrupt(threadContext);
418 isPaused = threadContext->state == THREAD_PAUSED;
419 MutexUnlock(&threadContext->stateMutex);
420 return isPaused;
421}
422
423void GBAThreadTogglePause(struct GBAThread* threadContext) {
424 bool frameOn = true;
425 MutexLock(&threadContext->stateMutex);
426 _waitOnInterrupt(threadContext);
427 if (threadContext->state == THREAD_PAUSED) {
428 threadContext->state = THREAD_RUNNING;
429 ConditionWake(&threadContext->stateCond);
430 } else if (threadContext->state == THREAD_RUNNING) {
431 if (threadContext->debugger && threadContext->debugger->state == DEBUGGER_RUNNING) {
432 threadContext->debugger->state = DEBUGGER_EXITING;
433 }
434 threadContext->state = THREAD_PAUSED;
435 frameOn = false;
436 }
437 MutexUnlock(&threadContext->stateMutex);
438 MutexLock(&threadContext->sync.videoFrameMutex);
439 if (frameOn != threadContext->sync.videoFrameOn) {
440 threadContext->sync.videoFrameOn = frameOn;
441 ConditionWake(&threadContext->sync.videoFrameAvailableCond);
442 }
443 MutexUnlock(&threadContext->sync.videoFrameMutex);
444}
445
446#ifdef USE_PTHREADS
447struct GBAThread* GBAThreadGetContext(void) {
448 pthread_once(&_contextOnce, _createTLS);
449 return pthread_getspecific(_contextKey);
450}
451#else
452struct GBAThread* GBAThreadGetContext(void) {
453 InitOnceExecuteOnce(&_contextOnce, _createTLS, NULL, 0);
454 return TlsGetValue(_contextKey);
455}
456#endif
457
458void GBASyncPostFrame(struct GBASync* sync) {
459 if (!sync) {
460 return;
461 }
462
463 MutexLock(&sync->videoFrameMutex);
464 ++sync->videoFramePending;
465 --sync->videoFrameSkip;
466 if (sync->videoFrameSkip < 0) {
467 ConditionWake(&sync->videoFrameAvailableCond);
468 while (sync->videoFrameWait && sync->videoFramePending) {
469 ConditionWait(&sync->videoFrameRequiredCond, &sync->videoFrameMutex);
470 }
471 }
472 MutexUnlock(&sync->videoFrameMutex);
473
474 struct GBAThread* thread = GBAThreadGetContext();
475 if (thread->rewindBuffer) {
476 --thread->rewindBufferNext;
477 if (thread->rewindBufferNext <= 0) {
478 thread->rewindBufferNext = thread->rewindBufferInterval;
479 GBARecordFrame(thread);
480 }
481 }
482 if (thread->frameCallback) {
483 thread->frameCallback(thread);
484 }
485}
486
487bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip) {
488 if (!sync) {
489 return true;
490 }
491
492 MutexLock(&sync->videoFrameMutex);
493 ConditionWake(&sync->videoFrameRequiredCond);
494 if (!sync->videoFrameOn) {
495 return false;
496 }
497 ConditionWait(&sync->videoFrameAvailableCond, &sync->videoFrameMutex);
498 sync->videoFramePending = 0;
499 sync->videoFrameSkip = frameskip;
500 return true;
501}
502
503void GBASyncWaitFrameEnd(struct GBASync* sync) {
504 if (!sync) {
505 return;
506 }
507
508 MutexUnlock(&sync->videoFrameMutex);
509}
510
511bool GBASyncDrawingFrame(struct GBASync* sync) {
512 return sync->videoFrameSkip <= 0;
513}
514
515void GBASyncProduceAudio(struct GBASync* sync, int wait) {
516 if (sync->audioWait && wait) {
517 // TODO loop properly in event of spurious wakeups
518 ConditionWait(&sync->audioRequiredCond, &sync->audioBufferMutex);
519 }
520 MutexUnlock(&sync->audioBufferMutex);
521}
522
523void GBASyncLockAudio(struct GBASync* sync) {
524 MutexLock(&sync->audioBufferMutex);
525}
526
527void GBASyncConsumeAudio(struct GBASync* sync) {
528 ConditionWake(&sync->audioRequiredCond);
529 MutexUnlock(&sync->audioBufferMutex);
530}