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
32enum ScreenMode {
33 SM_BACKDROP,
34 SM_PLAIN,
35 SM_FULL,
36 SM_MAX
37};
38
39static struct GBAVideoSoftwareRenderer renderer;
40static vita2d_texture* tex;
41static Thread audioThread;
42static struct GBASceRotationSource {
43 struct GBARotationSource d;
44 struct SceMotionSensorState state;
45} rotation;
46
47static int screenMode = 0;
48
49extern const uint8_t _binary_backdrop_png_start[];
50static vita2d_texture* backdrop = 0;
51
52#define PSP2_INPUT 0x50535032
53#define PSP2_SAMPLES 64
54#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 19)
55
56static struct GBAPSP2AudioContext {
57 struct CircleBuffer buffer;
58 Mutex mutex;
59 Condition cond;
60 bool running;
61} audioContext;
62
63static void _mapVitaKey(struct GBAInputMap* map, int pspKey, enum GBAKey key) {
64 GBAInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
65}
66
67static THREAD_ENTRY _audioThread(void* context) {
68 struct GBAPSP2AudioContext* audio = (struct GBAPSP2AudioContext*) context;
69 struct GBAStereoSample buffer[PSP2_SAMPLES];
70 int audioPort = sceAudioOutOpenPort(PSP2_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_SAMPLES, 48000, PSP2_AUDIO_OUT_MODE_STEREO);
71 while (audio->running) {
72 memset(buffer, 0, sizeof(buffer));
73 MutexLock(&audio->mutex);
74 int len = CircleBufferSize(&audio->buffer);
75 len /= sizeof(buffer[0]);
76 if (len > PSP2_SAMPLES) {
77 len = PSP2_SAMPLES;
78 }
79 if (len > 0) {
80 len &= ~(PSP2_AUDIO_MIN_LEN - 1);
81 CircleBufferRead(&audio->buffer, buffer, len * sizeof(buffer[0]));
82 MutexUnlock(&audio->mutex);
83 sceAudioOutOutput(audioPort, buffer);
84 MutexLock(&audio->mutex);
85 }
86
87 if (CircleBufferSize(&audio->buffer) < PSP2_SAMPLES) {
88 ConditionWait(&audio->cond, &audio->mutex);
89 }
90 MutexUnlock(&audio->mutex);
91 }
92 sceAudioOutReleasePort(audioPort);
93 return 0;
94}
95
96static void _sampleRotation(struct GBARotationSource* source) {
97 struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
98 sceMotionGetSensorState(&rotation->state, 1);
99}
100
101static int32_t _readTiltX(struct GBARotationSource* source) {
102 struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
103 return rotation->state.accelerometer.x * 0x60000000;
104}
105
106static int32_t _readTiltY(struct GBARotationSource* source) {
107 struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
108 return rotation->state.accelerometer.y * 0x60000000;
109}
110
111static int32_t _readGyroZ(struct GBARotationSource* source) {
112 struct GBASceRotationSource* rotation = (struct GBASceRotationSource*) source;
113 return rotation->state.gyro.z * 0x10000000;
114}
115
116uint16_t GBAPSP2PollInput(struct GBAGUIRunner* runner) {
117 SceCtrlData pad;
118 sceCtrlPeekBufferPositive(0, &pad, 1);
119
120 int activeKeys = GBAInputMapKeyBits(&runner->context.inputMap, PSP2_INPUT, pad.buttons, 0);
121 enum GBAKey angles = GBAInputMapAxis(&runner->context.inputMap, PSP2_INPUT, 0, pad.ly);
122 if (angles != GBA_KEY_NONE) {
123 activeKeys |= 1 << angles;
124 }
125 angles = GBAInputMapAxis(&runner->context.inputMap, PSP2_INPUT, 1, pad.lx);
126 if (angles != GBA_KEY_NONE) {
127 activeKeys |= 1 << angles;
128 }
129 angles = GBAInputMapAxis(&runner->context.inputMap, PSP2_INPUT, 2, pad.ry);
130 if (angles != GBA_KEY_NONE) {
131 activeKeys |= 1 << angles;
132 }
133 angles = GBAInputMapAxis(&runner->context.inputMap, PSP2_INPUT, 3, pad.rx);
134 if (angles != GBA_KEY_NONE) {
135 activeKeys |= 1 << angles;
136 }
137 return activeKeys;
138}
139
140void GBAPSP2Setup(struct GBAGUIRunner* runner) {
141 scePowerSetArmClockFrequency(80);
142 struct GBAOptions opts = {
143 .useBios = true,
144 .logLevel = 0,
145 .idleOptimization = IDLE_LOOP_DETECT
146 };
147 GBAConfigLoadDefaults(&runner->context.config, &opts);
148 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_CROSS, GBA_KEY_A);
149 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_CIRCLE, GBA_KEY_B);
150 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_START, GBA_KEY_START);
151 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_SELECT, GBA_KEY_SELECT);
152 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_UP, GBA_KEY_UP);
153 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_DOWN, GBA_KEY_DOWN);
154 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_LEFT, GBA_KEY_LEFT);
155 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_RIGHT, GBA_KEY_RIGHT);
156 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_LTRIGGER, GBA_KEY_L);
157 _mapVitaKey(&runner->context.inputMap, PSP2_CTRL_RTRIGGER, GBA_KEY_R);
158
159 struct GBAAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
160 GBAInputBindAxis(&runner->context.inputMap, PSP2_INPUT, 0, &desc);
161 desc = (struct GBAAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
162 GBAInputBindAxis(&runner->context.inputMap, PSP2_INPUT, 1, &desc);
163
164 tex = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
165
166 GBAVideoSoftwareRendererCreate(&renderer);
167 renderer.outputBuffer = vita2d_texture_get_datap(tex);
168 renderer.outputBufferStride = 256;
169 runner->context.renderer = &renderer.d;
170
171 rotation.d.sample = _sampleRotation;
172 rotation.d.readTiltX = _readTiltX;
173 rotation.d.readTiltY = _readTiltY;
174 rotation.d.readGyroZ = _readGyroZ;
175 runner->context.gba->rotationSource = &rotation.d;
176
177 backdrop = vita2d_load_PNG_buffer(_binary_backdrop_png_start);
178}
179
180void GBAPSP2LoadROM(struct GBAGUIRunner* runner) {
181 scePowerSetArmClockFrequency(444);
182 double ratio = GBAAudioCalculateRatio(1, 60, 1);
183 blip_set_rates(runner->context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
184 blip_set_rates(runner->context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
185
186 if (runner->context.gba->memory.hw.devices & (HW_TILT | HW_GYRO)) {
187 sceMotionStartSampling();
188 }
189
190 CircleBufferInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample));
191 MutexInit(&audioContext.mutex);
192 ConditionInit(&audioContext.cond);
193 audioContext.running = true;
194 ThreadCreate(&audioThread, _audioThread, &audioContext);
195}
196
197void GBAPSP2PrepareForFrame(struct GBAGUIRunner* runner) {
198 MutexLock(&audioContext.mutex);
199 while (blip_samples_avail(runner->context.gba->audio.left) >= PSP2_SAMPLES) {
200 if (CircleBufferSize(&audioContext.buffer) + PSP2_SAMPLES * sizeof(struct GBAStereoSample) > CircleBufferCapacity(&audioContext.buffer)) {
201 break;
202 }
203 struct GBAStereoSample samples[PSP2_SAMPLES];
204 blip_read_samples(runner->context.gba->audio.left, &samples[0].left, PSP2_SAMPLES, true);
205 blip_read_samples(runner->context.gba->audio.right, &samples[0].right, PSP2_SAMPLES, true);
206 int i;
207 for (i = 0; i < PSP2_SAMPLES; ++i) {
208 CircleBufferWrite16(&audioContext.buffer, samples[i].left);
209 CircleBufferWrite16(&audioContext.buffer, samples[i].right);
210 }
211 }
212 ConditionWake(&audioContext.cond);
213 MutexUnlock(&audioContext.mutex);
214}
215
216void GBAPSP2UnloadROM(struct GBAGUIRunner* runner) {
217 if (runner->context.gba->memory.hw.devices & (HW_TILT | HW_GYRO)) {
218 sceMotionStopSampling();
219 }
220 scePowerSetArmClockFrequency(80);
221}
222
223void GBAPSP2Teardown(struct GBAGUIRunner* runner) {
224 UNUSED(runner);
225 vita2d_free_texture(tex);
226 vita2d_free_texture(backdrop);
227}
228
229void GBAPSP2Draw(struct GBAGUIRunner* runner, bool faded) {
230 UNUSED(runner);
231 switch (screenMode) {
232 case SM_BACKDROP:
233 vita2d_draw_texture_tint(backdrop, 0, 0, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
234 // Fall through
235 case SM_PLAIN:
236 vita2d_draw_texture_tint_part_scale(tex, 120, 32, 0, 0, 240, 160, 3.0f, 3.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
237 break;
238 case SM_FULL:
239 vita2d_draw_texture_tint_scale(tex, 0, 0, 960.0f / 240.0f, 544.0f / 160.0f, (faded ? 0 : 0xC0000000) | 0x3FFFFFFF);
240 break;
241 }
242}
243
244__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
245 printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
246 exit(1);
247}