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