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