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