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 bool frameWasOn;
95
96 GBALogHandler logHandler;
97 int logLevel;
98 ThreadCallback startCallback;
99 ThreadCallback cleanCallback;
100 ThreadCallback frameCallback;
101 void* userData;
102 void (*run)(struct GBAThread*);
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 uint8_t* rewindScreenBuffer;
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 GBARunOnThread(struct GBAThread* threadContext, void (*run)(struct GBAThread*));
133
134void GBAThreadPause(struct GBAThread* threadContext);
135void GBAThreadUnpause(struct GBAThread* threadContext);
136bool GBAThreadIsPaused(struct GBAThread* threadContext);
137void GBAThreadTogglePause(struct GBAThread* threadContext);
138void GBAThreadPauseFromThread(struct GBAThread* threadContext);
139struct GBAThread* GBAThreadGetContext(void);
140
141#ifdef USE_PNG
142void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
143#endif
144
145void GBASyncPostFrame(struct GBASync* sync);
146void GBASyncForceFrame(struct GBASync* sync);
147bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
148void GBASyncWaitFrameEnd(struct GBASync* sync);
149bool GBASyncDrawingFrame(struct GBASync* sync);
150void GBASyncSetVideoSync(struct GBASync* sync, bool wait);
151
152void GBASyncProduceAudio(struct GBASync* sync, bool wait);
153void GBASyncLockAudio(struct GBASync* sync);
154void GBASyncUnlockAudio(struct GBASync* sync);
155void GBASyncConsumeAudio(struct GBASync* sync);
156
157#endif