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