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