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