Commit | Line | Data |
---|---|---|
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 | ||
805fe9bc | 8 | #define _GNU_SOURCE 1 // strcasestr |
38c2028e | 9 | #include <stdio.h> |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
74ebc05b | 12 | #include <strings.h> |
38c2028e | 13 | |
14 | #include "../libpcsxcore/misc.h" | |
15 | #include "../libpcsxcore/psxcounters.h" | |
87e5b45f | 16 | #include "../libpcsxcore/psxmem_map.h" |
38c2028e | 17 | #include "../libpcsxcore/new_dynarec/new_dynarec.h" |
23ea11bd | 18 | #include "../libpcsxcore/cdrom.h" |
19 | #include "../libpcsxcore/cdriso.h" | |
6e921e1d | 20 | #include "../libpcsxcore/cheat.h" |
07c13dfd | 21 | #include "../plugins/dfsound/out.h" |
805fe9bc | 22 | #include "../plugins/dfinput/externals.h" |
c82f907a | 23 | #include "cspace.h" |
38c2028e | 24 | #include "main.h" |
25 | #include "plugin.h" | |
26 | #include "plugin_lib.h" | |
27 | #include "revision.h" | |
28 | #include "libretro.h" | |
29 | ||
30 | static retro_video_refresh_t video_cb; | |
31 | static retro_input_poll_t input_poll_cb; | |
32 | static retro_input_state_t input_state_cb; | |
33 | static retro_environment_t environ_cb; | |
34 | static retro_audio_sample_batch_t audio_batch_cb; | |
35 | ||
36 | static void *vout_buf; | |
70d56ca3 | 37 | static int vout_width, vout_height; |
38 | static int vout_doffs_old, vout_fb_dirty; | |
1a6164a1 | 39 | static bool vout_can_dupe; |
70d56ca3 | 40 | |
38c2028e | 41 | static int samples_sent, samples_to_send; |
42 | static int plugins_opened; | |
17f84149 | 43 | static int is_pal_mode; |
38c2028e | 44 | |
f9f60dae TK |
45 | /* memory card data */ |
46 | extern char Mcd1Data[MCD_SIZE]; | |
47 | extern char McdDisable[2]; | |
38c2028e | 48 | |
49 | /* PCSX ReARMed core calls and stuff */ | |
50 | int in_type1, in_type2; | |
51 | int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 }; | |
52 | int in_keystate; | |
53 | int in_enable_vibration; | |
54 | ||
805fe9bc | 55 | /* PSX max resolution is 640x512, but with enhancement it's 1024x512 */ |
56 | #define VOUT_MAX_WIDTH 1024 | |
57 | #define VOUT_MAX_HEIGHT 512 | |
58 | ||
7df396ea TK |
59 | static void init_memcard(char *mcd_data) |
60 | { | |
61 | unsigned off = 0; | |
62 | unsigned i; | |
63 | ||
64 | memset(mcd_data, 0, MCD_SIZE); | |
65 | ||
66 | mcd_data[off++] = 'M'; | |
67 | mcd_data[off++] = 'C'; | |
68 | off += 0x7d; | |
69 | mcd_data[off++] = 0x0e; | |
70 | ||
71 | for (i = 0; i < 15; i++) { | |
72 | mcd_data[off++] = 0xa0; | |
73 | off += 0x07; | |
74 | mcd_data[off++] = 0xff; | |
75 | mcd_data[off++] = 0xff; | |
76 | off += 0x75; | |
77 | mcd_data[off++] = 0xa0; | |
78 | } | |
79 | ||
80 | for (i = 0; i < 20; i++) { | |
81 | mcd_data[off++] = 0xff; | |
82 | mcd_data[off++] = 0xff; | |
83 | mcd_data[off++] = 0xff; | |
84 | mcd_data[off++] = 0xff; | |
85 | off += 0x04; | |
86 | mcd_data[off++] = 0xff; | |
87 | mcd_data[off++] = 0xff; | |
88 | off += 0x76; | |
89 | } | |
90 | } | |
91 | ||
38c2028e | 92 | static int vout_open(void) |
93 | { | |
94 | return 0; | |
95 | } | |
96 | ||
e4c83ca6 | 97 | static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp) |
38c2028e | 98 | { |
70d56ca3 | 99 | vout_width = w; |
100 | vout_height = h; | |
38c2028e | 101 | } |
102 | ||
cc4f9b70 | 103 | #ifndef FRONTEND_SUPPORTS_RGB565 |
9c59f120 | 104 | static void convert(void *buf, size_t bytes) |
38c2028e | 105 | { |
106 | unsigned int i, v, *p = buf; | |
107 | ||
9c59f120 | 108 | for (i = 0; i < bytes / 4; i++) { |
38c2028e | 109 | v = p[i]; |
110 | p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0); | |
111 | } | |
112 | } | |
46aa5b98 | 113 | #endif |
38c2028e | 114 | |
fa56d360 | 115 | static void vout_flip(const void *vram, int stride, int bgr24, int w, int h) |
38c2028e | 116 | { |
fa56d360 | 117 | unsigned short *dest = vout_buf; |
118 | const unsigned short *src = vram; | |
70d56ca3 | 119 | int dstride = vout_width, h1 = h; |
120 | int doffs; | |
38c2028e | 121 | |
fa56d360 | 122 | if (vram == NULL) { |
123 | // blanking | |
cc56203b | 124 | memset(vout_buf, 0, dstride * h * 2); |
fa56d360 | 125 | goto out; |
126 | } | |
127 | ||
70d56ca3 | 128 | doffs = (vout_height - h) * dstride; |
129 | doffs += (dstride - w) / 2 & ~1; | |
130 | if (doffs != vout_doffs_old) { | |
131 | // clear borders | |
132 | memset(vout_buf, 0, dstride * h * 2); | |
133 | vout_doffs_old = doffs; | |
134 | } | |
135 | dest += doffs; | |
136 | ||
fa56d360 | 137 | if (bgr24) |
138 | { | |
139 | // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here? | |
140 | for (; h1-- > 0; dest += dstride, src += stride) | |
141 | { | |
142 | bgr888_to_rgb565(dest, src, w * 3); | |
143 | } | |
144 | } | |
145 | else | |
146 | { | |
147 | for (; h1-- > 0; dest += dstride, src += stride) | |
148 | { | |
149 | bgr555_to_rgb565(dest, src, w * 2); | |
150 | } | |
151 | } | |
38c2028e | 152 | |
fa56d360 | 153 | out: |
46aa5b98 | 154 | #ifndef FRONTEND_SUPPORTS_RGB565 |
70d56ca3 | 155 | convert(vout_buf, vout_width * vout_height * 2); |
46aa5b98 | 156 | #endif |
70d56ca3 | 157 | vout_fb_dirty = 1; |
fa56d360 | 158 | pl_rearmed_cbs.flip_cnt++; |
38c2028e | 159 | } |
160 | ||
161 | static void vout_close(void) | |
162 | { | |
163 | } | |
164 | ||
cc56203b | 165 | static void *pl_mmap(unsigned int size) |
166 | { | |
87e5b45f | 167 | return psxMap(0, size, 0, MAP_TAG_VRAM); |
cc56203b | 168 | } |
169 | ||
170 | static void pl_munmap(void *ptr, unsigned int size) | |
171 | { | |
87e5b45f | 172 | psxUnmap(ptr, size, MAP_TAG_VRAM); |
cc56203b | 173 | } |
174 | ||
38c2028e | 175 | struct rearmed_cbs pl_rearmed_cbs = { |
176 | .pl_vout_open = vout_open, | |
177 | .pl_vout_set_mode = vout_set_mode, | |
178 | .pl_vout_flip = vout_flip, | |
179 | .pl_vout_close = vout_close, | |
cc56203b | 180 | .mmap = pl_mmap, |
181 | .munmap = pl_munmap, | |
38c2028e | 182 | /* from psxcounters */ |
183 | .gpu_hcnt = &hSyncCount, | |
184 | .gpu_frame_count = &frame_counter, | |
185 | }; | |
186 | ||
187 | void pl_frame_limit(void) | |
188 | { | |
189 | /* called once per frame, make psxCpu->Execute() above return */ | |
190 | stop = 1; | |
191 | } | |
192 | ||
193 | void pl_timing_prepare(int is_pal) | |
194 | { | |
17f84149 | 195 | is_pal_mode = is_pal; |
38c2028e | 196 | } |
197 | ||
198 | void plat_trigger_vibrate(int is_strong) | |
199 | { | |
200 | } | |
201 | ||
e4c83ca6 | 202 | void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in) |
38c2028e | 203 | { |
204 | } | |
205 | ||
206 | /* sound calls */ | |
07c13dfd | 207 | static int snd_init(void) |
38c2028e | 208 | { |
07c13dfd | 209 | return 0; |
38c2028e | 210 | } |
211 | ||
07c13dfd | 212 | static void snd_finish(void) |
38c2028e | 213 | { |
214 | } | |
215 | ||
07c13dfd | 216 | static int snd_busy(void) |
38c2028e | 217 | { |
218 | if (samples_to_send > samples_sent) | |
219 | return 0; /* give more samples */ | |
220 | else | |
221 | return 1; | |
222 | } | |
223 | ||
07c13dfd | 224 | static void snd_feed(void *buf, int bytes) |
38c2028e | 225 | { |
226 | audio_batch_cb(buf, bytes / 4); | |
227 | samples_sent += bytes / 4; | |
228 | } | |
229 | ||
07c13dfd | 230 | void out_register_libretro(struct out_driver *drv) |
231 | { | |
232 | drv->name = "libretro"; | |
233 | drv->init = snd_init; | |
234 | drv->finish = snd_finish; | |
235 | drv->busy = snd_busy; | |
236 | drv->feed = snd_feed; | |
237 | } | |
238 | ||
38c2028e | 239 | /* libretro */ |
e268a47e | 240 | void retro_set_environment(retro_environment_t cb) |
241 | { | |
e268a47e | 242 | static const struct retro_variable vars[] = { |
7d68d030 | 243 | { "frameskip", "Frameskip; 0|1|2|3" }, |
9f5a28d9 | 244 | { "region", "Region; Auto|NTSC|PAL" }, |
7d68d030 | 245 | #ifdef __ARM_NEON__ |
9f5a28d9 | 246 | { "neon_interlace_enable", "Enable interlacing mode(s); disabled|enabled" }, |
e268a47e | 247 | { "neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" }, |
354329fa | 248 | { "neon_enhancement_no_main", "Enhanced resolution speed hack; disabled|enabled" }, |
7d68d030 | 249 | #endif |
e268a47e | 250 | { NULL, NULL }, |
251 | }; | |
252 | ||
253 | environ_cb = cb; | |
254 | ||
255 | cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars); | |
e268a47e | 256 | } |
257 | ||
38c2028e | 258 | void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; } |
259 | void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; } | |
260 | void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; } | |
261 | void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; } | |
262 | void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; } | |
263 | ||
264 | unsigned retro_api_version(void) | |
265 | { | |
266 | return RETRO_API_VERSION; | |
267 | } | |
268 | ||
269 | void retro_set_controller_port_device(unsigned port, unsigned device) | |
270 | { | |
271 | } | |
272 | ||
273 | void retro_get_system_info(struct retro_system_info *info) | |
274 | { | |
275 | memset(info, 0, sizeof(*info)); | |
276 | info->library_name = "PCSX-ReARMed"; | |
0ed87574 | 277 | info->library_version = "r19"; |
9fba39a9 | 278 | info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u"; |
38c2028e | 279 | info->need_fullpath = true; |
280 | } | |
281 | ||
282 | void retro_get_system_av_info(struct retro_system_av_info *info) | |
283 | { | |
284 | memset(info, 0, sizeof(*info)); | |
17f84149 | 285 | info->timing.fps = is_pal_mode ? 50 : 60; |
38c2028e | 286 | info->timing.sample_rate = 44100; |
287 | info->geometry.base_width = 320; | |
288 | info->geometry.base_height = 240; | |
805fe9bc | 289 | info->geometry.max_width = VOUT_MAX_WIDTH; |
290 | info->geometry.max_height = VOUT_MAX_HEIGHT; | |
38c2028e | 291 | info->geometry.aspect_ratio = 4.0 / 3.0; |
292 | } | |
293 | ||
6e921e1d | 294 | /* savestates */ |
38c2028e | 295 | size_t retro_serialize_size(void) |
296 | { | |
6e921e1d | 297 | // it's currently 4380651 bytes, but have some reserved for future |
298 | return 0x430000; | |
299 | } | |
300 | ||
301 | struct save_fp { | |
302 | char *buf; | |
303 | size_t pos; | |
304 | int is_write; | |
305 | }; | |
306 | ||
307 | static void *save_open(const char *name, const char *mode) | |
308 | { | |
309 | struct save_fp *fp; | |
310 | ||
311 | if (name == NULL || mode == NULL) | |
312 | return NULL; | |
313 | ||
314 | fp = malloc(sizeof(*fp)); | |
315 | if (fp == NULL) | |
316 | return NULL; | |
317 | ||
318 | fp->buf = (char *)name; | |
319 | fp->pos = 0; | |
320 | fp->is_write = (mode[0] == 'w' || mode[1] == 'w'); | |
321 | ||
322 | return fp; | |
323 | } | |
324 | ||
325 | static int save_read(void *file, void *buf, u32 len) | |
326 | { | |
327 | struct save_fp *fp = file; | |
328 | if (fp == NULL || buf == NULL) | |
329 | return -1; | |
330 | ||
331 | memcpy(buf, fp->buf + fp->pos, len); | |
332 | fp->pos += len; | |
333 | return len; | |
334 | } | |
335 | ||
336 | static int save_write(void *file, const void *buf, u32 len) | |
337 | { | |
338 | struct save_fp *fp = file; | |
339 | if (fp == NULL || buf == NULL) | |
340 | return -1; | |
341 | ||
342 | memcpy(fp->buf + fp->pos, buf, len); | |
343 | fp->pos += len; | |
344 | return len; | |
345 | } | |
346 | ||
347 | static long save_seek(void *file, long offs, int whence) | |
348 | { | |
349 | struct save_fp *fp = file; | |
350 | if (fp == NULL) | |
351 | return -1; | |
352 | ||
353 | switch (whence) { | |
354 | case SEEK_CUR: | |
355 | fp->pos += offs; | |
356 | return fp->pos; | |
357 | case SEEK_SET: | |
358 | fp->pos = offs; | |
359 | return fp->pos; | |
360 | default: | |
361 | return -1; | |
362 | } | |
363 | } | |
364 | ||
365 | static void save_close(void *file) | |
366 | { | |
367 | struct save_fp *fp = file; | |
368 | size_t r_size = retro_serialize_size(); | |
369 | if (fp == NULL) | |
370 | return; | |
371 | ||
372 | if (fp->pos > r_size) | |
373 | SysPrintf("ERROR: save buffer overflow detected\n"); | |
374 | else if (fp->is_write && fp->pos < r_size) | |
375 | // make sure we don't save trash in leftover space | |
376 | memset(fp->buf + fp->pos, 0, r_size - fp->pos); | |
377 | free(fp); | |
38c2028e | 378 | } |
379 | ||
380 | bool retro_serialize(void *data, size_t size) | |
381 | { | |
6e921e1d | 382 | int ret = SaveState(data); |
383 | return ret == 0 ? true : false; | |
38c2028e | 384 | } |
385 | ||
386 | bool retro_unserialize(const void *data, size_t size) | |
387 | { | |
6e921e1d | 388 | int ret = LoadState(data); |
389 | return ret == 0 ? true : false; | |
38c2028e | 390 | } |
391 | ||
23ea11bd | 392 | /* cheats */ |
38c2028e | 393 | void retro_cheat_reset(void) |
394 | { | |
6e921e1d | 395 | ClearAllCheats(); |
38c2028e | 396 | } |
397 | ||
398 | void retro_cheat_set(unsigned index, bool enabled, const char *code) | |
399 | { | |
6e921e1d | 400 | char buf[256]; |
401 | int ret; | |
402 | ||
403 | // cheat funcs are destructive, need a copy.. | |
404 | strncpy(buf, code, sizeof(buf)); | |
405 | buf[sizeof(buf) - 1] = 0; | |
406 | ||
407 | if (index < NumCheats) | |
408 | ret = EditCheat(index, "", buf); | |
409 | else | |
410 | ret = AddCheat("", buf); | |
411 | ||
412 | if (ret != 0) | |
413 | SysPrintf("Failed to set cheat %#u\n", index); | |
414 | else if (index < NumCheats) | |
415 | Cheats[index].Enabled = enabled; | |
38c2028e | 416 | } |
417 | ||
23ea11bd | 418 | /* multidisk support */ |
419 | static bool disk_ejected; | |
420 | static unsigned int disk_current_index; | |
51ba7408 | 421 | static unsigned int disk_count; |
23ea11bd | 422 | static struct disks_state { |
423 | char *fname; | |
424 | int internal_index; // for multidisk eboots | |
425 | } disks[8]; | |
426 | ||
427 | static bool disk_set_eject_state(bool ejected) | |
428 | { | |
429 | // weird PCSX API.. | |
430 | SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2)); | |
431 | LidInterrupt(); | |
432 | ||
433 | disk_ejected = ejected; | |
434 | return true; | |
435 | } | |
436 | ||
437 | static bool disk_get_eject_state(void) | |
438 | { | |
439 | /* can't be controlled by emulated software */ | |
440 | return disk_ejected; | |
441 | } | |
442 | ||
443 | static unsigned int disk_get_image_index(void) | |
444 | { | |
445 | return disk_current_index; | |
446 | } | |
447 | ||
448 | static bool disk_set_image_index(unsigned int index) | |
449 | { | |
450 | if (index >= sizeof(disks) / sizeof(disks[0])) | |
451 | return false; | |
452 | ||
453 | CdromId[0] = '\0'; | |
454 | CdromLabel[0] = '\0'; | |
455 | ||
456 | if (disks[index].fname == NULL) { | |
457 | SysPrintf("missing disk #%u\n", index); | |
458 | CDR_shutdown(); | |
459 | ||
460 | // RetroArch specifies "no disk" with index == count, | |
461 | // so don't fail here.. | |
462 | disk_current_index = index; | |
463 | return true; | |
464 | } | |
465 | ||
466 | SysPrintf("switching to disk %u: \"%s\" #%d\n", index, | |
467 | disks[index].fname, disks[index].internal_index); | |
468 | ||
469 | cdrIsoMultidiskSelect = disks[index].internal_index; | |
470 | set_cd_image(disks[index].fname); | |
471 | if (ReloadCdromPlugin() < 0) { | |
472 | SysPrintf("failed to load cdr plugin\n"); | |
473 | return false; | |
474 | } | |
475 | if (CDR_open() < 0) { | |
476 | SysPrintf("failed to open cdr plugin\n"); | |
477 | return false; | |
478 | } | |
479 | ||
480 | if (!disk_ejected) { | |
481 | SetCdOpenCaseTime(time(NULL) + 2); | |
482 | LidInterrupt(); | |
483 | } | |
484 | ||
485 | disk_current_index = index; | |
486 | return true; | |
487 | } | |
488 | ||
489 | static unsigned int disk_get_num_images(void) | |
490 | { | |
51ba7408 | 491 | return disk_count; |
23ea11bd | 492 | } |
493 | ||
494 | static bool disk_replace_image_index(unsigned index, | |
495 | const struct retro_game_info *info) | |
496 | { | |
497 | char *old_fname; | |
498 | bool ret = true; | |
499 | ||
500 | if (index >= sizeof(disks) / sizeof(disks[0])) | |
501 | return false; | |
502 | ||
503 | old_fname = disks[index].fname; | |
504 | disks[index].fname = NULL; | |
505 | disks[index].internal_index = 0; | |
506 | ||
507 | if (info != NULL) { | |
508 | disks[index].fname = strdup(info->path); | |
509 | if (index == disk_current_index) | |
510 | ret = disk_set_image_index(index); | |
511 | } | |
512 | ||
513 | if (old_fname != NULL) | |
514 | free(old_fname); | |
515 | ||
516 | return ret; | |
517 | } | |
518 | ||
519 | static bool disk_add_image_index(void) | |
520 | { | |
51ba7408 T |
521 | if (disk_count >= 8) |
522 | return false; | |
523 | ||
524 | disk_count++; | |
23ea11bd | 525 | return true; |
526 | } | |
527 | ||
528 | static struct retro_disk_control_callback disk_control = { | |
529 | .set_eject_state = disk_set_eject_state, | |
530 | .get_eject_state = disk_get_eject_state, | |
531 | .get_image_index = disk_get_image_index, | |
532 | .set_image_index = disk_set_image_index, | |
533 | .get_num_images = disk_get_num_images, | |
534 | .replace_image_index = disk_replace_image_index, | |
535 | .add_image_index = disk_add_image_index, | |
536 | }; | |
537 | ||
9fba39a9 T |
538 | // just in case, maybe a win-rt port in the future? |
539 | #ifdef _WIN32 | |
540 | #define SLASH '\\' | |
541 | #else | |
542 | #define SLASH '/' | |
543 | #endif | |
544 | ||
545 | static char base_dir[PATH_MAX]; | |
546 | ||
547 | static bool read_m3u(const char *file) | |
548 | { | |
549 | char line[PATH_MAX]; | |
550 | char name[PATH_MAX]; | |
9fba39a9 T |
551 | FILE *f = fopen(file, "r"); |
552 | if (!f) | |
553 | return false; | |
554 | ||
51ba7408 | 555 | while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) { |
9fba39a9 T |
556 | if (line[0] == '#') |
557 | continue; | |
558 | char *carrige_return = strchr(line, '\r'); | |
559 | if (carrige_return) | |
560 | *carrige_return = '\0'; | |
561 | char *newline = strchr(line, '\n'); | |
562 | if (newline) | |
563 | *newline = '\0'; | |
564 | ||
565 | if (line[0] != '\0') | |
566 | { | |
567 | snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line); | |
51ba7408 | 568 | disks[disk_count++].fname = strdup(name); |
9fba39a9 T |
569 | } |
570 | } | |
571 | ||
572 | fclose(f); | |
51ba7408 | 573 | return (disk_count != 0); |
9fba39a9 T |
574 | } |
575 | ||
576 | static void extract_directory(char *buf, const char *path, size_t size) | |
577 | { | |
578 | char *base; | |
579 | strncpy(buf, path, size - 1); | |
580 | buf[size - 1] = '\0'; | |
581 | ||
582 | base = strrchr(buf, '/'); | |
583 | if (!base) | |
584 | base = strrchr(buf, '\\'); | |
585 | ||
586 | if (base) | |
587 | *base = '\0'; | |
588 | else | |
589 | { | |
590 | buf[0] = '.'; | |
591 | buf[1] = '\0'; | |
592 | } | |
593 | } | |
594 | ||
41294f4d | 595 | #ifdef __QNX__ |
596 | /* Blackberry QNX doesn't have strcasestr */ | |
597 | ||
598 | /* | |
599 | * Find the first occurrence of find in s, ignore case. | |
600 | */ | |
601 | char * | |
602 | strcasestr(const char *s, const char*find) | |
603 | { | |
604 | char c, sc; | |
605 | size_t len; | |
606 | ||
607 | if ((c = *find++) != 0) { | |
608 | c = tolower((unsigned char)c); | |
609 | len = strlen(find); | |
610 | do { | |
611 | do { | |
612 | if ((sc = *s++) == 0) | |
613 | return (NULL); | |
614 | } while ((char)tolower((unsigned char)sc) != c); | |
615 | } while (strncasecmp(s, find, len) != 0); | |
616 | s--; | |
617 | } | |
618 | return ((char *)s); | |
619 | } | |
620 | #endif | |
621 | ||
38c2028e | 622 | bool retro_load_game(const struct retro_game_info *info) |
623 | { | |
23ea11bd | 624 | size_t i; |
9fba39a9 T |
625 | bool is_m3u = (strcasestr(info->path, ".m3u") != NULL); |
626 | ||
46aa5b98 | 627 | #ifdef FRONTEND_SUPPORTS_RGB565 |
c19aba43 | 628 | enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565; |
9f766dbe | 629 | if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { |
f29fbd53 | 630 | SysPrintf("RGB565 supported, using it\n"); |
c19aba43 | 631 | } |
46aa5b98 | 632 | #endif |
c19aba43 | 633 | |
23ea11bd | 634 | if (info == NULL || info->path == NULL) { |
635 | SysPrintf("info->path required\n"); | |
636 | return false; | |
637 | } | |
638 | ||
38c2028e | 639 | if (plugins_opened) { |
640 | ClosePlugins(); | |
641 | plugins_opened = 0; | |
642 | } | |
643 | ||
23ea11bd | 644 | for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) { |
645 | if (disks[i].fname != NULL) { | |
646 | free(disks[i].fname); | |
647 | disks[i].fname = NULL; | |
648 | } | |
649 | disks[i].internal_index = 0; | |
650 | } | |
651 | ||
652 | disk_current_index = 0; | |
9fba39a9 T |
653 | extract_directory(base_dir, info->path, sizeof(base_dir)); |
654 | ||
655 | if (is_m3u) { | |
656 | if (!read_m3u(info->path)) { | |
657 | SysPrintf("failed to read m3u file\n"); | |
658 | return false; | |
659 | } | |
51ba7408 T |
660 | } else { |
661 | disk_count = 1; | |
9fba39a9 | 662 | disks[0].fname = strdup(info->path); |
51ba7408 | 663 | } |
23ea11bd | 664 | |
665 | set_cd_image(disks[0].fname); | |
38c2028e | 666 | |
667 | /* have to reload after set_cd_image for correct cdr plugin */ | |
668 | if (LoadPlugins() == -1) { | |
f29fbd53 | 669 | SysPrintf("failed to load plugins\n"); |
38c2028e | 670 | return false; |
671 | } | |
672 | ||
673 | plugins_opened = 1; | |
674 | NetOpened = 0; | |
675 | ||
676 | if (OpenPlugins() == -1) { | |
f29fbd53 | 677 | SysPrintf("failed to open plugins\n"); |
38c2028e | 678 | return false; |
679 | } | |
680 | ||
681 | plugin_call_rearmed_cbs(); | |
19a784e6 | 682 | dfinput_activate(); |
38c2028e | 683 | |
684 | Config.PsxAuto = 1; | |
685 | if (CheckCdrom() == -1) { | |
f29fbd53 | 686 | SysPrintf("unsupported/invalid CD image: %s\n", info->path); |
38c2028e | 687 | return false; |
688 | } | |
689 | ||
690 | SysReset(); | |
691 | ||
692 | if (LoadCdrom() == -1) { | |
f29fbd53 | 693 | SysPrintf("could not load CD-ROM!\n"); |
38c2028e | 694 | return false; |
695 | } | |
79f216e3 | 696 | emu_on_new_cd(0); |
38c2028e | 697 | |
23ea11bd | 698 | // multidisk images |
9fba39a9 | 699 | if (!is_m3u) { |
51ba7408 | 700 | disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8; |
9fba39a9 T |
701 | for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) { |
702 | disks[i].fname = strdup(info->path); | |
703 | disks[i].internal_index = i; | |
704 | } | |
23ea11bd | 705 | } |
706 | ||
38c2028e | 707 | return true; |
708 | } | |
709 | ||
710 | bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) | |
711 | { | |
712 | return false; | |
713 | } | |
714 | ||
715 | void retro_unload_game(void) | |
716 | { | |
717 | } | |
718 | ||
719 | unsigned retro_get_region(void) | |
720 | { | |
17f84149 | 721 | return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC; |
38c2028e | 722 | } |
723 | ||
724 | void *retro_get_memory_data(unsigned id) | |
725 | { | |
b40a83db T |
726 | if (id == RETRO_MEMORY_SAVE_RAM) |
727 | return Mcd1Data; | |
728 | else | |
729 | return NULL; | |
38c2028e | 730 | } |
731 | ||
732 | size_t retro_get_memory_size(unsigned id) | |
733 | { | |
b40a83db T |
734 | if (id == RETRO_MEMORY_SAVE_RAM) |
735 | return MCD_SIZE; | |
736 | else | |
737 | return 0; | |
38c2028e | 738 | } |
739 | ||
740 | void retro_reset(void) | |
741 | { | |
742 | SysReset(); | |
743 | } | |
744 | ||
745 | static const unsigned short retro_psx_map[] = { | |
746 | [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS, | |
747 | [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE, | |
748 | [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT, | |
749 | [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START, | |
750 | [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP, | |
751 | [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN, | |
752 | [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT, | |
753 | [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT, | |
754 | [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE, | |
755 | [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE, | |
756 | [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1, | |
757 | [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1, | |
758 | [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2, | |
759 | [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2, | |
760 | [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3, | |
761 | [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3, | |
762 | }; | |
763 | #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0])) | |
764 | ||
805fe9bc | 765 | static void update_variables(bool in_flight) |
e268a47e | 766 | { |
e268a47e | 767 | struct retro_variable var; |
7d68d030 | 768 | |
e268a47e | 769 | var.value = NULL; |
7d68d030 | 770 | var.key = "frameskip"; |
e268a47e | 771 | |
7d68d030 | 772 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) |
773 | pl_rearmed_cbs.frameskip = atoi(var.value); | |
9f5a28d9 | 774 | |
775 | var.value = NULL; | |
776 | var.key = "region"; | |
777 | ||
778 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
779 | { | |
780 | Config.PsxAuto = 0; | |
781 | if (strcmp(var.value, "Automatic") == 0) | |
782 | Config.PsxAuto = 1; | |
783 | else if (strcmp(var.value, "NTSC") == 0) | |
784 | Config.PsxType = 0; | |
785 | else if (strcmp(var.value, "PAL") == 0) | |
786 | Config.PsxType = 1; | |
787 | } | |
7d68d030 | 788 | #ifdef __ARM_NEON__ |
9f5a28d9 | 789 | var.value = "NULL"; |
790 | var.key = "neon_interlace_enable"; | |
791 | ||
792 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
793 | { | |
794 | if (strcmp(var.value, "disabled") == 0) | |
795 | pl_rearmed_cbs.gpu_neon.allow_interlace = 0; | |
796 | else if (strcmp(var.value, "enabled") == 0) | |
797 | pl_rearmed_cbs.gpu_neon.allow_interlace = 1; | |
798 | } | |
799 | ||
7d68d030 | 800 | var.value = NULL; |
801 | var.key = "neon_enhancement_enable"; | |
802 | ||
803 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
804 | { | |
805 | if (strcmp(var.value, "disabled") == 0) | |
806 | pl_rearmed_cbs.gpu_neon.enhancement_enable = 0; | |
807 | else if (strcmp(var.value, "enabled") == 0) | |
808 | pl_rearmed_cbs.gpu_neon.enhancement_enable = 1; | |
809 | } | |
354329fa | 810 | |
811 | var.value = NULL; | |
812 | var.key = "neon_enhancement_no_main"; | |
813 | ||
814 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
815 | { | |
816 | if (strcmp(var.value, "disabled") == 0) | |
817 | pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0; | |
818 | else if (strcmp(var.value, "enabled") == 0) | |
819 | pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1; | |
820 | } | |
094ec204 | 821 | #endif |
805fe9bc | 822 | |
823 | if (in_flight) { | |
824 | // inform core things about possible config changes | |
825 | plugin_call_rearmed_cbs(); | |
826 | ||
827 | if (GPU_open != NULL && GPU_close != NULL) { | |
828 | GPU_close(); | |
829 | GPU_open(&gpuDisp, "PCSX", NULL); | |
830 | } | |
805fe9bc | 831 | |
19a784e6 | 832 | dfinput_activate(); |
833 | } | |
e268a47e | 834 | } |
835 | ||
38c2028e | 836 | void retro_run(void) |
837 | { | |
838 | int i; | |
839 | ||
840 | input_poll_cb(); | |
e268a47e | 841 | |
805fe9bc | 842 | bool updated = false; |
843 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) | |
844 | update_variables(true); | |
e268a47e | 845 | |
38c2028e | 846 | in_keystate = 0; |
847 | for (i = 0; i < RETRO_PSX_MAP_LEN; i++) | |
848 | if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i)) | |
849 | in_keystate |= retro_psx_map[i]; | |
850 | in_keystate <<= 16; | |
851 | for (i = 0; i < RETRO_PSX_MAP_LEN; i++) | |
852 | if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i)) | |
853 | in_keystate |= retro_psx_map[i]; | |
854 | ||
855 | stop = 0; | |
856 | psxCpu->Execute(); | |
857 | ||
17f84149 | 858 | samples_to_send += is_pal_mode ? 44100 / 50 : 44100 / 60; |
e2bdb933 | 859 | |
1a6164a1 | 860 | video_cb((vout_fb_dirty || !vout_can_dupe) ? vout_buf : NULL, |
861 | vout_width, vout_height, vout_width * 2); | |
70d56ca3 | 862 | vout_fb_dirty = 0; |
38c2028e | 863 | } |
864 | ||
74ebc05b | 865 | static bool try_use_bios(const char *path) |
866 | { | |
867 | FILE *f; | |
868 | long size; | |
869 | const char *name; | |
870 | ||
871 | f = fopen(path, "rb"); | |
872 | if (f == NULL) | |
873 | return false; | |
874 | ||
875 | fseek(f, 0, SEEK_END); | |
876 | size = ftell(f); | |
877 | fclose(f); | |
878 | ||
879 | if (size != 512 * 1024) | |
880 | return false; | |
881 | ||
882 | name = strrchr(path, SLASH); | |
883 | if (name++ == NULL) | |
884 | name = path; | |
885 | snprintf(Config.Bios, sizeof(Config.Bios), "%s", name); | |
886 | return true; | |
887 | } | |
888 | ||
889 | #if 1 | |
890 | #include <sys/types.h> | |
891 | #include <dirent.h> | |
892 | ||
893 | static bool find_any_bios(const char *dirpath, char *path, size_t path_size) | |
894 | { | |
895 | DIR *dir; | |
896 | struct dirent *ent; | |
897 | bool ret = false; | |
898 | ||
899 | dir = opendir(dirpath); | |
900 | if (dir == NULL) | |
901 | return false; | |
902 | ||
903 | while ((ent = readdir(dir))) { | |
904 | if (strncasecmp(ent->d_name, "scph", 4) != 0) | |
905 | continue; | |
906 | ||
907 | snprintf(path, path_size, "%s/%s", dirpath, ent->d_name); | |
908 | ret = try_use_bios(path); | |
909 | if (ret) | |
910 | break; | |
911 | } | |
912 | closedir(dir); | |
913 | return ret; | |
914 | } | |
915 | #else | |
916 | #define find_any_bios(...) false | |
917 | #endif | |
918 | ||
38c2028e | 919 | void retro_init(void) |
920 | { | |
921 | const char *bios[] = { "scph1001", "scph5501", "scph7001" }; | |
922 | const char *dir; | |
923 | char path[256]; | |
38c2028e | 924 | int i, ret, level; |
74ebc05b | 925 | bool found_bios = false; |
38c2028e | 926 | |
927 | ret = emu_core_preinit(); | |
928 | ret |= emu_core_init(); | |
929 | if (ret != 0) { | |
f29fbd53 | 930 | SysPrintf("PCSX init failed.\n"); |
38c2028e | 931 | exit(1); |
932 | } | |
933 | ||
805fe9bc | 934 | vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2); |
38c2028e | 935 | |
936 | if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) | |
937 | { | |
938 | snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir); | |
939 | ||
940 | for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) { | |
941 | snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]); | |
74ebc05b | 942 | found_bios = try_use_bios(path); |
943 | if (found_bios) | |
38c2028e | 944 | break; |
38c2028e | 945 | } |
74ebc05b | 946 | |
947 | if (!found_bios) | |
948 | found_bios = find_any_bios(dir, path, sizeof(path)); | |
38c2028e | 949 | } |
74ebc05b | 950 | if (found_bios) { |
f29fbd53 | 951 | SysPrintf("found BIOS file: %s\n", Config.Bios); |
38c2028e | 952 | } |
953 | else | |
74ebc05b | 954 | { |
f29fbd53 | 955 | SysPrintf("no BIOS files found.\n"); |
74ebc05b | 956 | struct retro_message msg = |
957 | { | |
958 | "no BIOS found, expect bugs!", | |
959 | 180 | |
960 | }; | |
961 | environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg); | |
962 | } | |
38c2028e | 963 | |
964 | level = 1; | |
965 | environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level); | |
f9f60dae | 966 | |
1a6164a1 | 967 | environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe); |
23ea11bd | 968 | environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control); |
1a6164a1 | 969 | |
5ca2ec64 | 970 | /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times) |
971 | * we have to do this because cache misses and some IO penalties | |
972 | * are not emulated. Warning: changing this may break compatibility. */ | |
973 | #ifdef __ARM_ARCH_7A__ | |
974 | cycle_multiplier = 175; | |
975 | #else | |
976 | cycle_multiplier = 200; | |
977 | #endif | |
978 | ||
f9f60dae TK |
979 | McdDisable[0] = 0; |
980 | McdDisable[1] = 1; | |
7df396ea | 981 | init_memcard(Mcd1Data); |
6e921e1d | 982 | |
983 | SaveFuncs.open = save_open; | |
984 | SaveFuncs.read = save_read; | |
985 | SaveFuncs.write = save_write; | |
986 | SaveFuncs.seek = save_seek; | |
987 | SaveFuncs.close = save_close; | |
e268a47e | 988 | |
805fe9bc | 989 | update_variables(false); |
38c2028e | 990 | } |
991 | ||
992 | void retro_deinit(void) | |
993 | { | |
994 | SysClose(); | |
995 | free(vout_buf); | |
996 | vout_buf = NULL; | |
997 | } |