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