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/audioout.h>
29#include <psp2/ctrl.h>
30#include <psp2/display.h>
31#include <psp2/gxm.h>
32#include <psp2/kernel/sysmem.h>
33#include <psp2/motion.h>
34
35#include <vita2d.h>
36
37#define RUMBLE_PWM 8
38
39static enum ScreenMode {
40 SM_BACKDROP,
41 SM_PLAIN,
42 SM_FULL,
43 SM_ASPECT,
44 SM_MAX
45} screenMode;
46
47static void* outputBuffer;
48static vita2d_texture* tex;
49static vita2d_texture* screenshot;
50static Thread audioThread;
51static struct mSceRotationSource {
52 struct mRotationSource d;
53 struct SceMotionSensorState state;
54} rotation;
55static struct mSceRumble {
56 struct mRumble d;
57 struct CircleBuffer history;
58 int current;
59} rumble;
60
61static struct mAVStream stream;
62
63bool frameLimiter = true;
64
65extern const uint8_t _binary_backdrop_png_start[];
66static vita2d_texture* backdrop = 0;
67
68#define PSP2_SAMPLES 512
69#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 16)
70
71static struct mPSP2AudioContext {
72 struct GBAStereoSample buffer[PSP2_AUDIO_BUFFER_SIZE];
73 size_t writeOffset;
74 size_t readOffset;
75 size_t samples;
76 Mutex mutex;
77 Condition cond;
78 bool running;
79} audioContext;
80
81void mPSP2MapKey(struct mInputMap* map, int pspKey, int key) {
82 mInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
83}
84
85static THREAD_ENTRY _audioThread(void* context) {
86 struct mPSP2AudioContext* audio = (struct mPSP2AudioContext*) context;
87 uint32_t zeroBuffer[PSP2_SAMPLES] = {0};
88 int audioPort = sceAudioOutOpenPort(SCE_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_SAMPLES, 48000, SCE_AUDIO_OUT_MODE_STEREO);
89 while (audio->running) {
90 MutexLock(&audio->mutex);
91 void* buffer;
92 if (audio->samples >= PSP2_SAMPLES) {
93 buffer = &audio->buffer[audio->readOffset];
94 audio->samples -= PSP2_SAMPLES;
95 audio->readOffset += PSP2_SAMPLES;
96 if (audio->readOffset >= PSP2_AUDIO_BUFFER_SIZE) {
97 audio->readOffset = 0;
98 }
99 ConditionWake(&audio->cond);
100 } else {
101 buffer = zeroBuffer;
102 }
103 MutexUnlock(&audio->mutex);
104
105 sceAudioOutOutput(audioPort, buffer);
106 }
107 sceAudioOutReleasePort(audioPort);
108 return 0;
109}
110
111static void _sampleRotation(struct mRotationSource* source) {
112 struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
113 sceMotionGetSensorState(&rotation->state, 1);
114}
115
116static int32_t _readTiltX(struct mRotationSource* source) {
117 struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
118 return rotation->state.accelerometer.x * 0x30000000;
119}
120
121static int32_t _readTiltY(struct mRotationSource* source) {
122 struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
123 return rotation->state.accelerometer.y * -0x30000000;
124}
125
126static int32_t _readGyroZ(struct mRotationSource* source) {
127 struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
128 return rotation->state.gyro.z * -0x10000000;
129}
130
131static void _setRumble(struct mRumble* source, int enable) {
132 struct mSceRumble* rumble = (struct mSceRumble*) source;
133 rumble->current += enable;
134 if (CircleBufferSize(&rumble->history) == RUMBLE_PWM) {
135 int8_t oldLevel;
136 CircleBufferRead8(&rumble->history, &oldLevel);
137 rumble->current -= oldLevel;
138 }
139 CircleBufferWrite8(&rumble->history, enable);
140 int small = (rumble->current << 21) / 65793;
141 int big = ((rumble->current * rumble->current) << 18) / 65793;
142 struct SceCtrlActuator state = {
143 small,
144 big
145 };
146 sceCtrlSetActuator(1, &state);
147}
148
149static void _postAudioBuffer(struct mAVStream* stream, blip_t* left, blip_t* right) {
150 UNUSED(stream);
151 MutexLock(&audioContext.mutex);
152 struct GBAStereoSample* samples = &audioContext.buffer[audioContext.writeOffset];
153 while (audioContext.samples == PSP2_AUDIO_BUFFER_SIZE) {
154 if (!frameLimiter) {
155 blip_clear(left);
156 blip_clear(right);
157 MutexUnlock(&audioContext.mutex);
158 return;
159 }
160 ConditionWait(&audioContext.cond, &audioContext.mutex);
161 }
162 blip_read_samples(left, &samples[0].left, PSP2_SAMPLES, true);
163 blip_read_samples(right, &samples[0].right, PSP2_SAMPLES, true);
164 audioContext.samples += PSP2_SAMPLES;
165 audioContext.writeOffset += PSP2_SAMPLES;
166 if (audioContext.writeOffset >= PSP2_AUDIO_BUFFER_SIZE) {
167 audioContext.writeOffset = 0;
168 }
169 MutexUnlock(&audioContext.mutex);
170}
171
172uint16_t mPSP2PollInput(struct mGUIRunner* runner) {
173 SceCtrlData pad;
174 sceCtrlPeekBufferPositive(0, &pad, 1);
175
176 int activeKeys = mInputMapKeyBits(&runner->core->inputMap, PSP2_INPUT, pad.buttons, 0);
177 int angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 0, pad.ly);
178 if (angles != GBA_KEY_NONE) {
179 activeKeys |= 1 << angles;
180 }
181 angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 1, pad.lx);
182 if (angles != GBA_KEY_NONE) {
183 activeKeys |= 1 << angles;
184 }
185 angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 2, pad.ry);
186 if (angles != GBA_KEY_NONE) {
187 activeKeys |= 1 << angles;
188 }
189 angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 3, pad.rx);
190 if (angles != GBA_KEY_NONE) {
191 activeKeys |= 1 << angles;
192 }
193 return activeKeys;
194}
195
196void mPSP2SetFrameLimiter(struct mGUIRunner* runner, bool limit) {
197 UNUSED(runner);
198 frameLimiter = limit;
199}
200
201void mPSP2Setup(struct mGUIRunner* runner) {
202 mCoreConfigSetDefaultIntValue(&runner->config, "threadedVideo", 1);
203 mCoreLoadForeignConfig(runner->core, &runner->config);
204
205 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_CROSS, GBA_KEY_A);
206 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_CIRCLE, GBA_KEY_B);
207 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_START, GBA_KEY_START);
208 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_SELECT, GBA_KEY_SELECT);
209 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_UP, GBA_KEY_UP);
210 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_DOWN, GBA_KEY_DOWN);
211 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_LEFT, GBA_KEY_LEFT);
212 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_RIGHT, GBA_KEY_RIGHT);
213 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_LTRIGGER, GBA_KEY_L);
214 mPSP2MapKey(&runner->core->inputMap, SCE_CTRL_RTRIGGER, GBA_KEY_R);
215
216 struct mInputAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
217 mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 0, &desc);
218 desc = (struct mInputAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
219 mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 1, &desc);
220
221 unsigned width, height;
222 runner->core->desiredVideoDimensions(runner->core, &width, &height);
223 tex = vita2d_create_empty_texture_format(256, toPow2(height), SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
224 screenshot = vita2d_create_empty_texture_format(256, toPow2(height), SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
225
226 outputBuffer = vita2d_texture_get_datap(tex);
227 runner->core->setVideoBuffer(runner->core, outputBuffer, 256);
228 runner->core->setAudioBufferSize(runner->core, PSP2_SAMPLES);
229
230 rotation.d.sample = _sampleRotation;
231 rotation.d.readTiltX = _readTiltX;
232 rotation.d.readTiltY = _readTiltY;
233 rotation.d.readGyroZ = _readGyroZ;
234 runner->core->setPeripheral(runner->core, mPERIPH_ROTATION, &rotation.d);
235
236 rumble.d.setRumble = _setRumble;
237 CircleBufferInit(&rumble.history, RUMBLE_PWM);
238 runner->core->setPeripheral(runner->core, mPERIPH_RUMBLE, &rumble.d);
239
240 stream.videoDimensionsChanged = NULL;
241 stream.postAudioFrame = NULL;
242 stream.postAudioBuffer = _postAudioBuffer;
243 stream.postVideoFrame = NULL;
244 runner->core->setAVStream(runner->core, &stream);
245
246 frameLimiter = true;
247 backdrop = vita2d_load_PNG_buffer(_binary_backdrop_png_start);
248
249 unsigned mode;
250 if (mCoreConfigGetUIntValue(&runner->config, "screenMode", &mode) && mode < SM_MAX) {
251 screenMode = mode;
252 }
253}
254
255void mPSP2LoadROM(struct mGUIRunner* runner) {
256 float rate = 60.0f / 1.001f;
257 sceDisplayGetRefreshRate(&rate);
258 double ratio = GBAAudioCalculateRatio(1, rate, 1);
259 blip_set_rates(runner->core->getAudioChannel(runner->core, 0), runner->core->frequency(runner->core), 48000 * ratio);
260 blip_set_rates(runner->core->getAudioChannel(runner->core, 1), runner->core->frequency(runner->core), 48000 * ratio);
261
262 switch (runner->core->platform(runner->core)) {
263#ifdef M_CORE_GBA
264 case PLATFORM_GBA:
265 if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
266 sceMotionStartSampling();
267 }
268 break;
269#endif
270#ifdef M_CORE_GB
271 case PLATFORM_GB:
272 if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
273 sceMotionStartSampling();
274 }
275 break;
276#endif
277 default:
278 break;
279 }
280
281 MutexInit(&audioContext.mutex);
282 ConditionInit(&audioContext.cond);
283 memset(audioContext.buffer, 0, sizeof(audioContext.buffer));
284 audioContext.readOffset = 0;
285 audioContext.writeOffset = 0;
286 audioContext.running = true;
287 ThreadCreate(&audioThread, _audioThread, &audioContext);
288}
289
290
291void mPSP2UnloadROM(struct mGUIRunner* runner) {
292 switch (runner->core->platform(runner->core)) {
293#ifdef M_CORE_GBA
294 case PLATFORM_GBA:
295 if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
296 sceMotionStopSampling();
297 }
298 break;
299#endif
300#ifdef M_CORE_GB
301 case PLATFORM_GB:
302 if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
303 sceMotionStopSampling();
304 }
305 break;
306#endif
307 default:
308 break;
309 }
310}
311
312void mPSP2Paused(struct mGUIRunner* runner) {
313 UNUSED(runner);
314 struct SceCtrlActuator state = {
315 0,
316 0
317 };
318 sceCtrlSetActuator(1, &state);
319 frameLimiter = true;
320}
321
322void mPSP2Unpaused(struct mGUIRunner* runner) {
323 unsigned mode;
324 if (mCoreConfigGetUIntValue(&runner->config, "screenMode", &mode) && mode != screenMode) {
325 screenMode = mode;
326 }
327}
328
329void mPSP2Teardown(struct mGUIRunner* runner) {
330 UNUSED(runner);
331 CircleBufferDeinit(&rumble.history);
332 vita2d_free_texture(tex);
333 vita2d_free_texture(screenshot);
334 frameLimiter = true;
335}
336
337void _drawTex(vita2d_texture* t, unsigned width, unsigned height, bool faded) {
338 unsigned w = width;
339 unsigned h = height;
340 // Get greatest common divisor
341 while (w != 0) {
342 int temp = h % w;
343 h = w;
344 w = temp;
345 }
346 int gcd = h;
347 int aspectw = width / gcd;
348 int aspecth = height / gcd;
349 float scalex;
350 float scaley;
351
352 switch (screenMode) {
353 case SM_BACKDROP:
354 default:
355 vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
356 // Fall through
357 case SM_PLAIN:
358 w = 960 / width;
359 h = 544 / height;
360 if (w * height > 544) {
361 scalex = h;
362 w = width * h;
363 h = height * h;
364 } else {
365 scalex = w;
366 w = width * w;
367 h = height * w;
368 }
369 scaley = scalex;
370 break;
371 case SM_ASPECT:
372 w = 960 / aspectw;
373 h = 544 / aspecth;
374 if (w * aspecth > 544) {
375 w = aspectw * h;
376 h = aspecth * h;
377 } else {
378 w = aspectw * w;
379 h = aspecth * w;
380 }
381 scalex = w / (float) width;
382 scaley = scalex;
383 break;
384 case SM_FULL:
385 w = 960;
386 h = 544;
387 scalex = 960.0f / width;
388 scaley = 544.0f / height;
389 break;
390 }
391 vita2d_draw_texture_tint_part_scale(t,
392 (960.0f - w) / 2.0f, (544.0f - h) / 2.0f,
393 0, 0, width, height,
394 scalex, scaley,
395 (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
396}
397
398void mPSP2Draw(struct mGUIRunner* runner, bool faded) {
399 unsigned width, height;
400 runner->core->desiredVideoDimensions(runner->core, &width, &height);
401 _drawTex(tex, width, height, faded);
402}
403
404void mPSP2DrawScreenshot(struct mGUIRunner* runner, const uint32_t* pixels, unsigned width, unsigned height, bool faded) {
405 UNUSED(runner);
406 uint32_t* texpixels = vita2d_texture_get_datap(screenshot);
407 unsigned y;
408 for (y = 0; y < height; ++y) {
409 memcpy(&texpixels[256 * y], &pixels[width * y], width * 4);
410 }
411 _drawTex(screenshot, width, height, faded);
412}
413
414void mPSP2IncrementScreenMode(struct mGUIRunner* runner) {
415 screenMode = (screenMode + 1) % SM_MAX;
416 mCoreConfigSetUIntValue(&runner->config, "screenMode", screenMode);
417}
418
419__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
420 printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
421 exit(1);
422}