all repos — mgba @ 2716a0f64fa6908a45e2f7f9bc42a836ea999b1a

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#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 52	struct GBADirectorySet dirs;
 53#endif
 54	struct VFile* rom;
 55	struct VFile* save;
 56	struct VFile* bios;
 57	struct VFile* patch;
 58	struct VFile* cheatsFile;
 59	const char* fname;
 60	const char* movie;
 61	int activeKeys;
 62	struct GBAAVStream* stream;
 63	struct Configuration* overrides;
 64	enum GBAIdleLoopOptimization idleOptimization;
 65	bool bootBios;
 66
 67	bool hasOverride;
 68	struct GBACartridgeOverride override;
 69
 70	// Run-time options
 71	int frameskip;
 72	float fpsTarget;
 73	size_t audioBuffers;
 74	bool skipBios;
 75	int volume;
 76	bool mute;
 77
 78	// Threading state
 79	Thread thread;
 80
 81	Mutex stateMutex;
 82	Condition stateCond;
 83	enum ThreadState savedState;
 84	int interruptDepth;
 85	bool frameWasOn;
 86
 87	GBALogHandler logHandler;
 88	int logLevel;
 89	ThreadCallback startCallback;
 90	ThreadCallback cleanCallback;
 91	ThreadCallback frameCallback;
 92	ThreadStopCallback stopCallback;
 93	void* userData;
 94	void (*run)(struct GBAThread*);
 95
 96	struct GBASync sync;
 97
 98	int rewindBufferSize;
 99	int rewindBufferCapacity;
100	int rewindBufferInterval;
101	int rewindBufferNext;
102	struct GBASerializedState** rewindBuffer;
103	int rewindBufferWriteOffset;
104	uint8_t* rewindScreenBuffer;
105
106	struct GBACheatDevice* cheats;
107};
108
109void GBAMapOptionsToContext(const struct GBAOptions*, struct GBAThread*);
110void GBAMapArgumentsToContext(const struct GBAArguments*, struct GBAThread*);
111
112bool GBAThreadStart(struct GBAThread* threadContext);
113bool GBAThreadHasStarted(struct GBAThread* threadContext);
114bool GBAThreadHasExited(struct GBAThread* threadContext);
115bool GBAThreadHasCrashed(struct GBAThread* threadContext);
116void GBAThreadEnd(struct GBAThread* threadContext);
117void GBAThreadReset(struct GBAThread* threadContext);
118void GBAThreadJoin(struct GBAThread* threadContext);
119
120bool GBAThreadIsActive(struct GBAThread* threadContext);
121void GBAThreadInterrupt(struct GBAThread* threadContext);
122void GBAThreadContinue(struct GBAThread* threadContext);
123
124void GBARunOnThread(struct GBAThread* threadContext, void (*run)(struct GBAThread*));
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
133void GBAThreadLoadROM(struct GBAThread* threadContext, const char* fname);
134void GBAThreadReplaceROM(struct GBAThread* threadContext, const char* fname);
135
136#ifdef USE_PNG
137void GBAThreadTakeScreenshot(struct GBAThread* threadContext);
138#endif
139
140#endif