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