src/gba/supervisor/thread.h (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef GBA_THREAD_H
7#define GBA_THREAD_H
8
9#include "util/common.h"
10
11#include "gba/gba.h"
12#include "gba/input.h"
13#include "gba/supervisor/overrides.h"
14
15#include "util/threading.h"
16
17struct GBAThread;
18struct GBAArguments;
19struct GBACheatSet;
20struct GBAOptions;
21
22typedef void (*ThreadCallback)(struct GBAThread* threadContext);
23typedef void (*LogHandler)(struct GBAThread*, enum GBALogLevel, const char* format, va_list args);
24
25enum ThreadState {
26 THREAD_INITIALIZED = -1,
27 THREAD_RUNNING = 0,
28 THREAD_INTERRUPTED,
29 THREAD_INTERRUPTING,
30 THREAD_PAUSED,
31 THREAD_PAUSING,
32 THREAD_RESETING,
33 THREAD_EXITING,
34 THREAD_SHUTDOWN,
35 THREAD_CRASHED
36};
37
38struct GBASync {
39 int videoFramePending;
40 bool videoFrameWait;
41 int videoFrameSkip;
42 bool videoFrameOn;
43 Mutex videoFrameMutex;
44 Condition videoFrameAvailableCond;
45 Condition videoFrameRequiredCond;
46
47 bool audioWait;
48 Condition audioRequiredCond;
49 Mutex audioBufferMutex;
50};
51
52struct GBAAVStream {
53 void (*postVideoFrame)(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
54 void (*postAudioFrame)(struct GBAAVStream*, int32_t left, int32_t right);
55};
56
57struct GBAThread {
58 // Output
59 enum ThreadState state;
60 struct GBA* gba;
61 struct ARMCore* cpu;
62
63 // Input
64 struct GBAVideoRenderer* renderer;
65 struct GBASIODriverSet sioDrivers;
66 struct ARMDebugger* debugger;
67 struct VDir* gameDir;
68 struct VDir* stateDir;
69 struct VFile* rom;
70 struct VFile* save;
71 struct VFile* bios;
72 struct VFile* patch;
73 struct VFile* cheatsFile;
74 const char* fname;
75 int activeKeys;
76 struct GBAAVStream* stream;
77 struct Configuration* overrides;
78 enum GBAIdleLoopOptimization idleOptimization;
79
80 bool hasOverride;
81 struct GBACartridgeOverride override;
82
83 // Run-time options
84 int frameskip;
85 float fpsTarget;
86 size_t audioBuffers;
87 bool skipBios;
88
89 // Threading state
90 Thread thread;
91
92 Mutex stateMutex;
93 Condition stateCond;
94 enum ThreadState savedState;
95 int interruptDepth;
96
97 LogHandler logHandler;
98 int logLevel;
99 ThreadCallback startCallback;
100 ThreadCallback cleanCallback;
101 ThreadCallback frameCallback;
102 void* userData;
103
104 struct GBASync sync;
105
106 int rewindBufferSize;
107 int rewindBufferCapacity;
108 int rewindBufferInterval;
109 int rewindBufferNext;
110 struct GBASerializedState** rewindBuffer;
111 int rewindBufferWriteOffset;
112
113 struct GBACheatSet* cheats;
114};
115
116void GBAMapOptionsToContext(const struct GBAOptions*, struct GBAThread*);
117void GBAMapArgumentsToContext(const struct GBAArguments*, struct GBAThread*);
118
119bool GBAThreadStart(struct GBAThread* threadContext);
120bool GBAThreadHasStarted(struct GBAThread* threadContext);
121bool GBAThreadHasExited(struct GBAThread* threadContext);
122bool GBAThreadHasCrashed(struct GBAThread* threadContext);
123void GBAThreadEnd(struct GBAThread* threadContext);
124void GBAThreadReset(struct GBAThread* threadContext);
125void GBAThreadJoin(struct GBAThread* threadContext);
126
127bool GBAThreadIsActive(struct GBAThread* threadContext);
128void GBAThreadInterrupt(struct GBAThread* threadContext);
129void GBAThreadContinue(struct GBAThread* threadContext);
130
131void GBAThreadPause(struct GBAThread* threadContext);
132void GBAThreadUnpause(struct GBAThread* threadContext);
133bool GBAThreadIsPaused(struct GBAThread* threadContext);
134void GBAThreadTogglePause(struct GBAThread* threadContext);
135void GBAThreadPauseFromThread(struct GBAThread* threadContext);
136struct GBAThread* GBAThreadGetContext(void);
137
138#ifdef USE_PNG
139void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
140#endif
141
142void GBASyncPostFrame(struct GBASync* sync);
143bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
144void GBASyncWaitFrameEnd(struct GBASync* sync);
145bool GBASyncDrawingFrame(struct GBASync* sync);
146
147void GBASyncSuspendDrawing(struct GBASync* sync);
148void GBASyncResumeDrawing(struct GBASync* sync);
149
150void GBASyncProduceAudio(struct GBASync* sync, bool wait);
151void GBASyncLockAudio(struct GBASync* sync);
152void GBASyncUnlockAudio(struct GBASync* sync);
153void GBASyncConsumeAudio(struct GBASync* sync);
154
155#endif