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 VFile* fd;
51 struct VFile* biosFd;
52 struct VFile* patchFd;
53 const char* fname;
54 int activeKeys;
55 int frameskip;
56
57 // Threading state
58 Thread thread;
59
60 Mutex stateMutex;
61 Condition stateCond;
62 enum ThreadState savedState;
63
64 GBALogHandler logHandler;
65 int logLevel;
66 ThreadCallback startCallback;
67 ThreadCallback cleanCallback;
68 ThreadCallback frameCallback;
69 void* userData;
70
71 struct GBASync sync;
72
73 int rewindBufferSize;
74 int rewindBufferCapacity;
75 int rewindBufferInterval;
76 int rewindBufferNext;
77 struct GBASerializedState** rewindBuffer;
78 int rewindBufferWriteOffset;
79};
80
81void GBAMapOptionsToContext(struct StartupOptions*, struct GBAThread*);
82
83bool GBAThreadStart(struct GBAThread* threadContext);
84bool GBAThreadHasStarted(struct GBAThread* threadContext);
85void GBAThreadEnd(struct GBAThread* threadContext);
86void GBAThreadReset(struct GBAThread* threadContext);
87void GBAThreadJoin(struct GBAThread* threadContext);
88
89void GBAThreadInterrupt(struct GBAThread* threadContext);
90void GBAThreadContinue(struct GBAThread* threadContext);
91
92void GBAThreadPause(struct GBAThread* threadContext);
93void GBAThreadUnpause(struct GBAThread* threadContext);
94bool GBAThreadIsPaused(struct GBAThread* threadContext);
95void GBAThreadTogglePause(struct GBAThread* threadContext);
96struct GBAThread* GBAThreadGetContext(void);
97
98void GBASyncPostFrame(struct GBASync* sync);
99bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
100void GBASyncWaitFrameEnd(struct GBASync* sync);
101bool GBASyncDrawingFrame(struct GBASync* sync);
102
103void GBASyncProduceAudio(struct GBASync* sync, int wait);
104void GBASyncLockAudio(struct GBASync* sync);
105void GBASyncConsumeAudio(struct GBASync* sync);
106
107#endif