all repos — mgba @ d008291fc8173d50be6dcb81cd19141a4dc2a4d4

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 * 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
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	unsigned mode;
179	if (mCoreConfigGetUIntValue(&runner->core->config, "screenMode", &mode) && mode < SM_MAX) {
180		screenMode = mode;
181	}
182}
183
184void mPSP2LoadROM(struct mGUIRunner* runner) {
185	scePowerSetArmClockFrequency(444);
186	double ratio = GBAAudioCalculateRatio(1, 60, 1);
187	blip_set_rates(runner->core->getAudioChannel(runner->core, 0), runner->core->frequency(runner->core), 48000 * ratio);
188	blip_set_rates(runner->core->getAudioChannel(runner->core, 1), runner->core->frequency(runner->core), 48000 * ratio);
189
190	switch (runner->core->platform(runner->core)) {
191#ifdef M_CORE_GBA
192	case PLATFORM_GBA:
193		if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
194			sceMotionStartSampling();
195		}
196		break;
197#endif
198#ifdef M_CORE_GB
199	case PLATFORM_GB:
200		if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
201			sceMotionStartSampling();
202		}
203		break;
204#endif
205	default:
206		break;
207	}
208
209	CircleBufferInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample));
210	MutexInit(&audioContext.mutex);
211	ConditionInit(&audioContext.cond);
212	audioContext.running = true;
213	ThreadCreate(&audioThread, _audioThread, &audioContext);
214}
215
216void mPSP2PrepareForFrame(struct mGUIRunner* runner) {
217	MutexLock(&audioContext.mutex);
218	while (blip_samples_avail(runner->core->getAudioChannel(runner->core, 0)) >= PSP2_SAMPLES) {
219		if (CircleBufferSize(&audioContext.buffer) + PSP2_SAMPLES * sizeof(struct GBAStereoSample) > CircleBufferCapacity(&audioContext.buffer)) {
220			break;
221		}
222		struct GBAStereoSample samples[PSP2_SAMPLES];
223		blip_read_samples(runner->core->getAudioChannel(runner->core, 0), &samples[0].left, PSP2_SAMPLES, true);
224		blip_read_samples(runner->core->getAudioChannel(runner->core, 1), &samples[0].right, PSP2_SAMPLES, true);
225		int i;
226		for (i = 0; i < PSP2_SAMPLES; ++i) {
227			CircleBufferWrite16(&audioContext.buffer, samples[i].left);
228			CircleBufferWrite16(&audioContext.buffer, samples[i].right);
229		}
230	}
231	ConditionWake(&audioContext.cond);
232	MutexUnlock(&audioContext.mutex);
233}
234
235void mPSP2UnloadROM(struct mGUIRunner* runner) {
236	switch (runner->core->platform(runner->core)) {
237#ifdef M_CORE_GBA
238	case PLATFORM_GBA:
239		if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
240			sceMotionStopSampling();
241		}
242		break;
243#endif
244#ifdef M_CORE_GB
245	case PLATFORM_GB:
246		if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
247			sceMotionStopSampling();
248		}
249		break;
250#endif
251	default:
252		break;
253	}
254	scePowerSetArmClockFrequency(80);
255}
256
257void mPSP2Unpaused(struct mGUIRunner* runner) {
258	unsigned mode;
259	if (mCoreConfigGetUIntValue(&runner->core->config, "screenMode", &mode) && mode != screenMode) {
260		screenMode = mode;
261	}
262}
263
264void mPSP2Teardown(struct mGUIRunner* runner) {
265	vita2d_free_texture(tex);
266	vita2d_free_texture(screenshot);
267}
268
269void mPSP2Draw(struct mGUIRunner* runner, bool faded) {
270	unsigned width, height;
271	runner->core->desiredVideoDimensions(runner->core, &width, &height);
272	switch (screenMode) {
273	case SM_BACKDROP:
274	default:
275		vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
276		// Fall through
277	case SM_PLAIN:
278		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);
279		break;
280	case SM_FULL:
281		vita2d_draw_texture_tint_scale(tex, 0, 0, 960.0f / width, 544.0f / height, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
282		break;
283	}
284}
285
286void mPSP2DrawScreenshot(struct mGUIRunner* runner, const uint32_t* pixels, bool faded) {
287	UNUSED(runner);
288	uint32_t* texpixels = vita2d_texture_get_datap(screenshot);
289	int y;
290	for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
291		memcpy(&texpixels[256 * y], &pixels[VIDEO_HORIZONTAL_PIXELS * y], VIDEO_HORIZONTAL_PIXELS * 4);
292	}
293	switch (screenMode) {
294	case SM_BACKDROP:
295	default:
296		vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
297		// Fall through
298	case SM_PLAIN:
299		vita2d_draw_texture_tint_part_scale(screenshot, 120, 32, 0, 0, 240, 160, 3.0f, 3.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
300		break;
301	case SM_FULL:
302		vita2d_draw_texture_tint_scale(screenshot, 0, 0, 960.0f / 240.0f, 544.0f / 160.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
303		break;
304	}
305}
306
307void mPSP2IncrementScreenMode(struct mGUIRunner* runner) {
308	screenMode = (screenMode + 1) % SM_MAX;
309	mCoreConfigSetUIntValue(&runner->core->config, "screenMode", screenMode);
310}
311
312__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
313	printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
314	exit(1);
315}