cdrom-async: Fix subchannel read with hardware CD-ROM
[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>
7a8d521f 14#if !defined(_WIN32) && !defined(NO_DYLIB)
003cfc63 15#include <dlfcn.h>
16#endif
d135f6d2 17#ifdef HAVE_RTHREADS
18#include "../frontend/libretro-rthreads.h"
fad9f755 19#endif
80c2304e 20
c5061935 21#include "main.h"
e906c010 22#include "plugin.h"
7c0f51de 23#include "plugin_lib.h"
14dffdb7 24#include "pcnt.h"
3c70c47b 25#include "menu.h"
47821672 26#include "plat.h"
80c2304e 27#include "../libpcsxcore/misc.h"
9c27c205 28#include "../libpcsxcore/cheat.h"
de74f599 29#include "../libpcsxcore/sio.h"
f3746eea 30#include "../libpcsxcore/database.h"
08d9d257 31#include "../libpcsxcore/cdrom-async.h"
799b0b87 32#include "../libpcsxcore/new_dynarec/new_dynarec.h"
47bf65ab 33#include "../plugins/cdrcimg/cdrcimg.h"
3154bfab 34#include "../plugins/dfsound/spu_config.h"
8f2bb0cb 35#include "arm_features.h"
38c2028e 36#include "revision.h"
80c2304e 37
a3c46b7f 38#if defined(__EMSCRIPTEN__)
39#define DO_CPU_CHECKS 0
40#elif defined(__has_builtin)
1f1128d0 41#define DO_CPU_CHECKS __has_builtin(__builtin_cpu_init)
42#elif defined(__x86_64__) || defined(__i386__)
43#define DO_CPU_CHECKS 1
44#else
45#define DO_CPU_CHECKS 0
46#endif
47
cc56203b 48#ifndef NO_FRONTEND
1ae743b1 49#include <sys/stat.h>
cc56203b 50#include "libpicofe/input.h"
51#include "libpicofe/plat.h"
52#include "libpicofe/readpng.h"
87e5b45f 53
54static void toggle_fast_forward(int force_off);
e6495add 55static void check_profile(void);
56static void check_memcards(void);
1ae743b1 57static int get_gameid_filename(char *buf, int size, const char *fmt, int i);
58static const char *get_home_dir(void);
59#define MAKE_PATH(buf, dir, fname) \
60 emu_make_path(buf, sizeof(buf), dir, fname)
61
cc56203b 62#endif
a1b44e36 63#ifndef BOOT_MSG
64#define BOOT_MSG "Booting up..."
65#endif
cc56203b 66
fc8145b7 67// don't include debug.h - it breaks ARM build (R1 redefined)
7d7672a5 68static void StartDebugger() {}
69static void StopDebugger() {}
fc8145b7 70
69e482e3 71int ready_to_go, g_emu_want_quit, g_emu_resetting;
c5061935 72unsigned long gpuDisp;
73char cfgfile_basename[MAXPATHLEN];
8f892648 74int state_slot;
75enum sched_action emu_action, emu_action_old;
76char hud_msg[64];
77int hud_new_msg;
78
47bf65ab 79void set_cd_image(const char *fname)
80{
08d9d257 81 SetIsoFile(fname);
47bf65ab 82}
83
6fe1f056 84static void set_default_paths(void)
85{
e6495add 86#ifndef NO_FRONTEND
1ae743b1 87 const char *home = get_home_dir();
88 struct stat st;
89 MAKE_PATH(Config.PatchesDir, PATCHES_DIR, NULL);
6fe1f056 90 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
91 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
1ae743b1 92 MAKE_PATH(Config.BiosDir, BIOS_DIR, NULL);
93
94 emu_make_data_path(Config.PluginsDir, "plugins", sizeof(Config.PluginsDir));
95
96 // prefer bios in working dir for compatibility
97 if (!strcmp(home, ".") && !stat("bios", &st))
98 strcpy(Config.BiosDir, "bios");
99
100 SysPrintf("dirs: profile=%s" PCSX_DOT_DIR ", bios=%s, plugins=%s\n",
101 home, Config.BiosDir, Config.PluginsDir);
e6495add 102#endif
6fe1f056 103
6fe1f056 104 strcpy(Config.Gpu, "builtin_gpu");
105 strcpy(Config.Spu, "builtin_spu");
6fe1f056 106}
107
33400707 108void emu_set_default_config(void)
109{
110 // try to set sane config on which most games work
d014a471 111 Config.Xa = Config.Cdda = 0;
112 Config.icache_emulation = 0;
33400707 113 Config.PsxAuto = 1;
d5aeda23 114 Config.cycle_multiplier = CYCLE_MULT_DEFAULT;
8c84ba5f 115 Config.GpuListWalking = -1;
4c8f1c25 116 Config.FractionalFramerate = -1;
33400707 117
ed8077c5 118 pl_rearmed_cbs.dithering = 1;
746fee51 119 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
0b02eb77 120 pl_rearmed_cbs.gpu_neon.enhancement_enable =
121 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
f0ca3e2f 122 pl_rearmed_cbs.gpu_neon.enhancement_tex_adj = 1;
33400707 123 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
5ddc3a7a 124 pl_rearmed_cbs.gpu_unai.old_renderer = 0;
4cfc568d 125 pl_rearmed_cbs.gpu_unai.ilace_force = 0;
4cfc568d 126 pl_rearmed_cbs.gpu_unai.lighting = 1;
127 pl_rearmed_cbs.gpu_unai.fast_lighting = 0;
128 pl_rearmed_cbs.gpu_unai.blending = 1;
e60da159 129 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
746fee51 130 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
cbb26048 131 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
33400707 132
3154bfab 133 spu_config.iUseReverb = 1;
134 spu_config.iUseInterpolation = 1;
135 spu_config.iXAPitch = 0;
136 spu_config.iVolume = 768;
137 spu_config.iTempo = 0;
16809ab9 138 // may cause issues, no effect if only 1 core is detected
139 spu_config.iUseThread = 0;
7a8d521f 140#if defined(HAVE_PRE_ARMV7) && !defined(_3DS) /* XXX GPH hack */
3154bfab 141 spu_config.iUseReverb = 0;
142 spu_config.iUseInterpolation = 0;
7a8d521f 143#ifndef HAVE_LIBRETRO
3154bfab 144 spu_config.iTempo = 1;
7a8d521f 145#endif
74265b79
A
146#endif
147#if defined(MIYOO)
148 pl_rearmed_cbs.dithering = 0;
149 pl_rearmed_cbs.gpu_unai.fast_lighting = 1;
150 pl_rearmed_cbs.gpu_unai.scale_hires = 1;
33400707 151#endif
0b1633d7 152 ndrc_g.hacks = 0;
33400707 153
7a8d521f 154 in_type[0] = PSE_PAD_TYPE_STANDARD;
155 in_type[1] = PSE_PAD_TYPE_STANDARD;
33400707 156}
157
f2af1700 158#ifndef NO_FRONTEND
159
8f892648 160void do_emu_action(void)
161{
162 int ret;
163
164 emu_action_old = emu_action;
165
166 switch (emu_action) {
8f892648 167 case SACTION_LOAD_STATE:
168 ret = emu_load_state(state_slot);
169 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
170 break;
171 case SACTION_SAVE_STATE:
172 ret = emu_save_state(state_slot);
173 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
174 break;
38c2028e 175 case SACTION_ENTER_MENU:
456f1b86 176 toggle_fast_forward(1);
38c2028e 177 menu_loop();
178 return;
8f892648 179 case SACTION_NEXT_SSLOT:
180 state_slot++;
181 if (state_slot > 9)
182 state_slot = 0;
183 goto do_state_slot;
184 case SACTION_PREV_SSLOT:
185 state_slot--;
186 if (state_slot < 0)
187 state_slot = 9;
38c2028e 188do_state_slot:
189 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
190 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
191 hud_new_msg = 3;
f29fbd53 192 SysPrintf("* %s\n", hud_msg);
38c2028e 193 break;
8f892648 194 case SACTION_TOGGLE_FSKIP:
ea4a16e7 195 pl_rearmed_cbs.fskip_advice = 0;
196 pl_rearmed_cbs.frameskip++;
197 if (pl_rearmed_cbs.frameskip > 1)
198 pl_rearmed_cbs.frameskip = -1;
199 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
200 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
201 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
202 plugin_call_rearmed_cbs();
8f892648 203 break;
fa56d360 204 case SACTION_SWITCH_DISPMODE:
205 pl_switch_dispmode();
d81b8e97 206 plugin_call_rearmed_cbs();
fa56d360 207 if (GPU_open != NULL && GPU_close != NULL) {
208 GPU_close();
209 GPU_open(&gpuDisp, "PCSX", NULL);
210 }
d81b8e97 211 break;
456f1b86 212 case SACTION_FAST_FORWARD:
213 toggle_fast_forward(0);
214 plugin_call_rearmed_cbs();
215 break;
dde7da71 216 case SACTION_TOGGLE_FPS:
217 if ((g_opts & (OPT_SHOWFPS|OPT_SHOWCPU))
218 == (OPT_SHOWFPS|OPT_SHOWCPU))
219 g_opts &= ~(OPT_SHOWFPS|OPT_SHOWCPU);
220 else if (g_opts & OPT_SHOWFPS)
221 g_opts |= OPT_SHOWCPU;
222 else
223 g_opts |= OPT_SHOWFPS;
224 break;
a8376201 225 case SACTION_TOGGLE_FULLSCREEN:
5b9aa749 226 plat_target.vout_fullscreen = !plat_target.vout_fullscreen;
c9099d02 227 if (GPU_open != NULL && GPU_close != NULL) {
228 GPU_close();
229 GPU_open(&gpuDisp, "PCSX", NULL);
230 }
a8376201 231 break;
29a8c4f3 232 case SACTION_SCREENSHOT:
233 {
38c2028e 234 char buf[MAXPATHLEN];
29a8c4f3 235 void *scrbuf;
236 int w, h, bpp;
237 time_t t = time(NULL);
238 struct tm *tb = localtime(&t);
239 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
240 tb->tm_min * 100 + tb->tm_sec;
241
242 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
243 get_gameid_filename(buf, sizeof(buf),
1ae743b1 244 "%s" SCREENSHOTS_DIR "%.32s-%.9s.%d.png", ti);
245 ret = -2;
29a8c4f3 246 if (scrbuf != 0 && bpp == 16)
247 ret = writepng(buf, scrbuf, w, h);
248 if (ret == 0)
249 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
1ae743b1 250 else
251 SysPrintf("writepng %s: %d\n", buf, ret);
29a8c4f3 252 break;
253 }
221be40d 254 case SACTION_VOLUME_UP:
255 case SACTION_VOLUME_DOWN:
bcfc48e3 256 {
257 static int volume;
258 plat_target_step_volume(&volume,
259 emu_action == SACTION_VOLUME_UP ? 1 : -1);
260 }
221be40d 261 return;
a805c855 262 case SACTION_MINIMIZE:
36dfb787 263 if (GPU_close != NULL)
264 GPU_close();
265
a805c855 266 plat_minimize();
36dfb787 267
268 if (GPU_open != NULL) {
269 ret = GPU_open(&gpuDisp, "PCSX", NULL);
270 if (ret)
f29fbd53 271 SysMessage("GPU_open returned %d", ret);
36dfb787 272 }
a805c855 273 return;
fb640dd9 274 case SACTION_ANALOG_TOGGLE:
275 ret = padToggleAnalog(0);
276 snprintf(hud_msg, sizeof(hud_msg), "ANALOG %s", ret ? "ON" : "OFF");
277 break;
4c08b9e7 278 default:
279 return;
8f892648 280 }
8f892648 281
8f892648 282 hud_new_msg = 3;
283}
284
f2af1700 285#endif
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{
7a8d521f 310 char line[256], buf[256], name[256], *p;
9c27c205 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 }
0aa7361c 403 if (Config.TurboCD)
404 SysPrintf("note: TurboCD is enabled, this breaks games\n");
9c27c205 405
79f216e3 406 if (show_hud_msg) {
f3746eea 407 if (check_unsatisfied_libcrypt())
408 snprintf(hud_msg, sizeof(hud_msg),
409 "LibCrypt protected game with missing SBI detected");
410 else
411 snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG);
79f216e3 412 hud_new_msg = 3;
413 }
9c27c205 414}
415
1f1128d0 416static void log_wrong_cpu(void)
417{
418#if DO_CPU_CHECKS
419 __builtin_cpu_init();
420 #define CHECK_CPU(name) if (!__builtin_cpu_supports(name)) \
421 SysPrintf("ERROR: compiled for " name ", which is unsupported by the CPU\n")
422#ifdef __SSE2__
423 CHECK_CPU("sse2");
424#endif
425#ifdef __SSSE3__
426 CHECK_CPU("ssse3");
427#endif
428#ifdef __SSE4_1__
429 CHECK_CPU("sse4.1");
430#endif
431#endif // DO_CPU_CHECKS
432}
433
38c2028e 434int emu_core_preinit(void)
80c2304e 435{
80c2304e 436 // what is the name of the config file?
437 // it may be redefined by -cfg on the command line
438 strcpy(cfgfile_basename, "pcsx.cfg");
439
ea12009c 440#ifdef IOS
441 emuLog = fopen("/User/Documents/pcsxr.log", "w");
442 if (emuLog == NULL)
443 emuLog = fopen("pcsxr.log", "w");
444 if (emuLog == NULL)
445#endif
e906c010 446 emuLog = stdout;
ea12009c 447
1f1128d0 448 log_wrong_cpu();
449
80c2304e 450 SetIsoFile(NULL);
80c2304e 451
e0c692d9 452 memset(&Config, 0, sizeof(Config));
453
e0c692d9 454 set_default_paths();
33400707 455 emu_set_default_config();
e0c692d9 456 strcpy(Config.Bios, "HLE");
457
38c2028e 458 return 0;
459}
460
461int emu_core_init(void)
462{
f906311d 463 SysPrintf("Starting PCSX-ReARMed " REV " (%s)\n", get_build_info());
8a1f23ef 464 SysPrintf("build time: " __DATE__ " " __TIME__ "\n");
45b97ede 465
30431e75 466#if defined(__arm__) && defined(__ARM_FP)
f6ede72d 467 // RunFast mode
468 u32 fpscr = ~0;
30431e75 469 __asm__ volatile("vmrs %0, fpscr" : "=r"(fpscr));
f6ede72d 470 SysPrintf("old fpscr = %08x\n", fpscr);
471 fpscr &= ~0x00009f9f;
472 fpscr |= 0x03000000; // DN | FZ
30431e75 473 __asm__ volatile("vmsr fpscr, %0" :: "r"(fpscr));
f6ede72d 474#endif
475
a6a0a4d3 476#ifdef HAVE_RTHREADS
d135f6d2 477 pcsxr_sthread_init();
a6a0a4d3 478#endif
e6495add 479#ifndef NO_FRONTEND
480 check_profile();
38c2028e 481 check_memcards();
e6495add 482#endif
38c2028e 483
484 if (EmuInit() == -1) {
f29fbd53 485 SysPrintf("PSX emulator couldn't be initialized.\n");
38c2028e 486 return -1;
487 }
488
489 LoadMcds(Config.Mcd1, Config.Mcd2);
490
491 if (Config.Debug) {
492 StartDebugger();
493 }
494
495 return 0;
496}
497
69e482e3 498void emu_core_ask_exit(void)
499{
c87406ff 500 psxRegs.stop++;
69e482e3 501 g_emu_want_quit = 1;
502}
503
38c2028e 504#ifndef NO_FRONTEND
003cfc63 505
506#include <sys/stat.h>
507#include <sys/types.h>
508
1ae743b1 509static const char *get_home_dir(void)
510{
372a1be9 511#if defined(PANDORA) || !defined(__unix__) || defined(MIYOO)
1ae743b1 512 return ".";
513#else
514 static const char *home = NULL;
515 struct stat st;
516 if (home)
517 return home;
518 // for compatibility with older versions, look for .pcsx in the working dir
519 if (stat(PCSX_DOT_DIR + 1, &st) != 0)
520 home = getenv("HOME");
521 if (home == NULL)
522 home = ".";
523 return home;
524#endif
525}
526
527void emu_make_path(char *buf, size_t size, const char *dir, const char *fname)
528{
529 const char *home = get_home_dir();
530 if (fname)
531 snprintf(buf, size, "%s%s%s", home, dir, fname);
532 else
533 snprintf(buf, size, "%s%s", home, dir);
534}
535
536void emu_make_data_path(char *buff, const char *end, int size)
537{
538 int pos, end_len;
539
540 end_len = strlen(end);
541 pos = plat_get_root_dir(buff, size);
542 strncpy(buff + pos, end, size - pos);
543 buff[size - 1] = 0;
544 if (pos + end_len > size - 1)
545 printf("Warning: path truncated: %s\n", buff);
546}
547
e6495add 548static void create_profile_dir(const char *directory) {
549 char path[MAXPATHLEN];
550
551 MAKE_PATH(path, directory, NULL);
552 mkdir(path, S_IRWXU | S_IRWXG);
553}
554
555static void check_profile(void) {
556 // make sure that ~/.pcsx exists
557 create_profile_dir(PCSX_DOT_DIR);
558
559 create_profile_dir(BIOS_DIR);
560 create_profile_dir(MEMCARD_DIR);
561 create_profile_dir(STATES_DIR);
e6495add 562 create_profile_dir(CHEATS_DIR);
563 create_profile_dir(PATCHES_DIR);
1ae743b1 564 create_profile_dir(CFG_DIR);
565 create_profile_dir(SCREENSHOTS_DIR);
e6495add 566}
567
568static void check_memcards(void)
569{
570 char buf[MAXPATHLEN];
571 FILE *f;
572 int i;
573
574 for (i = 1; i <= 9; i++) {
1ae743b1 575 snprintf(buf, sizeof(buf), "%s%scard%d.mcd",
576 get_home_dir(), MEMCARD_DIR, i);
e6495add 577
578 f = fopen(buf, "rb");
579 if (f == NULL) {
580 SysPrintf("Creating memcard: %s\n", buf);
581 CreateMcd(buf);
582 }
583 else
584 fclose(f);
585 }
586}
587
38c2028e 588int main(int argc, char *argv[])
589{
e0c692d9 590 char file[MAXPATHLEN] = "";
591 char path[MAXPATHLEN];
2ca1e56e 592 char isofilename[MAXPATHLEN];
e0c692d9 593 const char *cdfile = NULL;
de910c6b 594 const char *loadst_f = NULL;
0a151868 595 int psxout = 0;
e0c692d9 596 int loadst = 0;
597 int i;
598
38c2028e 599 emu_core_preinit();
600
80c2304e 601 // read command line options
602 for (i = 1; i < argc; i++) {
0a151868 603 if (!strcmp(argv[i], "-psxout")) psxout = 1;
80c2304e 604 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
605 else if (!strcmp(argv[i], "-cfg")) {
606 if (i+1 >= argc) break;
607 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
f29fbd53 608 SysPrintf("Using config file %s.\n", cfgfile_basename);
80c2304e 609 }
610 else if (!strcmp(argv[i], "-cdfile")) {
80c2304e 611 if (i+1 >= argc) break;
612 strncpy(isofilename, argv[++i], MAXPATHLEN);
613 if (isofilename[0] != '/') {
614 getcwd(path, MAXPATHLEN);
615 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
616 strcat(path, "/");
617 strcat(path, isofilename);
618 strcpy(isofilename, path);
619 } else
620 isofilename[0] = 0;
621 }
622
47bf65ab 623 cdfile = isofilename;
80c2304e 624 }
de910c6b 625 else if (!strcmp(argv[i], "-loadf")) {
626 if (i+1 >= argc) break;
627 loadst_f = argv[++i];
628 }
80c2304e 629 else if (!strcmp(argv[i], "-h") ||
630 !strcmp(argv[i], "-help") ||
631 !strcmp(argv[i], "--help")) {
38c2028e 632 printf("PCSX-ReARMed " REV "\n");
80c2304e 633 printf("%s\n", _(
634 " pcsx [options] [file]\n"
635 "\toptions:\n"
636 "\t-cdfile FILE\tRuns a CD image file\n"
80c2304e 637 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
638 "\t-psxout\t\tEnable PSX output\n"
df21c2fa 639 "\t-load STATENUM\tLoads savestate STATENUM (1-9)\n"
640 "\t-loadf FILE\tLoads savestate from FILE\n"
80c2304e 641 "\t-h -help\tDisplay this message\n"
38c2028e 642 "\tfile\t\tLoads a PSX EXE file\n"));
80c2304e 643 return 0;
644 } else {
645 strncpy(file, argv[i], MAXPATHLEN);
646 if (file[0] != '/') {
647 getcwd(path, MAXPATHLEN);
648 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
649 strcat(path, "/");
650 strcat(path, file);
651 strcpy(file, path);
652 } else
653 file[0] = 0;
654 }
655 }
656 }
657
47bf65ab 658 if (cdfile)
659 set_cd_image(cdfile);
660
69af03a2 661 // frontend stuff
b07c18e8 662 // init input but leave probing to platform code,
663 // they add input drivers and may need to modify them after probe
69af03a2 664 in_init();
59f1c85c 665 pl_init();
69af03a2 666 plat_init();
0a151868 667 menu_init(); // loads config
668
45b97ede 669 if (emu_core_init() != 0)
670 return 1;
38c2028e 671
0a151868 672 if (psxout)
673 Config.PsxOut = 1;
80c2304e 674
69af03a2 675 if (LoadPlugins() == -1) {
6fe1f056 676 // FIXME: this recovery doesn't work, just delete bad config and bail out
677 // SysMessage("could not load plugins, retrying with defaults\n");
678 set_default_paths();
1ae743b1 679 snprintf(path, sizeof(path), "%s" PCSX_DOT_DIR "%s",
680 get_home_dir(), cfgfile_basename);
6fe1f056 681 remove(path);
69af03a2 682 SysMessage("Failed loading plugins!");
683 return 1;
684 }
685 pcnt_hook_plugins();
80c2304e 686
69af03a2 687 if (OpenPlugins() == -1) {
688 return 1;
689 }
80c2304e 690
69af03a2 691 CheckCdrom();
168e884c 692 plugin_call_rearmed_cbs();
bbd837c6 693 SysReset();
69af03a2 694
695 if (file[0] != '\0') {
696 if (Load(file) != -1)
697 ready_to_go = 1;
698 } else {
df21c2fa 699 if (cdfile)
700 ready_to_go = menu_load_cd_image(cdfile) == 0;
69af03a2 701 }
80c2304e 702
ea66e884 703 if (loadst_f) {
704 int ret = LoadState(loadst_f);
f29fbd53 705 SysPrintf("%s state file: %s\n",
706 ret ? "failed to load" : "loaded", loadst_f);
ea66e884 707 ready_to_go |= ret == 0;
708 }
709
45d45c1e 710 if (ready_to_go) {
32631e6a 711 menu_prepare_emu();
45d45c1e 712
df21c2fa 713 // If a state slot has been specified, then load that
714 if (cdfile && loadst) {
45d45c1e 715 int ret = emu_load_state(loadst - 1);
df21c2fa 716 SysPrintf("%s state slot %d\n",
f29fbd53 717 ret ? "failed to load" : "loaded", loadst);
45d45c1e 718 }
719 }
3c70c47b 720 else
69af03a2 721 menu_loop();
722
97fa754e 723#ifndef LIGHTREC_DEBUG
7c0f51de 724 pl_start_watchdog();
97fa754e 725#endif
7c0f51de 726
69e482e3 727 while (!g_emu_want_quit)
69af03a2 728 {
c87406ff 729 psxRegs.stop = 0;
8f892648 730 emu_action = SACTION_NONE;
731
c87406ff 732 psxCpu->Execute(&psxRegs);
8f892648 733 if (emu_action != SACTION_NONE)
734 do_emu_action();
80c2304e 735 }
736
69e482e3 737 printf("Exit..\n");
738 ClosePlugins();
739 SysClose();
740 menu_finish();
741 plat_finish();
742
80c2304e 743 return 0;
744}
456f1b86 745
746static void toggle_fast_forward(int force_off)
747{
748 static int fast_forward;
749 static int normal_g_opts;
456f1b86 750 static int normal_enhancement_enable;
a8305381 751 //static int normal_frameskip;
456f1b86 752
753 if (force_off && !fast_forward)
754 return;
755
756 fast_forward = !fast_forward;
757 if (fast_forward) {
758 normal_g_opts = g_opts;
a8305381 759 //normal_frameskip = pl_rearmed_cbs.frameskip;
456f1b86 760 normal_enhancement_enable =
761 pl_rearmed_cbs.gpu_neon.enhancement_enable;
762
763 g_opts |= OPT_NO_FRAMELIM;
a8305381 764 // pl_rearmed_cbs.frameskip = 3; // too broken
456f1b86 765 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
766 } else {
767 g_opts = normal_g_opts;
a8305381 768 //pl_rearmed_cbs.frameskip = normal_frameskip;
456f1b86 769 pl_rearmed_cbs.gpu_neon.enhancement_enable =
770 normal_enhancement_enable;
771
772 pl_timing_prepare(Config.PsxType);
773 }
774
775 if (!force_off)
776 snprintf(hud_msg, sizeof(hud_msg), "FAST FORWARD %s",
777 fast_forward ? "ON" : "OFF");
778}
ee1a37fd 779
780static void SignalExit(int sig) {
1ae743b1 781 SysPrintf("got signal %d\n", sig);
ee1a37fd 782 // only to restore framebuffer/resolution on some devices
783 plat_finish();
cfa5a2af 784 _exit(1);
ee1a37fd 785}
33400707 786
1ae743b1 787static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
788 char trimlabel[33];
789 int j;
80c2304e 790
1ae743b1 791 strncpy(trimlabel, CdromLabel, 32);
792 trimlabel[32] = 0;
793 for (j = 31; j >= 0; j--)
794 if (trimlabel[j] == ' ')
795 trimlabel[j] = 0;
796 else
797 continue;
80c2304e 798
1ae743b1 799 snprintf(buf, size, fmt, get_home_dir(), trimlabel, CdromId, i);
80c2304e 800
1ae743b1 801 return 0;
80c2304e 802}
803
c5061935 804int get_state_filename(char *buf, int size, int i) {
29a8c4f3 805 return get_gameid_filename(buf, size,
1ae743b1 806 "%s" STATES_DIR "%.32s-%.9s.%3.3d", i);
80c2304e 807}
808
8f892648 809int emu_check_state(int slot)
810{
811 char fname[MAXPATHLEN];
812 int ret;
813
814 ret = get_state_filename(fname, sizeof(fname), slot);
815 if (ret != 0)
816 return ret;
817
818 return CheckState(fname);
819}
820
821int emu_save_state(int slot)
822{
823 char fname[MAXPATHLEN];
824 int ret;
825
826 ret = get_state_filename(fname, sizeof(fname), slot);
827 if (ret != 0)
828 return ret;
829
90f1d26c 830 ret = SaveState(fname);
7a8d521f 831#if defined(HAVE_PRE_ARMV7) && !defined(_3DS) && !defined(__SWITCH__) /* XXX GPH hack */
0b1dbe6b 832 sync();
833#endif
f29fbd53 834 SysPrintf("* %s \"%s\" [%d]\n",
835 ret == 0 ? "saved" : "failed to save", fname, slot);
90f1d26c 836 return ret;
8f892648 837}
838
839int emu_load_state(int slot)
840{
841 char fname[MAXPATHLEN];
842 int ret;
843
dde7da71 844 hud_msg[0] = 0;
845
8f892648 846 ret = get_state_filename(fname, sizeof(fname), slot);
847 if (ret != 0)
848 return ret;
849
850 return LoadState(fname);
851}
852
1ae743b1 853#endif // NO_FRONTEND
854
855static void CALLBACK dummy_lace(void)
856{
857}
858
859void SysReset() {
860 // rearmed hack: EmuReset() runs some code when real BIOS is used,
861 // but we usually do reset from menu while GPU is not open yet,
862 // so we need to prevent updateLace() call..
863 void *real_lace = GPU_updateLace;
864 GPU_updateLace = dummy_lace;
865 g_emu_resetting = 1;
866
867 // reset can run code, timing must be set
868 pl_timing_prepare(Config.PsxType);
869
870 EmuReset();
871
872 GPU_updateLace = real_lace;
873 g_emu_resetting = 0;
874}
875
876void SysClose() {
877 EmuShutdown();
878 ReleasePlugins();
879
880 StopDebugger();
881
882 if (emuLog != NULL && emuLog != stdout && emuLog != stderr) {
883 fclose(emuLog);
884 emuLog = NULL;
885 }
886}
887
7a8d521f 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
7a8d521f 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
08d9d257 945 ret = cdra_open();
49a2ddb8 946 if (UsingIso() && ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
c5061935 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; }
c5061935 954
c5061935 955 return 0;
956}
957
958int OpenPlugins() {
959 int ret;
960
961 while ((ret = _OpenPlugins()) == -2) {
962 ReleasePlugins();
963 LoadMcds(Config.Mcd1, Config.Mcd2);
964 if (LoadPlugins() == -1) return -1;
965 }
966 return ret;
967}
968
969void ClosePlugins() {
970 int ret;
971
ee1a37fd 972#ifndef NO_FRONTEND
c5061935 973 signal(SIGINT, SIG_DFL);
974 signal(SIGPIPE, SIG_DFL);
ee1a37fd 975#endif
976
08d9d257 977 cdra_close();
c5061935 978 ret = SPU_close();
08d9d257 979 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); }
c5061935 980 // pcsx-rearmed: we handle gpu elsewhere
981 //ret = GPU_close();
982 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
c5061935 983}
984
e906c010 985/* we hook statically linked plugins here */
986static const char *builtin_plugins[] = {
aa4cdd73 987 "builtin_gpu", "builtin_spu"
e906c010 988};
989
990static const int builtin_plugin_ids[] = {
aa4cdd73 991 PLUGIN_GPU, PLUGIN_SPU
e906c010 992};
993
80c2304e 994void *SysLoadLibrary(const char *lib) {
e906c010 995 const char *tmp = strrchr(lib, '/');
003cfc63 996 void *ret = NULL;
e906c010 997 int i;
998
f29fbd53 999 SysPrintf("plugin: %s\n", lib);
bbd837c6 1000
e906c010 1001 if (tmp != NULL) {
1002 tmp++;
1003 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
1004 if (strcmp(tmp, builtin_plugins[i]) == 0)
77e1e479 1005 return (void *)(uintptr_t)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
e906c010 1006 }
1007
7a8d521f 1008#if !defined(_WIN32) && !defined(NO_DYLIB)
bbd837c6 1009 ret = dlopen(lib, RTLD_NOW);
1010 if (ret == NULL)
f29fbd53 1011 SysMessage("dlopen: %s", dlerror());
003cfc63 1012#else
1013 /* no external plugin support, abi is no longer
1014 * compatible with psemu/pcsx anyway */
1015#endif
bbd837c6 1016 return ret;
80c2304e 1017}
1018
1019void *SysLoadSym(void *lib, const char *sym) {
77e1e479 1020 unsigned int plugid = (unsigned int)(uintptr_t)lib;
e906c010 1021
1022 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
1023 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
1024
7a8d521f 1025#if !defined(_WIN32) && !defined(NO_DYLIB)
80c2304e 1026 return dlsym(lib, sym);
003cfc63 1027#else
1028 return NULL;
1029#endif
80c2304e 1030}
1031
1032const char *SysLibError() {
7a8d521f 1033#if defined(NO_DYLIB)
1034 return NULL;
1035#elif !defined(_WIN32)
80c2304e 1036 return dlerror();
003cfc63 1037#else
1038 return "not supported";
1039#endif
80c2304e 1040}
1041
1042void SysCloseLibrary(void *lib) {
77e1e479 1043 unsigned int plugid = (unsigned int)(uintptr_t)lib;
e906c010 1044
1045 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
1046 return;
1047
7a8d521f 1048#if !defined(_WIN32) && !defined(NO_DYLIB)
80c2304e 1049 dlclose(lib);
003cfc63 1050#endif
80c2304e 1051}