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
76 bool hasOverride;
77 struct GBACartridgeOverride override;
78
79 // Run-time options
80 int frameskip;
81 float fpsTarget;
82 size_t audioBuffers;
83
84 // Threading state
85 Thread thread;
86
87 Mutex stateMutex;
88 Condition stateCond;
89 enum ThreadState savedState;
90 int interruptDepth;
91
92 LogHandler logHandler;
93 int logLevel;
94 ThreadCallback startCallback;
95 ThreadCallback cleanCallback;
96 ThreadCallback frameCallback;
97 void* userData;
98
99 struct GBASync sync;
100
101 int rewindBufferSize;
102 int rewindBufferCapacity;
103 int rewindBufferInterval;
104 int rewindBufferNext;
105 struct GBASerializedState** rewindBuffer;
106 int rewindBufferWriteOffset;
107
108 bool skipBios;
109};
110
111void GBAMapOptionsToContext(const struct GBAOptions*, struct GBAThread*);
112void GBAMapArgumentsToContext(const struct GBAArguments*, struct GBAThread*);
113
114bool GBAThreadStart(struct GBAThread* threadContext);
115bool GBAThreadHasStarted(struct GBAThread* threadContext);
116bool GBAThreadHasExited(struct GBAThread* threadContext);
117bool GBAThreadHasCrashed(struct GBAThread* threadContext);
118void GBAThreadEnd(struct GBAThread* threadContext);
119void GBAThreadReset(struct GBAThread* threadContext);
120void GBAThreadJoin(struct GBAThread* threadContext);
121
122bool GBAThreadIsActive(struct GBAThread* threadContext);
123void GBAThreadInterrupt(struct GBAThread* threadContext);
124void GBAThreadContinue(struct GBAThread* threadContext);
125
126void GBAThreadPause(struct GBAThread* threadContext);
127void GBAThreadUnpause(struct GBAThread* threadContext);
128bool GBAThreadIsPaused(struct GBAThread* threadContext);
129void GBAThreadTogglePause(struct GBAThread* threadContext);
130void GBAThreadPauseFromThread(struct GBAThread* threadContext);
131struct GBAThread* GBAThreadGetContext(void);
132
133#ifdef USE_PNG
134void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
135#endif
136
137void GBASyncPostFrame(struct GBASync* sync);
138bool GBASyncWaitFrameStart(struct GBASync* sync, int frameskip);
139void GBASyncWaitFrameEnd(struct GBASync* sync);
140bool GBASyncDrawingFrame(struct GBASync* sync);
141
142void GBASyncSuspendDrawing(struct GBASync* sync);
143void GBASyncResumeDrawing(struct GBASync* sync);
144
145void GBASyncProduceAudio(struct GBASync* sync, bool wait);
146void GBASyncLockAudio(struct GBASync* sync);
147void GBASyncUnlockAudio(struct GBASync* sync);
148void GBASyncConsumeAudio(struct GBASync* sync);
149
150#endif