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