all repos — mgba @ 97e2cf08ab727dd7a6f47ebd0fe2ba70b8eeb8b5

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 <mgba/core/blip_buf.h>
  9#include <mgba/core/core.h>
 10
 11#ifdef M_CORE_GBA
 12#include <mgba/internal/gba/gba.h>
 13#endif
 14#ifdef M_CORE_GB
 15#include <mgba/internal/gb/gb.h>
 16#endif
 17
 18#include "feature/gui/gui-runner.h"
 19#include <mgba/internal/gba/input.h>
 20
 21#include <mgba-util/memory.h>
 22#include <mgba-util/circle-buffer.h>
 23#include <mgba-util/math.h>
 24#include <mgba-util/threading.h>
 25#include <mgba-util/vfs.h>
 26#include <mgba-util/platform/psp2/sce-vfs.h>
 27
 28#include <psp2/appmgr.h>
 29#include <psp2/audioout.h>
 30#include <psp2/camera.h>
 31#include <psp2/ctrl.h>
 32#include <psp2/display.h>
 33#include <psp2/gxm.h>
 34#include <psp2/kernel/sysmem.h>
 35#include <psp2/motion.h>
 36
 37#include <vita2d.h>
 38
 39#define RUMBLE_PWM 8
 40#define CDRAM_ALIGN 0x40000
 41
 42mLOG_DECLARE_CATEGORY(GUI_PSP2);
 43mLOG_DEFINE_CATEGORY(GUI_PSP2, "Vita", "gui.psp2");
 44
 45static enum ScreenMode {
 46	SM_BACKDROP,
 47	SM_PLAIN,
 48	SM_FULL,
 49	SM_ASPECT,
 50	SM_MAX
 51} screenMode;
 52
 53static void* outputBuffer;
 54static int currentTex;
 55static vita2d_texture* tex[4];
 56static vita2d_texture* screenshot;
 57static Thread audioThread;
 58static bool interframeBlending = false;
 59static bool sgbCrop = false;
 60
 61static struct mSceRotationSource {
 62	struct mRotationSource d;
 63	struct SceMotionSensorState state;
 64} rotation;
 65
 66static struct mSceRumble {
 67	struct mRumble d;
 68	struct CircleBuffer history;
 69	int current;
 70} rumble;
 71
 72static struct mSceImageSource {
 73	struct mImageSource d;
 74	SceUID memblock;
 75	void* buffer;
 76	unsigned cam;
 77	size_t bufferOffset;
 78} camera;
 79
 80static struct mAVStream stream;
 81
 82bool frameLimiter = true;
 83
 84extern const uint8_t _binary_backdrop_png_start[];
 85static vita2d_texture* backdrop = 0;
 86
 87#define PSP2_SAMPLES 512
 88#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 16)
 89
 90static struct mPSP2AudioContext {
 91	struct GBAStereoSample buffer[PSP2_AUDIO_BUFFER_SIZE];
 92	size_t writeOffset;
 93	size_t readOffset;
 94	size_t samples;
 95	Mutex mutex;
 96	Condition cond;
 97	bool running;
 98} audioContext;
 99
100void mPSP2MapKey(struct mInputMap* map, int pspKey, int key) {
101	mInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
102}
103
104static THREAD_ENTRY _audioThread(void* context) {
105	struct mPSP2AudioContext* audio = (struct mPSP2AudioContext*) context;
106	uint32_t zeroBuffer[PSP2_SAMPLES] = {0};
107	void* buffer = zeroBuffer;
108	int audioPort = sceAudioOutOpenPort(SCE_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_SAMPLES, 48000, SCE_AUDIO_OUT_MODE_STEREO);
109	while (audio->running) {
110		MutexLock(&audio->mutex);
111		if (buffer != zeroBuffer) {
112			// Can only happen in successive iterations
113			audio->samples -= PSP2_SAMPLES;
114			ConditionWake(&audio->cond);
115		}
116		if (audio->samples >= PSP2_SAMPLES) {
117			buffer = &audio->buffer[audio->readOffset];
118			audio->readOffset += PSP2_SAMPLES;
119			if (audio->readOffset >= PSP2_AUDIO_BUFFER_SIZE) {
120				audio->readOffset = 0;
121			}
122			// Don't mark samples as read until the next loop iteration to prevent
123			// writing to the buffer while being read (see above)
124		} else {
125			buffer = zeroBuffer;
126		}
127		MutexUnlock(&audio->mutex);
128
129		sceAudioOutOutput(audioPort, buffer);
130	}
131	sceAudioOutReleasePort(audioPort);
132	return 0;
133}
134
135static void _sampleRotation(struct mRotationSource* source) {
136	struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
137	sceMotionGetSensorState(&rotation->state, 1);
138}
139
140static int32_t _readTiltX(struct mRotationSource* source) {
141	struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
142	return rotation->state.accelerometer.x * 0x30000000;
143}
144
145static int32_t _readTiltY(struct mRotationSource* source) {
146	struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
147	return rotation->state.accelerometer.y * -0x30000000;
148}
149
150static int32_t _readGyroZ(struct mRotationSource* source) {
151	struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
152	return rotation->state.gyro.z * -0x10000000;
153}
154
155static void _setRumble(struct mRumble* source, int enable) {
156	struct mSceRumble* rumble = (struct mSceRumble*) source;
157	rumble->current += enable;
158	if (CircleBufferSize(&rumble->history) == RUMBLE_PWM) {
159		int8_t oldLevel;
160		CircleBufferRead8(&rumble->history, &oldLevel);
161		rumble->current -= oldLevel;
162	}
163	CircleBufferWrite8(&rumble->history, enable);
164	int small = (rumble->current << 21) / 65793;
165	int big = ((rumble->current * rumble->current) << 18) / 65793;
166	struct SceCtrlActuator state = {
167		small,
168		big
169	};
170	sceCtrlSetActuator(1, &state);
171}
172
173static void _resetCamera(struct mSceImageSource* imageSource) {
174	if (!imageSource->cam) {
175		return;
176	}
177
178	sceCameraOpen(imageSource->cam - 1, &(SceCameraInfo) {
179		.size = sizeof(SceCameraInfo),
180		.format = 5, // SCE_CAMERA_FORMAT_ABGR
181		.resolution = SCE_CAMERA_RESOLUTION_176_144,
182		.framerate = SCE_CAMERA_FRAMERATE_30_FPS,
183		.sizeIBase = 176 * 144 * 4,
184		.pitch = 0,
185		.pIBase = imageSource->buffer,
186	});
187	sceCameraStart(imageSource->cam - 1);
188}
189
190static void _startRequestImage(struct mImageSource* source, unsigned w, unsigned h, int colorFormats) {
191	UNUSED(colorFormats);
192	struct mSceImageSource* imageSource = (struct mSceImageSource*) source;
193
194	if (!imageSource->buffer) {
195		imageSource->memblock = sceKernelAllocMemBlock("camera", SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW, CDRAM_ALIGN, NULL);
196		sceKernelGetMemBlockBase(imageSource->memblock, &imageSource->buffer);
197	}
198
199	if (!imageSource->cam) {
200		return;
201	}
202
203	_resetCamera(imageSource);
204	imageSource->bufferOffset = (176 - w) / 2 + (144 - h) * 176 / 2;
205
206	SceCameraRead read = {
207		sizeof(SceCameraRead),
208		1
209	};
210	sceCameraRead(imageSource->cam - 1, &read);
211}
212
213static void _stopRequestImage(struct mImageSource* source) {
214	struct mSceImageSource* imageSource = (struct mSceImageSource*) source;
215	if (imageSource->cam) {
216		sceCameraStop(imageSource->cam - 1);
217		sceCameraClose(imageSource->cam - 1);
218	}
219	sceKernelFreeMemBlock(imageSource->memblock);
220	imageSource->buffer = NULL;
221}
222
223
224static void _requestImage(struct mImageSource* source, const void** buffer, size_t* stride, enum mColorFormat* colorFormat) {
225	struct mSceImageSource* imageSource = (struct mSceImageSource*) source;
226
227	if (!imageSource->cam) {
228		memset(imageSource->buffer, 0, 176 * 144 * 4);
229		*buffer = (uint32_t*) imageSource->buffer;
230		*stride = 176;
231		*colorFormat = mCOLOR_XBGR8;
232		return;
233	}
234
235	*buffer = (uint32_t*) imageSource->buffer + imageSource->bufferOffset;
236	*stride = 176;
237	*colorFormat = mCOLOR_XBGR8;
238
239	SceCameraRead read = {
240		sizeof(SceCameraRead),
241		1
242	};
243	sceCameraRead(imageSource->cam - 1, &read);
244}
245
246static void _postAudioBuffer(struct mAVStream* stream, blip_t* left, blip_t* right) {
247	UNUSED(stream);
248	MutexLock(&audioContext.mutex);
249	while (audioContext.samples + PSP2_SAMPLES >= PSP2_AUDIO_BUFFER_SIZE) {
250		if (!frameLimiter) {
251			blip_clear(left);
252			blip_clear(right);
253			MutexUnlock(&audioContext.mutex);
254			return;
255		}
256		ConditionWait(&audioContext.cond, &audioContext.mutex);
257	}
258	struct GBAStereoSample* samples = &audioContext.buffer[audioContext.writeOffset];
259	blip_read_samples(left, &samples[0].left, PSP2_SAMPLES, true);
260	blip_read_samples(right, &samples[0].right, PSP2_SAMPLES, true);
261	audioContext.samples += PSP2_SAMPLES;
262	audioContext.writeOffset += PSP2_SAMPLES;
263	if (audioContext.writeOffset >= PSP2_AUDIO_BUFFER_SIZE) {
264		audioContext.writeOffset = 0;
265	}
266	MutexUnlock(&audioContext.mutex);
267}
268
269uint16_t mPSP2PollInput(struct mGUIRunner* runner) {
270	SceCtrlData pad;
271	sceCtrlPeekBufferPositiveExt2(0, &pad, 1);
272
273	int activeKeys = mInputMapKeyBits(&runner->core->inputMap, PSP2_INPUT, pad.buttons, 0);
274	int angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 0, pad.ly);
275	if (angles != GBA_KEY_NONE) {
276		activeKeys |= 1 << angles;
277	}
278	angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 1, pad.lx);
279	if (angles != GBA_KEY_NONE) {
280		activeKeys |= 1 << angles;
281	}
282	angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 2, pad.ry);
283	if (angles != GBA_KEY_NONE) {
284		activeKeys |= 1 << angles;
285	}
286	angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 3, pad.rx);
287	if (angles != GBA_KEY_NONE) {
288		activeKeys |= 1 << angles;
289	}
290	return activeKeys;
291}
292
293void mPSP2SetFrameLimiter(struct mGUIRunner* runner, bool limit) {
294	UNUSED(runner);
295	if (!frameLimiter && limit) {
296		MutexLock(&audioContext.mutex);
297		while (audioContext.samples) {
298			ConditionWait(&audioContext.cond, &audioContext.mutex);
299		}
300		MutexUnlock(&audioContext.mutex);
301	}
302	frameLimiter = limit;
303}
304
305void mPSP2Setup(struct mGUIRunner* runner) {
306	mCoreConfigSetDefaultIntValue(&runner->config, "threadedVideo", 1);
307	mCoreLoadForeignConfig(runner->core, &runner->config);
308
309	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_CROSS, GBA_KEY_A);
310	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_CIRCLE, GBA_KEY_B);
311	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_START, GBA_KEY_START);
312	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_SELECT, GBA_KEY_SELECT);
313	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_UP, GBA_KEY_UP);
314	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_DOWN, GBA_KEY_DOWN);
315	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_LEFT, GBA_KEY_LEFT);
316	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_RIGHT, GBA_KEY_RIGHT);
317	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_L1, GBA_KEY_L);
318	mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_R1, GBA_KEY_R);
319
320	struct mInputAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
321	mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 0, &desc);
322	desc = (struct mInputAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
323	mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 1, &desc);
324
325	unsigned width, height;
326	runner->core->desiredVideoDimensions(runner->core, &width, &height);
327	tex[0] = vita2d_create_empty_texture_format(256, toPow2(height), SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
328	tex[1] = vita2d_create_empty_texture_format(256, toPow2(height), SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
329	tex[2] = vita2d_create_empty_texture_format(256, toPow2(height), SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
330	tex[3] = vita2d_create_empty_texture_format(256, toPow2(height), SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
331	currentTex = 0;
332	screenshot = vita2d_create_empty_texture_format(256, toPow2(height), SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
333
334	runner->core->setVideoBuffer(runner->core, vita2d_texture_get_datap(tex[currentTex]), 256);
335	runner->core->setAudioBufferSize(runner->core, PSP2_SAMPLES);
336
337	rotation.d.sample = _sampleRotation;
338	rotation.d.readTiltX = _readTiltX;
339	rotation.d.readTiltY = _readTiltY;
340	rotation.d.readGyroZ = _readGyroZ;
341	runner->core->setPeripheral(runner->core, mPERIPH_ROTATION, &rotation.d);
342
343	rumble.d.setRumble = _setRumble;
344	CircleBufferInit(&rumble.history, RUMBLE_PWM);
345	runner->core->setPeripheral(runner->core, mPERIPH_RUMBLE, &rumble.d);
346
347	camera.d.startRequestImage = _startRequestImage;
348	camera.d.stopRequestImage = _stopRequestImage;
349	camera.d.requestImage = _requestImage;
350	camera.buffer = NULL;
351	camera.cam = 1;
352	runner->core->setPeripheral(runner->core, mPERIPH_IMAGE_SOURCE, &camera.d);
353
354
355	stream.videoDimensionsChanged = NULL;
356	stream.postAudioFrame = NULL;
357	stream.postAudioBuffer = _postAudioBuffer;
358	stream.postVideoFrame = NULL;
359	runner->core->setAVStream(runner->core, &stream);
360
361	frameLimiter = true;
362	backdrop = vita2d_load_PNG_buffer(_binary_backdrop_png_start);
363
364	unsigned mode;
365	if (mCoreConfigGetUIntValue(&runner->config, "screenMode", &mode) && mode < SM_MAX) {
366		screenMode = mode;
367	}
368	if (mCoreConfigGetUIntValue(&runner->config, "camera", &mode)) {
369		camera.cam = mode;
370	}
371	int fakeBool;
372	if (mCoreConfigGetIntValue(&runner->config, "sgb.borderCrop", &fakeBool)) {
373		sgbCrop = fakeBool;
374	}
375}
376
377void mPSP2LoadROM(struct mGUIRunner* runner) {
378	float rate = 60.0f / 1.001f;
379	sceDisplayGetRefreshRate(&rate);
380	double ratio = GBAAudioCalculateRatio(1, rate, 1);
381	blip_set_rates(runner->core->getAudioChannel(runner->core, 0), runner->core->frequency(runner->core), 48000 * ratio);
382	blip_set_rates(runner->core->getAudioChannel(runner->core, 1), runner->core->frequency(runner->core), 48000 * ratio);
383
384	switch (runner->core->platform(runner->core)) {
385#ifdef M_CORE_GBA
386	case PLATFORM_GBA:
387		if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
388			sceMotionStartSampling();
389		}
390		break;
391#endif
392#ifdef M_CORE_GB
393	case PLATFORM_GB:
394		if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
395			sceMotionStartSampling();
396		}
397		break;
398#endif
399	default:
400		break;
401	}
402
403	int fakeBool;
404	if (mCoreConfigGetIntValue(&runner->config, "interframeBlending", &fakeBool)) {
405		interframeBlending = fakeBool;
406	}
407
408	// Backcompat: Old versions of mGBA use an older binding system that has different mappings for L/R
409	if (!sceKernelIsPSVitaTV()) {
410		int key = mInputMapKey(&runner->core->inputMap, PSP2_INPUT, __builtin_ctz(SCE_CTRL_L2));
411		if (key >= 0) {
412			mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_L1, key);
413		}
414		key = mInputMapKey(&runner->core->inputMap, PSP2_INPUT, __builtin_ctz(SCE_CTRL_R2));
415		if (key >= 0) {
416			mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_R1, key);
417		}
418	}
419
420	MutexInit(&audioContext.mutex);
421	ConditionInit(&audioContext.cond);
422	memset(audioContext.buffer, 0, sizeof(audioContext.buffer));
423	audioContext.readOffset = 0;
424	audioContext.writeOffset = 0;
425	audioContext.running = true;
426	ThreadCreate(&audioThread, _audioThread, &audioContext);
427}
428
429
430void mPSP2UnloadROM(struct mGUIRunner* runner) {
431	switch (runner->core->platform(runner->core)) {
432#ifdef M_CORE_GBA
433	case PLATFORM_GBA:
434		if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
435			sceMotionStopSampling();
436		}
437		break;
438#endif
439#ifdef M_CORE_GB
440	case PLATFORM_GB:
441		if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
442			sceMotionStopSampling();
443		}
444		break;
445#endif
446	default:
447		break;
448	}
449	audioContext.running = false;
450	ThreadJoin(&audioThread);
451}
452
453void mPSP2Paused(struct mGUIRunner* runner) {
454	UNUSED(runner);
455	struct SceCtrlActuator state = {
456		0,
457		0
458	};
459	sceCtrlSetActuator(1, &state);
460	frameLimiter = true;
461}
462
463void mPSP2Unpaused(struct mGUIRunner* runner) {
464	unsigned mode;
465	if (mCoreConfigGetUIntValue(&runner->config, "screenMode", &mode) && mode != screenMode) {
466		screenMode = mode;
467	}
468
469	if (mCoreConfigGetUIntValue(&runner->config, "camera", &mode)) {
470		if (mode != camera.cam) {
471			if (camera.buffer) {
472				sceCameraStop(camera.cam - 1);
473				sceCameraClose(camera.cam - 1);
474			}
475			camera.cam = mode;
476			if (camera.buffer) {
477				_resetCamera(&camera);
478			}
479		}
480	}
481
482	int fakeBool;
483	if (mCoreConfigGetIntValue(&runner->config, "interframeBlending", &fakeBool)) {
484		interframeBlending = fakeBool;
485	}
486
487	if (mCoreConfigGetIntValue(&runner->config, "sgb.borderCrop", &fakeBool)) {
488		sgbCrop = fakeBool;
489	}
490}
491
492void mPSP2Teardown(struct mGUIRunner* runner) {
493	UNUSED(runner);
494	CircleBufferDeinit(&rumble.history);
495	vita2d_free_texture(tex[0]);
496	vita2d_free_texture(tex[1]);
497	vita2d_free_texture(tex[2]);
498	vita2d_free_texture(tex[3]);
499	vita2d_free_texture(screenshot);
500	mappedMemoryFree(outputBuffer, 256 * 256 * 4);
501	frameLimiter = true;
502}
503
504void _drawTex(vita2d_texture* t, unsigned width, unsigned height, bool faded, bool interframe) {
505	unsigned w = width;
506	unsigned h = height;
507	// Get greatest common divisor
508	while (w != 0) {
509		int temp = h % w;
510		h = w;
511		w = temp;
512	}
513	int gcd = h;
514	int aspectw = width / gcd;
515	int aspecth = height / gcd;
516	float scalex;
517	float scaley;
518
519	unsigned tint = 0x1FFFFFFF;
520	if (!faded) {
521		if (interframe) {
522			tint |= 0x60000000;
523		} else {
524			tint |= 0xE0000000;
525		}
526	} else if (!interframe) {
527		tint |= 0x20000000;
528	}
529
530	switch (screenMode) {
531	case SM_BACKDROP:
532	default:
533		vita2d_draw_texture_tint(backdrop, 0, 0, tint);
534		// Fall through
535	case SM_PLAIN:
536		if (sgbCrop && width == 256 && height == 224) {
537			w = 768;
538			h = 672;
539			scalex = 3;
540			scaley = 3;
541			break;
542		}
543		w = 960 / width;
544		h = 544 / height;
545		if (w * height > 544) {
546			scalex = h;
547			w = width * h;
548			h = height * h;
549		} else {
550			scalex = w;
551			w = width * w;
552			h = height * w;
553		}
554		scaley = scalex;
555		break;
556	case SM_ASPECT:
557		if (sgbCrop && width == 256 && height == 224) {
558			w = 967;
559			h = 846;
560			scalex = 34.0f / 9.0f;
561			scaley = scalex;
562			break;
563		}
564		w = 960 / aspectw;
565		h = 544 / aspecth;
566		if (w * aspecth > 544) {
567			w = aspectw * h;
568			h = aspecth * h;
569		} else {
570			w = aspectw * w;
571			h = aspecth * w;
572		}
573		scalex = w / (float) width;
574		scaley = scalex;
575		break;
576	case SM_FULL:
577		w = 960;
578		h = 544;
579		scalex = 960.0f / width;
580		scaley = 544.0f / height;
581		break;
582	}
583	vita2d_draw_texture_tint_part_scale(t,
584	                                    (960.0f - w) / 2.0f, (544.0f - h) / 2.0f,
585	                                    0, 0, width, height,
586	                                    scalex, scaley,
587	                                    tint);
588}
589
590void mPSP2Swap(struct mGUIRunner* runner) {
591	currentTex = (currentTex + 1) & 3;
592	runner->core->setVideoBuffer(runner->core, vita2d_texture_get_datap(tex[currentTex]), 256);
593}
594
595void mPSP2Draw(struct mGUIRunner* runner, bool faded) {
596	unsigned width, height;
597	runner->core->desiredVideoDimensions(runner->core, &width, &height);
598	if (interframeBlending) {
599		_drawTex(tex[(currentTex - 1) & 3], width, height, faded, false);
600	}
601	_drawTex(tex[currentTex], width, height, faded, interframeBlending);
602}
603
604void mPSP2DrawScreenshot(struct mGUIRunner* runner, const uint32_t* pixels, unsigned width, unsigned height, bool faded) {
605	UNUSED(runner);
606	uint32_t* texpixels = vita2d_texture_get_datap(screenshot);
607	unsigned y;
608	for (y = 0; y < height; ++y) {
609		memcpy(&texpixels[256 * y], &pixels[width * y], width * 4);
610	}
611	_drawTex(screenshot, width, height, faded, false);
612}
613
614void mPSP2IncrementScreenMode(struct mGUIRunner* runner) {
615	screenMode = (screenMode + 1) % SM_MAX;
616	mCoreConfigSetUIntValue(&runner->config, "screenMode", screenMode);
617}
618
619bool mPSP2SystemPoll(struct mGUIRunner* runner) {
620	SceAppMgrSystemEvent event;
621	if (sceAppMgrReceiveSystemEvent(&event) < 0) {
622		return true;
623	}
624	if (event.systemEvent == SCE_APPMGR_SYSTEMEVENT_ON_RESUME) {
625		mLOG(GUI_PSP2, INFO, "Suspend detected, reloading save");
626		mCoreAutoloadSave(runner->core);
627	}
628	return true;
629}
630
631__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
632	printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
633	exit(1);
634}