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