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#include "gba/supervisor/sync.h"
15
16#include "util/threading.h"
17
18struct GBAThread;
19struct GBAArguments;
20struct GBACheatSet;
21struct GBAOptions;
22
23typedef void (*ThreadCallback)(struct GBAThread* threadContext);
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_RUN_ON,
33 THREAD_RESETING,
34 THREAD_EXITING,
35 THREAD_SHUTDOWN,
36 THREAD_CRASHED
37};
38
39struct GBAThread {
40 // Output
41 enum ThreadState state;
42 struct GBA* gba;
43 struct ARMCore* cpu;
44
45 // Input
46 struct GBAVideoRenderer* renderer;
47 struct GBASIODriverSet sioDrivers;
48 struct ARMDebugger* debugger;
49 struct VDir* gameDir;
50 struct VDir* stateDir;
51 struct VFile* rom;
52 struct VFile* save;
53 struct VFile* bios;
54 struct VFile* patch;
55 struct VFile* cheatsFile;
56 const char* fname;
57 const char* movie;
58 int activeKeys;
59 struct GBAAVStream* stream;
60 struct Configuration* overrides;
61 enum GBAIdleLoopOptimization idleOptimization;
62 bool bootBios;
63
64 bool hasOverride;
65 struct GBACartridgeOverride override;
66
67 // Run-time options
68 int frameskip;
69 float fpsTarget;
70 size_t audioBuffers;
71 bool skipBios;
72 int volume;
73 bool mute;
74
75 // Threading state
76 Thread thread;
77
78 Mutex stateMutex;
79 Condition stateCond;
80 enum ThreadState savedState;
81 int interruptDepth;
82 bool frameWasOn;
83
84 GBALogHandler logHandler;
85 int logLevel;
86 ThreadCallback startCallback;
87 ThreadCallback cleanCallback;
88 ThreadCallback frameCallback;
89 void* userData;
90 void (*run)(struct GBAThread*);
91
92 struct GBASync sync;
93
94 int rewindBufferSize;
95 int rewindBufferCapacity;
96 int rewindBufferInterval;
97 int rewindBufferNext;
98 struct GBASerializedState** rewindBuffer;
99 int rewindBufferWriteOffset;
100 uint8_t* rewindScreenBuffer;
101
102 struct GBACheatDevice* cheats;
103};
104
105void GBAMapOptionsToContext(const struct GBAOptions*, struct GBAThread*);
106void GBAMapArgumentsToContext(const struct GBAArguments*, struct GBAThread*);
107
108bool GBAThreadStart(struct GBAThread* threadContext);
109bool GBAThreadHasStarted(struct GBAThread* threadContext);
110bool GBAThreadHasExited(struct GBAThread* threadContext);
111bool GBAThreadHasCrashed(struct GBAThread* threadContext);
112void GBAThreadEnd(struct GBAThread* threadContext);
113void GBAThreadReset(struct GBAThread* threadContext);
114void GBAThreadJoin(struct GBAThread* threadContext);
115
116bool GBAThreadIsActive(struct GBAThread* threadContext);
117void GBAThreadInterrupt(struct GBAThread* threadContext);
118void GBAThreadContinue(struct GBAThread* threadContext);
119
120void GBARunOnThread(struct GBAThread* threadContext, void (*run)(struct GBAThread*));
121
122void GBAThreadPause(struct GBAThread* threadContext);
123void GBAThreadUnpause(struct GBAThread* threadContext);
124bool GBAThreadIsPaused(struct GBAThread* threadContext);
125void GBAThreadTogglePause(struct GBAThread* threadContext);
126void GBAThreadPauseFromThread(struct GBAThread* threadContext);
127struct GBAThread* GBAThreadGetContext(void);
128
129#ifdef USE_PNG
130void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
131#endif
132
133#endif