all repos — mgba @ 7436a5afef25fcb9160aa1884cc31503bc739f2b

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2015 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/gba.h"
 12#include "gba/input.h"
 13#include "gba/context/directories.h"
 14#include "gba/context/overrides.h"
 15#include "gba/context/sync.h"
 16
 17#include "util/threading.h"
 18
 19struct GBAThread;
 20struct GBAArguments;
 21struct GBACheatSet;
 22struct GBAOptions;
 23
 24typedef void (*ThreadCallback)(struct GBAThread* threadContext);
 25typedef bool (*ThreadStopCallback)(struct GBAThread* threadContext);
 26
 27enum ThreadState {
 28	THREAD_INITIALIZED = -1,
 29	THREAD_RUNNING = 0,
 30	THREAD_INTERRUPTED,
 31	THREAD_INTERRUPTING,
 32	THREAD_PAUSED,
 33	THREAD_PAUSING,
 34	THREAD_RUN_ON,
 35	THREAD_RESETING,
 36	THREAD_EXITING,
 37	THREAD_SHUTDOWN,
 38	THREAD_CRASHED
 39};
 40
 41struct GBAThread {
 42	// Output
 43	enum ThreadState state;
 44	struct GBA* gba;
 45	struct ARMCore* cpu;
 46
 47	// Input
 48	struct GBAVideoRenderer* renderer;
 49	struct GBASIODriverSet sioDrivers;
 50	struct ARMDebugger* debugger;
 51	struct GBADirectorySet dirs;
 52	struct VFile* rom;
 53	struct VFile* save;
 54	struct VFile* bios;
 55	struct VFile* patch;
 56	struct VFile* cheatsFile;
 57	const char* fname;
 58	const char* movie;
 59	int activeKeys;
 60	struct GBAAVStream* stream;
 61	struct Configuration* overrides;
 62	enum GBAIdleLoopOptimization idleOptimization;
 63	bool bootBios;
 64
 65	bool hasOverride;
 66	struct GBACartridgeOverride override;
 67
 68	// Run-time options
 69	int frameskip;
 70	float fpsTarget;
 71	size_t audioBuffers;
 72	bool skipBios;
 73	int volume;
 74	bool mute;
 75
 76	// Threading state
 77	Thread thread;
 78
 79	Mutex stateMutex;
 80	Condition stateCond;
 81	enum ThreadState savedState;
 82	int interruptDepth;
 83	bool frameWasOn;
 84
 85	GBALogHandler logHandler;
 86	int logLevel;
 87	ThreadCallback startCallback;
 88	ThreadCallback cleanCallback;
 89	ThreadCallback frameCallback;
 90	ThreadStopCallback stopCallback;
 91	void* userData;
 92	void (*run)(struct GBAThread*);
 93
 94	struct GBASync sync;
 95
 96	int rewindBufferSize;
 97	int rewindBufferCapacity;
 98	int rewindBufferInterval;
 99	int rewindBufferNext;
100	struct GBASerializedState** rewindBuffer;
101	int rewindBufferWriteOffset;
102	uint8_t* rewindScreenBuffer;
103
104	struct GBACheatDevice* cheats;
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 GBARunOnThread(struct GBAThread* threadContext, void (*run)(struct GBAThread*));
123
124void GBAThreadPause(struct GBAThread* threadContext);
125void GBAThreadUnpause(struct GBAThread* threadContext);
126bool GBAThreadIsPaused(struct GBAThread* threadContext);
127void GBAThreadTogglePause(struct GBAThread* threadContext);
128void GBAThreadPauseFromThread(struct GBAThread* threadContext);
129struct GBAThread* GBAThreadGetContext(void);
130
131void GBAThreadLoadROM(struct GBAThread* threadContext, const char* fname);
132void GBAThreadReplaceROM(struct GBAThread* threadContext, const char* fname);
133
134#ifdef USE_PNG
135void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
136#endif
137
138#endif