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_RUN_ON,
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 GBAThread {
53 // Output
54 enum ThreadState state;
55 struct GBA* gba;
56 struct ARMCore* cpu;
57
58 // Input
59 struct GBAVideoRenderer* renderer;
60 struct GBASIODriverSet sioDrivers;
61 struct ARMDebugger* debugger;
62 struct VDir* gameDir;
63 struct VDir* stateDir;
64 struct VFile* rom;
65 struct VFile* save;
66 struct VFile* bios;
67 struct VFile* patch;
68 struct VFile* cheatsFile;
69 const char* fname;
70 const char* movie;
71 int activeKeys;
72 struct GBAAVStream* stream;
73 struct Configuration* overrides;
74 enum GBAIdleLoopOptimization idleOptimization;
75
76 bool hasOverride;
77 struct GBACartridgeOverride override;
78
79 // Run-time options
80 int frameskip;
81 float fpsTarget;
82 size_t audioBuffers;
83 bool skipBios;
84 int volume;
85 bool mute;
86
87 // Threading state
88 Thread thread;
89
90 Mutex stateMutex;
91 Condition stateCond;
92 enum ThreadState savedState;
93 int interruptDepth;
94
95 GBALogHandler logHandler;
96 int logLevel;
97 ThreadCallback startCallback;
98 ThreadCallback cleanCallback;
99 ThreadCallback frameCallback;
100 void* userData;
101 void (*run)(struct GBAThread*);
102
103 struct GBASync sync;
104
105 int rewindBufferSize;
106 int rewindBufferCapacity;
107 int rewindBufferInterval;
108 int rewindBufferNext;
109 struct GBASerializedState** rewindBuffer;
110 int rewindBufferWriteOffset;
111 uint8_t* rewindScreenBuffer;
112
113 struct GBACheatDevice* 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 GBARunOnThread(struct GBAThread* threadContext, void (*run)(struct GBAThread*));
132
133void GBAThreadPause(struct GBAThread* threadContext);
134void GBAThreadUnpause(struct GBAThread* threadContext);
135bool GBAThreadIsPaused(struct GBAThread* threadContext);
136void GBAThreadTogglePause(struct GBAThread* threadContext);
137void GBAThreadPauseFromThread(struct GBAThread* threadContext);
138struct GBAThread* GBAThreadGetContext(void);
139
140#ifdef USE_PNG
141void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
142#endif
143
144void GBASyncPostFrame(struct GBASync* sync);
145void GBASyncForceFrame(struct GBASync* sync);
146bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
147void GBASyncWaitFrameEnd(struct GBASync* sync);
148bool GBASyncDrawingFrame(struct GBASync* sync);
149void GBASyncSetVideoSync(struct GBASync* sync, bool wait);
150
151void GBASyncProduceAudio(struct GBASync* sync, bool wait);
152void GBASyncLockAudio(struct GBASync* sync);
153void GBASyncUnlockAudio(struct GBASync* sync);
154void GBASyncConsumeAudio(struct GBASync* sync);
155
156#endif