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 bool bootBios;
76
77 bool hasOverride;
78 struct GBACartridgeOverride override;
79
80 // Run-time options
81 int frameskip;
82 float fpsTarget;
83 size_t audioBuffers;
84 bool skipBios;
85 int volume;
86 bool mute;
87
88 // Threading state
89 Thread thread;
90
91 Mutex stateMutex;
92 Condition stateCond;
93 enum ThreadState savedState;
94 int interruptDepth;
95 bool frameWasOn;
96
97 GBALogHandler logHandler;
98 int logLevel;
99 ThreadCallback startCallback;
100 ThreadCallback cleanCallback;
101 ThreadCallback frameCallback;
102 void* userData;
103 void (*run)(struct GBAThread*);
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 uint8_t* rewindScreenBuffer;
114
115 struct GBACheatDevice* cheats;
116};
117
118void GBAMapOptionsToContext(const struct GBAOptions*, struct GBAThread*);
119void GBAMapArgumentsToContext(const struct GBAArguments*, struct GBAThread*);
120
121bool GBAThreadStart(struct GBAThread* threadContext);
122bool GBAThreadHasStarted(struct GBAThread* threadContext);
123bool GBAThreadHasExited(struct GBAThread* threadContext);
124bool GBAThreadHasCrashed(struct GBAThread* threadContext);
125void GBAThreadEnd(struct GBAThread* threadContext);
126void GBAThreadReset(struct GBAThread* threadContext);
127void GBAThreadJoin(struct GBAThread* threadContext);
128
129bool GBAThreadIsActive(struct GBAThread* threadContext);
130void GBAThreadInterrupt(struct GBAThread* threadContext);
131void GBAThreadContinue(struct GBAThread* threadContext);
132
133void GBARunOnThread(struct GBAThread* threadContext, void (*run)(struct GBAThread*));
134
135void GBAThreadPause(struct GBAThread* threadContext);
136void GBAThreadUnpause(struct GBAThread* threadContext);
137bool GBAThreadIsPaused(struct GBAThread* threadContext);
138void GBAThreadTogglePause(struct GBAThread* threadContext);
139void GBAThreadPauseFromThread(struct GBAThread* threadContext);
140struct GBAThread* GBAThreadGetContext(void);
141
142#ifdef USE_PNG
143void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
144#endif
145
146void GBASyncPostFrame(struct GBASync* sync);
147void GBASyncForceFrame(struct GBASync* sync);
148bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
149void GBASyncWaitFrameEnd(struct GBASync* sync);
150bool GBASyncDrawingFrame(struct GBASync* sync);
151void GBASyncSetVideoSync(struct GBASync* sync, bool wait);
152
153void GBASyncProduceAudio(struct GBASync* sync, bool wait);
154void GBASyncLockAudio(struct GBASync* sync);
155void GBASyncUnlockAudio(struct GBASync* sync);
156void GBASyncConsumeAudio(struct GBASync* sync);
157
158#endif