all repos — mgba @ d83520c5a246b8096ceb02ce001f4ee7291bba9f

mGBA Game Boy Advance Emulator

src/gba/gba-thread.h (view raw)

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