all repos — mgba @ a97276b3a017311198e8aefcb89de50b54604dc5

mGBA Game Boy Advance Emulator

src/platform/psp2/psp2-context.c (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#include "psp2-context.h"
  7
  8#include "core/core.h"
  9
 10#ifdef M_CORE_GBA
 11#include "gba/gba.h"
 12#endif
 13#ifdef M_CORE_GB
 14#include "gb/gb.h"
 15#endif
 16
 17#include "feature/gui/gui-runner.h"
 18#include "gba/input.h"
 19
 20#include "util/circle-buffer.h"
 21#include "util/memory.h"
 22#include "util/threading.h"
 23#include "util/vfs.h"
 24#include "platform/psp2/sce-vfs.h"
 25#include "third-party/blip_buf/blip_buf.h"
 26
 27#include <psp2/audioout.h>
 28#include <psp2/ctrl.h>
 29#include <psp2/display.h>
 30#include <psp2/gxm.h>
 31#include <psp2/kernel/sysmem.h>
 32#include <psp2/motion.h>
 33#include <psp2/power.h>
 34
 35#include <vita2d.h>
 36
 37static enum ScreenMode {
 38	SM_BACKDROP,
 39	SM_PLAIN,
 40	SM_FULL,
 41	SM_MAX
 42} screenMode;
 43
 44static void* outputBuffer;
 45static vita2d_texture* tex;
 46static vita2d_texture* screenshot;
 47static Thread audioThread;
 48static struct mSceRotationSource {
 49	struct mRotationSource d;
 50	struct SceMotionSensorState state;
 51} rotation;
 52
 53extern const uint8_t _binary_backdrop_png_start[];
 54static vita2d_texture* backdrop = 0;
 55
 56#define PSP2_SAMPLES 64
 57#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 19)
 58
 59static struct mPSP2AudioContext {
 60	struct CircleBuffer buffer;
 61	Mutex mutex;
 62	Condition cond;
 63	bool running;
 64} audioContext;
 65
 66static void _mapVitaKey(struct mInputMap* map, int pspKey, enum GBAKey key) {
 67	mInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
 68}
 69
 70static THREAD_ENTRY _audioThread(void* context) {
 71	struct mPSP2AudioContext* audio = (struct mPSP2AudioContext*) context;
 72	struct GBAStereoSample buffer[PSP2_SAMPLES];
 73	int audioPort = sceAudioOutOpenPort(SCE_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_SAMPLES, 48000, SCE_AUDIO_OUT_MODE_STEREO);
 74	while (audio->running) {
 75		memset(buffer, 0, sizeof(buffer));
 76		MutexLock(&audio->mutex);
 77		int len = CircleBufferSize(&audio->buffer);
 78		len /= sizeof(buffer[0]);
 79		if (len > PSP2_SAMPLES) {
 80			len = PSP2_SAMPLES;
 81		}
 82		if (len > 0) {
 83			len &= ~(SCE_AUDIO_MIN_LEN - 1);
 84			CircleBufferRead(&audio->buffer, buffer, len * sizeof(buffer[0]));
 85			MutexUnlock(&audio->mutex);
 86			sceAudioOutOutput(audioPort, buffer);
 87			MutexLock(&audio->mutex);
 88		}
 89
 90		if (CircleBufferSize(&audio->buffer) < PSP2_SAMPLES) {
 91			ConditionWait(&audio->cond, &audio->mutex);
 92		}
 93		MutexUnlock(&audio->mutex);
 94	}
 95	sceAudioOutReleasePort(audioPort);
 96	return 0;
 97}
 98
 99static void _sampleRotation(struct mRotationSource* source) {
100	struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
101	sceMotionGetSensorState(&rotation->state, 1);
102}
103
104static int32_t _readTiltX(struct mRotationSource* source) {
105	struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
106	return rotation->state.accelerometer.x * 0x60000000;
107}
108
109static int32_t _readTiltY(struct mRotationSource* source) {
110	struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
111	return rotation->state.accelerometer.y * 0x60000000;
112}
113
114static int32_t _readGyroZ(struct mRotationSource* source) {
115	struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
116	return rotation->state.gyro.z * 0x10000000;
117}
118
119uint16_t mPSP2PollInput(struct mGUIRunner* runner) {
120	SceCtrlData pad;
121	sceCtrlPeekBufferPositive(0, &pad, 1);
122
123	int activeKeys = mInputMapKeyBits(&runner->core->inputMap, PSP2_INPUT, pad.buttons, 0);
124	enum GBAKey angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 0, pad.ly);
125	if (angles != GBA_KEY_NONE) {
126		activeKeys |= 1 << angles;
127	}
128	angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 1, pad.lx);
129	if (angles != GBA_KEY_NONE) {
130		activeKeys |= 1 << angles;
131	}
132	angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 2, pad.ry);
133	if (angles != GBA_KEY_NONE) {
134		activeKeys |= 1 << angles;
135	}
136	angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 3, pad.rx);
137	if (angles != GBA_KEY_NONE) {
138		activeKeys |= 1 << angles;
139	}
140	return activeKeys;
141}
142
143void mPSP2Setup(struct mGUIRunner* runner) {
144	mCoreConfigSetDefaultIntValue(&runner->core->config, "threadedVideo", 1);
145	mCoreLoadConfig(runner->core);
146
147	scePowerSetArmClockFrequency(80);
148	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_CROSS, GBA_KEY_A);
149	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_CIRCLE, GBA_KEY_B);
150	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_START, GBA_KEY_START);
151	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_SELECT, GBA_KEY_SELECT);
152	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_UP, GBA_KEY_UP);
153	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_DOWN, GBA_KEY_DOWN);
154	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_LEFT, GBA_KEY_LEFT);
155	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_RIGHT, GBA_KEY_RIGHT);
156	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_LTRIGGER, GBA_KEY_L);
157	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_RTRIGGER, GBA_KEY_R);
158
159	struct mInputAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
160	mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 0, &desc);
161	desc = (struct mInputAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
162	mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 1, &desc);
163
164	tex = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
165	screenshot = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
166
167	outputBuffer = vita2d_texture_get_datap(tex);
168	runner->core->setVideoBuffer(runner->core, outputBuffer, 256);
169
170	rotation.d.sample = _sampleRotation;
171	rotation.d.readTiltX = _readTiltX;
172	rotation.d.readTiltY = _readTiltY;
173	rotation.d.readGyroZ = _readGyroZ;
174	runner->core->setRotation(runner->core, &rotation.d);
175
176	backdrop = vita2d_load_PNG_buffer(_binary_backdrop_png_start);
177}
178
179void mPSP2LoadROM(struct mGUIRunner* runner) {
180	scePowerSetArmClockFrequency(444);
181	double ratio = GBAAudioCalculateRatio(1, 60, 1);
182	blip_set_rates(runner->core->getAudioChannel(runner->core, 0), runner->core->frequency(runner->core), 48000 * ratio);
183	blip_set_rates(runner->core->getAudioChannel(runner->core, 1), runner->core->frequency(runner->core), 48000 * ratio);
184
185	switch (runner->core->platform(runner->core)) {
186#ifdef M_CORE_GBA
187	case PLATFORM_GBA:
188		if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
189			sceMotionStartSampling();
190		}
191		break;
192#endif
193#ifdef M_CORE_GB
194	case PLATFORM_GB:
195		if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
196			sceMotionStartSampling();
197		}
198		break;
199#endif
200	default:
201		break;
202	}
203
204	CircleBufferInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample));
205	MutexInit(&audioContext.mutex);
206	ConditionInit(&audioContext.cond);
207	audioContext.running = true;
208	ThreadCreate(&audioThread, _audioThread, &audioContext);
209}
210
211void mPSP2PrepareForFrame(struct mGUIRunner* runner) {
212	MutexLock(&audioContext.mutex);
213	while (blip_samples_avail(runner->core->getAudioChannel(runner->core, 0)) >= PSP2_SAMPLES) {
214		if (CircleBufferSize(&audioContext.buffer) + PSP2_SAMPLES * sizeof(struct GBAStereoSample) > CircleBufferCapacity(&audioContext.buffer)) {
215			break;
216		}
217		struct GBAStereoSample samples[PSP2_SAMPLES];
218		blip_read_samples(runner->core->getAudioChannel(runner->core, 0), &samples[0].left, PSP2_SAMPLES, true);
219		blip_read_samples(runner->core->getAudioChannel(runner->core, 1), &samples[0].right, PSP2_SAMPLES, true);
220		int i;
221		for (i = 0; i < PSP2_SAMPLES; ++i) {
222			CircleBufferWrite16(&audioContext.buffer, samples[i].left);
223			CircleBufferWrite16(&audioContext.buffer, samples[i].right);
224		}
225	}
226	ConditionWake(&audioContext.cond);
227	MutexUnlock(&audioContext.mutex);
228}
229
230void mPSP2UnloadROM(struct mGUIRunner* runner) {
231	switch (runner->core->platform(runner->core)) {
232#ifdef M_CORE_GBA
233	case PLATFORM_GBA:
234		if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
235			sceMotionStopSampling();
236		}
237		break;
238#endif
239#ifdef M_CORE_GB
240	case PLATFORM_GB:
241		if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
242			sceMotionStopSampling();
243		}
244		break;
245#endif
246	default:
247		break;
248	}
249	scePowerSetArmClockFrequency(80);
250}
251
252void mPSP2Teardown(struct mGUIRunner* runner) {
253	vita2d_free_texture(tex);
254	vita2d_free_texture(screenshot);
255}
256
257void mPSP2Draw(struct mGUIRunner* runner, bool faded) {
258	unsigned width, height;
259	runner->core->desiredVideoDimensions(runner->core, &width, &height);
260	switch (screenMode) {
261	case SM_BACKDROP:
262	default:
263		vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
264		// Fall through
265	case SM_PLAIN:
266		vita2d_draw_texture_tint_part_scale(tex, (960.0f - width * 3.0f) / 2.0f, (544.0f - height * 3.0f) / 2.0f, 0, 0, width, height, 3.0f, 3.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
267		break;
268	case SM_FULL:
269		vita2d_draw_texture_tint_scale(tex, 0, 0, 960.0f / width, 544.0f / height, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
270		break;
271	}
272}
273
274void mPSP2DrawScreenshot(struct mGUIRunner* runner, const uint32_t* pixels, bool faded) {
275	UNUSED(runner);
276	uint32_t* texpixels = vita2d_texture_get_datap(screenshot);
277	int y;
278	for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
279		memcpy(&texpixels[256 * y], &pixels[VIDEO_HORIZONTAL_PIXELS * y], VIDEO_HORIZONTAL_PIXELS * 4);
280	}
281	switch (screenMode) {
282	case SM_BACKDROP:
283	default:
284		vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
285		// Fall through
286	case SM_PLAIN:
287		vita2d_draw_texture_tint_part_scale(screenshot, 120, 32, 0, 0, 240, 160, 3.0f, 3.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
288		break;
289	case SM_FULL:
290		vita2d_draw_texture_tint_scale(screenshot, 0, 0, 960.0f / 240.0f, 544.0f / 160.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
291		break;
292	}
293}
294
295void mPSP2IncrementScreenMode(struct mGUIRunner* runner) {
296	unsigned mode;
297	if (mCoreConfigGetUIntValue(&runner->core->config, "screenMode", &mode) && mode != screenMode) {
298		screenMode = mode;
299	} else {
300		screenMode = (screenMode + 1) % SM_MAX;
301		mCoreConfigSetUIntValue(&runner->core->config, "screenMode", screenMode);
302	}
303}
304
305__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
306	printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
307	exit(1);
308}