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