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