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#include "gba/gba.h"
11#include "gba/audio.h"
12#include "gba/gui/gui-runner.h"
13#include "gba/input.h"
14
15#include "gba/renderers/video-software.h"
16#include "util/circle-buffer.h"
17#include "util/memory.h"
18#include "util/threading.h"
19#include "util/vfs.h"
20#include "platform/psp2/sce-vfs.h"
21#include "third-party/blip_buf/blip_buf.h"
22
23#include <psp2/audioout.h>
24#include <psp2/ctrl.h>
25#include <psp2/display.h>
26#include <psp2/gxm.h>
27#include <psp2/kernel/sysmem.h>
28#include <psp2/motion.h>
29#include <psp2/power.h>
30
31#include <vita2d.h>
32
33static enum ScreenMode {
34 SM_BACKDROP,
35 SM_PLAIN,
36 SM_FULL,
37 SM_MAX
38} screenMode;
39
40static void* outputBuffer;
41static vita2d_texture* tex;
42static vita2d_texture* screenshot;
43static Thread audioThread;
44static struct GBASceRotationSource {
45 struct mRotationSource d;
46 struct SceMotionSensorState state;
47} rotation;
48
49extern const uint8_t _binary_backdrop_png_start[];
50static vita2d_texture* backdrop = 0;
51
52#define PSP2_SAMPLES 64
53#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 19)
54
55static struct GBAPSP2AudioContext {
56 struct CircleBuffer buffer;
57 Mutex mutex;
58 Condition cond;
59 bool running;
60} audioContext;
61
62static void _mapVitaKey(struct mInputMap* map, int pspKey, enum GBAKey key) {
63 mInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
64}
65
66static THREAD_ENTRY _audioThread(void* context) {
67 struct GBAPSP2AudioContext* audio = (struct GBAPSP2AudioContext*) context;
68 struct GBAStereoSample buffer[PSP2_SAMPLES];
69 int audioPort = sceAudioOutOpenPort(SCE_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_SAMPLES, 48000, SCE_AUDIO_OUT_MODE_STEREO);
70 while (audio->running) {
71 memset(buffer, 0, sizeof(buffer));
72 MutexLock(&audio->mutex);
73 int len = CircleBufferSize(&audio->buffer);
74 len /= sizeof(buffer[0]);
75 if (len > PSP2_SAMPLES) {
76 len = PSP2_SAMPLES;
77 }
78 if (len > 0) {
79 len &= ~(SCE_AUDIO_MIN_LEN - 1);
80 CircleBufferRead(&audio->buffer, buffer, len * sizeof(buffer[0]));
81 MutexUnlock(&audio->mutex);
82 sceAudioOutOutput(audioPort, buffer);
83 MutexLock(&audio->mutex);
84 }
85
86 if (CircleBufferSize(&audio->buffer) < PSP2_SAMPLES) {
87 ConditionWait(&audio->cond, &audio->mutex);
88 }
89 MutexUnlock(&audio->mutex);
90 }
91 sceAudioOutReleasePort(audioPort);
92 return 0;
93}
94
95static void _sampleRotation(struct mRotationSource* source) {
96 struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
97 sceMotionGetSensorState(&rotation->state, 1);
98}
99
100static int32_t _readTiltX(struct mRotationSource* source) {
101 struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
102 return rotation->state.accelerometer.x * 0x60000000;
103}
104
105static int32_t _readTiltY(struct mRotationSource* source) {
106 struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
107 return rotation->state.accelerometer.y * 0x60000000;
108}
109
110static int32_t _readGyroZ(struct mRotationSource* source) {
111 struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
112 return rotation->state.gyro.z * 0x10000000;
113}
114
115uint16_t GBAPSP2PollInput(struct mGUIRunner* runner) {
116 SceCtrlData pad;
117 sceCtrlPeekBufferPositive(0, &pad, 1);
118
119 int activeKeys = mInputMapKeyBits(&runner->core->inputMap, PSP2_INPUT, pad.buttons, 0);
120 enum GBAKey angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 0, pad.ly);
121 if (angles != GBA_KEY_NONE) {
122 activeKeys |= 1 << angles;
123 }
124 angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 1, pad.lx);
125 if (angles != GBA_KEY_NONE) {
126 activeKeys |= 1 << angles;
127 }
128 angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 2, pad.ry);
129 if (angles != GBA_KEY_NONE) {
130 activeKeys |= 1 << angles;
131 }
132 angles = mInputMapAxis(&runner->core->inputMap, PSP2_INPUT, 3, pad.rx);
133 if (angles != GBA_KEY_NONE) {
134 activeKeys |= 1 << angles;
135 }
136 return activeKeys;
137}
138
139void GBAPSP2Setup(struct mGUIRunner* runner) {
140 scePowerSetArmClockFrequency(80);
141 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_CROSS, GBA_KEY_A);
142 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_CIRCLE, GBA_KEY_B);
143 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_START, GBA_KEY_START);
144 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_SELECT, GBA_KEY_SELECT);
145 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_UP, GBA_KEY_UP);
146 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_DOWN, GBA_KEY_DOWN);
147 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_LEFT, GBA_KEY_LEFT);
148 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_RIGHT, GBA_KEY_RIGHT);
149 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_LTRIGGER, GBA_KEY_L);
150 _mapVitaKey(&runner->core->inputMap, SCE_CTRL_RTRIGGER, GBA_KEY_R);
151
152 struct mInputAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
153 mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 0, &desc);
154 desc = (struct mInputAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
155 mInputBindAxis(&runner->core->inputMap, PSP2_INPUT, 1, &desc);
156
157 tex = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
158 screenshot = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
159
160 outputBuffer = vita2d_texture_get_datap(tex);
161 runner->core->setVideoBuffer(runner->core, outputBuffer, 256);
162
163 rotation.d.sample = _sampleRotation;
164 rotation.d.readTiltX = _readTiltX;
165 rotation.d.readTiltY = _readTiltY;
166 rotation.d.readGyroZ = _readGyroZ;
167 ((struct GBA*) runner->core->board)->rotationSource = &rotation.d;
168
169 backdrop = vita2d_load_PNG_buffer(_binary_backdrop_png_start);
170}
171
172void GBAPSP2LoadROM(struct mGUIRunner* runner) {
173 scePowerSetArmClockFrequency(444);
174 double ratio = GBAAudioCalculateRatio(1, 60, 1);
175 blip_set_rates(runner->core->getAudioChannel(runner->core, 0), GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
176 blip_set_rates(runner->core->getAudioChannel(runner->core, 0), GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
177
178 if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
179 sceMotionStartSampling();
180 }
181
182 CircleBufferInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample));
183 MutexInit(&audioContext.mutex);
184 ConditionInit(&audioContext.cond);
185 audioContext.running = true;
186 ThreadCreate(&audioThread, _audioThread, &audioContext);
187}
188
189void GBAPSP2PrepareForFrame(struct mGUIRunner* runner) {
190 MutexLock(&audioContext.mutex);
191 while (blip_samples_avail(runner->core->getAudioChannel(runner->core, 0)) >= PSP2_SAMPLES) {
192 if (CircleBufferSize(&audioContext.buffer) + PSP2_SAMPLES * sizeof(struct GBAStereoSample) > CircleBufferCapacity(&audioContext.buffer)) {
193 break;
194 }
195 struct GBAStereoSample samples[PSP2_SAMPLES];
196 blip_read_samples(runner->core->getAudioChannel(runner->core, 0), &samples[0].left, PSP2_SAMPLES, true);
197 blip_read_samples(runner->core->getAudioChannel(runner->core, 1), &samples[0].right, PSP2_SAMPLES, true);
198 int i;
199 for (i = 0; i < PSP2_SAMPLES; ++i) {
200 CircleBufferWrite16(&audioContext.buffer, samples[i].left);
201 CircleBufferWrite16(&audioContext.buffer, samples[i].right);
202 }
203 }
204 ConditionWake(&audioContext.cond);
205 MutexUnlock(&audioContext.mutex);
206}
207
208void GBAPSP2UnloadROM(struct mGUIRunner* runner) {
209 if (((struct GBA*) runner->core->board)->memory.hw.devices & (HW_TILT | HW_GYRO)) {
210 sceMotionStopSampling();
211 }
212 scePowerSetArmClockFrequency(80);
213}
214
215void GBAPSP2Teardown(struct mGUIRunner* runner) {
216 vita2d_free_texture(tex);
217 vita2d_free_texture(screenshot);
218}
219
220void GBAPSP2Draw(struct mGUIRunner* runner, bool faded) {
221 UNUSED(runner);
222 switch (screenMode) {
223 case SM_BACKDROP:
224 default:
225 vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
226 // Fall through
227 case SM_PLAIN:
228 vita2d_draw_texture_tint_part_scale(tex, 120, 32, 0, 0, 240, 160, 3.0f, 3.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
229 break;
230 case SM_FULL:
231 vita2d_draw_texture_tint_scale(tex, 0, 0, 960.0f / 240.0f, 544.0f / 160.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
232 break;
233 }
234}
235
236void GBAPSP2DrawScreenshot(struct mGUIRunner* runner, const uint32_t* pixels, bool faded) {
237 UNUSED(runner);
238 uint32_t* texpixels = vita2d_texture_get_datap(screenshot);
239 int y;
240 for (y = 0; y < VIDEO_VERTICAL_PIXELS; ++y) {
241 memcpy(&texpixels[256 * y], &pixels[VIDEO_HORIZONTAL_PIXELS * y], VIDEO_HORIZONTAL_PIXELS * 4);
242 }
243 switch (screenMode) {
244 case SM_BACKDROP:
245 default:
246 vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
247 // Fall through
248 case SM_PLAIN:
249 vita2d_draw_texture_tint_part_scale(screenshot, 120, 32, 0, 0, 240, 160, 3.0f, 3.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
250 break;
251 case SM_FULL:
252 vita2d_draw_texture_tint_scale(screenshot, 0, 0, 960.0f / 240.0f, 544.0f / 160.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
253 break;
254 }
255}
256
257void GBAPSP2IncrementScreenMode(struct mGUIRunner* runner) {
258 unsigned mode;
259 if (mCoreConfigGetUIntValue(&runner->core->config, "screenMode", &mode) && mode != screenMode) {
260 screenMode = mode;
261 } else {
262 screenMode = (screenMode + 1) % SM_MAX;
263 mCoreConfigSetUIntValue(&runner->core->config, "screenMode", screenMode);
264 }
265}
266
267__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
268 printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
269 exit(1);
270}