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