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);
23
24enum ThreadState {
25 THREAD_INITIALIZED = -1,
26 THREAD_RUNNING = 0,
27 THREAD_INTERRUPTED,
28 THREAD_INTERRUPTING,
29 THREAD_PAUSED,
30 THREAD_PAUSING,
31 THREAD_RESETING,
32 THREAD_EXITING,
33 THREAD_SHUTDOWN,
34 THREAD_CRASHED
35};
36
37struct GBASync {
38 int videoFramePending;
39 bool videoFrameWait;
40 int videoFrameSkip;
41 bool videoFrameOn;
42 Mutex videoFrameMutex;
43 Condition videoFrameAvailableCond;
44 Condition videoFrameRequiredCond;
45
46 bool audioWait;
47 Condition audioRequiredCond;
48 Mutex audioBufferMutex;
49};
50
51struct GBAThread {
52 // Output
53 enum ThreadState state;
54 struct GBA* gba;
55 struct ARMCore* cpu;
56
57 // Input
58 struct GBAVideoRenderer* renderer;
59 struct GBASIODriverSet sioDrivers;
60 struct ARMDebugger* debugger;
61 struct VDir* gameDir;
62 struct VDir* stateDir;
63 struct VFile* rom;
64 struct VFile* save;
65 struct VFile* bios;
66 struct VFile* patch;
67 struct VFile* cheatsFile;
68 const char* fname;
69 const char* movie;
70 int activeKeys;
71 struct GBAAVStream* stream;
72 struct Configuration* overrides;
73 enum GBAIdleLoopOptimization idleOptimization;
74
75 bool hasOverride;
76 struct GBACartridgeOverride override;
77
78 // Run-time options
79 int frameskip;
80 float fpsTarget;
81 size_t audioBuffers;
82 bool skipBios;
83 int volume;
84 bool mute;
85
86 // Threading state
87 Thread thread;
88
89 Mutex stateMutex;
90 Condition stateCond;
91 enum ThreadState savedState;
92 int interruptDepth;
93
94 GBALogHandler logHandler;
95 int logLevel;
96 ThreadCallback startCallback;
97 ThreadCallback cleanCallback;
98 ThreadCallback frameCallback;
99 void* userData;
100
101 struct GBASync sync;
102
103 int rewindBufferSize;
104 int rewindBufferCapacity;
105 int rewindBufferInterval;
106 int rewindBufferNext;
107 struct GBASerializedState** rewindBuffer;
108 int rewindBufferWriteOffset;
109 uint8_t* rewindScreenBuffer;
110
111 struct GBACheatDevice* cheats;
112};
113
114void GBAMapOptionsToContext(const struct GBAOptions*, struct GBAThread*);
115void GBAMapArgumentsToContext(const struct GBAArguments*, struct GBAThread*);
116
117bool GBAThreadStart(struct GBAThread* threadContext);
118bool GBAThreadHasStarted(struct GBAThread* threadContext);
119bool GBAThreadHasExited(struct GBAThread* threadContext);
120bool GBAThreadHasCrashed(struct GBAThread* threadContext);
121void GBAThreadEnd(struct GBAThread* threadContext);
122void GBAThreadReset(struct GBAThread* threadContext);
123void GBAThreadJoin(struct GBAThread* threadContext);
124
125bool GBAThreadIsActive(struct GBAThread* threadContext);
126void GBAThreadInterrupt(struct GBAThread* threadContext);
127void GBAThreadContinue(struct GBAThread* threadContext);
128
129void GBAThreadPause(struct GBAThread* threadContext);
130void GBAThreadUnpause(struct GBAThread* threadContext);
131bool GBAThreadIsPaused(struct GBAThread* threadContext);
132void GBAThreadTogglePause(struct GBAThread* threadContext);
133void GBAThreadPauseFromThread(struct GBAThread* threadContext);
134struct GBAThread* GBAThreadGetContext(void);
135
136#ifdef USE_PNG
137void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
138#endif
139
140void GBASyncPostFrame(struct GBASync* sync);
141void GBASyncForceFrame(struct GBASync* sync);
142bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
143void GBASyncWaitFrameEnd(struct GBASync* sync);
144bool GBASyncDrawingFrame(struct GBASync* sync);
145
146void GBASyncSuspendDrawing(struct GBASync* sync);
147void GBASyncResumeDrawing(struct GBASync* sync);
148
149void GBASyncProduceAudio(struct GBASync* sync, bool wait);
150void GBASyncLockAudio(struct GBASync* sync);
151void GBASyncUnlockAudio(struct GBASync* sync);
152void GBASyncConsumeAudio(struct GBASync* sync);
153
154#endif