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