src/gba/gba-thread.h (view raw)
1#ifndef GBA_THREAD_H
2#define GBA_THREAD_H
3
4#include "util/common.h"
5
6#include "gba.h"
7#include "gba-input.h"
8
9#include "util/threading.h"
10#include "platform/commandline.h"
11
12struct GBAThread;
13typedef void (*ThreadCallback)(struct GBAThread* threadContext);
14typedef void (*LogHandler)(struct GBAThread*, enum GBALogLevel, const char* format, va_list args);
15
16enum ThreadState {
17 THREAD_INITIALIZED = -1,
18 THREAD_RUNNING = 0,
19 THREAD_INTERRUPTED,
20 THREAD_INTERRUPTING,
21 THREAD_PAUSED,
22 THREAD_PAUSING,
23 THREAD_RESETING,
24 THREAD_EXITING,
25 THREAD_SHUTDOWN
26};
27
28struct GBASync {
29 int videoFramePending;
30 bool videoFrameWait;
31 int videoFrameSkip;
32 bool videoFrameOn;
33 Mutex videoFrameMutex;
34 Condition videoFrameAvailableCond;
35 Condition videoFrameRequiredCond;
36
37 bool audioWait;
38 Condition audioRequiredCond;
39 Mutex audioBufferMutex;
40};
41
42struct GBAAVStream {
43 void (*postVideoFrame)(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
44 void (*postAudioFrame)(struct GBAAVStream*, int32_t left, int32_t right);
45};
46
47struct GBAThread {
48 // Output
49 enum ThreadState state;
50 struct GBA* gba;
51 struct ARMCore* cpu;
52
53 // Input
54 struct GBAVideoRenderer* renderer;
55 struct GBASIODriverSet sioDrivers;
56 struct ARMDebugger* debugger;
57 struct VDir* gameDir;
58 struct VDir* stateDir;
59 struct VFile* rom;
60 struct VFile* save;
61 struct VFile* bios;
62 struct VFile* patch;
63 const char* fname;
64 int activeKeys;
65 struct GBAInputMap inputMap;
66 struct GBAAVStream* stream;
67
68 // Run-time options
69 int frameskip;
70 float fpsTarget;
71 size_t audioBuffers;
72
73 // Threading state
74 Thread thread;
75
76 Mutex stateMutex;
77 Condition stateCond;
78 enum ThreadState savedState;
79
80 LogHandler logHandler;
81 int logLevel;
82 ThreadCallback startCallback;
83 ThreadCallback cleanCallback;
84 ThreadCallback frameCallback;
85 void* userData;
86
87 struct GBASync sync;
88
89 int rewindBufferSize;
90 int rewindBufferCapacity;
91 int rewindBufferInterval;
92 int rewindBufferNext;
93 struct GBASerializedState** rewindBuffer;
94 int rewindBufferWriteOffset;
95};
96
97void GBAMapOptionsToContext(struct StartupOptions*, struct GBAThread*);
98
99bool GBAThreadStart(struct GBAThread* threadContext);
100bool GBAThreadHasStarted(struct GBAThread* threadContext);
101void GBAThreadEnd(struct GBAThread* threadContext);
102void GBAThreadReset(struct GBAThread* threadContext);
103void GBAThreadJoin(struct GBAThread* threadContext);
104
105void GBAThreadInterrupt(struct GBAThread* threadContext);
106void GBAThreadContinue(struct GBAThread* threadContext);
107
108void GBAThreadPause(struct GBAThread* threadContext);
109void GBAThreadUnpause(struct GBAThread* threadContext);
110bool GBAThreadIsPaused(struct GBAThread* threadContext);
111void GBAThreadTogglePause(struct GBAThread* threadContext);
112void GBAThreadPauseFromThread(struct GBAThread* threadContext);
113struct GBAThread* GBAThreadGetContext(void);
114
115#ifdef USE_PNG
116void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
117#endif
118
119void GBASyncPostFrame(struct GBASync* sync);
120bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
121void GBASyncWaitFrameEnd(struct GBASync* sync);
122bool GBASyncDrawingFrame(struct GBASync* sync);
123
124void GBASyncSuspendDrawing(struct GBASync* sync);
125void GBASyncResumeDrawing(struct GBASync* sync);
126
127void GBASyncProduceAudio(struct GBASync* sync, bool wait);
128void GBASyncLockAudio(struct GBASync* sync);
129void GBASyncUnlockAudio(struct GBASync* sync);
130void GBASyncConsumeAudio(struct GBASync* sync);
131
132#endif