cdrom: make the timing hack conditional
[pcsx_rearmed.git] / frontend / main.c
CommitLineData
e906c010 1/*
dde7da71 2 * (C) notaz, 2010-2012
e906c010 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
80c2304e 8#include <stdio.h>
9#include <string.h>
10#include <stdarg.h>
80c2304e 11#include <unistd.h>
c5061935 12#include <signal.h>
29a8c4f3 13#include <time.h>
7a8d521f 14#if !defined(_WIN32) && !defined(NO_DYLIB)
003cfc63 15#include <dlfcn.h>
16#endif
80c2304e 17
c5061935 18#include "main.h"
e906c010 19#include "plugin.h"
7c0f51de 20#include "plugin_lib.h"
14dffdb7 21#include "pcnt.h"
3c70c47b 22#include "menu.h"
47821672 23#include "plat.h"
80c2304e 24#include "../libpcsxcore/misc.h"
9c27c205 25#include "../libpcsxcore/cheat.h"
799b0b87 26#include "../libpcsxcore/new_dynarec/new_dynarec.h"
47bf65ab 27#include "../plugins/cdrcimg/cdrcimg.h"
3154bfab 28#include "../plugins/dfsound/spu_config.h"
8f2bb0cb 29#include "arm_features.h"
38c2028e 30#include "revision.h"
80c2304e 31
1f1128d0 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
cc56203b 40#ifndef NO_FRONTEND
41#include "libpicofe/input.h"
42#include "libpicofe/plat.h"
43#include "libpicofe/readpng.h"
87e5b45f 44
45static void toggle_fast_forward(int force_off);
e6495add 46static void check_profile(void);
47static void check_memcards(void);
cc56203b 48#endif
a1b44e36 49#ifndef BOOT_MSG
50#define BOOT_MSG "Booting up..."
51#endif
cc56203b 52
fc8145b7 53// don't include debug.h - it breaks ARM build (R1 redefined)
7d7672a5 54static void StartDebugger() {}
55static void StopDebugger() {}
fc8145b7 56
69e482e3 57int ready_to_go, g_emu_want_quit, g_emu_resetting;
c5061935 58unsigned long gpuDisp;
59char cfgfile_basename[MAXPATHLEN];
8f892648 60int state_slot;
61enum sched_action emu_action, emu_action_old;
62char hud_msg[64];
63int hud_new_msg;
64
80c2304e 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
29a8c4f3 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;
80c2304e 90}
91
47bf65ab 92void set_cd_image(const char *fname)
93{
e16a7e51 94 const char *ext = NULL;
47bf65ab 95
33716956 96 if (fname != NULL)
97 ext = strrchr(fname, '.');
47bf65ab 98
33716956 99 if (ext && (
100 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
9a92bffb 101 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
47bf65ab 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
6fe1f056 111static void set_default_paths(void)
112{
e6495add 113#ifndef NO_FRONTEND
114 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
6fe1f056 115 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
116 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
117 strcpy(Config.BiosDir, "bios");
e6495add 118#endif
6fe1f056 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");
6fe1f056 127}
128
33400707 129void emu_set_default_config(void)
130{
131 // try to set sane config on which most games work
d014a471 132 Config.Xa = Config.Cdda = 0;
133 Config.icache_emulation = 0;
33400707 134 Config.PsxAuto = 1;
d5aeda23 135 Config.cycle_multiplier = CYCLE_MULT_DEFAULT;
33400707 136
746fee51 137 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
0b02eb77 138 pl_rearmed_cbs.gpu_neon.enhancement_enable =
139 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
33400707 140 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
141 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
0bfe8d59 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;
33400707 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;
e60da159 151 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
746fee51 152 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
cbb26048 153 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
33400707 154
3154bfab 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;
63a4f6b6 160 spu_config.iUseThread = 1; // no effect if only 1 core is detected
7a8d521f 161#if defined(HAVE_PRE_ARMV7) && !defined(_3DS) /* XXX GPH hack */
3154bfab 162 spu_config.iUseReverb = 0;
163 spu_config.iUseInterpolation = 0;
7a8d521f 164#ifndef HAVE_LIBRETRO
3154bfab 165 spu_config.iTempo = 1;
7a8d521f 166#endif
33400707 167#endif
168 new_dynarec_hacks = 0;
33400707 169
7a8d521f 170 in_type[0] = PSE_PAD_TYPE_STANDARD;
171 in_type[1] = PSE_PAD_TYPE_STANDARD;
33400707 172}
173
8f892648 174void do_emu_action(void)
175{
176 int ret;
177
178 emu_action_old = emu_action;
179
180 switch (emu_action) {
8f892648 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;
38c2028e 189#ifndef NO_FRONTEND
190 case SACTION_ENTER_MENU:
456f1b86 191 toggle_fast_forward(1);
38c2028e 192 menu_loop();
193 return;
8f892648 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;
38c2028e 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;
f29fbd53 207 SysPrintf("* %s\n", hud_msg);
38c2028e 208 break;
8f892648 209 case SACTION_TOGGLE_FSKIP:
ea4a16e7 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();
8f892648 218 break;
fa56d360 219 case SACTION_SWITCH_DISPMODE:
220 pl_switch_dispmode();
d81b8e97 221 plugin_call_rearmed_cbs();
fa56d360 222 if (GPU_open != NULL && GPU_close != NULL) {
223 GPU_close();
224 GPU_open(&gpuDisp, "PCSX", NULL);
225 }
d81b8e97 226 break;
456f1b86 227 case SACTION_FAST_FORWARD:
228 toggle_fast_forward(0);
229 plugin_call_rearmed_cbs();
230 break;
dde7da71 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;
a8376201 240 case SACTION_TOGGLE_FULLSCREEN:
5b9aa749 241 plat_target.vout_fullscreen = !plat_target.vout_fullscreen;
c9099d02 242 if (GPU_open != NULL && GPU_close != NULL) {
243 GPU_close();
244 GPU_open(&gpuDisp, "PCSX", NULL);
245 }
a8376201 246 break;
29a8c4f3 247 case SACTION_SCREENSHOT:
248 {
38c2028e 249 char buf[MAXPATHLEN];
29a8c4f3 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 }
221be40d 267 case SACTION_VOLUME_UP:
268 case SACTION_VOLUME_DOWN:
bcfc48e3 269 {
270 static int volume;
271 plat_target_step_volume(&volume,
272 emu_action == SACTION_VOLUME_UP ? 1 : -1);
273 }
221be40d 274 return;
a805c855 275 case SACTION_MINIMIZE:
36dfb787 276 if (GPU_close != NULL)
277 GPU_close();
278
a805c855 279 plat_minimize();
36dfb787 280
281 if (GPU_open != NULL) {
282 ret = GPU_open(&gpuDisp, "PCSX", NULL);
283 if (ret)
f29fbd53 284 SysMessage("GPU_open returned %d", ret);
36dfb787 285 }
a805c855 286 return;
38c2028e 287#endif
4c08b9e7 288 default:
289 return;
8f892648 290 }
8f892648 291
8f892648 292 hud_new_msg = 3;
293}
294
cae602d4 295static char basic_lcase(char c)
296{
297 if ('A' <= c && c <= 'Z')
298 return c - 'A' + 'a';
299 return c;
300}
301
9c27c205 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; }
cae602d4 307 if (basic_lcase(*id1) != basic_lcase(*id2))
9c27c205 308 break;
309 id1++;
310 id2++;
311 }
312
313 return *id1 - *id2;
314}
315
316static void parse_cwcheat(void)
317{
7a8d521f 318 char line[256], buf[256], name[256], *p;
9c27c205 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
f29fbd53 338 SysPrintf("cwcheat section found for %s\n", CdromId);
9c27c205 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) {
f29fbd53 350 SysPrintf(" cwcheat game name: '%s'\n", line + 3);
9c27c205 351 continue;
352 }
353 if (strncmp(line, "_C0", 3) == 0) {
354 if (!newcheat && Cheats[NumCheats - 1].n == 0) {
f29fbd53 355 SysPrintf("cheat '%s' failed to parse\n", name);
9c27c205 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) {
f29fbd53 364 SysPrintf("line failed to parse: '%s'\n", line);
9c27c205 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;
2c843d96 378 Cheats[NumCheats].WasEnabled = 0;
9c27c205 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
79f216e3 402void emu_on_new_cd(int show_hud_msg)
9c27c205 403{
404 ClearAllCheats();
405 parse_cwcheat();
406
407 if (Config.HLE) {
f29fbd53 408 SysPrintf("note: running with HLE BIOS, expect compatibility problems\n");
409 SysPrintf("----------------------------------------------------------\n");
9c27c205 410 }
411
79f216e3 412 if (show_hud_msg) {
413 snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG);
414 hud_new_msg = 3;
415 }
9c27c205 416}
417
1f1128d0 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
38c2028e 436int emu_core_preinit(void)
80c2304e 437{
80c2304e 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
ea12009c 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
e906c010 448 emuLog = stdout;
ea12009c 449
1f1128d0 450 log_wrong_cpu();
451
80c2304e 452 SetIsoFile(NULL);
80c2304e 453
e0c692d9 454 memset(&Config, 0, sizeof(Config));
455
e0c692d9 456 set_default_paths();
33400707 457 emu_set_default_config();
e0c692d9 458 strcpy(Config.Bios, "HLE");
459
38c2028e 460 return 0;
461}
462
463int emu_core_init(void)
464{
45b97ede 465 SysPrintf("Starting PCSX-ReARMed " REV "\n");
466
e6495add 467#ifndef NO_FRONTEND
468 check_profile();
38c2028e 469 check_memcards();
e6495add 470#endif
38c2028e 471
472 if (EmuInit() == -1) {
f29fbd53 473 SysPrintf("PSX emulator couldn't be initialized.\n");
38c2028e 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
69e482e3 486void emu_core_ask_exit(void)
487{
488 stop = 1;
489 g_emu_want_quit = 1;
490}
491
38c2028e 492#ifndef NO_FRONTEND
003cfc63 493
494#include <sys/stat.h>
495#include <sys/types.h>
496
e6495add 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
38c2028e 538int main(int argc, char *argv[])
539{
e0c692d9 540 char file[MAXPATHLEN] = "";
541 char path[MAXPATHLEN];
542 const char *cdfile = NULL;
de910c6b 543 const char *loadst_f = NULL;
0a151868 544 int psxout = 0;
e0c692d9 545 int loadst = 0;
546 int i;
547
38c2028e 548 emu_core_preinit();
549
80c2304e 550 // read command line options
551 for (i = 1; i < argc; i++) {
0a151868 552 if (!strcmp(argv[i], "-psxout")) psxout = 1;
80c2304e 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 */
f29fbd53 557 SysPrintf("Using config file %s.\n", cfgfile_basename);
80c2304e 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
47bf65ab 574 cdfile = isofilename;
80c2304e 575 }
de910c6b 576 else if (!strcmp(argv[i], "-loadf")) {
577 if (i+1 >= argc) break;
578 loadst_f = argv[++i];
579 }
80c2304e 580 else if (!strcmp(argv[i], "-h") ||
581 !strcmp(argv[i], "-help") ||
582 !strcmp(argv[i], "--help")) {
38c2028e 583 printf("PCSX-ReARMed " REV "\n");
80c2304e 584 printf("%s\n", _(
585 " pcsx [options] [file]\n"
586 "\toptions:\n"
587 "\t-cdfile FILE\tRuns a CD image file\n"
80c2304e 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"
38c2028e 592 "\tfile\t\tLoads a PSX EXE file\n"));
80c2304e 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
47bf65ab 608 if (cdfile)
609 set_cd_image(cdfile);
610
69af03a2 611 // frontend stuff
b07c18e8 612 // init input but leave probing to platform code,
613 // they add input drivers and may need to modify them after probe
69af03a2 614 in_init();
59f1c85c 615 pl_init();
69af03a2 616 plat_init();
0a151868 617 menu_init(); // loads config
618
45b97ede 619 if (emu_core_init() != 0)
620 return 1;
38c2028e 621
0a151868 622 if (psxout)
623 Config.PsxOut = 1;
80c2304e 624
69af03a2 625 if (LoadPlugins() == -1) {
6fe1f056 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);
69af03a2 631 SysMessage("Failed loading plugins!");
632 return 1;
633 }
634 pcnt_hook_plugins();
80c2304e 635
69af03a2 636 if (OpenPlugins() == -1) {
637 return 1;
638 }
201c21e2 639 plugin_call_rearmed_cbs();
80c2304e 640
69af03a2 641 CheckCdrom();
bbd837c6 642 SysReset();
69af03a2 643
644 if (file[0] != '\0') {
645 if (Load(file) != -1)
646 ready_to_go = 1;
647 } else {
47bf65ab 648 if (cdfile) {
69af03a2 649 if (LoadCdrom() == -1) {
650 ClosePlugins();
f29fbd53 651 SysPrintf(_("Could not load CD-ROM!\n"));
69af03a2 652 return -1;
80c2304e 653 }
79f216e3 654 emu_on_new_cd(!loadst);
69af03a2 655 ready_to_go = 1;
80c2304e 656 }
69af03a2 657 }
80c2304e 658
ea66e884 659 if (loadst_f) {
660 int ret = LoadState(loadst_f);
f29fbd53 661 SysPrintf("%s state file: %s\n",
662 ret ? "failed to load" : "loaded", loadst_f);
ea66e884 663 ready_to_go |= ret == 0;
664 }
665
45d45c1e 666 if (ready_to_go) {
32631e6a 667 menu_prepare_emu();
45d45c1e 668
669 // If a state has been specified, then load that
670 if (loadst) {
671 int ret = emu_load_state(loadst - 1);
f29fbd53 672 SysPrintf("%s state %d\n",
673 ret ? "failed to load" : "loaded", loadst);
45d45c1e 674 }
675 }
3c70c47b 676 else
69af03a2 677 menu_loop();
678
7c0f51de 679 pl_start_watchdog();
680
69e482e3 681 while (!g_emu_want_quit)
69af03a2 682 {
799b0b87 683 stop = 0;
8f892648 684 emu_action = SACTION_NONE;
685
80c2304e 686 psxCpu->Execute();
8f892648 687 if (emu_action != SACTION_NONE)
688 do_emu_action();
80c2304e 689 }
690
69e482e3 691 printf("Exit..\n");
692 ClosePlugins();
693 SysClose();
694 menu_finish();
695 plat_finish();
696
80c2304e 697 return 0;
698}
456f1b86 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}
ee1a37fd 733
734static void SignalExit(int sig) {
735 // only to restore framebuffer/resolution on some devices
736 plat_finish();
737 exit(1);
738}
38c2028e 739#endif
80c2304e 740
741void SysRunGui() {
742 printf("SysRunGui\n");
743}
744
029e681c 745static void CALLBACK dummy_lace()
e6eb2066 746{
747}
748
80c2304e 749void SysReset() {
e6eb2066 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;
69e482e3 755 g_emu_resetting = 1;
e6eb2066 756
33400707 757 // reset can run code, timing must be set
758 pl_timing_prepare(Config.PsxType);
759
bd6267e6 760 // hmh core forgets this
761 CDR_stop();
7a8d521f 762
763 EmuReset();
e6eb2066 764
765 GPU_updateLace = real_lace;
69e482e3 766 g_emu_resetting = 0;
80c2304e 767}
768
769void SysClose() {
770 EmuShutdown();
771 ReleasePlugins();
772
773 StopDebugger();
774
69e482e3 775 if (emuLog != NULL && emuLog != stdout && emuLog != stderr) {
776 fclose(emuLog);
777 emuLog = NULL;
778 }
80c2304e 779}
780
781void SysUpdate() {
80c2304e 782}
783
c5061935 784int get_state_filename(char *buf, int size, int i) {
29a8c4f3 785 return get_gameid_filename(buf, size,
786 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
80c2304e 787}
788
8f892648 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
90f1d26c 810 ret = SaveState(fname);
7a8d521f 811#if defined(HAVE_PRE_ARMV7) && !defined(_3DS) && !defined(__SWITCH__) /* XXX GPH hack */
0b1dbe6b 812 sync();
813#endif
f29fbd53 814 SysPrintf("* %s \"%s\" [%d]\n",
815 ret == 0 ? "saved" : "failed to save", fname, slot);
90f1d26c 816 return ret;
8f892648 817}
818
819int emu_load_state(int slot)
820{
821 char fname[MAXPATHLEN];
822 int ret;
823
dde7da71 824 hud_msg[0] = 0;
825
8f892648 826 ret = get_state_filename(fname, sizeof(fname), slot);
827 if (ret != 0)
828 return ret;
829
830 return LoadState(fname);
831}
832
7a8d521f 833#ifndef HAVE_LIBRETRO
45b97ede 834#ifndef ANDROID
835
80c2304e 836void SysPrintf(const char *fmt, ...) {
837 va_list list;
80c2304e 838
839 va_start(list, fmt);
f29fbd53 840 vfprintf(emuLog, fmt, list);
80c2304e 841 va_end(list);
82fe9e47 842 fflush(emuLog);
80c2304e 843}
844
45b97ede 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
7a8d521f 858#endif /* HAVE_LIBRETRO */
45b97ede 859
80c2304e 860void SysMessage(const char *fmt, ...) {
45b97ede 861 va_list list;
862 char msg[512];
863 int ret;
80c2304e 864
45b97ede 865 va_start(list, fmt);
866 ret = vsnprintf(msg, sizeof(msg), fmt, list);
867 va_end(list);
80c2304e 868
45b97ede 869 if (ret < sizeof(msg) && msg[ret - 1] == '\n')
870 msg[ret - 1] = 0;
80c2304e 871
f29fbd53 872 SysPrintf("%s\n", msg);
80c2304e 873}
874
c5061935 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
ee1a37fd 885#ifndef NO_FRONTEND
c5061935 886 signal(SIGINT, SignalExit);
887 signal(SIGPIPE, SignalExit);
ee1a37fd 888#endif
c5061935 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);
2b30c129 897 SPU_registerScheduleCb(SPUschedule);
c5061935 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;
7a8d521f 908 char path[MAXPATHLEN * 2];
c5061935 909 char dotdir[MAXPATHLEN];
910
911 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
912
38c2028e 913 strcpy(info.EmuName, "PCSX");
7a8d521f 914 memcpy(info.CdromID, CdromId, 9); /* no \0 trailing character? */
915 memcpy(info.CdromLabel, CdromLabel, 9);
916 info.CdromLabel[9] = '\0';
c5061935 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
ee1a37fd 981#ifndef NO_FRONTEND
c5061935 982 signal(SIGINT, SIG_DFL);
983 signal(SIGPIPE, SIG_DFL);
ee1a37fd 984#endif
985
c5061935 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
e906c010 1003/* we hook statically linked plugins here */
1004static const char *builtin_plugins[] = {
47bf65ab 1005 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
1006 "builtin_cdrcimg",
e906c010 1007};
1008
1009static const int builtin_plugin_ids[] = {
1010 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
47bf65ab 1011 PLUGIN_CDRCIMG,
e906c010 1012};
1013
80c2304e 1014void *SysLoadLibrary(const char *lib) {
e906c010 1015 const char *tmp = strrchr(lib, '/');
003cfc63 1016 void *ret = NULL;
e906c010 1017 int i;
1018
f29fbd53 1019 SysPrintf("plugin: %s\n", lib);
bbd837c6 1020
e906c010 1021 if (tmp != NULL) {
1022 tmp++;
1023 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
1024 if (strcmp(tmp, builtin_plugins[i]) == 0)
77e1e479 1025 return (void *)(uintptr_t)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
e906c010 1026 }
1027
7a8d521f 1028#if !defined(_WIN32) && !defined(NO_DYLIB)
bbd837c6 1029 ret = dlopen(lib, RTLD_NOW);
1030 if (ret == NULL)
f29fbd53 1031 SysMessage("dlopen: %s", dlerror());
003cfc63 1032#else
1033 /* no external plugin support, abi is no longer
1034 * compatible with psemu/pcsx anyway */
1035#endif
bbd837c6 1036 return ret;
80c2304e 1037}
1038
1039void *SysLoadSym(void *lib, const char *sym) {
77e1e479 1040 unsigned int plugid = (unsigned int)(uintptr_t)lib;
e906c010 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
7a8d521f 1045#if !defined(_WIN32) && !defined(NO_DYLIB)
80c2304e 1046 return dlsym(lib, sym);
003cfc63 1047#else
1048 return NULL;
1049#endif
80c2304e 1050}
1051
1052const char *SysLibError() {
7a8d521f 1053#if defined(NO_DYLIB)
1054 return NULL;
1055#elif !defined(_WIN32)
80c2304e 1056 return dlerror();
003cfc63 1057#else
1058 return "not supported";
1059#endif
80c2304e 1060}
1061
1062void SysCloseLibrary(void *lib) {
77e1e479 1063 unsigned int plugid = (unsigned int)(uintptr_t)lib;
e906c010 1064
1065 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
1066 return;
1067
7a8d521f 1068#if !defined(_WIN32) && !defined(NO_DYLIB)
80c2304e 1069 dlclose(lib);
003cfc63 1070#endif
80c2304e 1071}