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 const char* movie;
76 int activeKeys;
77 struct GBAAVStream* stream;
78 struct Configuration* overrides;
79 enum GBAIdleLoopOptimization idleOptimization;
80
81 bool hasOverride;
82 struct GBACartridgeOverride override;
83
84 // Run-time options
85 int frameskip;
86 float fpsTarget;
87 size_t audioBuffers;
88 bool skipBios;
89
90 // Threading state
91 Thread thread;
92
93 Mutex stateMutex;
94 Condition stateCond;
95 enum ThreadState savedState;
96 int interruptDepth;
97
98 LogHandler logHandler;
99 int logLevel;
100 ThreadCallback startCallback;
101 ThreadCallback cleanCallback;
102 ThreadCallback frameCallback;
103 void* userData;
104
105 struct GBASync sync;
106
107 int rewindBufferSize;
108 int rewindBufferCapacity;
109 int rewindBufferInterval;
110 int rewindBufferNext;
111 struct GBASerializedState** rewindBuffer;
112 int rewindBufferWriteOffset;
113
114 struct GBACheatDevice* cheats;
115};
116
117void GBAMapOptionsToContext(const struct GBAOptions*, struct GBAThread*);
118void GBAMapArgumentsToContext(const struct GBAArguments*, struct GBAThread*);
119
120bool GBAThreadStart(struct GBAThread* threadContext);
121bool GBAThreadHasStarted(struct GBAThread* threadContext);
122bool GBAThreadHasExited(struct GBAThread* threadContext);
123bool GBAThreadHasCrashed(struct GBAThread* threadContext);
124void GBAThreadEnd(struct GBAThread* threadContext);
125void GBAThreadReset(struct GBAThread* threadContext);
126void GBAThreadJoin(struct GBAThread* threadContext);
127
128bool GBAThreadIsActive(struct GBAThread* threadContext);
129void GBAThreadInterrupt(struct GBAThread* threadContext);
130void GBAThreadContinue(struct GBAThread* threadContext);
131
132void GBAThreadPause(struct GBAThread* threadContext);
133void GBAThreadUnpause(struct GBAThread* threadContext);
134bool GBAThreadIsPaused(struct GBAThread* threadContext);
135void GBAThreadTogglePause(struct GBAThread* threadContext);
136void GBAThreadPauseFromThread(struct GBAThread* threadContext);
137struct GBAThread* GBAThreadGetContext(void);
138
139#ifdef USE_PNG
140void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
141#endif
142
143void GBASyncPostFrame(struct GBASync* sync);
144bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
145void GBASyncWaitFrameEnd(struct GBASync* sync);
146bool GBASyncDrawingFrame(struct GBASync* sync);
147
148void GBASyncSuspendDrawing(struct GBASync* sync);
149void GBASyncResumeDrawing(struct GBASync* sync);
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