cdrom: make the timing hack conditional
[pcsx_rearmed.git] / frontend / main.c
... / ...
CommitLineData
1/*
2 * (C) notaz, 2010-2012
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8#include <stdio.h>
9#include <string.h>
10#include <stdarg.h>
11#include <unistd.h>
12#include <signal.h>
13#include <time.h>
14#if !defined(_WIN32) && !defined(NO_DYLIB)
15#include <dlfcn.h>
16#endif
17
18#include "main.h"
19#include "plugin.h"
20#include "plugin_lib.h"
21#include "pcnt.h"
22#include "menu.h"
23#include "plat.h"
24#include "../libpcsxcore/misc.h"
25#include "../libpcsxcore/cheat.h"
26#include "../libpcsxcore/new_dynarec/new_dynarec.h"
27#include "../plugins/cdrcimg/cdrcimg.h"
28#include "../plugins/dfsound/spu_config.h"
29#include "arm_features.h"
30#include "revision.h"
31
32#if defined(__has_builtin)
33#define DO_CPU_CHECKS __has_builtin(__builtin_cpu_init)
34#elif defined(__x86_64__) || defined(__i386__)
35#define DO_CPU_CHECKS 1
36#else
37#define DO_CPU_CHECKS 0
38#endif
39
40#ifndef NO_FRONTEND
41#include "libpicofe/input.h"
42#include "libpicofe/plat.h"
43#include "libpicofe/readpng.h"
44
45static void toggle_fast_forward(int force_off);
46static void check_profile(void);
47static void check_memcards(void);
48#endif
49#ifndef BOOT_MSG
50#define BOOT_MSG "Booting up..."
51#endif
52
53// don't include debug.h - it breaks ARM build (R1 redefined)
54static void StartDebugger() {}
55static void StopDebugger() {}
56
57int ready_to_go, g_emu_want_quit, g_emu_resetting;
58unsigned long gpuDisp;
59char cfgfile_basename[MAXPATHLEN];
60int state_slot;
61enum sched_action emu_action, emu_action_old;
62char hud_msg[64];
63int hud_new_msg;
64
65static void make_path(char *buf, size_t size, const char *dir, const char *fname)
66{
67 if (fname)
68 snprintf(buf, size, ".%s%s", dir, fname);
69 else
70 snprintf(buf, size, ".%s", dir);
71}
72#define MAKE_PATH(buf, dir, fname) \
73 make_path(buf, sizeof(buf), dir, fname)
74
75static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
76 char trimlabel[33];
77 int j;
78
79 strncpy(trimlabel, CdromLabel, 32);
80 trimlabel[32] = 0;
81 for (j = 31; j >= 0; j--)
82 if (trimlabel[j] == ' ')
83 trimlabel[j] = 0;
84 else
85 continue;
86
87 snprintf(buf, size, fmt, trimlabel, CdromId, i);
88
89 return 0;
90}
91
92void set_cd_image(const char *fname)
93{
94 const char *ext = NULL;
95
96 if (fname != NULL)
97 ext = strrchr(fname, '.');
98
99 if (ext && (
100 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
101 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
102 SetIsoFile(NULL);
103 cdrcimg_set_fname(fname);
104 strcpy(Config.Cdr, "builtin_cdrcimg");
105 } else {
106 SetIsoFile(fname);
107 strcpy(Config.Cdr, "builtin_cdr");
108 }
109}
110
111static void set_default_paths(void)
112{
113#ifndef NO_FRONTEND
114 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
115 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
116 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
117 strcpy(Config.BiosDir, "bios");
118#endif
119
120 strcpy(Config.PluginsDir, "plugins");
121 strcpy(Config.Gpu, "builtin_gpu");
122 strcpy(Config.Spu, "builtin_spu");
123 strcpy(Config.Cdr, "builtin_cdr");
124 strcpy(Config.Pad1, "builtin_pad");
125 strcpy(Config.Pad2, "builtin_pad");
126 strcpy(Config.Net, "Disabled");
127}
128
129void emu_set_default_config(void)
130{
131 // try to set sane config on which most games work
132 Config.Xa = Config.Cdda = 0;
133 Config.icache_emulation = 0;
134 Config.PsxAuto = 1;
135 Config.cycle_multiplier = CYCLE_MULT_DEFAULT;
136
137 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
138 pl_rearmed_cbs.gpu_neon.enhancement_enable =
139 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
140 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
141 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
142 pl_rearmed_cbs.gpu_senquack.ilace_force = 0;
143 pl_rearmed_cbs.gpu_senquack.pixel_skip = 0;
144 pl_rearmed_cbs.gpu_senquack.lighting = 1;
145 pl_rearmed_cbs.gpu_senquack.fast_lighting = 0;
146 pl_rearmed_cbs.gpu_senquack.blending = 1;
147 pl_rearmed_cbs.gpu_senquack.dithering = 0;
148 pl_rearmed_cbs.gpu_unai.abe_hack =
149 pl_rearmed_cbs.gpu_unai.no_light =
150 pl_rearmed_cbs.gpu_unai.no_blend = 0;
151 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
152 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
153 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
154
155 spu_config.iUseReverb = 1;
156 spu_config.iUseInterpolation = 1;
157 spu_config.iXAPitch = 0;
158 spu_config.iVolume = 768;
159 spu_config.iTempo = 0;
160 spu_config.iUseThread = 1; // no effect if only 1 core is detected
161#if defined(HAVE_PRE_ARMV7) && !defined(_3DS) /* XXX GPH hack */
162 spu_config.iUseReverb = 0;
163 spu_config.iUseInterpolation = 0;
164#ifndef HAVE_LIBRETRO
165 spu_config.iTempo = 1;
166#endif
167#endif
168 new_dynarec_hacks = 0;
169
170 in_type[0] = PSE_PAD_TYPE_STANDARD;
171 in_type[1] = PSE_PAD_TYPE_STANDARD;
172}
173
174void do_emu_action(void)
175{
176 int ret;
177
178 emu_action_old = emu_action;
179
180 switch (emu_action) {
181 case SACTION_LOAD_STATE:
182 ret = emu_load_state(state_slot);
183 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
184 break;
185 case SACTION_SAVE_STATE:
186 ret = emu_save_state(state_slot);
187 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
188 break;
189#ifndef NO_FRONTEND
190 case SACTION_ENTER_MENU:
191 toggle_fast_forward(1);
192 menu_loop();
193 return;
194 case SACTION_NEXT_SSLOT:
195 state_slot++;
196 if (state_slot > 9)
197 state_slot = 0;
198 goto do_state_slot;
199 case SACTION_PREV_SSLOT:
200 state_slot--;
201 if (state_slot < 0)
202 state_slot = 9;
203do_state_slot:
204 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
205 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
206 hud_new_msg = 3;
207 SysPrintf("* %s\n", hud_msg);
208 break;
209 case SACTION_TOGGLE_FSKIP:
210 pl_rearmed_cbs.fskip_advice = 0;
211 pl_rearmed_cbs.frameskip++;
212 if (pl_rearmed_cbs.frameskip > 1)
213 pl_rearmed_cbs.frameskip = -1;
214 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
215 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
216 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
217 plugin_call_rearmed_cbs();
218 break;
219 case SACTION_SWITCH_DISPMODE:
220 pl_switch_dispmode();
221 plugin_call_rearmed_cbs();
222 if (GPU_open != NULL && GPU_close != NULL) {
223 GPU_close();
224 GPU_open(&gpuDisp, "PCSX", NULL);
225 }
226 break;
227 case SACTION_FAST_FORWARD:
228 toggle_fast_forward(0);
229 plugin_call_rearmed_cbs();
230 break;
231 case SACTION_TOGGLE_FPS:
232 if ((g_opts & (OPT_SHOWFPS|OPT_SHOWCPU))
233 == (OPT_SHOWFPS|OPT_SHOWCPU))
234 g_opts &= ~(OPT_SHOWFPS|OPT_SHOWCPU);
235 else if (g_opts & OPT_SHOWFPS)
236 g_opts |= OPT_SHOWCPU;
237 else
238 g_opts |= OPT_SHOWFPS;
239 break;
240 case SACTION_TOGGLE_FULLSCREEN:
241 plat_target.vout_fullscreen = !plat_target.vout_fullscreen;
242 if (GPU_open != NULL && GPU_close != NULL) {
243 GPU_close();
244 GPU_open(&gpuDisp, "PCSX", NULL);
245 }
246 break;
247 case SACTION_SCREENSHOT:
248 {
249 char buf[MAXPATHLEN];
250 void *scrbuf;
251 int w, h, bpp;
252 time_t t = time(NULL);
253 struct tm *tb = localtime(&t);
254 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
255 tb->tm_min * 100 + tb->tm_sec;
256
257 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
258 get_gameid_filename(buf, sizeof(buf),
259 "screenshots/%.32s-%.9s.%d.png", ti);
260 ret = -1;
261 if (scrbuf != 0 && bpp == 16)
262 ret = writepng(buf, scrbuf, w, h);
263 if (ret == 0)
264 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
265 break;
266 }
267 case SACTION_VOLUME_UP:
268 case SACTION_VOLUME_DOWN:
269 {
270 static int volume;
271 plat_target_step_volume(&volume,
272 emu_action == SACTION_VOLUME_UP ? 1 : -1);
273 }
274 return;
275 case SACTION_MINIMIZE:
276 if (GPU_close != NULL)
277 GPU_close();
278
279 plat_minimize();
280
281 if (GPU_open != NULL) {
282 ret = GPU_open(&gpuDisp, "PCSX", NULL);
283 if (ret)
284 SysMessage("GPU_open returned %d", ret);
285 }
286 return;
287#endif
288 default:
289 return;
290 }
291
292 hud_new_msg = 3;
293}
294
295static char basic_lcase(char c)
296{
297 if ('A' <= c && c <= 'Z')
298 return c - 'A' + 'a';
299 return c;
300}
301
302static int cdidcmp(const char *id1, const char *id2)
303{
304 while (*id1 != 0 && *id2 != 0) {
305 if (*id1 == '_') { id1++; continue; }
306 if (*id2 == '_') { id2++; continue; }
307 if (basic_lcase(*id1) != basic_lcase(*id2))
308 break;
309 id1++;
310 id2++;
311 }
312
313 return *id1 - *id2;
314}
315
316static void parse_cwcheat(void)
317{
318 char line[256], buf[256], name[256], *p;
319 int newcheat = 1;
320 u32 a, v;
321 FILE *f;
322
323 f = fopen("cheatpops.db", "r");
324 if (f == NULL)
325 return;
326
327 /* find the game */
328 while (fgets(line, sizeof(line), f)) {
329 if (sscanf(line, "_S %63s", buf) != 1)
330 continue;
331 if (cdidcmp(buf, CdromId) == 0)
332 break;
333 }
334
335 if (feof(f))
336 goto out;
337
338 SysPrintf("cwcheat section found for %s\n", CdromId);
339 while (fgets(line, sizeof(line), f))
340 {
341 p = line + strlen(line);
342 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
343 *p = 0;
344 if (*p == 0 || *p == '#' || *p == ';')
345 continue;
346
347 if (strncmp(line, "_S", 2) == 0)
348 break;
349 if (strncmp(line, "_G", 2) == 0) {
350 SysPrintf(" cwcheat game name: '%s'\n", line + 3);
351 continue;
352 }
353 if (strncmp(line, "_C0", 3) == 0) {
354 if (!newcheat && Cheats[NumCheats - 1].n == 0) {
355 SysPrintf("cheat '%s' failed to parse\n", name);
356 free(Cheats[NumCheats - 1].Descr);
357 NumCheats--;
358 }
359 snprintf(name, sizeof(name), "%s", line + 4);
360 newcheat = 1;
361 continue;
362 }
363 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
364 SysPrintf("line failed to parse: '%s'\n", line);
365 continue;
366 }
367
368 if (newcheat) {
369 if (NumCheats >= NumCheatsAllocated) {
370 NumCheatsAllocated += 16;
371 Cheats = realloc(Cheats, sizeof(Cheat) *
372 NumCheatsAllocated);
373 if (Cheats == NULL)
374 break;
375 }
376 Cheats[NumCheats].Descr = strdup(name);
377 Cheats[NumCheats].Enabled = 0;
378 Cheats[NumCheats].WasEnabled = 0;
379 Cheats[NumCheats].First = NumCodes;
380 Cheats[NumCheats].n = 0;
381 NumCheats++;
382 newcheat = 0;
383 }
384
385 if (NumCodes >= NumCodesAllocated) {
386 NumCodesAllocated += 16;
387 CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
388 NumCodesAllocated);
389 if (CheatCodes == NULL)
390 break;
391 }
392 CheatCodes[NumCodes].Addr = a;
393 CheatCodes[NumCodes].Val = v;
394 NumCodes++;
395 Cheats[NumCheats - 1].n++;
396 }
397
398out:
399 fclose(f);
400}
401
402void emu_on_new_cd(int show_hud_msg)
403{
404 ClearAllCheats();
405 parse_cwcheat();
406
407 if (Config.HLE) {
408 SysPrintf("note: running with HLE BIOS, expect compatibility problems\n");
409 SysPrintf("----------------------------------------------------------\n");
410 }
411
412 if (show_hud_msg) {
413 snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG);
414 hud_new_msg = 3;
415 }
416}
417
418static void log_wrong_cpu(void)
419{
420#if DO_CPU_CHECKS
421 __builtin_cpu_init();
422 #define CHECK_CPU(name) if (!__builtin_cpu_supports(name)) \
423 SysPrintf("ERROR: compiled for " name ", which is unsupported by the CPU\n")
424#ifdef __SSE2__
425 CHECK_CPU("sse2");
426#endif
427#ifdef __SSSE3__
428 CHECK_CPU("ssse3");
429#endif
430#ifdef __SSE4_1__
431 CHECK_CPU("sse4.1");
432#endif
433#endif // DO_CPU_CHECKS
434}
435
436int emu_core_preinit(void)
437{
438 // what is the name of the config file?
439 // it may be redefined by -cfg on the command line
440 strcpy(cfgfile_basename, "pcsx.cfg");
441
442#ifdef IOS
443 emuLog = fopen("/User/Documents/pcsxr.log", "w");
444 if (emuLog == NULL)
445 emuLog = fopen("pcsxr.log", "w");
446 if (emuLog == NULL)
447#endif
448 emuLog = stdout;
449
450 log_wrong_cpu();
451
452 SetIsoFile(NULL);
453
454 memset(&Config, 0, sizeof(Config));
455
456 set_default_paths();
457 emu_set_default_config();
458 strcpy(Config.Bios, "HLE");
459
460 return 0;
461}
462
463int emu_core_init(void)
464{
465 SysPrintf("Starting PCSX-ReARMed " REV "\n");
466
467#ifndef NO_FRONTEND
468 check_profile();
469 check_memcards();
470#endif
471
472 if (EmuInit() == -1) {
473 SysPrintf("PSX emulator couldn't be initialized.\n");
474 return -1;
475 }
476
477 LoadMcds(Config.Mcd1, Config.Mcd2);
478
479 if (Config.Debug) {
480 StartDebugger();
481 }
482
483 return 0;
484}
485
486void emu_core_ask_exit(void)
487{
488 stop = 1;
489 g_emu_want_quit = 1;
490}
491
492#ifndef NO_FRONTEND
493
494#include <sys/stat.h>
495#include <sys/types.h>
496
497static void create_profile_dir(const char *directory) {
498 char path[MAXPATHLEN];
499
500 MAKE_PATH(path, directory, NULL);
501 mkdir(path, S_IRWXU | S_IRWXG);
502}
503
504static void check_profile(void) {
505 // make sure that ~/.pcsx exists
506 create_profile_dir(PCSX_DOT_DIR);
507
508 create_profile_dir(BIOS_DIR);
509 create_profile_dir(MEMCARD_DIR);
510 create_profile_dir(STATES_DIR);
511 create_profile_dir(PLUGINS_DIR);
512 create_profile_dir(PLUGINS_CFG_DIR);
513 create_profile_dir(CHEATS_DIR);
514 create_profile_dir(PATCHES_DIR);
515 create_profile_dir(PCSX_DOT_DIR "cfg");
516 create_profile_dir("/screenshots/");
517}
518
519static void check_memcards(void)
520{
521 char buf[MAXPATHLEN];
522 FILE *f;
523 int i;
524
525 for (i = 1; i <= 9; i++) {
526 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
527
528 f = fopen(buf, "rb");
529 if (f == NULL) {
530 SysPrintf("Creating memcard: %s\n", buf);
531 CreateMcd(buf);
532 }
533 else
534 fclose(f);
535 }
536}
537
538int main(int argc, char *argv[])
539{
540 char file[MAXPATHLEN] = "";
541 char path[MAXPATHLEN];
542 const char *cdfile = NULL;
543 const char *loadst_f = NULL;
544 int psxout = 0;
545 int loadst = 0;
546 int i;
547
548 emu_core_preinit();
549
550 // read command line options
551 for (i = 1; i < argc; i++) {
552 if (!strcmp(argv[i], "-psxout")) psxout = 1;
553 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
554 else if (!strcmp(argv[i], "-cfg")) {
555 if (i+1 >= argc) break;
556 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
557 SysPrintf("Using config file %s.\n", cfgfile_basename);
558 }
559 else if (!strcmp(argv[i], "-cdfile")) {
560 char isofilename[MAXPATHLEN];
561
562 if (i+1 >= argc) break;
563 strncpy(isofilename, argv[++i], MAXPATHLEN);
564 if (isofilename[0] != '/') {
565 getcwd(path, MAXPATHLEN);
566 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
567 strcat(path, "/");
568 strcat(path, isofilename);
569 strcpy(isofilename, path);
570 } else
571 isofilename[0] = 0;
572 }
573
574 cdfile = isofilename;
575 }
576 else if (!strcmp(argv[i], "-loadf")) {
577 if (i+1 >= argc) break;
578 loadst_f = argv[++i];
579 }
580 else if (!strcmp(argv[i], "-h") ||
581 !strcmp(argv[i], "-help") ||
582 !strcmp(argv[i], "--help")) {
583 printf("PCSX-ReARMed " REV "\n");
584 printf("%s\n", _(
585 " pcsx [options] [file]\n"
586 "\toptions:\n"
587 "\t-cdfile FILE\tRuns a CD image file\n"
588 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
589 "\t-psxout\t\tEnable PSX output\n"
590 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
591 "\t-h -help\tDisplay this message\n"
592 "\tfile\t\tLoads a PSX EXE file\n"));
593 return 0;
594 } else {
595 strncpy(file, argv[i], MAXPATHLEN);
596 if (file[0] != '/') {
597 getcwd(path, MAXPATHLEN);
598 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
599 strcat(path, "/");
600 strcat(path, file);
601 strcpy(file, path);
602 } else
603 file[0] = 0;
604 }
605 }
606 }
607
608 if (cdfile)
609 set_cd_image(cdfile);
610
611 // frontend stuff
612 // init input but leave probing to platform code,
613 // they add input drivers and may need to modify them after probe
614 in_init();
615 pl_init();
616 plat_init();
617 menu_init(); // loads config
618
619 if (emu_core_init() != 0)
620 return 1;
621
622 if (psxout)
623 Config.PsxOut = 1;
624
625 if (LoadPlugins() == -1) {
626 // FIXME: this recovery doesn't work, just delete bad config and bail out
627 // SysMessage("could not load plugins, retrying with defaults\n");
628 set_default_paths();
629 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
630 remove(path);
631 SysMessage("Failed loading plugins!");
632 return 1;
633 }
634 pcnt_hook_plugins();
635
636 if (OpenPlugins() == -1) {
637 return 1;
638 }
639 plugin_call_rearmed_cbs();
640
641 CheckCdrom();
642 SysReset();
643
644 if (file[0] != '\0') {
645 if (Load(file) != -1)
646 ready_to_go = 1;
647 } else {
648 if (cdfile) {
649 if (LoadCdrom() == -1) {
650 ClosePlugins();
651 SysPrintf(_("Could not load CD-ROM!\n"));
652 return -1;
653 }
654 emu_on_new_cd(!loadst);
655 ready_to_go = 1;
656 }
657 }
658
659 if (loadst_f) {
660 int ret = LoadState(loadst_f);
661 SysPrintf("%s state file: %s\n",
662 ret ? "failed to load" : "loaded", loadst_f);
663 ready_to_go |= ret == 0;
664 }
665
666 if (ready_to_go) {
667 menu_prepare_emu();
668
669 // If a state has been specified, then load that
670 if (loadst) {
671 int ret = emu_load_state(loadst - 1);
672 SysPrintf("%s state %d\n",
673 ret ? "failed to load" : "loaded", loadst);
674 }
675 }
676 else
677 menu_loop();
678
679 pl_start_watchdog();
680
681 while (!g_emu_want_quit)
682 {
683 stop = 0;
684 emu_action = SACTION_NONE;
685
686 psxCpu->Execute();
687 if (emu_action != SACTION_NONE)
688 do_emu_action();
689 }
690
691 printf("Exit..\n");
692 ClosePlugins();
693 SysClose();
694 menu_finish();
695 plat_finish();
696
697 return 0;
698}
699
700static void toggle_fast_forward(int force_off)
701{
702 static int fast_forward;
703 static int normal_g_opts;
704 static int normal_frameskip;
705 static int normal_enhancement_enable;
706
707 if (force_off && !fast_forward)
708 return;
709
710 fast_forward = !fast_forward;
711 if (fast_forward) {
712 normal_g_opts = g_opts;
713 normal_frameskip = pl_rearmed_cbs.frameskip;
714 normal_enhancement_enable =
715 pl_rearmed_cbs.gpu_neon.enhancement_enable;
716
717 g_opts |= OPT_NO_FRAMELIM;
718 pl_rearmed_cbs.frameskip = 3;
719 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
720 } else {
721 g_opts = normal_g_opts;
722 pl_rearmed_cbs.frameskip = normal_frameskip;
723 pl_rearmed_cbs.gpu_neon.enhancement_enable =
724 normal_enhancement_enable;
725
726 pl_timing_prepare(Config.PsxType);
727 }
728
729 if (!force_off)
730 snprintf(hud_msg, sizeof(hud_msg), "FAST FORWARD %s",
731 fast_forward ? "ON" : "OFF");
732}
733
734static void SignalExit(int sig) {
735 // only to restore framebuffer/resolution on some devices
736 plat_finish();
737 exit(1);
738}
739#endif
740
741void SysRunGui() {
742 printf("SysRunGui\n");
743}
744
745static void CALLBACK dummy_lace()
746{
747}
748
749void SysReset() {
750 // rearmed hack: EmuReset() runs some code when real BIOS is used,
751 // but we usually do reset from menu while GPU is not open yet,
752 // so we need to prevent updateLace() call..
753 void *real_lace = GPU_updateLace;
754 GPU_updateLace = dummy_lace;
755 g_emu_resetting = 1;
756
757 // reset can run code, timing must be set
758 pl_timing_prepare(Config.PsxType);
759
760 // hmh core forgets this
761 CDR_stop();
762
763 EmuReset();
764
765 GPU_updateLace = real_lace;
766 g_emu_resetting = 0;
767}
768
769void SysClose() {
770 EmuShutdown();
771 ReleasePlugins();
772
773 StopDebugger();
774
775 if (emuLog != NULL && emuLog != stdout && emuLog != stderr) {
776 fclose(emuLog);
777 emuLog = NULL;
778 }
779}
780
781void SysUpdate() {
782}
783
784int get_state_filename(char *buf, int size, int i) {
785 return get_gameid_filename(buf, size,
786 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
787}
788
789int emu_check_state(int slot)
790{
791 char fname[MAXPATHLEN];
792 int ret;
793
794 ret = get_state_filename(fname, sizeof(fname), slot);
795 if (ret != 0)
796 return ret;
797
798 return CheckState(fname);
799}
800
801int emu_save_state(int slot)
802{
803 char fname[MAXPATHLEN];
804 int ret;
805
806 ret = get_state_filename(fname, sizeof(fname), slot);
807 if (ret != 0)
808 return ret;
809
810 ret = SaveState(fname);
811#if defined(HAVE_PRE_ARMV7) && !defined(_3DS) && !defined(__SWITCH__) /* XXX GPH hack */
812 sync();
813#endif
814 SysPrintf("* %s \"%s\" [%d]\n",
815 ret == 0 ? "saved" : "failed to save", fname, slot);
816 return ret;
817}
818
819int emu_load_state(int slot)
820{
821 char fname[MAXPATHLEN];
822 int ret;
823
824 hud_msg[0] = 0;
825
826 ret = get_state_filename(fname, sizeof(fname), slot);
827 if (ret != 0)
828 return ret;
829
830 return LoadState(fname);
831}
832
833#ifndef HAVE_LIBRETRO
834#ifndef ANDROID
835
836void SysPrintf(const char *fmt, ...) {
837 va_list list;
838
839 va_start(list, fmt);
840 vfprintf(emuLog, fmt, list);
841 va_end(list);
842 fflush(emuLog);
843}
844
845#else
846
847#include <android/log.h>
848
849void SysPrintf(const char *fmt, ...) {
850 va_list list;
851
852 va_start(list, fmt);
853 __android_log_vprint(ANDROID_LOG_INFO, "PCSX", fmt, list);
854 va_end(list);
855}
856
857#endif
858#endif /* HAVE_LIBRETRO */
859
860void SysMessage(const char *fmt, ...) {
861 va_list list;
862 char msg[512];
863 int ret;
864
865 va_start(list, fmt);
866 ret = vsnprintf(msg, sizeof(msg), fmt, list);
867 va_end(list);
868
869 if (ret < sizeof(msg) && msg[ret - 1] == '\n')
870 msg[ret - 1] = 0;
871
872 SysPrintf("%s\n", msg);
873}
874
875#define PARSEPATH(dst, src) \
876 ptr = src + strlen(src); \
877 while (*ptr != '\\' && ptr != src) ptr--; \
878 if (ptr != src) { \
879 strcpy(dst, ptr+1); \
880 }
881
882static int _OpenPlugins(void) {
883 int ret;
884
885#ifndef NO_FRONTEND
886 signal(SIGINT, SignalExit);
887 signal(SIGPIPE, SignalExit);
888#endif
889
890 GPU_clearDynarec(clearDynarec);
891
892 ret = CDR_open();
893 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
894 ret = SPU_open();
895 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
896 SPU_registerCallback(SPUirq);
897 SPU_registerScheduleCb(SPUschedule);
898 // pcsx-rearmed: we handle gpu elsewhere
899 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
900 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
901 ret = PAD1_open(&gpuDisp);
902 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
903 ret = PAD2_open(&gpuDisp);
904 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
905
906 if (Config.UseNet && !NetOpened) {
907 netInfo info;
908 char path[MAXPATHLEN * 2];
909 char dotdir[MAXPATHLEN];
910
911 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
912
913 strcpy(info.EmuName, "PCSX");
914 memcpy(info.CdromID, CdromId, 9); /* no \0 trailing character? */
915 memcpy(info.CdromLabel, CdromLabel, 9);
916 info.CdromLabel[9] = '\0';
917 info.psxMem = psxM;
918 info.GPU_showScreenPic = GPU_showScreenPic;
919 info.GPU_displayText = GPU_displayText;
920 info.GPU_showScreenPic = GPU_showScreenPic;
921 info.PAD_setSensitive = PAD1_setSensitive;
922 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
923 strcpy(info.BIOSpath, path);
924 strcpy(info.MCD1path, Config.Mcd1);
925 strcpy(info.MCD2path, Config.Mcd2);
926 sprintf(path, "%s%s", dotdir, Config.Gpu);
927 strcpy(info.GPUpath, path);
928 sprintf(path, "%s%s", dotdir, Config.Spu);
929 strcpy(info.SPUpath, path);
930 sprintf(path, "%s%s", dotdir, Config.Cdr);
931 strcpy(info.CDRpath, path);
932 NET_setInfo(&info);
933
934 ret = NET_open(&gpuDisp);
935 if (ret < 0) {
936 if (ret == -2) {
937 // -2 is returned when something in the info
938 // changed and needs to be synced
939 char *ptr;
940
941 PARSEPATH(Config.Bios, info.BIOSpath);
942 PARSEPATH(Config.Gpu, info.GPUpath);
943 PARSEPATH(Config.Spu, info.SPUpath);
944 PARSEPATH(Config.Cdr, info.CDRpath);
945
946 strcpy(Config.Mcd1, info.MCD1path);
947 strcpy(Config.Mcd2, info.MCD2path);
948 return -2;
949 } else {
950 Config.UseNet = FALSE;
951 }
952 } else {
953 if (NET_queryPlayer() == 1) {
954 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
955 } else {
956 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
957 }
958 }
959 NetOpened = TRUE;
960 } else if (Config.UseNet) {
961 NET_resume();
962 }
963
964 return 0;
965}
966
967int OpenPlugins() {
968 int ret;
969
970 while ((ret = _OpenPlugins()) == -2) {
971 ReleasePlugins();
972 LoadMcds(Config.Mcd1, Config.Mcd2);
973 if (LoadPlugins() == -1) return -1;
974 }
975 return ret;
976}
977
978void ClosePlugins() {
979 int ret;
980
981#ifndef NO_FRONTEND
982 signal(SIGINT, SIG_DFL);
983 signal(SIGPIPE, SIG_DFL);
984#endif
985
986 ret = CDR_close();
987 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
988 ret = SPU_close();
989 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
990 ret = PAD1_close();
991 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
992 ret = PAD2_close();
993 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
994 // pcsx-rearmed: we handle gpu elsewhere
995 //ret = GPU_close();
996 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
997
998 if (Config.UseNet) {
999 NET_pause();
1000 }
1001}
1002
1003/* we hook statically linked plugins here */
1004static const char *builtin_plugins[] = {
1005 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
1006 "builtin_cdrcimg",
1007};
1008
1009static const int builtin_plugin_ids[] = {
1010 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
1011 PLUGIN_CDRCIMG,
1012};
1013
1014void *SysLoadLibrary(const char *lib) {
1015 const char *tmp = strrchr(lib, '/');
1016 void *ret = NULL;
1017 int i;
1018
1019 SysPrintf("plugin: %s\n", lib);
1020
1021 if (tmp != NULL) {
1022 tmp++;
1023 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
1024 if (strcmp(tmp, builtin_plugins[i]) == 0)
1025 return (void *)(uintptr_t)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
1026 }
1027
1028#if !defined(_WIN32) && !defined(NO_DYLIB)
1029 ret = dlopen(lib, RTLD_NOW);
1030 if (ret == NULL)
1031 SysMessage("dlopen: %s", dlerror());
1032#else
1033 /* no external plugin support, abi is no longer
1034 * compatible with psemu/pcsx anyway */
1035#endif
1036 return ret;
1037}
1038
1039void *SysLoadSym(void *lib, const char *sym) {
1040 unsigned int plugid = (unsigned int)(uintptr_t)lib;
1041
1042 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
1043 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
1044
1045#if !defined(_WIN32) && !defined(NO_DYLIB)
1046 return dlsym(lib, sym);
1047#else
1048 return NULL;
1049#endif
1050}
1051
1052const char *SysLibError() {
1053#if defined(NO_DYLIB)
1054 return NULL;
1055#elif !defined(_WIN32)
1056 return dlerror();
1057#else
1058 return "not supported";
1059#endif
1060}
1061
1062void SysCloseLibrary(void *lib) {
1063 unsigned int plugid = (unsigned int)(uintptr_t)lib;
1064
1065 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
1066 return;
1067
1068#if !defined(_WIN32) && !defined(NO_DYLIB)
1069 dlclose(lib);
1070#endif
1071}