src/gba/gba-thread.h (view raw)
1/* Copyright (c) 2013-2014 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.h"
12#include "gba-input.h"
13
14#include "util/threading.h"
15
16struct GBAThread;
17struct GBAArguments;
18struct GBAOptions;
19typedef void (*ThreadCallback)(struct GBAThread* threadContext);
20typedef void (*LogHandler)(struct GBAThread*, enum GBALogLevel, const char* format, va_list args);
21
22enum ThreadState {
23 THREAD_INITIALIZED = -1,
24 THREAD_RUNNING = 0,
25 THREAD_INTERRUPTED,
26 THREAD_INTERRUPTING,
27 THREAD_PAUSED,
28 THREAD_PAUSING,
29 THREAD_RESETING,
30 THREAD_EXITING,
31 THREAD_SHUTDOWN,
32 THREAD_CRASHED
33};
34
35struct GBASync {
36 int videoFramePending;
37 bool videoFrameWait;
38 int videoFrameSkip;
39 bool videoFrameOn;
40 Mutex videoFrameMutex;
41 Condition videoFrameAvailableCond;
42 Condition videoFrameRequiredCond;
43
44 bool audioWait;
45 Condition audioRequiredCond;
46 Mutex audioBufferMutex;
47};
48
49struct GBAAVStream {
50 void (*postVideoFrame)(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
51 void (*postAudioFrame)(struct GBAAVStream*, int32_t left, int32_t right);
52};
53
54struct GBAThread {
55 // Output
56 enum ThreadState state;
57 struct GBA* gba;
58 struct ARMCore* cpu;
59
60 // Input
61 struct GBAVideoRenderer* renderer;
62 struct GBASIODriverSet sioDrivers;
63 struct ARMDebugger* debugger;
64 struct VDir* gameDir;
65 struct VDir* stateDir;
66 struct VFile* rom;
67 struct VFile* save;
68 struct VFile* bios;
69 struct VFile* patch;
70 const char* fname;
71 int activeKeys;
72 struct GBAAVStream* stream;
73 struct Configuration* overrides;
74
75 // Run-time options
76 int frameskip;
77 float fpsTarget;
78 size_t audioBuffers;
79
80 // Threading state
81 Thread thread;
82
83 Mutex stateMutex;
84 Condition stateCond;
85 enum ThreadState savedState;
86 int interruptDepth;
87
88 LogHandler logHandler;
89 int logLevel;
90 ThreadCallback startCallback;
91 ThreadCallback cleanCallback;
92 ThreadCallback frameCallback;
93 void* userData;
94
95 struct GBASync sync;
96
97 int rewindBufferSize;
98 int rewindBufferCapacity;
99 int rewindBufferInterval;
100 int rewindBufferNext;
101 struct GBASerializedState** rewindBuffer;
102 int rewindBufferWriteOffset;
103
104 bool skipBios;
105};
106
107void GBAMapOptionsToContext(const struct GBAOptions*, struct GBAThread*);
108void GBAMapArgumentsToContext(const struct GBAArguments*, struct GBAThread*);
109
110bool GBAThreadStart(struct GBAThread* threadContext);
111bool GBAThreadHasStarted(struct GBAThread* threadContext);
112bool GBAThreadHasExited(struct GBAThread* threadContext);
113bool GBAThreadHasCrashed(struct GBAThread* threadContext);
114void GBAThreadEnd(struct GBAThread* threadContext);
115void GBAThreadReset(struct GBAThread* threadContext);
116void GBAThreadJoin(struct GBAThread* threadContext);
117
118bool GBAThreadIsActive(struct GBAThread* threadContext);
119void GBAThreadInterrupt(struct GBAThread* threadContext);
120void GBAThreadContinue(struct GBAThread* threadContext);
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
133void GBASyncPostFrame(struct GBASync* sync);
134bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
135void GBASyncWaitFrameEnd(struct GBASync* sync);
136bool GBASyncDrawingFrame(struct GBASync* sync);
137
138void GBASyncSuspendDrawing(struct GBASync* sync);
139void GBASyncResumeDrawing(struct GBASync* sync);
140
141void GBASyncProduceAudio(struct GBASync* sync, bool wait);
142void GBASyncLockAudio(struct GBASync* sync);
143void GBASyncUnlockAudio(struct GBASync* sync);
144void GBASyncConsumeAudio(struct GBASync* sync);
145
146#endif