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" }, |
4615b075 | 245 | { "pad1type", "Pad 1 Type; standard|analog" }, |
48f615ba | 246 | #ifndef DRC_DISABLE |
247 | { "rearmed_drc", "Dynamic recompiler; enabled|disabled" }, | |
248 | #endif | |
7d68d030 | 249 | #ifdef __ARM_NEON__ |
9f5a28d9 | 250 | { "neon_interlace_enable", "Enable interlacing mode(s); disabled|enabled" }, |
e268a47e | 251 | { "neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" }, |
354329fa | 252 | { "neon_enhancement_no_main", "Enhanced resolution speed hack; disabled|enabled" }, |
7d68d030 | 253 | #endif |
e268a47e | 254 | { NULL, NULL }, |
255 | }; | |
256 | ||
257 | environ_cb = cb; | |
258 | ||
259 | cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars); | |
e268a47e | 260 | } |
261 | ||
38c2028e | 262 | void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; } |
263 | void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; } | |
264 | void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; } | |
265 | void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; } | |
266 | void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; } | |
267 | ||
268 | unsigned retro_api_version(void) | |
269 | { | |
270 | return RETRO_API_VERSION; | |
271 | } | |
272 | ||
273 | void retro_set_controller_port_device(unsigned port, unsigned device) | |
274 | { | |
275 | } | |
276 | ||
277 | void retro_get_system_info(struct retro_system_info *info) | |
278 | { | |
279 | memset(info, 0, sizeof(*info)); | |
280 | info->library_name = "PCSX-ReARMed"; | |
0ed87574 | 281 | info->library_version = "r19"; |
9fba39a9 | 282 | info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u"; |
38c2028e | 283 | info->need_fullpath = true; |
284 | } | |
285 | ||
286 | void retro_get_system_av_info(struct retro_system_av_info *info) | |
287 | { | |
288 | memset(info, 0, sizeof(*info)); | |
17f84149 | 289 | info->timing.fps = is_pal_mode ? 50 : 60; |
38c2028e | 290 | info->timing.sample_rate = 44100; |
291 | info->geometry.base_width = 320; | |
292 | info->geometry.base_height = 240; | |
805fe9bc | 293 | info->geometry.max_width = VOUT_MAX_WIDTH; |
294 | info->geometry.max_height = VOUT_MAX_HEIGHT; | |
38c2028e | 295 | info->geometry.aspect_ratio = 4.0 / 3.0; |
296 | } | |
297 | ||
6e921e1d | 298 | /* savestates */ |
38c2028e | 299 | size_t retro_serialize_size(void) |
300 | { | |
6e921e1d | 301 | // it's currently 4380651 bytes, but have some reserved for future |
302 | return 0x430000; | |
303 | } | |
304 | ||
305 | struct save_fp { | |
306 | char *buf; | |
307 | size_t pos; | |
308 | int is_write; | |
309 | }; | |
310 | ||
311 | static void *save_open(const char *name, const char *mode) | |
312 | { | |
313 | struct save_fp *fp; | |
314 | ||
315 | if (name == NULL || mode == NULL) | |
316 | return NULL; | |
317 | ||
318 | fp = malloc(sizeof(*fp)); | |
319 | if (fp == NULL) | |
320 | return NULL; | |
321 | ||
322 | fp->buf = (char *)name; | |
323 | fp->pos = 0; | |
324 | fp->is_write = (mode[0] == 'w' || mode[1] == 'w'); | |
325 | ||
326 | return fp; | |
327 | } | |
328 | ||
329 | static int save_read(void *file, void *buf, u32 len) | |
330 | { | |
331 | struct save_fp *fp = file; | |
332 | if (fp == NULL || buf == NULL) | |
333 | return -1; | |
334 | ||
335 | memcpy(buf, fp->buf + fp->pos, len); | |
336 | fp->pos += len; | |
337 | return len; | |
338 | } | |
339 | ||
340 | static int save_write(void *file, const void *buf, u32 len) | |
341 | { | |
342 | struct save_fp *fp = file; | |
343 | if (fp == NULL || buf == NULL) | |
344 | return -1; | |
345 | ||
346 | memcpy(fp->buf + fp->pos, buf, len); | |
347 | fp->pos += len; | |
348 | return len; | |
349 | } | |
350 | ||
351 | static long save_seek(void *file, long offs, int whence) | |
352 | { | |
353 | struct save_fp *fp = file; | |
354 | if (fp == NULL) | |
355 | return -1; | |
356 | ||
357 | switch (whence) { | |
358 | case SEEK_CUR: | |
359 | fp->pos += offs; | |
360 | return fp->pos; | |
361 | case SEEK_SET: | |
362 | fp->pos = offs; | |
363 | return fp->pos; | |
364 | default: | |
365 | return -1; | |
366 | } | |
367 | } | |
368 | ||
369 | static void save_close(void *file) | |
370 | { | |
371 | struct save_fp *fp = file; | |
372 | size_t r_size = retro_serialize_size(); | |
373 | if (fp == NULL) | |
374 | return; | |
375 | ||
376 | if (fp->pos > r_size) | |
377 | SysPrintf("ERROR: save buffer overflow detected\n"); | |
378 | else if (fp->is_write && fp->pos < r_size) | |
379 | // make sure we don't save trash in leftover space | |
380 | memset(fp->buf + fp->pos, 0, r_size - fp->pos); | |
381 | free(fp); | |
38c2028e | 382 | } |
383 | ||
384 | bool retro_serialize(void *data, size_t size) | |
385 | { | |
6e921e1d | 386 | int ret = SaveState(data); |
387 | return ret == 0 ? true : false; | |
38c2028e | 388 | } |
389 | ||
390 | bool retro_unserialize(const void *data, size_t size) | |
391 | { | |
6e921e1d | 392 | int ret = LoadState(data); |
393 | return ret == 0 ? true : false; | |
38c2028e | 394 | } |
395 | ||
23ea11bd | 396 | /* cheats */ |
38c2028e | 397 | void retro_cheat_reset(void) |
398 | { | |
6e921e1d | 399 | ClearAllCheats(); |
38c2028e | 400 | } |
401 | ||
402 | void retro_cheat_set(unsigned index, bool enabled, const char *code) | |
403 | { | |
6e921e1d | 404 | char buf[256]; |
405 | int ret; | |
406 | ||
407 | // cheat funcs are destructive, need a copy.. | |
408 | strncpy(buf, code, sizeof(buf)); | |
409 | buf[sizeof(buf) - 1] = 0; | |
410 | ||
411 | if (index < NumCheats) | |
412 | ret = EditCheat(index, "", buf); | |
413 | else | |
414 | ret = AddCheat("", buf); | |
415 | ||
416 | if (ret != 0) | |
417 | SysPrintf("Failed to set cheat %#u\n", index); | |
418 | else if (index < NumCheats) | |
419 | Cheats[index].Enabled = enabled; | |
38c2028e | 420 | } |
421 | ||
23ea11bd | 422 | /* multidisk support */ |
423 | static bool disk_ejected; | |
424 | static unsigned int disk_current_index; | |
51ba7408 | 425 | static unsigned int disk_count; |
23ea11bd | 426 | static struct disks_state { |
427 | char *fname; | |
428 | int internal_index; // for multidisk eboots | |
429 | } disks[8]; | |
430 | ||
431 | static bool disk_set_eject_state(bool ejected) | |
432 | { | |
433 | // weird PCSX API.. | |
434 | SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2)); | |
435 | LidInterrupt(); | |
436 | ||
437 | disk_ejected = ejected; | |
438 | return true; | |
439 | } | |
440 | ||
441 | static bool disk_get_eject_state(void) | |
442 | { | |
443 | /* can't be controlled by emulated software */ | |
444 | return disk_ejected; | |
445 | } | |
446 | ||
447 | static unsigned int disk_get_image_index(void) | |
448 | { | |
449 | return disk_current_index; | |
450 | } | |
451 | ||
452 | static bool disk_set_image_index(unsigned int index) | |
453 | { | |
454 | if (index >= sizeof(disks) / sizeof(disks[0])) | |
455 | return false; | |
456 | ||
457 | CdromId[0] = '\0'; | |
458 | CdromLabel[0] = '\0'; | |
459 | ||
460 | if (disks[index].fname == NULL) { | |
461 | SysPrintf("missing disk #%u\n", index); | |
462 | CDR_shutdown(); | |
463 | ||
464 | // RetroArch specifies "no disk" with index == count, | |
465 | // so don't fail here.. | |
466 | disk_current_index = index; | |
467 | return true; | |
468 | } | |
469 | ||
470 | SysPrintf("switching to disk %u: \"%s\" #%d\n", index, | |
471 | disks[index].fname, disks[index].internal_index); | |
472 | ||
473 | cdrIsoMultidiskSelect = disks[index].internal_index; | |
474 | set_cd_image(disks[index].fname); | |
475 | if (ReloadCdromPlugin() < 0) { | |
476 | SysPrintf("failed to load cdr plugin\n"); | |
477 | return false; | |
478 | } | |
479 | if (CDR_open() < 0) { | |
480 | SysPrintf("failed to open cdr plugin\n"); | |
481 | return false; | |
482 | } | |
483 | ||
484 | if (!disk_ejected) { | |
485 | SetCdOpenCaseTime(time(NULL) + 2); | |
486 | LidInterrupt(); | |
487 | } | |
488 | ||
489 | disk_current_index = index; | |
490 | return true; | |
491 | } | |
492 | ||
493 | static unsigned int disk_get_num_images(void) | |
494 | { | |
51ba7408 | 495 | return disk_count; |
23ea11bd | 496 | } |
497 | ||
498 | static bool disk_replace_image_index(unsigned index, | |
499 | const struct retro_game_info *info) | |
500 | { | |
501 | char *old_fname; | |
502 | bool ret = true; | |
503 | ||
504 | if (index >= sizeof(disks) / sizeof(disks[0])) | |
505 | return false; | |
506 | ||
507 | old_fname = disks[index].fname; | |
508 | disks[index].fname = NULL; | |
509 | disks[index].internal_index = 0; | |
510 | ||
511 | if (info != NULL) { | |
512 | disks[index].fname = strdup(info->path); | |
513 | if (index == disk_current_index) | |
514 | ret = disk_set_image_index(index); | |
515 | } | |
516 | ||
517 | if (old_fname != NULL) | |
518 | free(old_fname); | |
519 | ||
520 | return ret; | |
521 | } | |
522 | ||
523 | static bool disk_add_image_index(void) | |
524 | { | |
51ba7408 T |
525 | if (disk_count >= 8) |
526 | return false; | |
527 | ||
528 | disk_count++; | |
23ea11bd | 529 | return true; |
530 | } | |
531 | ||
532 | static struct retro_disk_control_callback disk_control = { | |
533 | .set_eject_state = disk_set_eject_state, | |
534 | .get_eject_state = disk_get_eject_state, | |
535 | .get_image_index = disk_get_image_index, | |
536 | .set_image_index = disk_set_image_index, | |
537 | .get_num_images = disk_get_num_images, | |
538 | .replace_image_index = disk_replace_image_index, | |
539 | .add_image_index = disk_add_image_index, | |
540 | }; | |
541 | ||
9fba39a9 T |
542 | // just in case, maybe a win-rt port in the future? |
543 | #ifdef _WIN32 | |
544 | #define SLASH '\\' | |
545 | #else | |
546 | #define SLASH '/' | |
547 | #endif | |
548 | ||
549 | static char base_dir[PATH_MAX]; | |
550 | ||
551 | static bool read_m3u(const char *file) | |
552 | { | |
553 | char line[PATH_MAX]; | |
554 | char name[PATH_MAX]; | |
9fba39a9 T |
555 | FILE *f = fopen(file, "r"); |
556 | if (!f) | |
557 | return false; | |
558 | ||
51ba7408 | 559 | while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) { |
9fba39a9 T |
560 | if (line[0] == '#') |
561 | continue; | |
562 | char *carrige_return = strchr(line, '\r'); | |
563 | if (carrige_return) | |
564 | *carrige_return = '\0'; | |
565 | char *newline = strchr(line, '\n'); | |
566 | if (newline) | |
567 | *newline = '\0'; | |
568 | ||
569 | if (line[0] != '\0') | |
570 | { | |
571 | snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line); | |
51ba7408 | 572 | disks[disk_count++].fname = strdup(name); |
9fba39a9 T |
573 | } |
574 | } | |
575 | ||
576 | fclose(f); | |
51ba7408 | 577 | return (disk_count != 0); |
9fba39a9 T |
578 | } |
579 | ||
580 | static void extract_directory(char *buf, const char *path, size_t size) | |
581 | { | |
582 | char *base; | |
583 | strncpy(buf, path, size - 1); | |
584 | buf[size - 1] = '\0'; | |
585 | ||
586 | base = strrchr(buf, '/'); | |
587 | if (!base) | |
588 | base = strrchr(buf, '\\'); | |
589 | ||
590 | if (base) | |
591 | *base = '\0'; | |
592 | else | |
593 | { | |
594 | buf[0] = '.'; | |
595 | buf[1] = '\0'; | |
596 | } | |
597 | } | |
598 | ||
41294f4d | 599 | #ifdef __QNX__ |
600 | /* Blackberry QNX doesn't have strcasestr */ | |
601 | ||
602 | /* | |
603 | * Find the first occurrence of find in s, ignore case. | |
604 | */ | |
605 | char * | |
606 | strcasestr(const char *s, const char*find) | |
607 | { | |
608 | char c, sc; | |
609 | size_t len; | |
610 | ||
611 | if ((c = *find++) != 0) { | |
612 | c = tolower((unsigned char)c); | |
613 | len = strlen(find); | |
614 | do { | |
615 | do { | |
616 | if ((sc = *s++) == 0) | |
617 | return (NULL); | |
618 | } while ((char)tolower((unsigned char)sc) != c); | |
619 | } while (strncasecmp(s, find, len) != 0); | |
620 | s--; | |
621 | } | |
622 | return ((char *)s); | |
623 | } | |
624 | #endif | |
625 | ||
38c2028e | 626 | bool retro_load_game(const struct retro_game_info *info) |
627 | { | |
23ea11bd | 628 | size_t i; |
9fba39a9 T |
629 | bool is_m3u = (strcasestr(info->path, ".m3u") != NULL); |
630 | ||
46aa5b98 | 631 | #ifdef FRONTEND_SUPPORTS_RGB565 |
c19aba43 | 632 | enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565; |
9f766dbe | 633 | if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { |
f29fbd53 | 634 | SysPrintf("RGB565 supported, using it\n"); |
c19aba43 | 635 | } |
46aa5b98 | 636 | #endif |
c19aba43 | 637 | |
23ea11bd | 638 | if (info == NULL || info->path == NULL) { |
639 | SysPrintf("info->path required\n"); | |
640 | return false; | |
641 | } | |
642 | ||
38c2028e | 643 | if (plugins_opened) { |
644 | ClosePlugins(); | |
645 | plugins_opened = 0; | |
646 | } | |
647 | ||
23ea11bd | 648 | for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) { |
649 | if (disks[i].fname != NULL) { | |
650 | free(disks[i].fname); | |
651 | disks[i].fname = NULL; | |
652 | } | |
653 | disks[i].internal_index = 0; | |
654 | } | |
655 | ||
656 | disk_current_index = 0; | |
9fba39a9 T |
657 | extract_directory(base_dir, info->path, sizeof(base_dir)); |
658 | ||
659 | if (is_m3u) { | |
660 | if (!read_m3u(info->path)) { | |
661 | SysPrintf("failed to read m3u file\n"); | |
662 | return false; | |
663 | } | |
51ba7408 T |
664 | } else { |
665 | disk_count = 1; | |
9fba39a9 | 666 | disks[0].fname = strdup(info->path); |
51ba7408 | 667 | } |
23ea11bd | 668 | |
669 | set_cd_image(disks[0].fname); | |
38c2028e | 670 | |
671 | /* have to reload after set_cd_image for correct cdr plugin */ | |
672 | if (LoadPlugins() == -1) { | |
f29fbd53 | 673 | SysPrintf("failed to load plugins\n"); |
38c2028e | 674 | return false; |
675 | } | |
676 | ||
677 | plugins_opened = 1; | |
678 | NetOpened = 0; | |
679 | ||
680 | if (OpenPlugins() == -1) { | |
f29fbd53 | 681 | SysPrintf("failed to open plugins\n"); |
38c2028e | 682 | return false; |
683 | } | |
684 | ||
685 | plugin_call_rearmed_cbs(); | |
19a784e6 | 686 | dfinput_activate(); |
38c2028e | 687 | |
688 | Config.PsxAuto = 1; | |
689 | if (CheckCdrom() == -1) { | |
f29fbd53 | 690 | SysPrintf("unsupported/invalid CD image: %s\n", info->path); |
38c2028e | 691 | return false; |
692 | } | |
693 | ||
694 | SysReset(); | |
695 | ||
696 | if (LoadCdrom() == -1) { | |
f29fbd53 | 697 | SysPrintf("could not load CD-ROM!\n"); |
38c2028e | 698 | return false; |
699 | } | |
79f216e3 | 700 | emu_on_new_cd(0); |
38c2028e | 701 | |
23ea11bd | 702 | // multidisk images |
9fba39a9 | 703 | if (!is_m3u) { |
51ba7408 | 704 | disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8; |
9fba39a9 T |
705 | for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) { |
706 | disks[i].fname = strdup(info->path); | |
707 | disks[i].internal_index = i; | |
708 | } | |
23ea11bd | 709 | } |
710 | ||
38c2028e | 711 | return true; |
712 | } | |
713 | ||
714 | bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) | |
715 | { | |
716 | return false; | |
717 | } | |
718 | ||
719 | void retro_unload_game(void) | |
720 | { | |
721 | } | |
722 | ||
723 | unsigned retro_get_region(void) | |
724 | { | |
17f84149 | 725 | return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC; |
38c2028e | 726 | } |
727 | ||
728 | void *retro_get_memory_data(unsigned id) | |
729 | { | |
b40a83db T |
730 | if (id == RETRO_MEMORY_SAVE_RAM) |
731 | return Mcd1Data; | |
732 | else | |
733 | return NULL; | |
38c2028e | 734 | } |
735 | ||
736 | size_t retro_get_memory_size(unsigned id) | |
737 | { | |
b40a83db T |
738 | if (id == RETRO_MEMORY_SAVE_RAM) |
739 | return MCD_SIZE; | |
740 | else | |
741 | return 0; | |
38c2028e | 742 | } |
743 | ||
744 | void retro_reset(void) | |
745 | { | |
746 | SysReset(); | |
747 | } | |
748 | ||
749 | static const unsigned short retro_psx_map[] = { | |
750 | [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS, | |
751 | [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE, | |
752 | [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT, | |
753 | [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START, | |
754 | [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP, | |
755 | [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN, | |
756 | [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT, | |
757 | [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT, | |
758 | [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE, | |
759 | [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE, | |
760 | [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1, | |
761 | [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1, | |
762 | [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2, | |
763 | [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2, | |
764 | [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3, | |
765 | [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3, | |
766 | }; | |
767 | #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0])) | |
768 | ||
805fe9bc | 769 | static void update_variables(bool in_flight) |
e268a47e | 770 | { |
e268a47e | 771 | struct retro_variable var; |
7d68d030 | 772 | |
e268a47e | 773 | var.value = NULL; |
7d68d030 | 774 | var.key = "frameskip"; |
e268a47e | 775 | |
7d68d030 | 776 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) |
777 | pl_rearmed_cbs.frameskip = atoi(var.value); | |
9f5a28d9 | 778 | |
779 | var.value = NULL; | |
780 | var.key = "region"; | |
781 | ||
782 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
783 | { | |
784 | Config.PsxAuto = 0; | |
785 | if (strcmp(var.value, "Automatic") == 0) | |
786 | Config.PsxAuto = 1; | |
787 | else if (strcmp(var.value, "NTSC") == 0) | |
788 | Config.PsxType = 0; | |
789 | else if (strcmp(var.value, "PAL") == 0) | |
790 | Config.PsxType = 1; | |
791 | } | |
4615b075 | 792 | |
793 | var.value = NULL; | |
794 | var.key = "pad1type"; | |
795 | ||
796 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
797 | { | |
798 | in_type1 = PSE_PAD_TYPE_STANDARD; | |
799 | if (strcmp(var.value, "analog") == 0) | |
800 | in_type1 = PSE_PAD_TYPE_ANALOGPAD; | |
801 | } | |
802 | ||
7d68d030 | 803 | #ifdef __ARM_NEON__ |
9f5a28d9 | 804 | var.value = "NULL"; |
805 | var.key = "neon_interlace_enable"; | |
806 | ||
807 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
808 | { | |
809 | if (strcmp(var.value, "disabled") == 0) | |
810 | pl_rearmed_cbs.gpu_neon.allow_interlace = 0; | |
811 | else if (strcmp(var.value, "enabled") == 0) | |
812 | pl_rearmed_cbs.gpu_neon.allow_interlace = 1; | |
813 | } | |
814 | ||
7d68d030 | 815 | var.value = NULL; |
816 | var.key = "neon_enhancement_enable"; | |
817 | ||
818 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
819 | { | |
820 | if (strcmp(var.value, "disabled") == 0) | |
821 | pl_rearmed_cbs.gpu_neon.enhancement_enable = 0; | |
822 | else if (strcmp(var.value, "enabled") == 0) | |
823 | pl_rearmed_cbs.gpu_neon.enhancement_enable = 1; | |
824 | } | |
354329fa | 825 | |
826 | var.value = NULL; | |
827 | var.key = "neon_enhancement_no_main"; | |
828 | ||
829 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
830 | { | |
831 | if (strcmp(var.value, "disabled") == 0) | |
832 | pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0; | |
833 | else if (strcmp(var.value, "enabled") == 0) | |
834 | pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1; | |
835 | } | |
094ec204 | 836 | #endif |
48f615ba | 837 | #ifndef DRC_DISABLE |
838 | var.value = NULL; | |
839 | var.key = "rearmed_drc"; | |
840 | ||
841 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value) | |
842 | { | |
843 | R3000Acpu *prev_cpu = psxCpu; | |
844 | ||
845 | if (strcmp(var.value, "disabled") == 0) | |
846 | Config.Cpu = CPU_INTERPRETER; | |
847 | else if (strcmp(var.value, "enabled") == 0) | |
848 | Config.Cpu = CPU_DYNAREC; | |
849 | ||
850 | psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec; | |
851 | if (psxCpu != prev_cpu) { | |
852 | prev_cpu->Shutdown(); | |
853 | psxCpu->Init(); | |
854 | psxCpu->Reset(); // not really a reset.. | |
855 | } | |
856 | } | |
857 | #endif | |
805fe9bc | 858 | |
859 | if (in_flight) { | |
860 | // inform core things about possible config changes | |
861 | plugin_call_rearmed_cbs(); | |
862 | ||
863 | if (GPU_open != NULL && GPU_close != NULL) { | |
864 | GPU_close(); | |
865 | GPU_open(&gpuDisp, "PCSX", NULL); | |
866 | } | |
805fe9bc | 867 | |
19a784e6 | 868 | dfinput_activate(); |
869 | } | |
e268a47e | 870 | } |
871 | ||
38c2028e | 872 | void retro_run(void) |
873 | { | |
874 | int i; | |
875 | ||
876 | input_poll_cb(); | |
e268a47e | 877 | |
805fe9bc | 878 | bool updated = false; |
879 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) | |
880 | update_variables(true); | |
e268a47e | 881 | |
38c2028e | 882 | in_keystate = 0; |
883 | for (i = 0; i < RETRO_PSX_MAP_LEN; i++) | |
884 | if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i)) | |
885 | in_keystate |= retro_psx_map[i]; | |
886 | in_keystate <<= 16; | |
887 | for (i = 0; i < RETRO_PSX_MAP_LEN; i++) | |
888 | if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i)) | |
889 | in_keystate |= retro_psx_map[i]; | |
890 | ||
4615b075 | 891 | if (in_type1 == PSE_PAD_TYPE_ANALOGPAD) |
892 | { | |
893 | in_a1[0] = (input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X) / 256) + 128; | |
894 | in_a1[1] = (input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y) / 256) + 128; | |
895 | in_a2[0] = (input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X) / 256) + 128; | |
896 | in_a2[1] = (input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y) / 256) + 128; | |
897 | } | |
898 | ||
38c2028e | 899 | stop = 0; |
900 | psxCpu->Execute(); | |
901 | ||
17f84149 | 902 | samples_to_send += is_pal_mode ? 44100 / 50 : 44100 / 60; |
e2bdb933 | 903 | |
1a6164a1 | 904 | video_cb((vout_fb_dirty || !vout_can_dupe) ? vout_buf : NULL, |
905 | vout_width, vout_height, vout_width * 2); | |
70d56ca3 | 906 | vout_fb_dirty = 0; |
38c2028e | 907 | } |
908 | ||
74ebc05b | 909 | static bool try_use_bios(const char *path) |
910 | { | |
911 | FILE *f; | |
912 | long size; | |
913 | const char *name; | |
914 | ||
915 | f = fopen(path, "rb"); | |
916 | if (f == NULL) | |
917 | return false; | |
918 | ||
919 | fseek(f, 0, SEEK_END); | |
920 | size = ftell(f); | |
921 | fclose(f); | |
922 | ||
923 | if (size != 512 * 1024) | |
924 | return false; | |
925 | ||
926 | name = strrchr(path, SLASH); | |
927 | if (name++ == NULL) | |
928 | name = path; | |
929 | snprintf(Config.Bios, sizeof(Config.Bios), "%s", name); | |
930 | return true; | |
931 | } | |
932 | ||
933 | #if 1 | |
934 | #include <sys/types.h> | |
935 | #include <dirent.h> | |
936 | ||
937 | static bool find_any_bios(const char *dirpath, char *path, size_t path_size) | |
938 | { | |
939 | DIR *dir; | |
940 | struct dirent *ent; | |
941 | bool ret = false; | |
942 | ||
943 | dir = opendir(dirpath); | |
944 | if (dir == NULL) | |
945 | return false; | |
946 | ||
947 | while ((ent = readdir(dir))) { | |
948 | if (strncasecmp(ent->d_name, "scph", 4) != 0) | |
949 | continue; | |
950 | ||
951 | snprintf(path, path_size, "%s/%s", dirpath, ent->d_name); | |
952 | ret = try_use_bios(path); | |
953 | if (ret) | |
954 | break; | |
955 | } | |
956 | closedir(dir); | |
957 | return ret; | |
958 | } | |
959 | #else | |
960 | #define find_any_bios(...) false | |
961 | #endif | |
962 | ||
38c2028e | 963 | void retro_init(void) |
964 | { | |
965 | const char *bios[] = { "scph1001", "scph5501", "scph7001" }; | |
966 | const char *dir; | |
967 | char path[256]; | |
38c2028e | 968 | int i, ret, level; |
74ebc05b | 969 | bool found_bios = false; |
38c2028e | 970 | |
971 | ret = emu_core_preinit(); | |
972 | ret |= emu_core_init(); | |
973 | if (ret != 0) { | |
f29fbd53 | 974 | SysPrintf("PCSX init failed.\n"); |
38c2028e | 975 | exit(1); |
976 | } | |
977 | ||
805fe9bc | 978 | vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2); |
38c2028e | 979 | |
980 | if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) | |
981 | { | |
982 | snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir); | |
983 | ||
984 | for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) { | |
985 | snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]); | |
74ebc05b | 986 | found_bios = try_use_bios(path); |
987 | if (found_bios) | |
38c2028e | 988 | break; |
38c2028e | 989 | } |
74ebc05b | 990 | |
991 | if (!found_bios) | |
992 | found_bios = find_any_bios(dir, path, sizeof(path)); | |
38c2028e | 993 | } |
74ebc05b | 994 | if (found_bios) { |
f29fbd53 | 995 | SysPrintf("found BIOS file: %s\n", Config.Bios); |
38c2028e | 996 | } |
997 | else | |
74ebc05b | 998 | { |
f29fbd53 | 999 | SysPrintf("no BIOS files found.\n"); |
74ebc05b | 1000 | struct retro_message msg = |
1001 | { | |
1002 | "no BIOS found, expect bugs!", | |
1003 | 180 | |
1004 | }; | |
1005 | environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg); | |
1006 | } | |
38c2028e | 1007 | |
1008 | level = 1; | |
1009 | environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level); | |
f9f60dae | 1010 | |
1a6164a1 | 1011 | environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe); |
23ea11bd | 1012 | environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control); |
1a6164a1 | 1013 | |
5ca2ec64 | 1014 | /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times) |
1015 | * we have to do this because cache misses and some IO penalties | |
1016 | * are not emulated. Warning: changing this may break compatibility. */ | |
1017 | #ifdef __ARM_ARCH_7A__ | |
1018 | cycle_multiplier = 175; | |
1019 | #else | |
1020 | cycle_multiplier = 200; | |
1021 | #endif | |
1022 | ||
f9f60dae TK |
1023 | McdDisable[0] = 0; |
1024 | McdDisable[1] = 1; | |
7df396ea | 1025 | init_memcard(Mcd1Data); |
6e921e1d | 1026 | |
1027 | SaveFuncs.open = save_open; | |
1028 | SaveFuncs.read = save_read; | |
1029 | SaveFuncs.write = save_write; | |
1030 | SaveFuncs.seek = save_seek; | |
1031 | SaveFuncs.close = save_close; | |
e268a47e | 1032 | |
805fe9bc | 1033 | update_variables(false); |
38c2028e | 1034 | } |
1035 | ||
1036 | void retro_deinit(void) | |
1037 | { | |
1038 | SysClose(); | |
1039 | free(vout_buf); | |
1040 | vout_buf = NULL; | |
1041 | } |