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