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