all repos — mgba @ 50402c830729f2ba5a6fc3e6facfd8b258f7f97d

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 "gba/gba.h"
  9#include "gba/audio.h"
 10#include "gba/context/context.h"
 11#include "gba/gui/gui-runner.h"
 12#include "gba/input.h"
 13
 14#include "gba/renderers/video-software.h"
 15#include "util/circle-buffer.h"
 16#include "util/memory.h"
 17#include "util/threading.h"
 18#include "util/vfs.h"
 19#include "platform/psp2/sce-vfs.h"
 20#include "third-party/blip_buf/blip_buf.h"
 21
 22#include <psp2/audioout.h>
 23#include <psp2/ctrl.h>
 24#include <psp2/display.h>
 25#include <psp2/gxm.h>
 26#include <psp2/kernel/sysmem.h>
 27#include <psp2/motion.h>
 28#include <psp2/power.h>
 29
 30#include <vita2d.h>
 31
 32static enum ScreenMode {
 33	SM_BACKDROP,
 34	SM_PLAIN,
 35	SM_FULL,
 36	SM_MAX
 37} screenMode;
 38
 39static struct GBAVideoSoftwareRenderer renderer;
 40static vita2d_texture* tex;
 41static vita2d_texture* screenshot;
 42static Thread audioThread;
 43static struct GBASceRotationSource {
 44	struct GBARotationSource d;
 45	struct SceMotionSensorState state;
 46} rotation;
 47
 48extern const uint8_t _binary_backdrop_png_start[];
 49static vita2d_texture* backdrop = 0;
 50
 51#define PSP2_INPUT 0x50535032
 52#define PSP2_SAMPLES 64
 53#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 19)
 54
 55static struct GBAPSP2AudioContext {
 56	struct CircleBuffer buffer;
 57	Mutex mutex;
 58	Condition cond;
 59	bool running;
 60} audioContext;
 61
 62static void _mapVitaKey(struct GBAInputMap* map, int pspKey, enum GBAKey key) {
 63	GBAInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
 64}
 65
 66static THREAD_ENTRY _audioThread(void* context) {
 67	struct GBAPSP2AudioContext* audio = (struct GBAPSP2AudioContext*) context;
 68	struct GBAStereoSample buffer[PSP2_SAMPLES];
 69	int audioPort = sceAudioOutOpenPort(PSP2_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_SAMPLES, 48000, PSP2_AUDIO_OUT_MODE_STEREO);
 70	while (audio->running) {
 71		memset(buffer, 0, sizeof(buffer));
 72		MutexLock(&audio->mutex);
 73		int len = CircleBufferSize(&audio->buffer);
 74		len /= sizeof(buffer[0]);
 75		if (len > PSP2_SAMPLES) {
 76			len = PSP2_SAMPLES;
 77		}
 78		if (len > 0) {
 79			len &= ~(PSP2_AUDIO_MIN_LEN - 1);
 80			CircleBufferRead(&audio->buffer, buffer, len * sizeof(buffer[0]));
 81			MutexUnlock(&audio->mutex);
 82			sceAudioOutOutput(audioPort, buffer);
 83			MutexLock(&audio->mutex);
 84		}
 85
 86		if (CircleBufferSize(&audio->buffer) < PSP2_SAMPLES) {
 87			ConditionWait(&audio->cond, &audio->mutex);
 88		}
 89		MutexUnlock(&audio->mutex);
 90	}
 91	sceAudioOutReleasePort(audioPort);
 92	return 0;
 93}
 94
 95static void _sampleRotation(struct GBARotationSource* source) {
 96	struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
 97	sceMotionGetSensorState(&rotation->state, 1);
 98}
 99
100static int32_t _readTiltX(struct GBARotationSource* source) {
101	struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
102	return rotation->state.accelerometer.x * 0x60000000;
103}
104
105static int32_t _readTiltY(struct GBARotationSource* source) {
106	struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
107	return rotation->state.accelerometer.y * 0x60000000;
108}
109
110static int32_t _readGyroZ(struct GBARotationSource* source) {
111	struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
112	return rotation->state.gyro.z * 0x10000000;
113}
114
115uint16_t GBAPSP2PollInput(struct GBAGUIRunner* runner) {
116	SceCtrlData pad;
117	sceCtrlPeekBufferPositive(0, &pad, 1);
118
119	int activeKeys = GBAInputMapKeyBits(&runner->context.inputMap, PSP2_INPUT, pad.buttons, 0);
120	enum GBAKey angles = GBAInputMapAxis(&runner->context.inputMap, PSP2_INPUT, 0, pad.ly);
121	if (angles != GBA_KEY_NONE) {
122		activeKeys |= 1 << angles;
123	}
124	angles = GBAInputMapAxis(&runner->context.inputMap, PSP2_INPUT, 1, pad.lx);
125	if (angles != GBA_KEY_NONE) {
126		activeKeys |= 1 << angles;
127	}
128	angles = GBAInputMapAxis(&runner->context.inputMap, PSP2_INPUT, 2, pad.ry);
129	if (angles != GBA_KEY_NONE) {
130		activeKeys |= 1 << angles;
131	}
132	angles = GBAInputMapAxis(&runner->context.inputMap, PSP2_INPUT, 3, pad.rx);
133	if (angles != GBA_KEY_NONE) {
134		activeKeys |= 1 << angles;
135	}
136	return activeKeys;
137}
138
139void GBAPSP2Setup(struct GBAGUIRunner* runner) {
140	scePowerSetArmClockFrequency(80);
141	struct GBAOptions opts = {
142		.useBios = true,
143		.idleOptimization = IDLE_LOOP_DETECT
144	};
145	GBAConfigLoadDefaults(&runner->context.config, &opts);
146	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_CROSS, GBA_KEY_A);
147	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_CIRCLE, GBA_KEY_B);
148	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_START, GBA_KEY_START);
149	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_SELECT, GBA_KEY_SELECT);
150	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_UP, GBA_KEY_UP);
151	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_DOWN, GBA_KEY_DOWN);
152	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_LEFT, GBA_KEY_LEFT);
153	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_RIGHT, GBA_KEY_RIGHT);
154	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_LTRIGGER, GBA_KEY_L);
155	_mapVitaKey(&runner->context.inputMap, PSP2_CTRL_RTRIGGER, GBA_KEY_R);
156
157	struct GBAAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
158	GBAInputBindAxis(&runner->context.inputMap, PSP2_INPUT, 0, &desc);
159	desc = (struct GBAAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
160	GBAInputBindAxis(&runner->context.inputMap, PSP2_INPUT, 1, &desc);
161
162	tex = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
163	screenshot = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
164
165	GBAVideoSoftwareRendererCreate(&renderer);
166	renderer.outputBuffer = vita2d_texture_get_datap(tex);
167	renderer.outputBufferStride = 256;
168	runner->context.renderer = &renderer.d;
169
170	rotation.d.sample = _sampleRotation;
171	rotation.d.readTiltX = _readTiltX;
172	rotation.d.readTiltY = _readTiltY;
173	rotation.d.readGyroZ = _readGyroZ;
174	runner->context.gba->rotationSource = &rotation.d;
175
176	backdrop = vita2d_load_PNG_buffer(_binary_backdrop_png_start);
177}
178
179void GBAPSP2LoadROM(struct GBAGUIRunner* runner) {
180	scePowerSetArmClockFrequency(444);
181	double ratio = GBAAudioCalculateRatio(1, 60, 1);
182	blip_set_rates(runner->context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
183	blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
184
185	if (runner->context.gba->memory.hw.devices & (HW_TILT | HW_GYRO)) {
186		sceMotionStartSampling();
187	}
188
189	CircleBufferInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample));
190	MutexInit(&audioContext.mutex);
191	ConditionInit(&audioContext.cond);
192	audioContext.running = true;
193	ThreadCreate(&audioThread, _audioThread, &audioContext);
194}
195
196void GBAPSP2PrepareForFrame(struct GBAGUIRunner* runner) {
197	MutexLock(&audioContext.mutex);
198	while (blip_samples_avail(runner->context.gba->audio.left) >= PSP2_SAMPLES) {
199		if (CircleBufferSize(&audioContext.buffer) + PSP2_SAMPLES * sizeof(struct GBAStereoSample) > CircleBufferCapacity(&audioContext.buffer)) {
200			break;
201		}
202		struct GBAStereoSample samples[PSP2_SAMPLES];
203		blip_read_samples(runner->context.gba->audio.left, &samples[0].left, PSP2_SAMPLES, true);
204		blip_read_samples(runner->context.gba->audio.right, &samples[0].right, PSP2_SAMPLES, true);
205		int i;
206		for (i = 0; i < PSP2_SAMPLES; ++i) {
207			CircleBufferWrite16(&audioContext.buffer, samples[i].left);
208			CircleBufferWrite16(&audioContext.buffer, samples[i].right);
209		}
210	}
211	ConditionWake(&audioContext.cond);
212	MutexUnlock(&audioContext.mutex);
213}
214
215void GBAPSP2UnloadROM(struct GBAGUIRunner* runner) {
216	if (runner->context.gba->memory.hw.devices & (HW_TILT | HW_GYRO)) {
217		sceMotionStopSampling();
218	}
219	scePowerSetArmClockFrequency(80);
220}
221
222void GBAPSP2Teardown(struct GBAGUIRunner* runner) {
223	UNUSED(runner);
224	vita2d_free_texture(tex);
225	vita2d_free_texture(screenshot);
226}
227
228void GBAPSP2Draw(struct GBAGUIRunner* runner, bool faded) {
229	UNUSED(runner);
230	switch (screenMode) {
231	case SM_BACKDROP:
232	default:
233		vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
234		// Fall through
235	case SM_PLAIN:
236		vita2d_draw_texture_tint_part_scale(tex, 120, 32, 0, 0, 240, 160, 3.0f, 3.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
237		break;
238	case SM_FULL:
239		vita2d_draw_texture_tint_scale(tex, 0, 0, 960.0f / 240.0f, 544.0f / 160.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
240		break;
241	}
242}
243
244void GBAPSP2DrawScreenshot(struct GBAGUIRunner* runner, const uint32_t* pixels, bool faded) {
245	UNUSED(runner);
246	uint32_t* texpixels = vita2d_texture_get_datap(screenshot);
247	int y;
248	for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
249		memcpy(&texpixels[256 * y], &pixels[VIDEO_HORIZONTAL_PIXELS * y], VIDEO_HORIZONTAL_PIXELS * 4);
250	}
251	switch (screenMode) {
252	case SM_BACKDROP:
253	default:
254		vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
255		// Fall through
256	case SM_PLAIN:
257		vita2d_draw_texture_tint_part_scale(screenshot, 120, 32, 0, 0, 240, 160, 3.0f, 3.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
258		break;
259	case SM_FULL:
260		vita2d_draw_texture_tint_scale(screenshot, 0, 0, 960.0f / 240.0f, 544.0f / 160.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
261		break;
262	}
263}
264
265void GBAPSP2IncrementScreenMode(struct GBAGUIRunner* runner) {
266	unsigned mode;
267	if (GBAConfigGetUIntValue(&runner->context.config, "screenMode", &mode) && mode != screenMode) {
268		screenMode = mode;
269	} else {
270		screenMode = (screenMode + 1) % SM_MAX;
271		GBAConfigSetUIntValue(&runner->context.config, "screenMode", screenMode);
272	}
273}
274
275__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
276	printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
277	exit(1);
278}