pipe error messages through single function
[pcsx_rearmed.git] / frontend / libretro.c
CommitLineData
38c2028e 1/*
2 * (C) notaz, 2012
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "../libpcsxcore/misc.h"
13#include "../libpcsxcore/psxcounters.h"
87e5b45f 14#include "../libpcsxcore/psxmem_map.h"
38c2028e 15#include "../libpcsxcore/new_dynarec/new_dynarec.h"
07c13dfd 16#include "../plugins/dfsound/out.h"
fa56d360 17#include "../plugins/gpulib/cspace.h"
38c2028e 18#include "main.h"
19#include "plugin.h"
20#include "plugin_lib.h"
21#include "revision.h"
22#include "libretro.h"
23
24static retro_video_refresh_t video_cb;
25static retro_input_poll_t input_poll_cb;
26static retro_input_state_t input_state_cb;
27static retro_environment_t environ_cb;
28static retro_audio_sample_batch_t audio_batch_cb;
29
30static void *vout_buf;
38c2028e 31static int samples_sent, samples_to_send;
32static int plugins_opened;
c19aba43 33static int native_rgb565;
38c2028e 34
35/* PCSX ReARMed core calls and stuff */
36int in_type1, in_type2;
37int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
38int in_keystate;
39int in_enable_vibration;
40
41static int vout_open(void)
42{
43 return 0;
44}
45
e4c83ca6 46static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
38c2028e 47{
38c2028e 48}
49
9c59f120 50static void convert(void *buf, size_t bytes)
38c2028e 51{
52 unsigned int i, v, *p = buf;
53
9c59f120 54 for (i = 0; i < bytes / 4; i++) {
38c2028e 55 v = p[i];
56 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
57 }
58}
59
fa56d360 60static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
38c2028e 61{
fa56d360 62 unsigned short *dest = vout_buf;
63 const unsigned short *src = vram;
64 int dstride = w, h1 = h;
38c2028e 65
fa56d360 66 if (vram == NULL) {
67 // blanking
cc56203b 68 memset(vout_buf, 0, dstride * h * 2);
fa56d360 69 goto out;
70 }
71
72 if (bgr24)
73 {
74 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
75 for (; h1-- > 0; dest += dstride, src += stride)
76 {
77 bgr888_to_rgb565(dest, src, w * 3);
78 }
79 }
80 else
81 {
82 for (; h1-- > 0; dest += dstride, src += stride)
83 {
84 bgr555_to_rgb565(dest, src, w * 2);
85 }
86 }
38c2028e 87
fa56d360 88out:
2857d72e 89 if (!native_rgb565)
90 convert(vout_buf, w * h * 2);
fa56d360 91 video_cb(vout_buf, w, h, w * 2);
92 pl_rearmed_cbs.flip_cnt++;
38c2028e 93}
94
95static void vout_close(void)
96{
97}
98
cc56203b 99static void *pl_mmap(unsigned int size)
100{
87e5b45f 101 return psxMap(0, size, 0, MAP_TAG_VRAM);
cc56203b 102}
103
104static void pl_munmap(void *ptr, unsigned int size)
105{
87e5b45f 106 psxUnmap(ptr, size, MAP_TAG_VRAM);
cc56203b 107}
108
38c2028e 109struct rearmed_cbs pl_rearmed_cbs = {
110 .pl_vout_open = vout_open,
111 .pl_vout_set_mode = vout_set_mode,
112 .pl_vout_flip = vout_flip,
113 .pl_vout_close = vout_close,
cc56203b 114 .mmap = pl_mmap,
115 .munmap = pl_munmap,
38c2028e 116 /* from psxcounters */
117 .gpu_hcnt = &hSyncCount,
118 .gpu_frame_count = &frame_counter,
119};
120
121void pl_frame_limit(void)
122{
123 /* called once per frame, make psxCpu->Execute() above return */
124 stop = 1;
125}
126
127void pl_timing_prepare(int is_pal)
128{
129}
130
131void plat_trigger_vibrate(int is_strong)
132{
133}
134
e4c83ca6 135void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
38c2028e 136{
137}
138
139/* sound calls */
07c13dfd 140static int snd_init(void)
38c2028e 141{
07c13dfd 142 return 0;
38c2028e 143}
144
07c13dfd 145static void snd_finish(void)
38c2028e 146{
147}
148
07c13dfd 149static int snd_busy(void)
38c2028e 150{
151 if (samples_to_send > samples_sent)
152 return 0; /* give more samples */
153 else
154 return 1;
155}
156
07c13dfd 157static void snd_feed(void *buf, int bytes)
38c2028e 158{
159 audio_batch_cb(buf, bytes / 4);
160 samples_sent += bytes / 4;
161}
162
07c13dfd 163void out_register_libretro(struct out_driver *drv)
164{
165 drv->name = "libretro";
166 drv->init = snd_init;
167 drv->finish = snd_finish;
168 drv->busy = snd_busy;
169 drv->feed = snd_feed;
170}
171
38c2028e 172/* libretro */
173void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
174void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
175void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
176void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
177void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
178void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
179
180unsigned retro_api_version(void)
181{
182 return RETRO_API_VERSION;
183}
184
185void retro_set_controller_port_device(unsigned port, unsigned device)
186{
187}
188
189void retro_get_system_info(struct retro_system_info *info)
190{
191 memset(info, 0, sizeof(*info));
192 info->library_name = "PCSX-ReARMed";
193 info->library_version = REV;
194 info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
195 info->need_fullpath = true;
196}
197
198void retro_get_system_av_info(struct retro_system_av_info *info)
199{
200 memset(info, 0, sizeof(*info));
201 info->timing.fps = 60;
202 info->timing.sample_rate = 44100;
203 info->geometry.base_width = 320;
204 info->geometry.base_height = 240;
205 info->geometry.max_width = 640;
206 info->geometry.max_height = 512;
207 info->geometry.aspect_ratio = 4.0 / 3.0;
208}
209
210/* TODO */
211size_t retro_serialize_size(void)
212{
213 return 0;
214}
215
216bool retro_serialize(void *data, size_t size)
217{
218 return false;
219}
220
221bool retro_unserialize(const void *data, size_t size)
222{
223 return false;
224}
225
226void retro_cheat_reset(void)
227{
228}
229
230void retro_cheat_set(unsigned index, bool enabled, const char *code)
231{
232}
233
234bool retro_load_game(const struct retro_game_info *info)
235{
c19aba43 236 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
9f766dbe 237 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
c19aba43 238 native_rgb565 = 1;
f29fbd53 239 SysPrintf("RGB565 supported, using it\n");
c19aba43
TK
240 }
241
38c2028e 242 if (plugins_opened) {
243 ClosePlugins();
244 plugins_opened = 0;
245 }
246
247 set_cd_image(info->path);
248
249 /* have to reload after set_cd_image for correct cdr plugin */
250 if (LoadPlugins() == -1) {
f29fbd53 251 SysPrintf("failed to load plugins\n");
38c2028e 252 return false;
253 }
254
255 plugins_opened = 1;
256 NetOpened = 0;
257
258 if (OpenPlugins() == -1) {
f29fbd53 259 SysPrintf("failed to open plugins\n");
38c2028e 260 return false;
261 }
262
263 plugin_call_rearmed_cbs();
264
265 Config.PsxAuto = 1;
266 if (CheckCdrom() == -1) {
f29fbd53 267 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
38c2028e 268 return false;
269 }
270
271 SysReset();
272
273 if (LoadCdrom() == -1) {
f29fbd53 274 SysPrintf("could not load CD-ROM!\n");
38c2028e 275 return false;
276 }
79f216e3 277 emu_on_new_cd(0);
38c2028e 278
279 return true;
280}
281
282bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
283{
284 return false;
285}
286
287void retro_unload_game(void)
288{
289}
290
291unsigned retro_get_region(void)
292{
293 return RETRO_REGION_NTSC;
294}
295
296void *retro_get_memory_data(unsigned id)
297{
298 return NULL;
299}
300
301size_t retro_get_memory_size(unsigned id)
302{
303 return 0;
304}
305
306void retro_reset(void)
307{
308 SysReset();
309}
310
311static const unsigned short retro_psx_map[] = {
312 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
313 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
314 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
315 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
316 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
317 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
318 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
319 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
320 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
321 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
322 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
323 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
324 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
325 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
326 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
327 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
328};
329#define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
330
331void retro_run(void)
332{
333 int i;
334
335 input_poll_cb();
336 in_keystate = 0;
337 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
338 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
339 in_keystate |= retro_psx_map[i];
340 in_keystate <<= 16;
341 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
342 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
343 in_keystate |= retro_psx_map[i];
344
345 stop = 0;
346 psxCpu->Execute();
347
348 samples_to_send += 44100 / 60;
349}
350
351void retro_init(void)
352{
353 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
354 const char *dir;
355 char path[256];
356 FILE *f = NULL;
357 int i, ret, level;
358
359 ret = emu_core_preinit();
360 ret |= emu_core_init();
361 if (ret != 0) {
f29fbd53 362 SysPrintf("PCSX init failed.\n");
38c2028e 363 exit(1);
364 }
365
366 vout_buf = malloc(640 * 512 * 2);
367
368 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
369 {
370 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
371
372 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
373 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
374 f = fopen(path, "r");
375 if (f != NULL) {
376 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
377 break;
378 }
379 }
380 }
381 if (f != NULL) {
f29fbd53 382 SysPrintf("found BIOS file: %s\n", Config.Bios);
38c2028e 383 fclose(f);
384 }
385 else
f29fbd53 386 SysPrintf("no BIOS files found.\n");
38c2028e 387
388 level = 1;
389 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
390}
391
392void retro_deinit(void)
393{
394 SysClose();
395 free(vout_buf);
396 vout_buf = NULL;
397}
398