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
11struct GBAThread;
12struct GBAArguments;
13struct GBAOptions;
14typedef void (*ThreadCallback)(struct GBAThread* threadContext);
15typedef void (*LogHandler)(struct GBAThread*, enum GBALogLevel, const char* format, va_list args);
16
17enum ThreadState {
18 THREAD_INITIALIZED = -1,
19 THREAD_RUNNING = 0,
20 THREAD_INTERRUPTED,
21 THREAD_INTERRUPTING,
22 THREAD_PAUSED,
23 THREAD_PAUSING,
24 THREAD_RESETING,
25 THREAD_EXITING,
26 THREAD_SHUTDOWN
27};
28
29struct GBASync {
30 int videoFramePending;
31 bool videoFrameWait;
32 int videoFrameSkip;
33 bool videoFrameOn;
34 Mutex videoFrameMutex;
35 Condition videoFrameAvailableCond;
36 Condition videoFrameRequiredCond;
37
38 bool audioWait;
39 Condition audioRequiredCond;
40 Mutex audioBufferMutex;
41};
42
43struct GBAAVStream {
44 void (*postVideoFrame)(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
45 void (*postAudioFrame)(struct GBAAVStream*, int32_t left, int32_t right);
46};
47
48struct GBAThread {
49 // Output
50 enum ThreadState state;
51 struct GBA* gba;
52 struct ARMCore* cpu;
53
54 // Input
55 struct GBAVideoRenderer* renderer;
56 struct GBASIODriverSet sioDrivers;
57 struct ARMDebugger* debugger;
58 struct VDir* gameDir;
59 struct VDir* stateDir;
60 struct VFile* rom;
61 struct VFile* save;
62 struct VFile* bios;
63 struct VFile* patch;
64 const char* fname;
65 int activeKeys;
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 int interruptDepth;
80
81 LogHandler logHandler;
82 int logLevel;
83 ThreadCallback startCallback;
84 ThreadCallback cleanCallback;
85 ThreadCallback frameCallback;
86 void* userData;
87
88 struct GBASync sync;
89
90 int rewindBufferSize;
91 int rewindBufferCapacity;
92 int rewindBufferInterval;
93 int rewindBufferNext;
94 struct GBASerializedState** rewindBuffer;
95 int rewindBufferWriteOffset;
96};
97
98void GBAMapOptionsToContext(const struct GBAOptions*, struct GBAThread*);
99void GBAMapArgumentsToContext(const struct GBAArguments*, struct GBAThread*);
100
101bool GBAThreadStart(struct GBAThread* threadContext);
102bool GBAThreadHasStarted(struct GBAThread* threadContext);
103void GBAThreadEnd(struct GBAThread* threadContext);
104void GBAThreadReset(struct GBAThread* threadContext);
105void GBAThreadJoin(struct GBAThread* threadContext);
106
107void GBAThreadInterrupt(struct GBAThread* threadContext);
108void GBAThreadContinue(struct GBAThread* threadContext);
109
110void GBAThreadPause(struct GBAThread* threadContext);
111void GBAThreadUnpause(struct GBAThread* threadContext);
112bool GBAThreadIsPaused(struct GBAThread* threadContext);
113void GBAThreadTogglePause(struct GBAThread* threadContext);
114void GBAThreadPauseFromThread(struct GBAThread* threadContext);
115struct GBAThread* GBAThreadGetContext(void);
116
117#ifdef USE_PNG
118void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
119#endif
120
121void GBASyncPostFrame(struct GBASync* sync);
122bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
123void GBASyncWaitFrameEnd(struct GBASync* sync);
124bool GBASyncDrawingFrame(struct GBASync* sync);
125
126void GBASyncSuspendDrawing(struct GBASync* sync);
127void GBASyncResumeDrawing(struct GBASync* sync);
128
129void GBASyncProduceAudio(struct GBASync* sync, bool wait);
130void GBASyncLockAudio(struct GBASync* sync);
131void GBASyncUnlockAudio(struct GBASync* sync);
132void GBASyncConsumeAudio(struct GBASync* sync);
133
134#endif