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/input.h"
10#include "gba/audio.h"
11#include "gba/supervisor/context.h"
12
13#include "gba/renderers/video-software.h"
14#include "util/circle-buffer.h"
15#include "util/memory.h"
16#include "util/threading.h"
17#include "util/vfs.h"
18#include "platform/psp2/sce-vfs.h"
19#include "third-party/blip_buf/blip_buf.h"
20
21#include <psp2/audioout.h>
22#include <psp2/ctrl.h>
23#include <psp2/display.h>
24#include <psp2/gxm.h>
25#include <psp2/kernel/sysmem.h>
26
27#include <vita2d.h>
28
29static struct GBAContext context;
30static struct GBAVideoSoftwareRenderer renderer;
31static vita2d_texture* tex;
32static Thread audioThread;
33
34static bool fullscreen = false;
35
36#define PSP2_INPUT 0x50535032
37#define PSP2_SAMPLES 64
38#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 19)
39
40static struct GBAPSP2AudioContext {
41 struct CircleBuffer buffer;
42 Mutex mutex;
43 Condition cond;
44 bool running;
45} audioContext;
46
47static void _mapVitaKey(struct GBAInputMap* map, int pspKey, enum GBAKey key) {
48 GBAInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
49}
50
51static THREAD_ENTRY _audioThread(void* context) {
52 struct GBAPSP2AudioContext* audio = (struct GBAPSP2AudioContext*) context;
53 struct GBAStereoSample buffer[PSP2_AUDIO_BUFFER_SIZE];
54 int audioPort = sceAudioOutOpenPort(PSP2_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_AUDIO_BUFFER_SIZE, 48000, PSP2_AUDIO_OUT_MODE_STEREO);
55 while (audio->running) {
56 MutexLock(&audio->mutex);
57 int len = CircleBufferSize(&audio->buffer);
58 len /= sizeof(buffer[0]);
59 if (len > PSP2_AUDIO_BUFFER_SIZE) {
60 len = PSP2_AUDIO_BUFFER_SIZE;
61 }
62 if (len > 0) {
63 len &= ~(PSP2_AUDIO_MIN_LEN - 1);
64 CircleBufferRead(&audio->buffer, buffer, len * sizeof(buffer[0]));
65 MutexUnlock(&audio->mutex);
66 sceAudioOutSetConfig(audioPort, len, -1, -1);
67 sceAudioOutOutput(audioPort, buffer);
68 MutexLock(&audio->mutex);
69 }
70 ConditionWait(&audio->cond, &audio->mutex);
71 MutexUnlock(&audio->mutex);
72 }
73 sceAudioOutReleasePort(audioPort);
74 return 0;
75}
76
77void GBAPSP2Setup() {
78 GBAContextInit(&context, 0);
79 struct GBAOptions opts = {
80 .useBios = true,
81 .logLevel = 0,
82 .idleOptimization = IDLE_LOOP_DETECT
83 };
84 GBAConfigLoadDefaults(&context.config, &opts);
85 _mapVitaKey(&context.inputMap, PSP2_CTRL_CROSS, GBA_KEY_A);
86 _mapVitaKey(&context.inputMap, PSP2_CTRL_CIRCLE, GBA_KEY_B);
87 _mapVitaKey(&context.inputMap, PSP2_CTRL_START, GBA_KEY_START);
88 _mapVitaKey(&context.inputMap, PSP2_CTRL_SELECT, GBA_KEY_SELECT);
89 _mapVitaKey(&context.inputMap, PSP2_CTRL_UP, GBA_KEY_UP);
90 _mapVitaKey(&context.inputMap, PSP2_CTRL_DOWN, GBA_KEY_DOWN);
91 _mapVitaKey(&context.inputMap, PSP2_CTRL_LEFT, GBA_KEY_LEFT);
92 _mapVitaKey(&context.inputMap, PSP2_CTRL_RIGHT, GBA_KEY_RIGHT);
93 _mapVitaKey(&context.inputMap, PSP2_CTRL_LTRIGGER, GBA_KEY_L);
94 _mapVitaKey(&context.inputMap, PSP2_CTRL_RTRIGGER, GBA_KEY_R);
95
96 struct GBAAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
97 GBAInputBindAxis(&context.inputMap, PSP2_INPUT, 0, &desc);
98 desc = (struct GBAAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
99 GBAInputBindAxis(&context.inputMap, PSP2_INPUT, 1, &desc);
100
101 tex = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
102
103 GBAVideoSoftwareRendererCreate(&renderer);
104 renderer.outputBuffer = vita2d_texture_get_datap(tex);
105 renderer.outputBufferStride = 256;
106 GBAVideoAssociateRenderer(&context.gba->video, &renderer.d);
107 printf("%s starting", projectName);
108}
109
110bool GBAPSP2LoadROM(const char* path) {
111 if (!GBAContextLoadROM(&context, path, true)) {
112 printf("%s failed to load!", path);
113 return false;
114 }
115 printf("%s loaded, starting...", path);
116 GBAContextStart(&context);
117 char gameTitle[13];
118 GBAGetGameTitle(context.gba, gameTitle);
119 printf("%s started!", gameTitle);
120 double ratio = GBAAudioCalculateRatio(1, 60, 1);
121 blip_set_rates(context.gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
122 blip_set_rates(context.gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
123
124 CircleBufferInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample));
125 MutexInit(&audioContext.mutex);
126 ConditionInit(&audioContext.cond);
127 audioContext.running = true;
128 ThreadCreate(&audioThread, _audioThread, &audioContext);
129 return true;
130}
131
132void GBAPSP2Runloop(void) {
133 int activeKeys = 0;
134
135 bool fsToggle = false;
136 while (true) {
137 SceCtrlData pad;
138 sceCtrlPeekBufferPositive(0, &pad, 1);
139 if (pad.buttons & PSP2_CTRL_TRIANGLE) {
140 break;
141 }
142 if (pad.buttons & PSP2_CTRL_SQUARE) {
143 if (!fsToggle) {
144 fullscreen = !fullscreen;
145 }
146 fsToggle = true;
147 } else {
148 fsToggle = false;
149 }
150
151 activeKeys = GBAInputMapKeyBits(&context.inputMap, PSP2_INPUT, pad.buttons, 0);
152 enum GBAKey angles = GBAInputMapAxis(&context.inputMap, PSP2_INPUT, 0, pad.ly);
153 if (angles != GBA_KEY_NONE) {
154 activeKeys |= 1 << angles;
155 }
156 angles = GBAInputMapAxis(&context.inputMap, PSP2_INPUT, 1, pad.lx);
157 if (angles != GBA_KEY_NONE) {
158 activeKeys |= 1 << angles;
159 }
160 angles = GBAInputMapAxis(&context.inputMap, PSP2_INPUT, 2, pad.ry);
161 if (angles != GBA_KEY_NONE) {
162 activeKeys |= 1 << angles;
163 }
164 angles = GBAInputMapAxis(&context.inputMap, PSP2_INPUT, 3, pad.rx);
165 if (angles != GBA_KEY_NONE) {
166 activeKeys |= 1 << angles;
167 }
168
169 GBAContextFrame(&context, activeKeys);
170
171 MutexLock(&audioContext.mutex);
172 while (blip_samples_avail(context.gba->audio.left) >= PSP2_SAMPLES) {
173 if (CircleBufferSize(&audioContext.buffer) + PSP2_SAMPLES * sizeof(struct GBAStereoSample) > CircleBufferCapacity(&audioContext.buffer)) {
174 break;
175 }
176 struct GBAStereoSample samples[PSP2_SAMPLES];
177 blip_read_samples(context.gba->audio.left, &samples[0].left, PSP2_SAMPLES, true);
178 blip_read_samples(context.gba->audio.right, &samples[0].right, PSP2_SAMPLES, true);
179 int i;
180 for (i = 0; i < PSP2_SAMPLES; ++i) {
181 CircleBufferWrite16(&audioContext.buffer, samples[i].left);
182 CircleBufferWrite16(&audioContext.buffer, samples[i].right);
183 }
184 }
185 ConditionWake(&audioContext.cond);
186 MutexUnlock(&audioContext.mutex);
187
188 vita2d_start_drawing();
189 vita2d_clear_screen();
190 GBAPSP2Draw();
191 vita2d_end_drawing();
192 vita2d_swap_buffers();
193 }
194}
195
196void GBAPSP2UnloadROM(void) {
197 GBAContextStop(&context);
198}
199
200void GBAPSP2Teardown(void) {
201 GBAContextDeinit(&context);
202 vita2d_free_texture(tex);
203}
204
205void GBAPSP2Draw(void) {
206 if (fullscreen) {
207 vita2d_draw_texture_scale(tex, 0, 0, 960.0f / 240.0f, 544.0f / 160.0f);
208 } else {
209 vita2d_draw_texture_scale(tex, 120, 32, 3.0f, 3.0f);
210 }
211}
212
213__attribute__((noreturn, weak)) void __assert_func(const char* file, int line, const char* func, const char* expr) {
214 printf("ASSERT FAILED: %s in %s at %s:%i\n", expr, func, file, line);
215 exit(1);
216}