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