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