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