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