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};
33
34struct GBASync {
35 int videoFramePending;
36 bool videoFrameWait;
37 int videoFrameSkip;
38 bool videoFrameOn;
39 Mutex videoFrameMutex;
40 Condition videoFrameAvailableCond;
41 Condition videoFrameRequiredCond;
42
43 bool audioWait;
44 Condition audioRequiredCond;
45 Mutex audioBufferMutex;
46};
47
48struct GBAAVStream {
49 void (*postVideoFrame)(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
50 void (*postAudioFrame)(struct GBAAVStream*, int32_t left, int32_t right);
51};
52
53struct GBAThread {
54 // Output
55 enum ThreadState state;
56 struct GBA* gba;
57 struct ARMCore* cpu;
58
59 // Input
60 struct GBAVideoRenderer* renderer;
61 struct GBASIODriverSet sioDrivers;
62 struct ARMDebugger* debugger;
63 struct VDir* gameDir;
64 struct VDir* stateDir;
65 struct VFile* rom;
66 struct VFile* save;
67 struct VFile* bios;
68 struct VFile* patch;
69 const char* fname;
70 int activeKeys;
71 struct GBAAVStream* stream;
72
73 // Run-time options
74 int frameskip;
75 float fpsTarget;
76 size_t audioBuffers;
77
78 // Threading state
79 Thread thread;
80
81 Mutex stateMutex;
82 Condition stateCond;
83 enum ThreadState savedState;
84 int interruptDepth;
85
86 LogHandler logHandler;
87 int logLevel;
88 ThreadCallback startCallback;
89 ThreadCallback cleanCallback;
90 ThreadCallback frameCallback;
91 void* userData;
92
93 struct GBASync sync;
94
95 int rewindBufferSize;
96 int rewindBufferCapacity;
97 int rewindBufferInterval;
98 int rewindBufferNext;
99 struct GBASerializedState** rewindBuffer;
100 int rewindBufferWriteOffset;
101
102 bool skipBios;
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);
110void GBAThreadEnd(struct GBAThread* threadContext);
111void GBAThreadReset(struct GBAThread* threadContext);
112void GBAThreadJoin(struct GBAThread* threadContext);
113
114bool GBAThreadIsActive(struct GBAThread* threadContext);
115void GBAThreadInterrupt(struct GBAThread* threadContext);
116void GBAThreadContinue(struct GBAThread* threadContext);
117
118void GBAThreadPause(struct GBAThread* threadContext);
119void GBAThreadUnpause(struct GBAThread* threadContext);
120bool GBAThreadIsPaused(struct GBAThread* threadContext);
121void GBAThreadTogglePause(struct GBAThread* threadContext);
122void GBAThreadPauseFromThread(struct GBAThread* threadContext);
123struct GBAThread* GBAThreadGetContext(void);
124
125#ifdef USE_PNG
126void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
127#endif
128
129void GBASyncPostFrame(struct GBASync* sync);
130bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
131void GBASyncWaitFrameEnd(struct GBASync* sync);
132bool GBASyncDrawingFrame(struct GBASync* sync);
133
134void GBASyncSuspendDrawing(struct GBASync* sync);
135void GBASyncResumeDrawing(struct GBASync* sync);
136
137void GBASyncProduceAudio(struct GBASync* sync, bool wait);
138void GBASyncLockAudio(struct GBASync* sync);
139void GBASyncUnlockAudio(struct GBASync* sync);
140void GBASyncConsumeAudio(struct GBASync* sync);
141
142#endif