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/ring-fifo.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;
52static struct mRumble rumble;
53
54extern const uint8_t _binary_backdrop_png_start[];
55static vita2d_texture* backdrop = 0;
56
57#define PSP2_SAMPLES 64
58#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 40)
59
60static struct mPSP2AudioContext {
61 struct RingFIFO buffer;
62 size_t samples;
63 Mutex mutex;
64 Condition cond;
65 bool running;
66} audioContext;
67
68static void _mapVitaKey(struct mInputMap* map, int pspKey, enum GBAKey key) {
69 mInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
70}
71
72static THREAD_ENTRY _audioThread(void* context) {
73 struct mPSP2AudioContext* audio = (struct mPSP2AudioContext*) context;
74 int audioPort = sceAudioOutOpenPort(SCE_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_SAMPLES, 48000, SCE_AUDIO_OUT_MODE_STEREO);
75 while (audio->running) {
76 MutexLock(&audio->mutex);
77 int len = audio->samples;
78 if (len > PSP2_SAMPLES) {
79 len = PSP2_SAMPLES;
80 }
81 struct GBAStereoSample* buffer = audio->buffer.readPtr;
82 RingFIFORead(&audio->buffer, NULL, len * 4);
83 audio->samples -= len;
84
85 MutexUnlock(&audio->mutex);
86 sceAudioOutOutput(audioPort, buffer);
87 MutexLock(&audio->mutex);
88
89 if (audio->samples < PSP2_SAMPLES) {
90 ConditionWait(&audio->cond, &audio->mutex);
91 }
92 MutexUnlock(&audio->mutex);
93 }
94 sceAudioOutReleasePort(audioPort);
95 return 0;
96}
97
98static void _sampleRotation(struct mRotationSource* source) {
99 struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
100 sceMotionGetSensorState(&rotation->state, 1);
101}
102
103static int32_t _readTiltX(struct mRotationSource* source) {
104 struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
105 return rotation->state.accelerometer.x * 0x30000000;
106}
107
108static int32_t _readTiltY(struct mRotationSource* source) {
109 struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
110 return rotation->state.accelerometer.y * -0x30000000;
111}
112
113static int32_t _readGyroZ(struct mRotationSource* source) {
114 struct mSceRotationSource* rotation = (struct mSceRotationSource*) source;
115 return rotation->state.gyro.z * -0x10000000;
116}
117
118static void _setRumble(struct mRumble* rumble, int enable) {
119 UNUSED(rumble);
120 struct SceCtrlActuator state = {
121 enable,
122 0
123 };
124 sceCtrlSetActuator(1, &state);
125}
126
127uint16_t mPSP2PollInput(struct mGUIRunner* runner) {
128 SceCtrlData pad;
129 sceCtrlPeekBufferPositive(0, &pad, 1);
130
131 int activeKeys = mInputMapKeyBits(&runner->core->inputMap, PSP2_INPUT, pad.buttons, 0);
132 enum GBAKey angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 0, pad.ly);
133 if (angles != GBA_KEY_NONE) {
134 activeKeys |= 1 << angles;
135 }
136 angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 1, pad.lx);
137 if (angles != GBA_KEY_NONE) {
138 activeKeys |= 1 << angles;
139 }
140 angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 2, pad.ry);
141 if (angles != GBA_KEY_NONE) {
142 activeKeys |= 1 << angles;
143 }
144 angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 3, pad.rx);
145 if (angles != GBA_KEY_NONE) {
146 activeKeys |= 1 << angles;
147 }
148 return activeKeys;
149}
150
151void mPSP2Setup(struct mGUIRunner* runner) {
152 mCoreConfigSetDefaultIntValue(&runner->core->config, "threadedVideo", 1);
153 mCoreLoadConfig(runner->core);
154
155 scePowerSetArmClockFrequency(80);
156 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_CROSS, GBA_KEY_A);
157 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_CIRCLE, GBA_KEY_B);
158 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_START, GBA_KEY_START);
159 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_SELECT, GBA_KEY_SELECT);
160 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_UP, GBA_KEY_UP);
161 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_DOWN, GBA_KEY_DOWN);
162 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_LEFT, GBA_KEY_LEFT);
163 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_RIGHT, GBA_KEY_RIGHT);
164 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_LTRIGGER, GBA_KEY_L);
165 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_RTRIGGER, GBA_KEY_R);
166
167 struct mInputAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
168 mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 0, &desc);
169 desc = (struct mInputAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
170 mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 1, &desc);
171
172 tex = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
173 screenshot = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
174
175 outputBuffer = vita2d_texture_get_datap(tex);
176 runner->core->setVideoBuffer(runner->core, outputBuffer, 256);
177
178 rotation.d.sample = _sampleRotation;
179 rotation.d.readTiltX = _readTiltX;
180 rotation.d.readTiltY = _readTiltY;
181 rotation.d.readGyroZ = _readGyroZ;
182 runner->core->setRotation(runner->core, &rotation.d);
183
184 rumble.setRumble = _setRumble;
185 runner->core->setRumble(runner->core, &rumble);
186
187 backdrop = vita2d_load_PNG_buffer(_binary_backdrop_png_start);
188
189 unsigned mode;
190 if (mCoreConfigGetUIntValue(&runner->core->config, "screenMode", &mode) && mode < SM_MAX) {
191 screenMode = mode;
192 }
193}
194
195void mPSP2LoadROM(struct mGUIRunner* runner) {
196 scePowerSetArmClockFrequency(444);
197 double ratio = GBAAudioCalculateRatio(1, 60, 1);
198 blip_set_rates(runner->core->getAudioChannel(runner->core, 0), runner->core->frequency(runner->core), 48000 * ratio);
199 blip_set_rates(runner->core->getAudioChannel(runner->core, 1), runner->core->frequency(runner->core), 48000 * ratio);
200
201 switch (runner->core->platform(runner->core)) {
202#ifdef M_CORE_GBA
203 case PLATFORM_GBA:
204 if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
205 sceMotionStartSampling();
206 }
207 break;
208#endif
209#ifdef M_CORE_GB
210 case PLATFORM_GB:
211 if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
212 sceMotionStartSampling();
213 }
214 break;
215#endif
216 default:
217 break;
218 }
219
220 RingFIFOInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample), PSP2_SAMPLES * 4);
221 MutexInit(&audioContext.mutex);
222 ConditionInit(&audioContext.cond);
223 audioContext.running = true;
224 ThreadCreate(&audioThread, _audioThread, &audioContext);
225}
226
227void mPSP2PrepareForFrame(struct mGUIRunner* runner) {
228 MutexLock(&audioContext.mutex);
229 while (blip_samples_avail(runner->core->getAudioChannel(runner->core, 0)) >= PSP2_SAMPLES) {
230 struct GBAStereoSample* samples = audioContext.buffer.writePtr;
231 blip_read_samples(runner->core->getAudioChannel(runner->core, 0), &samples[0].left, PSP2_SAMPLES, true);
232 blip_read_samples(runner->core->getAudioChannel(runner->core, 1), &samples[0].right, PSP2_SAMPLES, true);
233 RingFIFOWrite(&audioContext.buffer, NULL, PSP2_SAMPLES * 4);
234 audioContext.samples += PSP2_SAMPLES;
235 }
236 ConditionWake(&audioContext.cond);
237 MutexUnlock(&audioContext.mutex);
238}
239
240void mPSP2UnloadROM(struct mGUIRunner* runner) {
241 switch (runner->core->platform(runner->core)) {
242#ifdef M_CORE_GBA
243 case PLATFORM_GBA:
244 if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
245 sceMotionStopSampling();
246 }
247 break;
248#endif
249#ifdef M_CORE_GB
250 case PLATFORM_GB:
251 if (((struct GB*) runner->core->board)->memory.mbcType == GB_MBC7) {
252 sceMotionStopSampling();
253 }
254 break;
255#endif
256 default:
257 break;
258 }
259 scePowerSetArmClockFrequency(80);
260}
261
262void mPSP2Paused(struct mGUIRunner* runner) {
263 UNUSED(runner);
264 struct SceCtrlActuator state = {
265 0,
266 0
267 };
268 sceCtrlSetActuator(1, &state);
269}
270
271void mPSP2Unpaused(struct mGUIRunner* runner) {
272 unsigned mode;
273 if (mCoreConfigGetUIntValue(&runner->core->config, "screenMode", &mode) && mode != screenMode) {
274 screenMode = mode;
275 }
276}
277
278void mPSP2Teardown(struct mGUIRunner* runner) {
279 vita2d_free_texture(tex);
280 vita2d_free_texture(screenshot);
281}
282
283void mPSP2Draw(struct mGUIRunner* runner, bool faded) {
284 unsigned width, height;
285 runner->core->desiredVideoDimensions(runner->core, &width, &height);
286 switch (screenMode) {
287 case SM_BACKDROP:
288 default:
289 vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
290 // Fall through
291 case SM_PLAIN:
292 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);
293 break;
294 case SM_FULL:
295 vita2d_draw_texture_tint_scale(tex, 0, 0, 960.0f / width, 544.0f / height, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
296 break;
297 }
298}
299
300void mPSP2DrawScreenshot(struct mGUIRunner* runner, const uint32_t* pixels, unsigned width, unsigned height, bool faded) {
301 UNUSED(runner);
302 uint32_t* texpixels = vita2d_texture_get_datap(screenshot);
303 int y;
304 for (y = 0; y < height; ++y) {
305 memcpy(&texpixels[256 * y], &pixels[width * y], width * 4);
306 }
307 switch (screenMode) {
308 case SM_BACKDROP:
309 default:
310 vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
311 // Fall through
312 case SM_PLAIN:
313 vita2d_draw_texture_tint_part_scale(screenshot, (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);
314 break;
315 case SM_FULL:
316 vita2d_draw_texture_tint_scale(screenshot, 0, 0, 960.0f / width, 544.0f / height, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
317 break;
318 }
319}
320
321void mPSP2IncrementScreenMode(struct mGUIRunner* runner) {
322 screenMode = (screenMode + 1) % SM_MAX;
323 mCoreConfigSetUIntValue(&runner->core->config, "screenMode", screenMode);
324}
325
326__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
327 printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
328 exit(1);
329}