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