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/overrides.h"
12#include "gba/video.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
28#include <vita2d.h>
29
30static char gameName[13];
31static struct GBA* gba;
32static struct ARMCore* cpu;
33static struct VFile* rom;
34static struct VFile* save;
35static struct GBAVideoSoftwareRenderer renderer;
36static struct GBAInputMap inputMap;
37static vita2d_texture* tex;
38static Thread audioThread;
39
40static bool fullscreen = false;
41
42#define PSP2_INPUT 0x50535032
43#define PSP2_SAMPLES 64
44#define PSP2_AUDIO_BUFFER_SIZE (PSP2_SAMPLES * 19)
45
46static struct GBAPSP2AudioContext {
47 struct CircleBuffer buffer;
48 Mutex mutex;
49 Condition cond;
50 bool running;
51} audioContext;
52
53static void _mapVitaKey(struct GBAInputMap* map, int pspKey, enum GBAKey key) {
54 GBAInputBindKey(map, PSP2_INPUT, __builtin_ctz(pspKey), key);
55}
56
57static THREAD_ENTRY _audioThread(void* context) {
58 struct GBAPSP2AudioContext* audio = (struct GBAPSP2AudioContext*) context;
59 struct GBAStereoSample buffer[PSP2_AUDIO_BUFFER_SIZE];
60 int audioPort = sceAudioOutOpenPort(PSP2_AUDIO_OUT_PORT_TYPE_MAIN, PSP2_AUDIO_BUFFER_SIZE, 48000, PSP2_AUDIO_OUT_MODE_STEREO);
61 while (audio->running) {
62 MutexLock(&audio->mutex);
63 int len = CircleBufferSize(&audio->buffer);
64 len /= sizeof(buffer[0]);
65 if (len > PSP2_AUDIO_BUFFER_SIZE) {
66 len = PSP2_AUDIO_BUFFER_SIZE;
67 }
68 if (len > 0) {
69 len &= ~(PSP2_AUDIO_MIN_LEN - 1);
70 CircleBufferRead(&audio->buffer, buffer, len * sizeof(buffer[0]));
71 MutexUnlock(&audio->mutex);
72 sceAudioOutSetConfig(audioPort, len, -1, -1);
73 sceAudioOutOutput(audioPort, buffer);
74 MutexLock(&audio->mutex);
75 }
76 ConditionWait(&audio->cond, &audio->mutex);
77 MutexUnlock(&audio->mutex);
78 }
79 sceAudioOutReleasePort(audioPort);
80 return 0;
81}
82
83void GBAPSP2Setup() {
84 GBAInputMapInit(&inputMap);
85 _mapVitaKey(&inputMap, PSP2_CTRL_CROSS, GBA_KEY_A);
86 _mapVitaKey(&inputMap, PSP2_CTRL_CIRCLE, GBA_KEY_B);
87 _mapVitaKey(&inputMap, PSP2_CTRL_START, GBA_KEY_START);
88 _mapVitaKey(&inputMap, PSP2_CTRL_SELECT, GBA_KEY_SELECT);
89 _mapVitaKey(&inputMap, PSP2_CTRL_UP, GBA_KEY_UP);
90 _mapVitaKey(&inputMap, PSP2_CTRL_DOWN, GBA_KEY_DOWN);
91 _mapVitaKey(&inputMap, PSP2_CTRL_LEFT, GBA_KEY_LEFT);
92 _mapVitaKey(&inputMap, PSP2_CTRL_RIGHT, GBA_KEY_RIGHT);
93 _mapVitaKey(&inputMap, PSP2_CTRL_LTRIGGER, GBA_KEY_L);
94 _mapVitaKey(&inputMap, PSP2_CTRL_RTRIGGER, GBA_KEY_R);
95
96 struct GBAAxis desc = { GBA_KEY_DOWN, GBA_KEY_UP, 192, 64 };
97 GBAInputBindAxis(&inputMap, PSP2_INPUT, 0, &desc);
98 desc = (struct GBAAxis) { GBA_KEY_RIGHT, GBA_KEY_LEFT, 192, 64 };
99 GBAInputBindAxis(&inputMap, PSP2_INPUT, 1, &desc);
100
101 tex = vita2d_create_empty_texture_format(256, 256, SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1BGR);
102
103 renderer.outputBuffer = vita2d_texture_get_datap(tex);
104 renderer.outputBufferStride = 256;
105}
106
107void GBAPSP2LoadROM(const char* path) {
108 GBAVideoSoftwareRendererCreate(&renderer);
109
110 gba = anonymousMemoryMap(sizeof(struct GBA));
111 cpu = anonymousMemoryMap(sizeof(struct ARMCore));
112
113 printf("GBA: %08X", gba);
114 printf("CPU: %08X", cpu);
115
116 rom = VFileOpenSce(path, PSP2_O_RDONLY, 0666);
117 save = VDirOptionalOpenFile(0, path, 0, ".sav", PSP2_O_RDWR | PSP2_O_CREAT);
118
119 printf("ROM: %08X", rom);
120 printf("Save: %08X", save);
121
122 GBACreate(gba);
123 ARMSetComponents(cpu, &gba->d, 0, 0);
124 ARMInit(cpu);
125 printf("%s initialized.", "CPU");
126
127 gba->sync = 0;
128
129 GBAVideoAssociateRenderer(&gba->video, &renderer.d);
130
131 GBALoadROM(gba, rom, save, 0);
132 GBAOverrideApplyDefaults(gba);
133 GBAGetGameTitle(gba, gameName);
134 printf("ROM loaded: %s", gameName);
135
136 ARMReset(cpu);
137 double ratio = GBAAudioCalculateRatio(1, 60, 1);
138 blip_set_rates(gba->audio.left, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
139 blip_set_rates(gba->audio.right, GBA_ARM7TDMI_FREQUENCY, 48000 * ratio);
140
141 CircleBufferInit(&audioContext.buffer, PSP2_AUDIO_BUFFER_SIZE * sizeof(struct GBAStereoSample));
142 MutexInit(&audioContext.mutex);
143 ConditionInit(&audioContext.cond);
144 audioContext.running = true;
145 ThreadCreate(&audioThread, _audioThread, &audioContext);
146
147 printf("%s all set and ready to roll.", projectName);
148}
149
150void GBAPSP2Runloop(void) {
151 int activeKeys = 0;
152 gba->keySource = &activeKeys;
153
154 bool fsToggle = false;
155 int frameCounter = 0;
156 while (true) {
157 ARMRunLoop(cpu);
158
159 if (frameCounter != gba->video.frameCounter) {
160 SceCtrlData pad;
161 sceCtrlPeekBufferPositive(0, &pad, 1);
162 if (pad.buttons & PSP2_CTRL_TRIANGLE) {
163 break;
164 }
165 if (pad.buttons & PSP2_CTRL_SQUARE) {
166 if (!fsToggle) {
167 fullscreen = !fullscreen;
168 }
169 fsToggle = true;
170 } else {
171 fsToggle = false;
172 }
173
174 activeKeys = GBAInputMapKeyBits(&inputMap, PSP2_INPUT, pad.buttons, 0);
175 enum GBAKey angles = GBAInputMapAxis(&inputMap, PSP2_INPUT, 0, pad.ly);
176 if (angles != GBA_KEY_NONE) {
177 activeKeys |= 1 << angles;
178 }
179 angles = GBAInputMapAxis(&inputMap, PSP2_INPUT, 1, pad.lx);
180 if (angles != GBA_KEY_NONE) {
181 activeKeys |= 1 << angles;
182 }
183 angles = GBAInputMapAxis(&inputMap, PSP2_INPUT, 2, pad.ry);
184 if (angles != GBA_KEY_NONE) {
185 activeKeys |= 1 << angles;
186 }
187 angles = GBAInputMapAxis(&inputMap, PSP2_INPUT, 3, pad.rx);
188 if (angles != GBA_KEY_NONE) {
189 activeKeys |= 1 << angles;
190 }
191
192 MutexLock(&audioContext.mutex);
193 while (blip_samples_avail(gba->audio.left) >= PSP2_SAMPLES) {
194 if (CircleBufferSize(&audioContext.buffer) + PSP2_SAMPLES * sizeof(struct GBAStereoSample) > CircleBufferCapacity(&audioContext.buffer)) {
195 break;
196 }
197 struct GBAStereoSample samples[PSP2_SAMPLES];
198 blip_read_samples(gba->audio.left, &samples[0].left, PSP2_SAMPLES, true);
199 blip_read_samples(gba->audio.right, &samples[0].right, PSP2_SAMPLES, true);
200 int i;
201 for (i = 0; i < PSP2_SAMPLES; ++i) {
202 CircleBufferWrite16(&audioContext.buffer, samples[i].left);
203 CircleBufferWrite16(&audioContext.buffer, samples[i].right);
204 }
205 }
206 ConditionWake(&audioContext.cond);
207 MutexUnlock(&audioContext.mutex);
208
209 vita2d_start_drawing();
210 vita2d_clear_screen();
211 GBAPSP2Draw();
212 vita2d_end_drawing();
213 vita2d_swap_buffers();
214
215 frameCounter = gba->video.frameCounter;
216 }
217 }
218}
219
220void GBAPSP2UnloadROM(void) {
221 printf("%s shutting down...", projectName);
222
223 ARMDeinit(cpu);
224 GBADestroy(gba);
225
226 rom->close(rom);
227 save->close(save);
228
229 MutexLock(&audioContext.mutex);
230 audioContext.running = false;
231 ConditionWake(&audioContext.cond);
232 MutexUnlock(&audioContext.mutex);
233 ThreadJoin(audioThread);
234 CircleBufferDeinit(&audioContext.buffer);
235 MutexDeinit(&audioContext.mutex);
236 ConditionDeinit(&audioContext.cond);
237
238 mappedMemoryFree(gba, 0);
239 mappedMemoryFree(cpu, 0);
240
241 gba = 0;
242 cpu = 0;
243 rom = 0;
244 save = 0;
245}
246
247void GBAPSP2Teardown(void) {
248 GBAInputMapDeinit(&inputMap);
249
250 vita2d_free_texture(tex);
251}
252
253void GBAPSP2Draw(void) {
254 if (fullscreen) {
255 vita2d_draw_texture_scale(tex, 0, 0, 960.0f / 240.0f, 544.0f / 160.0f);
256 } else {
257 vita2d_draw_texture_scale(tex, 120, 32, 3.0f, 3.0f);
258 }
259}