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