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