all repos — mgba @ b620ac1af24a01e0f7671598b4c9fba378188744

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/memory.h"
 21#include "util/ring-fifo.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_ASPECT,
 42	SM_MAX
 43} screenMode;
 44
 45static void* outputBuffer;
 46static vita2d_texture* tex;
 47static vita2d_texture* screenshot;
 48static Thread audioThread;
 49static struct mSceRotationSource {
 50	struct mRotationSource d;
 51	struct SceMotionSensorState state;
 52} rotation;
 53static struct mRumble rumble;
 54
 55extern const uint8_t _binary_backdrop_png_start[];
 56static vita2d_texture* backdrop = 0;
 57
 58#define PSP2_SAMPLES 64
 59#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 40)
 60
 61static struct mPSP2AudioContext {
 62	struct RingFIFO buffer;
 63	size_t samples;
 64	Mutex mutex;
 65	Condition cond;
 66	bool running;
 67} audioContext;
 68
 69static void _mapVitaKey(struct mInputMap* map, int pspKey, enum GBAKey key) {
 70	mInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
 71}
 72
 73static THREAD_ENTRY _audioThread(void* context) {
 74	struct mPSP2AudioContext* audio = (struct mPSP2AudioContext*) context;
 75	int audioPort = sceAudioOutOpenPort(SCE_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_SAMPLES, 48000, SCE_AUDIO_OUT_MODE_STEREO);
 76	while (audio->running) {
 77		MutexLock(&audio->mutex);
 78		int len = audio->samples;
 79		if (len > PSP2_SAMPLES) {
 80			len = PSP2_SAMPLES;
 81		}
 82		struct GBAStereoSample* buffer = audio->buffer.readPtr;
 83		RingFIFORead(&audio->buffer, NULL, len * 4);
 84		audio->samples -= len;
 85
 86		MutexUnlock(&audio->mutex);
 87		sceAudioOutOutput(audioPort, buffer);
 88		MutexLock(&audio->mutex);
 89
 90		if (audio->samples < 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 * 0x30000000;
107}
108
109static int32_t _readTiltY(struct mRotationSource* source) {
110	struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
111	return rotation->state.accelerometer.y * -0x30000000;
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
119static void _setRumble(struct mRumble* rumble, int enable) {
120	UNUSED(rumble);
121	struct SceCtrlActuator state = {
122		enable,
123		0
124	};
125	sceCtrlSetActuator(1, &state);
126}
127
128uint16_t mPSP2PollInput(struct mGUIRunner* runner) {
129	SceCtrlData pad;
130	sceCtrlPeekBufferPositive(0, &pad, 1);
131
132	int activeKeys = mInputMapKeyBits(&runner->core->inputMap, PSP2_INPUT, pad.buttons, 0);
133	enum GBAKey angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 0, pad.ly);
134	if (angles != GBA_KEY_NONE) {
135		activeKeys |= 1 << angles;
136	}
137	angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 1, pad.lx);
138	if (angles != GBA_KEY_NONE) {
139		activeKeys |= 1 << angles;
140	}
141	angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 2, pad.ry);
142	if (angles != GBA_KEY_NONE) {
143		activeKeys |= 1 << angles;
144	}
145	angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 3, pad.rx);
146	if (angles != GBA_KEY_NONE) {
147		activeKeys |= 1 << angles;
148	}
149	return activeKeys;
150}
151
152void mPSP2Setup(struct mGUIRunner* runner) {
153	mCoreConfigSetDefaultIntValue(&runner->core->config, "threadedVideo", 1);
154	mCoreLoadConfig(runner->core);
155
156	scePowerSetArmClockFrequency(80);
157	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_CROSS, GBA_KEY_A);
158	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_CIRCLE, GBA_KEY_B);
159	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_START, GBA_KEY_START);
160	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_SELECT, GBA_KEY_SELECT);
161	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_UP, GBA_KEY_UP);
162	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_DOWN, GBA_KEY_DOWN);
163	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_LEFT, GBA_KEY_LEFT);
164	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_RIGHT, GBA_KEY_RIGHT);
165	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_LTRIGGER, GBA_KEY_L);
166	_mapVitaKey(&runner->core->inputMap, SCE_CTRL_RTRIGGER, GBA_KEY_R);
167
168	struct mInputAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
169	mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 0, &desc);
170	desc = (struct mInputAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
171	mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 1, &desc);
172
173	tex = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
174	screenshot = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
175
176	outputBuffer = vita2d_texture_get_datap(tex);
177	runner->core->setVideoBuffer(runner->core, outputBuffer, 256);
178
179	rotation.d.sample = _sampleRotation;
180	rotation.d.readTiltX = _readTiltX;
181	rotation.d.readTiltY = _readTiltY;
182	rotation.d.readGyroZ = _readGyroZ;
183	runner->core->setRotation(runner->core, &rotation.d);
184
185	rumble.setRumble = _setRumble;
186	runner->core->setRumble(runner->core, &rumble);
187
188	backdrop = vita2d_load_PNG_buffer(_binary_backdrop_png_start);
189
190	unsigned mode;
191	if (mCoreConfigGetUIntValue(&runner->core->config, "screenMode", &mode) && mode < SM_MAX) {
192		screenMode = mode;
193	}
194}
195
196void mPSP2LoadROM(struct mGUIRunner* runner) {
197	scePowerSetArmClockFrequency(444);
198	double ratio = GBAAudioCalculateRatio(1, 60, 1);
199	blip_set_rates(runner->core->getAudioChannel(runner->core, 0), runner->core->frequency(runner->core), 48000 * ratio);
200	blip_set_rates(runner->core->getAudioChannel(runner->core, 1), runner->core->frequency(runner->core), 48000 * ratio);
201
202	switch (runner->core->platform(runner->core)) {
203#ifdef M_CORE_GBA
204	case PLATFORM_GBA:
205		if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
206			sceMotionStartSampling();
207		}
208		break;
209#endif
210#ifdef M_CORE_GB
211	case PLATFORM_GB:
212		if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
213			sceMotionStartSampling();
214		}
215		break;
216#endif
217	default:
218		break;
219	}
220
221	RingFIFOInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample), PSP2_SAMPLES * 4);
222	MutexInit(&audioContext.mutex);
223	ConditionInit(&audioContext.cond);
224	audioContext.running = true;
225	ThreadCreate(&audioThread, _audioThread, &audioContext);
226}
227
228void mPSP2PrepareForFrame(struct mGUIRunner* runner) {
229	MutexLock(&audioContext.mutex);
230	while (blip_samples_avail(runner->core->getAudioChannel(runner->core, 0)) >= PSP2_SAMPLES) {
231		struct GBAStereoSample* samples = audioContext.buffer.writePtr;
232		blip_read_samples(runner->core->getAudioChannel(runner->core, 0), &samples[0].left, PSP2_SAMPLES, true);
233		blip_read_samples(runner->core->getAudioChannel(runner->core, 1), &samples[0].right, PSP2_SAMPLES, true);
234		RingFIFOWrite(&audioContext.buffer, NULL, PSP2_SAMPLES * 4);
235		audioContext.samples += PSP2_SAMPLES;
236	}
237	ConditionWake(&audioContext.cond);
238	MutexUnlock(&audioContext.mutex);
239}
240
241void mPSP2UnloadROM(struct mGUIRunner* runner) {
242	switch (runner->core->platform(runner->core)) {
243#ifdef M_CORE_GBA
244	case PLATFORM_GBA:
245		if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
246			sceMotionStopSampling();
247		}
248		break;
249#endif
250#ifdef M_CORE_GB
251	case PLATFORM_GB:
252		if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
253			sceMotionStopSampling();
254		}
255		break;
256#endif
257	default:
258		break;
259	}
260	scePowerSetArmClockFrequency(80);
261}
262
263void mPSP2Paused(struct mGUIRunner* runner) {
264	UNUSED(runner);
265	struct SceCtrlActuator state = {
266		0,
267		0
268	};
269	sceCtrlSetActuator(1, &state);
270}
271
272void mPSP2Unpaused(struct mGUIRunner* runner) {
273	unsigned mode;
274	if (mCoreConfigGetUIntValue(&runner->core->config, "screenMode", &mode) && mode != screenMode) {
275		screenMode = mode;
276	}
277}
278
279void mPSP2Teardown(struct mGUIRunner* runner) {
280	vita2d_free_texture(tex);
281	vita2d_free_texture(screenshot);
282}
283
284
285void _drawTex(vita2d_texture* t, unsigned width, unsigned height, bool faded) {
286	unsigned w = width;
287	unsigned h = height;
288	// Get greatest common divisor
289	while (w != 0) {
290		int temp = h % w;
291		h = w;
292		w = temp;
293	}
294	int gcd = h;
295	int aspectw = width / gcd;
296	int aspecth = height / gcd;
297	float scalex;
298	float scaley;
299
300	switch (screenMode) {
301	case SM_BACKDROP:
302	default:
303		vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
304		// Fall through
305	case SM_PLAIN:
306		w = 960 / width;
307		h = 544 / height;
308		if (w * height > 544) {
309			scalex = h;
310			w = width * h;
311			h = height * h;
312		} else {
313			scalex = w;
314			w = width * w;
315			h = height * w;
316		}
317		scaley = scalex;
318		break;
319	case SM_ASPECT:
320		w = 960 / aspectw;
321		h = 544 / aspecth;
322		if (w * aspecth > 544) {
323			w = aspectw * h;
324			h = aspecth * h;
325		} else {
326			w = aspectw * w;
327			h = aspecth * w;
328		}
329		scalex = w / (float) width;
330		scaley = scalex;
331		break;
332	case SM_FULL:
333		w = 960;
334		h = 544;
335		scalex = 960.0f / width;
336		scaley = 544.0f / height;
337		break;
338	}
339	vita2d_draw_texture_tint_part_scale(t,
340	                                    (960.0f - w) / 2.0f, (544.0f - h) / 2.0f,
341	                                    0, 0, width, height,
342	                                    scalex, scaley,
343	                                    (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
344}
345
346void mPSP2Draw(struct mGUIRunner* runner, bool faded) {
347	unsigned width, height;
348	runner->core->desiredVideoDimensions(runner->core, &width, &height);
349	_drawTex(tex, width, height, faded);
350}
351
352void mPSP2DrawScreenshot(struct mGUIRunner* runner, const uint32_t* pixels, unsigned width, unsigned height, bool faded) {
353	UNUSED(runner);
354	uint32_t* texpixels = vita2d_texture_get_datap(screenshot);
355	int y;
356	for (y = 0; y < height; ++y) {
357		memcpy(&texpixels[256 * y], &pixels[width * y], width * 4);
358	}
359	_drawTex(screenshot, width, height, faded);
360}
361
362void mPSP2IncrementScreenMode(struct mGUIRunner* runner) {
363	screenMode = (screenMode + 1) % SM_MAX;
364	mCoreConfigSetUIntValue(&runner->core->config, "screenMode", screenMode);
365}
366
367__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
368	printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
369	exit(1);
370}