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 GBAAVStream {
52 void (*postVideoFrame)(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
53 void (*postAudioFrame)(struct GBAAVStream*, int32_t left, int32_t right);
54};
55
56struct GBAThread {
57 // Output
58 enum ThreadState state;
59 struct GBA* gba;
60 struct ARMCore* cpu;
61
62 // Input
63 struct GBAVideoRenderer* renderer;
64 struct GBASIODriverSet sioDrivers;
65 struct ARMDebugger* debugger;
66 struct VDir* gameDir;
67 struct VDir* stateDir;
68 struct VFile* rom;
69 struct VFile* save;
70 struct VFile* bios;
71 struct VFile* patch;
72 struct VFile* cheatsFile;
73 const char* fname;
74 const char* movie;
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 GBALogHandler 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 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 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