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