f013066e |
1 | // (c) Copyright 2006-2007 notaz, All rights reserved.\r |
2 | // Free for non-commercial use.\r |
3 | \r |
4 | // For commercial use, separate licencing terms must be obtained.\r |
5 | \r |
6 | #include <stdio.h>\r |
7 | #include <stdlib.h>\r |
b5bfb864 |
8 | #include <stdarg.h>\r |
f013066e |
9 | #ifndef NO_SYNC\r |
10 | #include <unistd.h>\r |
11 | #endif\r |
12 | \r |
13 | #include "emu.h"\r |
14 | #include "menu.h"\r |
15 | #include "fonts.h"\r |
16 | #include "lprintf.h"\r |
0ae25549 |
17 | #include "config.h"\r |
049a6b3e |
18 | #include "plat.h"\r |
388947f3 |
19 | #include "input.h"\r |
93c18cb4 |
20 | #include "posix.h"\r |
f013066e |
21 | \r |
f11bad75 |
22 | #include <pico/pico_int.h>\r |
23 | #include <pico/patch.h>\r |
24 | #include <pico/cd/cue.h>\r |
f013066e |
25 | #include <zlib/zlib.h>\r |
26 | \r |
f013066e |
27 | \r |
e31266dd |
28 | void *g_screen_ptr;\r |
29 | \r |
30 | #if !SCREEN_SIZE_FIXED\r |
31 | int g_screen_width = SCREEN_WIDTH;\r |
32 | int g_screen_height = SCREEN_HEIGHT;\r |
33 | #endif\r |
34 | \r |
3e85ebdd |
35 | char *PicoConfigFile = "config.cfg";\r |
0ae25549 |
36 | currentConfig_t currentConfig, defaultConfig;\r |
f013066e |
37 | int state_slot = 0;\r |
38 | int config_slot = 0, config_slot_current = 0;\r |
93c18cb4 |
39 | int pico_pen_x = 320/2, pico_pen_y = 240/2;\r |
8a091e48 |
40 | int pico_inp_mode = 0;\r |
049a6b3e |
41 | int engineState = PGS_Menu;\r |
42 | \r |
43 | /* TODO: len checking */\r |
44 | char rom_fname_reload[512] = { 0, };\r |
45 | char rom_fname_loaded[512] = { 0, };\r |
46 | int rom_loaded = 0;\r |
93c18cb4 |
47 | int reset_timing = 0;\r |
b5bfb864 |
48 | static unsigned int notice_msg_time; /* when started showing */\r |
49 | static char noticeMsg[40];\r |
f013066e |
50 | \r |
51 | unsigned char *movie_data = NULL;\r |
52 | static int movie_size = 0;\r |
53 | \r |
f013066e |
54 | \r |
fa8d1331 |
55 | /* don't use tolower() for easy old glibc binary compatibility */\r |
61f66fe0 |
56 | static void strlwr_(char *string)\r |
f013066e |
57 | {\r |
61f66fe0 |
58 | char *p;\r |
59 | for (p = string; *p; p++)\r |
fa8d1331 |
60 | if ('A' <= *p && *p <= 'Z')\r |
61 | *p += 'a' - 'A';\r |
f013066e |
62 | }\r |
63 | \r |
36f6fd5a |
64 | static int try_rfn_cut(char *fname)\r |
f013066e |
65 | {\r |
66 | FILE *tmp;\r |
67 | char *p;\r |
68 | \r |
36f6fd5a |
69 | p = fname + strlen(fname) - 1;\r |
70 | for (; p > fname; p--)\r |
f013066e |
71 | if (*p == '.') break;\r |
72 | *p = 0;\r |
73 | \r |
36f6fd5a |
74 | if((tmp = fopen(fname, "rb"))) {\r |
f013066e |
75 | fclose(tmp);\r |
76 | return 1;\r |
77 | }\r |
78 | return 0;\r |
79 | }\r |
80 | \r |
81 | static void get_ext(char *file, char *ext)\r |
82 | {\r |
83 | char *p;\r |
84 | \r |
85 | p = file + strlen(file) - 4;\r |
86 | if (p < file) p = file;\r |
87 | strncpy(ext, p, 4);\r |
88 | ext[4] = 0;\r |
89 | strlwr_(ext);\r |
90 | }\r |
91 | \r |
b5bfb864 |
92 | void emu_status_msg(const char *format, ...)\r |
93 | {\r |
94 | va_list vl;\r |
95 | \r |
96 | va_start(vl, format);\r |
97 | vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);\r |
98 | va_end(vl);\r |
99 | \r |
100 | notice_msg_time = plat_get_ticks_ms();\r |
101 | }\r |
102 | \r |
ee0f881e |
103 | static const char *biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" };\r |
104 | static const char *biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" };\r |
105 | static const char *biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r |
f013066e |
106 | \r |
ee0f881e |
107 | static int find_bios(int region, char **bios_file)\r |
f013066e |
108 | {\r |
109 | static char bios_path[1024];\r |
110 | int i, count;\r |
ee0f881e |
111 | const char **files;\r |
f013066e |
112 | FILE *f = NULL;\r |
113 | \r |
114 | if (region == 4) { // US\r |
115 | files = biosfiles_us;\r |
116 | count = sizeof(biosfiles_us) / sizeof(char *);\r |
117 | } else if (region == 8) { // EU\r |
118 | files = biosfiles_eu;\r |
119 | count = sizeof(biosfiles_eu) / sizeof(char *);\r |
120 | } else if (region == 1 || region == 2) {\r |
121 | files = biosfiles_jp;\r |
122 | count = sizeof(biosfiles_jp) / sizeof(char *);\r |
123 | } else {\r |
124 | return 0;\r |
125 | }\r |
126 | \r |
127 | for (i = 0; i < count; i++)\r |
128 | {\r |
45deb409 |
129 | emu_make_path(bios_path, files[i], sizeof(bios_path) - 4);\r |
f013066e |
130 | strcat(bios_path, ".bin");\r |
131 | f = fopen(bios_path, "rb");\r |
132 | if (f) break;\r |
133 | \r |
134 | bios_path[strlen(bios_path) - 4] = 0;\r |
135 | strcat(bios_path, ".zip");\r |
136 | f = fopen(bios_path, "rb");\r |
137 | if (f) break;\r |
138 | }\r |
139 | \r |
140 | if (f) {\r |
141 | lprintf("using bios: %s\n", bios_path);\r |
142 | fclose(f);\r |
143 | if (bios_file) *bios_file = bios_path;\r |
144 | return 1;\r |
145 | } else {\r |
3ffee69c |
146 | sprintf(bios_path, "no %s BIOS files found, read docs",\r |
f013066e |
147 | region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r |
3ffee69c |
148 | me_update_msg(bios_path);\r |
f013066e |
149 | return 0;\r |
150 | }\r |
151 | }\r |
152 | \r |
c6334564 |
153 | /* check if the name begins with BIOS name */\r |
367b6f1f |
154 | /*\r |
c6334564 |
155 | static int emu_isBios(const char *name)\r |
156 | {\r |
157 | int i;\r |
158 | for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)\r |
159 | if (strstr(name, biosfiles_us[i]) != NULL) return 1;\r |
160 | for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)\r |
161 | if (strstr(name, biosfiles_eu[i]) != NULL) return 1;\r |
162 | for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)\r |
163 | if (strstr(name, biosfiles_jp[i]) != NULL) return 1;\r |
164 | return 0;\r |
165 | }\r |
367b6f1f |
166 | */\r |
c6334564 |
167 | \r |
367b6f1f |
168 | static unsigned char id_header[0x100];\r |
0ae25549 |
169 | \r |
36f6fd5a |
170 | /* checks if fname points to valid MegaCD image\r |
f013066e |
171 | * if so, checks for suitable BIOS */\r |
ee0f881e |
172 | int emu_cd_check(int *pregion, char *fname_in)\r |
f013066e |
173 | {\r |
174 | unsigned char buf[32];\r |
175 | pm_file *cd_f;\r |
9fe01b96 |
176 | int region = 4; // 1: Japan, 4: US, 8: Europe\r |
36f6fd5a |
177 | char ext[5], *fname = fname_in;\r |
9fe01b96 |
178 | cue_track_type type = CT_UNKNOWN;\r |
179 | cue_data_t *cue_data = NULL;\r |
1ceda417 |
180 | \r |
36f6fd5a |
181 | get_ext(fname_in, ext);\r |
9fe01b96 |
182 | if (strcasecmp(ext, ".cue") == 0) {\r |
36f6fd5a |
183 | cue_data = cue_parse(fname_in);\r |
9fe01b96 |
184 | if (cue_data != NULL) {\r |
185 | fname = cue_data->tracks[1].fname;\r |
186 | type = cue_data->tracks[1].type;\r |
187 | }\r |
e9a13365 |
188 | else\r |
189 | return -1;\r |
9fe01b96 |
190 | }\r |
191 | \r |
192 | cd_f = pm_open(fname);\r |
193 | if (cue_data != NULL)\r |
194 | cue_destroy(cue_data);\r |
f013066e |
195 | \r |
e9a13365 |
196 | if (cd_f == NULL) return 0; // let the upper level handle this\r |
f013066e |
197 | \r |
198 | if (pm_read(buf, 32, cd_f) != 32) {\r |
199 | pm_close(cd_f);\r |
e9a13365 |
200 | return -1;\r |
f013066e |
201 | }\r |
202 | \r |
9fe01b96 |
203 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) {\r |
204 | if (type && type != CT_ISO)\r |
205 | elprintf(EL_STATUS, ".cue has wrong type: %i", type);\r |
206 | type = CT_ISO; // Sega CD (ISO)\r |
207 | }\r |
208 | if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) {\r |
209 | if (type && type != CT_BIN)\r |
210 | elprintf(EL_STATUS, ".cue has wrong type: %i", type);\r |
211 | type = CT_BIN; // Sega CD (BIN)\r |
212 | }\r |
213 | \r |
214 | if (type == CT_UNKNOWN) {\r |
f013066e |
215 | pm_close(cd_f);\r |
216 | return 0;\r |
217 | }\r |
218 | \r |
9fe01b96 |
219 | pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);\r |
367b6f1f |
220 | pm_read(id_header, sizeof(id_header), cd_f);\r |
0ae25549 |
221 | \r |
f013066e |
222 | /* it seems we have a CD image here. Try to detect region now.. */\r |
9fe01b96 |
223 | pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);\r |
f013066e |
224 | pm_read(buf, 1, cd_f);\r |
225 | pm_close(cd_f);\r |
226 | \r |
227 | if (buf[0] == 0x64) region = 8; // EU\r |
228 | if (buf[0] == 0xa1) region = 1; // JAP\r |
229 | \r |
230 | lprintf("detected %s Sega/Mega CD image with %s region\n",\r |
9fe01b96 |
231 | type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r |
f013066e |
232 | \r |
233 | if (pregion != NULL) *pregion = region;\r |
234 | \r |
235 | return type;\r |
236 | }\r |
237 | \r |
ff63afa1 |
238 | static int extract_text(char *dest, const unsigned char *src, int len, int swab)\r |
0ae25549 |
239 | {\r |
240 | char *p = dest;\r |
241 | int i;\r |
242 | \r |
243 | if (swab) swab = 1;\r |
244 | \r |
245 | for (i = len - 1; i >= 0; i--)\r |
246 | {\r |
247 | if (src[i^swab] != ' ') break;\r |
248 | }\r |
249 | len = i + 1;\r |
250 | \r |
251 | for (i = 0; i < len; i++)\r |
252 | {\r |
253 | unsigned char s = src[i^swab];\r |
254 | if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&\r |
255 | s != '[' && s != ']' && s != '\\')\r |
256 | {\r |
257 | *p++ = s;\r |
258 | }\r |
259 | else\r |
260 | {\r |
261 | sprintf(p, "\\%02x", s);\r |
262 | p += 3;\r |
263 | }\r |
264 | }\r |
265 | \r |
266 | return p - dest;\r |
267 | }\r |
268 | \r |
ee0f881e |
269 | static char *emu_make_rom_id(void)\r |
0ae25549 |
270 | {\r |
36f6fd5a |
271 | static char id_string[3+0xe*3+0x3*3+0x30*3+3];\r |
8e708f92 |
272 | int pos, swab = 1;\r |
0ae25549 |
273 | \r |
dd5fd477 |
274 | if (PicoAHW & PAHW_MCD) {\r |
8e708f92 |
275 | strcpy(id_string, "CD|");\r |
276 | swab = 0;\r |
277 | }\r |
367b6f1f |
278 | else strcpy(id_string, "MD|");\r |
0ae25549 |
279 | pos = 3;\r |
280 | \r |
8e708f92 |
281 | pos += extract_text(id_string + pos, id_header + 0x80, 0x0e, swab); // serial\r |
0ae25549 |
282 | id_string[pos] = '|'; pos++;\r |
8e708f92 |
283 | pos += extract_text(id_string + pos, id_header + 0xf0, 0x03, swab); // region\r |
0ae25549 |
284 | id_string[pos] = '|'; pos++;\r |
8e708f92 |
285 | pos += extract_text(id_string + pos, id_header + 0x50, 0x30, swab); // overseas name\r |
0ae25549 |
286 | id_string[pos] = 0;\r |
287 | \r |
0ae25549 |
288 | return id_string;\r |
289 | }\r |
290 | \r |
36f6fd5a |
291 | // buffer must be at least 150 byte long\r |
ee0f881e |
292 | void emu_get_game_name(char *str150)\r |
36f6fd5a |
293 | {\r |
294 | int ret, swab = (PicoAHW & PAHW_MCD) ? 0 : 1;\r |
295 | char *s, *d;\r |
296 | \r |
297 | ret = extract_text(str150, id_header + 0x50, 0x30, swab); // overseas name\r |
298 | \r |
299 | for (s = d = str150 + 1; s < str150+ret; s++)\r |
300 | {\r |
301 | if (*s == 0) break;\r |
302 | if (*s != ' ' || d[-1] != ' ')\r |
303 | *d++ = *s;\r |
304 | }\r |
305 | *d = 0;\r |
306 | }\r |
307 | \r |
ee0f881e |
308 | static void shutdown_MCD(void)\r |
309 | {\r |
310 | if ((PicoAHW & PAHW_MCD) && Pico_mcd != NULL)\r |
311 | Stop_CD();\r |
312 | PicoAHW &= ~PAHW_MCD;\r |
313 | }\r |
314 | \r |
36f6fd5a |
315 | // note: this function might mangle rom_fname\r |
ee0f881e |
316 | int emu_reload_rom(char *rom_fname)\r |
f013066e |
317 | {\r |
318 | unsigned int rom_size = 0;\r |
36f6fd5a |
319 | char *used_rom_name = rom_fname;\r |
144a28a0 |
320 | unsigned char *rom_data = NULL;\r |
f013066e |
321 | char ext[5];\r |
e9a13365 |
322 | pm_file *rom = NULL;\r |
f013066e |
323 | int ret, cd_state, cd_region, cfg_loaded = 0;\r |
324 | \r |
36f6fd5a |
325 | lprintf("emu_ReloadRom(%s)\n", rom_fname);\r |
f013066e |
326 | \r |
36f6fd5a |
327 | get_ext(rom_fname, ext);\r |
f013066e |
328 | \r |
329 | // detect wrong extensions\r |
144a28a0 |
330 | if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz\r |
3ffee69c |
331 | me_update_msg("Not a ROM/CD selected.");\r |
f013066e |
332 | return 0;\r |
333 | }\r |
334 | \r |
335 | PicoPatchUnload();\r |
336 | \r |
337 | // check for movie file\r |
144a28a0 |
338 | if (movie_data) {\r |
f013066e |
339 | free(movie_data);\r |
340 | movie_data = 0;\r |
341 | }\r |
88bfc63d |
342 | if (!strcmp(ext, ".gmv"))\r |
343 | {\r |
f013066e |
344 | // check for both gmv and rom\r |
345 | int dummy;\r |
36f6fd5a |
346 | FILE *movie_file = fopen(rom_fname, "rb");\r |
f013066e |
347 | if(!movie_file) {\r |
3ffee69c |
348 | me_update_msg("Failed to open movie.");\r |
f013066e |
349 | return 0;\r |
350 | }\r |
351 | fseek(movie_file, 0, SEEK_END);\r |
352 | movie_size = ftell(movie_file);\r |
353 | fseek(movie_file, 0, SEEK_SET);\r |
354 | if(movie_size < 64+3) {\r |
3ffee69c |
355 | me_update_msg("Invalid GMV file.");\r |
f013066e |
356 | fclose(movie_file);\r |
357 | return 0;\r |
358 | }\r |
359 | movie_data = malloc(movie_size);\r |
360 | if(movie_data == NULL) {\r |
3ffee69c |
361 | me_update_msg("low memory.");\r |
f013066e |
362 | fclose(movie_file);\r |
363 | return 0;\r |
364 | }\r |
365 | fread(movie_data, 1, movie_size, movie_file);\r |
366 | fclose(movie_file);\r |
367 | if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r |
3ffee69c |
368 | me_update_msg("Invalid GMV file.");\r |
f013066e |
369 | return 0;\r |
370 | }\r |
36f6fd5a |
371 | dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r |
f013066e |
372 | if (!dummy) {\r |
3ffee69c |
373 | me_update_msg("Could't find a ROM for movie.");\r |
f013066e |
374 | return 0;\r |
375 | }\r |
36f6fd5a |
376 | get_ext(rom_fname, ext);\r |
61f66fe0 |
377 | lprintf("gmv loaded for %s\n", rom_fname);\r |
f013066e |
378 | }\r |
36f6fd5a |
379 | else if (!strcmp(ext, ".pat"))\r |
380 | {\r |
f013066e |
381 | int dummy;\r |
36f6fd5a |
382 | PicoPatchLoad(rom_fname);\r |
383 | dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r |
f013066e |
384 | if (!dummy) {\r |
3ffee69c |
385 | me_update_msg("Could't find a ROM to patch.");\r |
f013066e |
386 | return 0;\r |
387 | }\r |
36f6fd5a |
388 | get_ext(rom_fname, ext);\r |
f013066e |
389 | }\r |
390 | \r |
ee0f881e |
391 | shutdown_MCD();\r |
f013066e |
392 | \r |
393 | // check for MegaCD image\r |
ee0f881e |
394 | cd_state = emu_cd_check(&cd_region, rom_fname);\r |
e9a13365 |
395 | if (cd_state >= 0 && cd_state != CIT_NOT_CD)\r |
f013066e |
396 | {\r |
dd5fd477 |
397 | PicoAHW |= PAHW_MCD;\r |
f013066e |
398 | // valid CD image, check for BIOS..\r |
399 | \r |
400 | // we need to have config loaded at this point\r |
ee0f881e |
401 | ret = emu_read_config(1, 1);\r |
402 | if (!ret) emu_read_config(0, 1);\r |
f013066e |
403 | cfg_loaded = 1;\r |
404 | \r |
405 | if (PicoRegionOverride) {\r |
406 | cd_region = PicoRegionOverride;\r |
407 | lprintf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA");\r |
408 | }\r |
ee0f881e |
409 | if (!find_bios(cd_region, &used_rom_name)) {\r |
dd5fd477 |
410 | PicoAHW &= ~PAHW_MCD;\r |
f013066e |
411 | return 0;\r |
412 | }\r |
413 | \r |
f013066e |
414 | get_ext(used_rom_name, ext);\r |
415 | }\r |
416 | else\r |
417 | {\r |
dd5fd477 |
418 | if (PicoAHW & PAHW_MCD) Stop_CD();\r |
419 | PicoAHW &= ~PAHW_MCD;\r |
f013066e |
420 | }\r |
421 | \r |
422 | rom = pm_open(used_rom_name);\r |
144a28a0 |
423 | if (!rom) {\r |
3ffee69c |
424 | me_update_msg("Failed to open ROM/CD image");\r |
e9a13365 |
425 | goto fail;\r |
426 | }\r |
427 | \r |
428 | if (cd_state < 0) {\r |
3ffee69c |
429 | me_update_msg("Invalid CD image");\r |
e9a13365 |
430 | goto fail;\r |
f013066e |
431 | }\r |
432 | \r |
a6df06b7 |
433 | menu_romload_prepare(used_rom_name); // also CD load\r |
f013066e |
434 | \r |
144a28a0 |
435 | PicoCartUnload();\r |
436 | rom_loaded = 0;\r |
f013066e |
437 | \r |
144a28a0 |
438 | if ( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {\r |
3ffee69c |
439 | if (ret == 2) me_update_msg("Out of memory");\r |
440 | else if (ret == 3) me_update_msg("Read failed");\r |
441 | else me_update_msg("PicoCartLoad() failed.");\r |
e9a13365 |
442 | goto fail2;\r |
f013066e |
443 | }\r |
444 | pm_close(rom);\r |
e9a13365 |
445 | rom = NULL;\r |
f013066e |
446 | \r |
447 | // detect wrong files (Pico crashes on very small files), also see if ROM EP is good\r |
144a28a0 |
448 | if (rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||\r |
f013066e |
449 | ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {\r |
450 | if (rom_data) free(rom_data);\r |
3ffee69c |
451 | me_update_msg("Not a ROM selected.");\r |
e9a13365 |
452 | goto fail2;\r |
f013066e |
453 | }\r |
454 | \r |
455 | // load config for this ROM (do this before insert to get correct region)\r |
dd5fd477 |
456 | if (!(PicoAHW & PAHW_MCD))\r |
367b6f1f |
457 | memcpy(id_header, rom_data + 0x100, sizeof(id_header));\r |
f013066e |
458 | if (!cfg_loaded) {\r |
ee0f881e |
459 | ret = emu_read_config(1, 1);\r |
460 | if (!ret) emu_read_config(0, 1);\r |
f013066e |
461 | }\r |
462 | \r |
463 | lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);\r |
144a28a0 |
464 | if (PicoCartInsert(rom_data, rom_size)) {\r |
3ffee69c |
465 | me_update_msg("Failed to load ROM.");\r |
e9a13365 |
466 | goto fail2;\r |
f013066e |
467 | }\r |
468 | \r |
f013066e |
469 | // insert CD if it was detected\r |
1ceda417 |
470 | if (cd_state != CIT_NOT_CD) {\r |
36f6fd5a |
471 | ret = Insert_CD(rom_fname, cd_state);\r |
f013066e |
472 | if (ret != 0) {\r |
3ffee69c |
473 | me_update_msg("Insert_CD() failed, invalid CD image?");\r |
e9a13365 |
474 | goto fail2;\r |
f013066e |
475 | }\r |
476 | }\r |
477 | \r |
a6df06b7 |
478 | menu_romload_end();\r |
479 | \r |
f013066e |
480 | if (PicoPatches) {\r |
481 | PicoPatchPrepare();\r |
482 | PicoPatchApply();\r |
483 | }\r |
484 | \r |
485 | // additional movie stuff\r |
e9a13365 |
486 | if (movie_data)\r |
487 | {\r |
488 | if (movie_data[0x14] == '6')\r |
dd5fd477 |
489 | PicoOpt |= POPT_6BTN_PAD; // 6 button pad\r |
490 | else PicoOpt &= ~POPT_6BTN_PAD;\r |
a4790056 |
491 | PicoOpt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing\r |
e9a13365 |
492 | if (movie_data[0xF] >= 'A') {\r |
493 | if (movie_data[0x16] & 0x80) {\r |
f013066e |
494 | PicoRegionOverride = 8;\r |
495 | } else {\r |
496 | PicoRegionOverride = 4;\r |
497 | }\r |
8d5cb10d |
498 | PicoReset();\r |
f013066e |
499 | // TODO: bits 6 & 5\r |
500 | }\r |
501 | movie_data[0x18+30] = 0;\r |
b5bfb864 |
502 | emu_status_msg("MOVIE: %s", (char *) &movie_data[0x18]);\r |
f013066e |
503 | }\r |
504 | else\r |
505 | {\r |
dd5fd477 |
506 | PicoOpt &= ~POPT_DIS_VDP_FIFO;\r |
b5bfb864 |
507 | emu_status_msg(Pico.m.pal ? "PAL SYSTEM / 50 FPS" : "NTSC SYSTEM / 60 FPS");\r |
f013066e |
508 | }\r |
f013066e |
509 | \r |
510 | // load SRAM for this ROM\r |
ee0f881e |
511 | if (currentConfig.EmuOpt & EOPT_EN_SRAM)\r |
512 | emu_save_load_game(1, 1);\r |
f013066e |
513 | \r |
049a6b3e |
514 | strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);\r |
515 | rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;\r |
144a28a0 |
516 | rom_loaded = 1;\r |
f013066e |
517 | return 1;\r |
e9a13365 |
518 | \r |
519 | fail2:\r |
520 | menu_romload_end();\r |
521 | fail:\r |
522 | if (rom != NULL) pm_close(rom);\r |
523 | return 0;\r |
f013066e |
524 | }\r |
525 | \r |
f013066e |
526 | static void romfname_ext(char *dst, const char *prefix, const char *ext)\r |
527 | {\r |
528 | char *p;\r |
529 | int prefix_len = 0;\r |
530 | \r |
531 | // make save filename\r |
049a6b3e |
532 | p = rom_fname_loaded + strlen(rom_fname_loaded) - 1;\r |
533 | for (; p >= rom_fname_loaded && *p != PATH_SEP_C; p--); p++;\r |
f013066e |
534 | *dst = 0;\r |
535 | if (prefix) {\r |
93c18cb4 |
536 | int len = plat_get_root_dir(dst, 512);\r |
36f6fd5a |
537 | strcpy(dst + len, prefix);\r |
538 | prefix_len = len + strlen(prefix);\r |
f013066e |
539 | }\r |
36f6fd5a |
540 | #ifdef UIQ3\r |
049a6b3e |
541 | else p = rom_fname_loaded; // backward compatibility\r |
36f6fd5a |
542 | #endif\r |
f013066e |
543 | strncpy(dst + prefix_len, p, 511-prefix_len);\r |
544 | dst[511-8] = 0;\r |
545 | if (dst[strlen(dst)-4] == '.') dst[strlen(dst)-4] = 0;\r |
546 | if (ext) strcat(dst, ext);\r |
547 | }\r |
548 | \r |
45deb409 |
549 | void emu_make_path(char *buff, const char *end, int size)\r |
2b1fd520 |
550 | {\r |
45deb409 |
551 | int pos, end_len;\r |
552 | \r |
553 | end_len = strlen(end);\r |
554 | pos = plat_get_root_dir(buff, size);\r |
555 | strncpy(buff + pos, end, size - pos);\r |
556 | buff[size - 1] = 0;\r |
557 | if (pos + end_len > size - 1)\r |
558 | lprintf("Warning: path truncated: %s\n", buff);\r |
559 | }\r |
560 | \r |
561 | static void make_config_cfg(char *cfg_buff_512)\r |
562 | {\r |
563 | emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);\r |
2b1fd520 |
564 | if (config_slot != 0)\r |
565 | {\r |
45deb409 |
566 | char *p = strrchr(cfg_buff_512, '.');\r |
567 | if (p == NULL)\r |
568 | p = cfg_buff_512 + strlen(cfg_buff_512);\r |
2b1fd520 |
569 | sprintf(p, ".%i.cfg", config_slot);\r |
570 | }\r |
45deb409 |
571 | cfg_buff_512[511] = 0;\r |
2b1fd520 |
572 | }\r |
573 | \r |
9ecdd73a |
574 | void emu_set_defconfig(void)\r |
36f6fd5a |
575 | {\r |
ee0f881e |
576 | memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig));\r |
36f6fd5a |
577 | PicoOpt = currentConfig.s_PicoOpt;\r |
578 | PsndRate = currentConfig.s_PsndRate;\r |
579 | PicoRegionOverride = currentConfig.s_PicoRegion;\r |
580 | PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r |
581 | PicoCDBuffers = currentConfig.s_PicoCDBuffers;\r |
582 | }\r |
583 | \r |
ee0f881e |
584 | int emu_read_config(int game, int no_defaults)\r |
f013066e |
585 | {\r |
0ae25549 |
586 | char cfg[512];\r |
0ae25549 |
587 | int ret;\r |
f013066e |
588 | \r |
589 | if (!game)\r |
590 | {\r |
591 | if (!no_defaults)\r |
9ecdd73a |
592 | emu_set_defconfig();\r |
2b1fd520 |
593 | make_config_cfg(cfg);\r |
0ae25549 |
594 | ret = config_readsect(cfg, NULL);\r |
f013066e |
595 | }\r |
0ae25549 |
596 | else\r |
597 | {\r |
ee0f881e |
598 | char *sect = emu_make_rom_id();\r |
f013066e |
599 | \r |
0ae25549 |
600 | // try new .cfg way\r |
601 | if (config_slot != 0)\r |
602 | sprintf(cfg, "game.%i.cfg", config_slot);\r |
603 | else strcpy(cfg, "game.cfg");\r |
367b6f1f |
604 | \r |
605 | ret = -1;\r |
2b1fd520 |
606 | if (config_havesect(cfg, sect))\r |
607 | {\r |
608 | // read user's config\r |
67dfdf5f |
609 | int vol = currentConfig.volume;\r |
9ecdd73a |
610 | emu_set_defconfig();\r |
367b6f1f |
611 | ret = config_readsect(cfg, sect);\r |
67dfdf5f |
612 | currentConfig.volume = vol; // make vol global (bah)\r |
367b6f1f |
613 | }\r |
558d8e1f |
614 | else\r |
2b1fd520 |
615 | {\r |
616 | // read global config, and apply game_def.cfg on top\r |
617 | make_config_cfg(cfg);\r |
618 | config_readsect(cfg, NULL);\r |
619 | ret = config_readsect("game_def.cfg", sect);\r |
620 | }\r |
0ae25549 |
621 | \r |
36f6fd5a |
622 | if (ret == 0)\r |
0ae25549 |
623 | {\r |
367b6f1f |
624 | lprintf("loaded cfg from sect \"%s\"\n", sect);\r |
0ae25549 |
625 | }\r |
f013066e |
626 | }\r |
0ae25549 |
627 | \r |
fa8d1331 |
628 | plat_validate_config();\r |
629 | \r |
f013066e |
630 | // some sanity checks\r |
93c0d147 |
631 | #ifdef PSP\r |
fa8d1331 |
632 | /* TODO: mv to plat_validate_config() */\r |
633 | if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r |
93c0d147 |
634 | if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;\r |
960a8e27 |
635 | if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;\r |
93c0d147 |
636 | #endif\r |
fa8d1331 |
637 | if (currentConfig.volume < 0 || currentConfig.volume > 99)\r |
638 | currentConfig.volume = 50;\r |
639 | \r |
640 | if (ret == 0)\r |
641 | config_slot_current = config_slot;\r |
82abf46f |
642 | \r |
0ae25549 |
643 | return (ret == 0);\r |
f013066e |
644 | }\r |
645 | \r |
646 | \r |
ee0f881e |
647 | int emu_write_config(int is_game)\r |
f013066e |
648 | {\r |
0ae25549 |
649 | char cfg[512], *game_sect = NULL;\r |
650 | int ret, write_lrom = 0;\r |
f013066e |
651 | \r |
0ae25549 |
652 | if (!is_game)\r |
f013066e |
653 | {\r |
36f6fd5a |
654 | make_config_cfg(cfg);\r |
0ae25549 |
655 | write_lrom = 1;\r |
f013066e |
656 | } else {\r |
657 | if (config_slot != 0)\r |
0ae25549 |
658 | sprintf(cfg, "game.%i.cfg", config_slot);\r |
659 | else strcpy(cfg, "game.cfg");\r |
ee0f881e |
660 | game_sect = emu_make_rom_id();\r |
661 | lprintf("emu_write_config: sect \"%s\"\n", game_sect);\r |
f013066e |
662 | }\r |
663 | \r |
ee0f881e |
664 | lprintf("emu_write_config: %s ", cfg);\r |
0ae25549 |
665 | ret = config_writesect(cfg, game_sect);\r |
367b6f1f |
666 | if (write_lrom) config_writelrom(cfg);\r |
f013066e |
667 | #ifndef NO_SYNC\r |
0ae25549 |
668 | sync();\r |
f013066e |
669 | #endif\r |
0ae25549 |
670 | lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");\r |
f013066e |
671 | \r |
0ae25549 |
672 | if (ret == 0) config_slot_current = config_slot;\r |
673 | return ret == 0;\r |
f013066e |
674 | }\r |
675 | \r |
676 | \r |
e31266dd |
677 | /* always using built-in font */\r |
678 | \r |
b7911801 |
679 | #define mk_text_out(name, type, val, topleft, step_x, step_y) \\r |
e31266dd |
680 | void name(int x, int y, const char *text) \\r |
681 | { \\r |
682 | int i, l, len = strlen(text); \\r |
b7911801 |
683 | type *screen = (type *)(topleft) + x * step_x + y * step_y; \\r |
e31266dd |
684 | \\r |
b7911801 |
685 | for (i = 0; i < len; i++, screen += 8 * step_x) \\r |
e31266dd |
686 | { \\r |
687 | for (l = 0; l < 8; l++) \\r |
688 | { \\r |
689 | unsigned char fd = fontdata8x8[text[i] * 8 + l];\\r |
b7911801 |
690 | type *s = screen + l * step_y; \\r |
691 | if (fd&0x80) s[step_x * 0] = val; \\r |
692 | if (fd&0x40) s[step_x * 1] = val; \\r |
693 | if (fd&0x20) s[step_x * 2] = val; \\r |
694 | if (fd&0x10) s[step_x * 3] = val; \\r |
695 | if (fd&0x08) s[step_x * 4] = val; \\r |
696 | if (fd&0x04) s[step_x * 5] = val; \\r |
697 | if (fd&0x02) s[step_x * 6] = val; \\r |
698 | if (fd&0x01) s[step_x * 7] = val; \\r |
e31266dd |
699 | } \\r |
700 | } \\r |
f013066e |
701 | }\r |
702 | \r |
b7911801 |
703 | mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_width)\r |
704 | mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_width)\r |
705 | mk_text_out(emu_text_out8_rot, unsigned char, 0xf0,\r |
706 | (char *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)\r |
707 | mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,\r |
708 | (short *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)\r |
f013066e |
709 | \r |
e31266dd |
710 | #undef mk_text_out\r |
f013066e |
711 | \r |
712 | \r |
388947f3 |
713 | void update_movie(void)\r |
f013066e |
714 | {\r |
715 | int offs = Pico.m.frame_count*3 + 0x40;\r |
716 | if (offs+3 > movie_size) {\r |
717 | free(movie_data);\r |
718 | movie_data = 0;\r |
b5bfb864 |
719 | emu_status_msg("END OF MOVIE.");\r |
f013066e |
720 | lprintf("END OF MOVIE.\n");\r |
f013066e |
721 | } else {\r |
722 | // MXYZ SACB RLDU\r |
723 | PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU\r |
61f66fe0 |
724 | if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // C\r |
725 | if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // A\r |
726 | if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // B\r |
f013066e |
727 | PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r |
61f66fe0 |
728 | if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // C\r |
729 | if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // A\r |
730 | if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // B\r |
f013066e |
731 | PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r |
732 | if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r |
733 | if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r |
734 | PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r |
735 | if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r |
736 | if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r |
737 | }\r |
738 | }\r |
739 | \r |
740 | \r |
741 | static size_t gzRead2(void *p, size_t _size, size_t _n, void *file)\r |
742 | {\r |
743 | return gzread(file, p, _n);\r |
744 | }\r |
745 | \r |
746 | \r |
747 | static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)\r |
748 | {\r |
749 | return gzwrite(file, p, _n);\r |
750 | }\r |
751 | \r |
752 | static int try_ropen_file(const char *fname)\r |
753 | {\r |
754 | FILE *f;\r |
755 | \r |
756 | f = fopen(fname, "rb");\r |
757 | if (f) {\r |
758 | fclose(f);\r |
759 | return 1;\r |
760 | }\r |
761 | return 0;\r |
762 | }\r |
763 | \r |
ee0f881e |
764 | char *emu_get_save_fname(int load, int is_sram, int slot)\r |
f013066e |
765 | {\r |
766 | static char saveFname[512];\r |
767 | char ext[16];\r |
768 | \r |
769 | if (is_sram)\r |
770 | {\r |
36f6fd5a |
771 | romfname_ext(saveFname, (PicoAHW&1) ? "brm"PATH_SEP : "srm"PATH_SEP, (PicoAHW&1) ? ".brm" : ".srm");\r |
f013066e |
772 | if (load) {\r |
773 | if (try_ropen_file(saveFname)) return saveFname;\r |
774 | // try in current dir..\r |
dd5fd477 |
775 | romfname_ext(saveFname, NULL, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");\r |
f013066e |
776 | if (try_ropen_file(saveFname)) return saveFname;\r |
777 | return NULL; // give up\r |
778 | }\r |
779 | }\r |
780 | else\r |
781 | {\r |
782 | ext[0] = 0;\r |
783 | if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r |
36f6fd5a |
784 | strcat(ext, (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds.gz" : ".mds");\r |
f013066e |
785 | \r |
36f6fd5a |
786 | romfname_ext(saveFname, "mds" PATH_SEP, ext);\r |
f013066e |
787 | if (load) {\r |
788 | if (try_ropen_file(saveFname)) return saveFname;\r |
789 | romfname_ext(saveFname, NULL, ext);\r |
790 | if (try_ropen_file(saveFname)) return saveFname;\r |
36f6fd5a |
791 | // no gzipped states, search for non-gzipped\r |
792 | if (currentConfig.EmuOpt & EOPT_GZIP_SAVES)\r |
793 | {\r |
f013066e |
794 | ext[0] = 0;\r |
795 | if(slot > 0 && slot < 10) sprintf(ext, ".%i", slot);\r |
796 | strcat(ext, ".mds");\r |
797 | \r |
36f6fd5a |
798 | romfname_ext(saveFname, "mds"PATH_SEP, ext);\r |
f013066e |
799 | if (try_ropen_file(saveFname)) return saveFname;\r |
800 | romfname_ext(saveFname, NULL, ext);\r |
801 | if (try_ropen_file(saveFname)) return saveFname;\r |
802 | }\r |
803 | return NULL;\r |
804 | }\r |
805 | }\r |
806 | \r |
807 | return saveFname;\r |
808 | }\r |
809 | \r |
ee0f881e |
810 | int emu_check_save_file(int slot)\r |
f013066e |
811 | {\r |
ee0f881e |
812 | return emu_get_save_fname(1, 0, slot) ? 1 : 0;\r |
f013066e |
813 | }\r |
814 | \r |
815 | void emu_setSaveStateCbs(int gz)\r |
816 | {\r |
817 | if (gz) {\r |
818 | areaRead = gzRead2;\r |
819 | areaWrite = gzWrite2;\r |
820 | areaEof = (areaeof *) gzeof;\r |
821 | areaSeek = (areaseek *) gzseek;\r |
822 | areaClose = (areaclose *) gzclose;\r |
823 | } else {\r |
824 | areaRead = (arearw *) fread;\r |
825 | areaWrite = (arearw *) fwrite;\r |
826 | areaEof = (areaeof *) feof;\r |
827 | areaSeek = (areaseek *) fseek;\r |
828 | areaClose = (areaclose *) fclose;\r |
829 | }\r |
830 | }\r |
831 | \r |
ee0f881e |
832 | int emu_save_load_game(int load, int sram)\r |
f013066e |
833 | {\r |
834 | int ret = 0;\r |
835 | char *saveFname;\r |
836 | \r |
837 | // make save filename\r |
ee0f881e |
838 | saveFname = emu_get_save_fname(load, sram, state_slot);\r |
f013066e |
839 | if (saveFname == NULL) {\r |
388947f3 |
840 | if (!sram)\r |
b5bfb864 |
841 | emu_status_msg(load ? "LOAD FAILED (missing file)" : "SAVE FAILED");\r |
f013066e |
842 | return -1;\r |
843 | }\r |
844 | \r |
845 | lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r |
846 | \r |
11f4e722 |
847 | if (sram)\r |
848 | {\r |
f013066e |
849 | FILE *sramFile;\r |
850 | int sram_size;\r |
851 | unsigned char *sram_data;\r |
852 | int truncate = 1;\r |
dd5fd477 |
853 | if (PicoAHW & PAHW_MCD)\r |
854 | {\r |
855 | if (PicoOpt&POPT_EN_MCD_RAMCART) {\r |
f013066e |
856 | sram_size = 0x12000;\r |
857 | sram_data = SRam.data;\r |
858 | if (sram_data)\r |
859 | memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);\r |
860 | } else {\r |
861 | sram_size = 0x2000;\r |
862 | sram_data = Pico_mcd->bram;\r |
863 | truncate = 0; // the .brm may contain RAM cart data after normal brm\r |
864 | }\r |
865 | } else {\r |
866 | sram_size = SRam.end-SRam.start+1;\r |
867 | if(Pico.m.sram_reg & 4) sram_size=0x2000;\r |
868 | sram_data = SRam.data;\r |
869 | }\r |
870 | if (!sram_data) return 0; // SRam forcefully disabled for this game\r |
871 | \r |
dd5fd477 |
872 | if (load)\r |
873 | {\r |
f013066e |
874 | sramFile = fopen(saveFname, "rb");\r |
875 | if(!sramFile) return -1;\r |
876 | fread(sram_data, 1, sram_size, sramFile);\r |
877 | fclose(sramFile);\r |
dd5fd477 |
878 | if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))\r |
f013066e |
879 | memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);\r |
880 | } else {\r |
881 | // sram save needs some special processing\r |
882 | // see if we have anything to save\r |
883 | for (; sram_size > 0; sram_size--)\r |
884 | if (sram_data[sram_size-1]) break;\r |
885 | \r |
886 | if (sram_size) {\r |
887 | sramFile = fopen(saveFname, truncate ? "wb" : "r+b");\r |
888 | if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry\r |
889 | if (!sramFile) return -1;\r |
890 | ret = fwrite(sram_data, 1, sram_size, sramFile);\r |
891 | ret = (ret != sram_size) ? -1 : 0;\r |
892 | fclose(sramFile);\r |
893 | #ifndef NO_SYNC\r |
894 | sync();\r |
895 | #endif\r |
896 | }\r |
897 | }\r |
898 | return ret;\r |
899 | }\r |
900 | else\r |
901 | {\r |
902 | void *PmovFile = NULL;\r |
11f4e722 |
903 | if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0)\r |
904 | {\r |
f013066e |
905 | if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r |
906 | emu_setSaveStateCbs(1);\r |
11f4e722 |
907 | if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r |
f013066e |
908 | }\r |
909 | }\r |
910 | else\r |
911 | {\r |
912 | if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r |
913 | emu_setSaveStateCbs(0);\r |
914 | }\r |
915 | }\r |
916 | if(PmovFile) {\r |
917 | ret = PmovState(load ? 6 : 5, PmovFile);\r |
918 | areaClose(PmovFile);\r |
919 | PmovFile = 0;\r |
920 | if (load) Pico.m.dirtyPal=1;\r |
921 | #ifndef NO_SYNC\r |
922 | else sync();\r |
923 | #endif\r |
924 | }\r |
925 | else ret = -1;\r |
926 | if (!ret)\r |
b5bfb864 |
927 | emu_status_msg(load ? "GAME LOADED" : "GAME SAVED");\r |
f013066e |
928 | else\r |
929 | {\r |
b5bfb864 |
930 | emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");\r |
f013066e |
931 | ret = -1;\r |
932 | }\r |
933 | \r |
f013066e |
934 | return ret;\r |
935 | }\r |
936 | }\r |
8e708f92 |
937 | \r |
ee0f881e |
938 | void emu_set_fastforward(int set_on)\r |
8a091e48 |
939 | {\r |
940 | static void *set_PsndOut = NULL;\r |
941 | static int set_Frameskip, set_EmuOpt, is_on = 0;\r |
942 | \r |
943 | if (set_on && !is_on) {\r |
944 | set_PsndOut = PsndOut;\r |
945 | set_Frameskip = currentConfig.Frameskip;\r |
946 | set_EmuOpt = currentConfig.EmuOpt;\r |
947 | PsndOut = NULL;\r |
948 | currentConfig.Frameskip = 8;\r |
949 | currentConfig.EmuOpt &= ~4;\r |
950 | currentConfig.EmuOpt |= 0x40000;\r |
951 | is_on = 1;\r |
b5bfb864 |
952 | emu_status_msg("FAST FORWARD");\r |
8a091e48 |
953 | }\r |
954 | else if (!set_on && is_on) {\r |
955 | PsndOut = set_PsndOut;\r |
956 | currentConfig.Frameskip = set_Frameskip;\r |
957 | currentConfig.EmuOpt = set_EmuOpt;\r |
958 | PsndRerate(1);\r |
959 | is_on = 0;\r |
960 | }\r |
961 | }\r |
962 | \r |
93c18cb4 |
963 | static void emu_msg_tray_open(void)\r |
8a091e48 |
964 | {\r |
b5bfb864 |
965 | emu_status_msg("CD tray opened");\r |
93c18cb4 |
966 | }\r |
967 | \r |
968 | void emu_reset_game(void)\r |
969 | {\r |
970 | PicoReset();\r |
971 | reset_timing = 1;\r |
972 | }\r |
973 | \r |
974 | void run_events_pico(unsigned int events)\r |
975 | {\r |
976 | int lim_x;\r |
977 | \r |
978 | if (events & PEV_PICO_SWINP) {\r |
8a091e48 |
979 | pico_inp_mode++;\r |
388947f3 |
980 | if (pico_inp_mode > 2)\r |
981 | pico_inp_mode = 0;\r |
8a091e48 |
982 | switch (pico_inp_mode) {\r |
b5bfb864 |
983 | case 2: emu_status_msg("Input: Pen on Pad"); break;\r |
984 | case 1: emu_status_msg("Input: Pen on Storyware"); break;\r |
985 | case 0: emu_status_msg("Input: Joystick");\r |
8a091e48 |
986 | PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r |
987 | break;\r |
988 | }\r |
8a091e48 |
989 | }\r |
93c18cb4 |
990 | if (events & PEV_PICO_PPREV) {\r |
8a091e48 |
991 | PicoPicohw.page--;\r |
388947f3 |
992 | if (PicoPicohw.page < 0)\r |
993 | PicoPicohw.page = 0;\r |
b5bfb864 |
994 | emu_status_msg("Page %i", PicoPicohw.page);\r |
8a091e48 |
995 | }\r |
93c18cb4 |
996 | if (events & PEV_PICO_PNEXT) {\r |
8a091e48 |
997 | PicoPicohw.page++;\r |
388947f3 |
998 | if (PicoPicohw.page > 6)\r |
999 | PicoPicohw.page = 6;\r |
b5bfb864 |
1000 | emu_status_msg("Page %i", PicoPicohw.page);\r |
8a091e48 |
1001 | }\r |
93c18cb4 |
1002 | \r |
1003 | if (pico_inp_mode == 0)\r |
1004 | return;\r |
1005 | \r |
1006 | /* handle other input modes */\r |
1007 | if (PicoPad[0] & 1) pico_pen_y--;\r |
1008 | if (PicoPad[0] & 2) pico_pen_y++;\r |
1009 | if (PicoPad[0] & 4) pico_pen_x--;\r |
1010 | if (PicoPad[0] & 8) pico_pen_x++;\r |
1011 | PicoPad[0] &= ~0x0f; // release UDLR\r |
1012 | \r |
1013 | lim_x = (Pico.video.reg[12]&1) ? 319 : 255;\r |
1014 | if (pico_pen_y < 8)\r |
1015 | pico_pen_y = 8;\r |
1016 | if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y)\r |
1017 | pico_pen_y = 224 - PICO_PEN_ADJUST_Y;\r |
1018 | if (pico_pen_x < 0)\r |
1019 | pico_pen_x = 0;\r |
1020 | if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X)\r |
1021 | pico_pen_x = lim_x - PICO_PEN_ADJUST_X;\r |
1022 | \r |
1023 | PicoPicohw.pen_pos[0] = pico_pen_x;\r |
1024 | if (!(Pico.video.reg[12] & 1))\r |
1025 | PicoPicohw.pen_pos[0] += pico_pen_x / 4;\r |
1026 | PicoPicohw.pen_pos[0] += 0x3c;\r |
1027 | PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r |
8a091e48 |
1028 | }\r |
1029 | \r |
388947f3 |
1030 | static void do_turbo(int *pad, int acts)\r |
6589c840 |
1031 | {\r |
1032 | static int turbo_pad = 0;\r |
1033 | static unsigned char turbo_cnt[3] = { 0, 0, 0 };\r |
1034 | int inc = currentConfig.turbo_rate * 2;\r |
1035 | \r |
1036 | if (acts & 0x1000) {\r |
1037 | turbo_cnt[0] += inc;\r |
1038 | if (turbo_cnt[0] >= 60)\r |
1039 | turbo_pad ^= 0x10, turbo_cnt[0] = 0;\r |
1040 | }\r |
1041 | if (acts & 0x2000) {\r |
1042 | turbo_cnt[1] += inc;\r |
1043 | if (turbo_cnt[1] >= 60)\r |
1044 | turbo_pad ^= 0x20, turbo_cnt[1] = 0;\r |
1045 | }\r |
1046 | if (acts & 0x4000) {\r |
1047 | turbo_cnt[2] += inc;\r |
1048 | if (turbo_cnt[2] >= 60)\r |
1049 | turbo_pad ^= 0x40, turbo_cnt[2] = 0;\r |
1050 | }\r |
1051 | *pad |= turbo_pad & (acts >> 8);\r |
1052 | }\r |
8a091e48 |
1053 | \r |
93c18cb4 |
1054 | static void run_events_ui(unsigned int which)\r |
388947f3 |
1055 | {\r |
1056 | if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))\r |
1057 | {\r |
1058 | int do_it = 1;\r |
ee0f881e |
1059 | if ( emu_check_save_file(state_slot) &&\r |
388947f3 |
1060 | (((which & PEV_STATE_LOAD) && (currentConfig.EmuOpt & EOPT_CONFIRM_LOAD)) ||\r |
1061 | ((which & PEV_STATE_SAVE) && (currentConfig.EmuOpt & EOPT_CONFIRM_SAVE))) )\r |
1062 | {\r |
1063 | const char *nm;\r |
1064 | char tmp[64];\r |
1065 | int keys, len;\r |
1066 | \r |
1067 | strcpy(tmp, (which & PEV_STATE_LOAD) ? "LOAD STATE?" : "OVERWRITE SAVE?");\r |
1068 | len = strlen(tmp);\r |
1069 | nm = in_get_key_name(-1, -PBTN_MA3);\r |
1070 | snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);\r |
1071 | len = strlen(tmp);\r |
1072 | nm = in_get_key_name(-1, -PBTN_MBACK);\r |
1073 | snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);\r |
1074 | \r |
1075 | plat_status_msg_busy_first(tmp);\r |
1076 | \r |
1077 | in_set_blocking(1);\r |
1078 | while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK))\r |
1079 | ;\r |
1080 | while ( !((keys = in_menu_wait_any(50)) & (PBTN_MA3|PBTN_MBACK)) )\r |
1081 | ;\r |
1082 | if (keys & PBTN_MBACK)\r |
1083 | do_it = 0;\r |
1084 | while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK))\r |
1085 | ;\r |
1086 | in_set_blocking(0);\r |
1087 | }\r |
1088 | if (do_it) {\r |
1089 | plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING GAME" : "SAVING GAME");\r |
1090 | PicoStateProgressCB = plat_status_msg_busy_next;\r |
ee0f881e |
1091 | emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0);\r |
388947f3 |
1092 | PicoStateProgressCB = NULL;\r |
1093 | }\r |
1094 | }\r |
1095 | if (which & PEV_SWITCH_RND)\r |
1096 | {\r |
9ecdd73a |
1097 | plat_video_toggle_renderer(1, 0);\r |
388947f3 |
1098 | }\r |
1099 | if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT))\r |
1100 | {\r |
1101 | if (which & PEV_SSLOT_PREV) {\r |
1102 | state_slot -= 1;\r |
1103 | if (state_slot < 0)\r |
1104 | state_slot = 9;\r |
1105 | } else {\r |
1106 | state_slot += 1;\r |
1107 | if (state_slot > 9)\r |
1108 | state_slot = 0;\r |
1109 | }\r |
1110 | \r |
b5bfb864 |
1111 | emu_status_msg("SAVE SLOT %i [%s]", state_slot,\r |
ee0f881e |
1112 | emu_check_save_file(state_slot) ? "USED" : "FREE");\r |
388947f3 |
1113 | }\r |
1114 | if (which & PEV_MENU)\r |
1115 | engineState = PGS_Menu;\r |
1116 | }\r |
1117 | \r |
1118 | void emu_update_input(void)\r |
1119 | {\r |
8e77275e |
1120 | static int prev_events = 0;\r |
1121 | int actions[IN_BINDTYPE_COUNT] = { 0, };\r |
1122 | int pl_actions[2];\r |
1123 | int events;\r |
388947f3 |
1124 | \r |
8e77275e |
1125 | in_update(actions);\r |
388947f3 |
1126 | \r |
8e77275e |
1127 | pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];\r |
1128 | pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;\r |
388947f3 |
1129 | \r |
8e77275e |
1130 | PicoPad[0] = pl_actions[0] & 0xfff;\r |
1131 | PicoPad[1] = pl_actions[1] & 0xfff;\r |
388947f3 |
1132 | \r |
8e77275e |
1133 | if (pl_actions[0] & 0x7000)\r |
1134 | do_turbo(&PicoPad[0], pl_actions[0]);\r |
1135 | if (pl_actions[1] & 0x7000)\r |
1136 | do_turbo(&PicoPad[1], pl_actions[1]);\r |
1137 | \r |
1138 | events = actions[IN_BINDTYPE_EMU] & PEV_MASK;\r |
388947f3 |
1139 | \r |
1140 | // volume is treated in special way and triggered every frame\r |
1141 | if (events & (PEV_VOL_DOWN|PEV_VOL_UP))\r |
1142 | plat_update_volume(1, events & PEV_VOL_UP);\r |
1143 | \r |
8e77275e |
1144 | if ((events ^ prev_events) & PEV_FF) {\r |
ee0f881e |
1145 | emu_set_fastforward(events & PEV_FF);\r |
388947f3 |
1146 | plat_update_volume(0, 0);\r |
93c18cb4 |
1147 | reset_timing = 1;\r |
388947f3 |
1148 | }\r |
1149 | \r |
8e77275e |
1150 | events &= ~prev_events;\r |
388947f3 |
1151 | \r |
93c18cb4 |
1152 | if (PicoAHW == PAHW_PICO)\r |
1153 | run_events_pico(events);\r |
388947f3 |
1154 | if (events)\r |
93c18cb4 |
1155 | run_events_ui(events);\r |
388947f3 |
1156 | if (movie_data)\r |
1157 | update_movie();\r |
1158 | \r |
8e77275e |
1159 | prev_events = actions[IN_BINDTYPE_EMU] & PEV_MASK;\r |
388947f3 |
1160 | }\r |
1161 | \r |
93c18cb4 |
1162 | static void mkdir_path(char *path_with_reserve, int pos, const char *name)\r |
1163 | {\r |
1164 | strcpy(path_with_reserve + pos, name);\r |
1165 | if (plat_is_dir(path_with_reserve))\r |
1166 | return;\r |
1167 | if (mkdir(path_with_reserve, 0777) < 0)\r |
1168 | lprintf("failed to create: %s\n", path_with_reserve);\r |
1169 | }\r |
1170 | \r |
1171 | void emu_init(void)\r |
1172 | {\r |
45deb409 |
1173 | char path[512];\r |
93c18cb4 |
1174 | int pos;\r |
1175 | \r |
1176 | /* make dirs for saves */\r |
45deb409 |
1177 | pos = plat_get_root_dir(path, sizeof(path) - 4);\r |
1178 | mkdir_path(path, pos, "mds");\r |
1179 | mkdir_path(path, pos, "srm");\r |
1180 | mkdir_path(path, pos, "brm");\r |
1181 | \r |
1182 | make_config_cfg(path);\r |
1183 | config_readlrom(path);\r |
93c18cb4 |
1184 | \r |
1185 | PicoInit();\r |
1186 | PicoMessage = plat_status_msg_busy_next;\r |
1187 | PicoMCDopenTray = emu_msg_tray_open;\r |
1188 | PicoMCDcloseTray = menu_loop_tray;\r |
1189 | }\r |
1190 | \r |
1191 | void emu_finish(void)\r |
1192 | {\r |
1193 | // save SRAM\r |
ee0f881e |
1194 | if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r |
1195 | emu_save_load_game(0, 1);\r |
93c18cb4 |
1196 | SRam.changed = 0;\r |
1197 | }\r |
1198 | \r |
45deb409 |
1199 | if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {\r |
1200 | char cfg[512];\r |
1201 | make_config_cfg(cfg);\r |
1202 | config_writelrom(cfg);\r |
1203 | #ifndef NO_SYNC\r |
1204 | sync();\r |
1205 | #endif\r |
1206 | }\r |
93c18cb4 |
1207 | \r |
1208 | PicoExit();\r |
1209 | }\r |
1210 | \r |
b5bfb864 |
1211 | static void skip_frame(int do_audio)\r |
1212 | {\r |
1213 | PicoSkipFrame = do_audio ? 1 : 2;\r |
1214 | PicoFrame();\r |
1215 | PicoSkipFrame = 0;\r |
1216 | }\r |
1217 | \r |
1218 | /* our tick here is 1 us right now */\r |
1219 | #define ms_to_ticks(x) (unsigned int)(x * 1000)\r |
1220 | #define get_ticks() plat_get_ticks_us()\r |
1221 | \r |
1222 | void emu_loop(void)\r |
1223 | {\r |
1224 | int pframes_done; /* "period" frames, used for sync */\r |
1225 | int frames_done, frames_shown; /* actual frames for fps counter */\r |
1226 | int oldmodes, target_fps, target_frametime;\r |
1227 | unsigned int timestamp_base = 0, timestamp_fps;\r |
1228 | char *notice_msg = NULL;\r |
1229 | char fpsbuff[24];\r |
1230 | int i;\r |
1231 | \r |
1232 | fpsbuff[0] = 0;\r |
1233 | \r |
1234 | /* make sure we are in correct mode */\r |
1235 | oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r |
1236 | Pico.m.dirtyPal = 1;\r |
1237 | \r |
1238 | /* number of ticks per frame */\r |
1239 | if (Pico.m.pal) {\r |
1240 | target_fps = 50;\r |
1241 | target_frametime = ms_to_ticks(1000) / 50;\r |
1242 | } else {\r |
1243 | target_fps = 60;\r |
1244 | target_frametime = ms_to_ticks(1000) / 60 + 1;\r |
1245 | }\r |
1246 | \r |
1247 | // prepare CD buffer\r |
1248 | if (PicoAHW & PAHW_MCD)\r |
1249 | PicoCDBufferInit();\r |
1250 | \r |
1251 | if (currentConfig.EmuOpt & EOPT_PSYNC)\r |
1252 | plat_video_wait_vsync();\r |
1253 | \r |
1254 | pemu_loop_prep();\r |
1255 | \r |
1256 | timestamp_fps = get_ticks();\r |
1257 | reset_timing = 1;\r |
1258 | \r |
1259 | frames_done = frames_shown = pframes_done = 0;\r |
1260 | \r |
1261 | /* loop with resync every 1 sec. */\r |
1262 | while (engineState == PGS_Running)\r |
1263 | {\r |
1264 | unsigned int timestamp;\r |
1265 | int diff, diff_lim;\r |
1266 | int modes;\r |
1267 | \r |
1268 | timestamp = get_ticks();\r |
1269 | if (reset_timing) {\r |
1270 | reset_timing = 0;\r |
1271 | timestamp_base = timestamp;\r |
1272 | pframes_done = 0;\r |
1273 | }\r |
1274 | \r |
1275 | // show notice_msg message?\r |
1276 | if (notice_msg_time != 0)\r |
1277 | {\r |
1278 | static int noticeMsgSum;\r |
1279 | if (timestamp - ms_to_ticks(notice_msg_time) > ms_to_ticks(2000)) {\r |
1280 | notice_msg_time = 0;\r |
1281 | plat_status_msg_clear();\r |
1282 | notice_msg = NULL;\r |
1283 | } else {\r |
1284 | int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];\r |
1285 | if (sum != noticeMsgSum) {\r |
1286 | plat_status_msg_clear();\r |
1287 | noticeMsgSum = sum;\r |
1288 | }\r |
1289 | notice_msg = noticeMsg;\r |
1290 | }\r |
1291 | }\r |
1292 | \r |
1293 | // check for mode changes\r |
1294 | modes = ((Pico.video.reg[12]&1)<<2) | (Pico.video.reg[1]&8);\r |
1295 | if (modes != oldmodes) {\r |
1296 | oldmodes = modes;\r |
1297 | pemu_video_mode_change(!(modes & 4), (modes & 8));\r |
1298 | }\r |
1299 | \r |
1300 | // second changed?\r |
1301 | if (timestamp - timestamp_fps >= ms_to_ticks(1000))\r |
1302 | {\r |
1303 | #ifdef BENCHMARK\r |
1304 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r |
1305 | if (++bench == 10) {\r |
1306 | bench = 0;\r |
1307 | bench_fps_s = bench_fps;\r |
1308 | bf[bfp++ & 3] = bench_fps;\r |
1309 | bench_fps = 0;\r |
1310 | }\r |
1311 | bench_fps += frames_shown;\r |
1312 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r |
1313 | #else\r |
1314 | if (currentConfig.EmuOpt & EOPT_SHOW_FPS) {\r |
1315 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r |
1316 | if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r |
1317 | }\r |
1318 | #endif\r |
1319 | frames_shown = frames_done = 0;\r |
1320 | timestamp_fps += ms_to_ticks(1000);\r |
1321 | }\r |
1322 | #ifdef PFRAMES\r |
1323 | sprintf(fpsbuff, "%i", Pico.m.frame_count);\r |
1324 | #endif\r |
1325 | \r |
1326 | if (timestamp - timestamp_base >= ms_to_ticks(1000))\r |
1327 | {\r |
1328 | if (PsndOut == 0 && currentConfig.Frameskip >= 0)\r |
1329 | pframes_done = 0;\r |
1330 | else\r |
1331 | pframes_done -= target_fps;\r |
1332 | timestamp_base += ms_to_ticks(1000);\r |
1333 | }\r |
1334 | \r |
1335 | diff = timestamp - timestamp_base;\r |
1336 | diff_lim = (pframes_done + 1) * target_frametime;\r |
1337 | \r |
1338 | if (currentConfig.Frameskip >= 0) // frameskip enabled\r |
1339 | {\r |
1340 | for (i = 0; i < currentConfig.Frameskip; i++) {\r |
1341 | emu_update_input();\r |
1342 | skip_frame(1);\r |
1343 | pframes_done++; frames_done++;\r |
1344 | diff_lim += target_frametime;\r |
1345 | \r |
1346 | if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r |
1347 | timestamp = get_ticks();\r |
1348 | diff = timestamp - timestamp_base;\r |
1349 | if (diff < diff_lim) // we are too fast\r |
1350 | plat_wait_till_us(timestamp_base + diff_lim);\r |
1351 | }\r |
1352 | }\r |
1353 | }\r |
1354 | else if (diff > diff_lim)\r |
1355 | {\r |
1356 | /* no time left for this frame - skip */\r |
1357 | if (diff - diff_lim >= ms_to_ticks(300)) {\r |
1358 | /* if too much behind, reset instead */\r |
1359 | reset_timing = 1;\r |
1360 | continue;\r |
1361 | }\r |
1362 | emu_update_input();\r |
1363 | skip_frame(diff < diff_lim + target_frametime * 2);\r |
1364 | pframes_done++; frames_done++;\r |
1365 | continue;\r |
1366 | }\r |
1367 | \r |
1368 | emu_update_input();\r |
1369 | PicoFrame();\r |
1370 | \r |
1371 | /* frame limiter */\r |
1372 | if (!reset_timing && (PsndOut != NULL || currentConfig.Frameskip < 0))\r |
1373 | {\r |
1374 | timestamp = get_ticks();\r |
1375 | diff = timestamp - timestamp_base;\r |
1376 | \r |
1377 | // sleep or vsync if we are still too fast\r |
1378 | if (diff < diff_lim)\r |
1379 | {\r |
1380 | // we are too fast\r |
1381 | if (currentConfig.EmuOpt & EOPT_PSYNC) {\r |
1382 | if (diff_lim - diff > target_frametime/2)\r |
1383 | plat_wait_till_us(timestamp_base + target_frametime/4);\r |
1384 | plat_video_wait_vsync();\r |
1385 | } else\r |
1386 | plat_wait_till_us(timestamp_base + diff_lim);\r |
1387 | }\r |
1388 | }\r |
1389 | \r |
1390 | pemu_update_display(fpsbuff, notice_msg);\r |
1391 | \r |
1392 | pframes_done++; frames_done++; frames_shown++;\r |
1393 | }\r |
1394 | \r |
1395 | emu_set_fastforward(0);\r |
1396 | \r |
1397 | if (PicoAHW & PAHW_MCD)\r |
1398 | PicoCDBufferFree();\r |
1399 | \r |
1400 | // save SRAM\r |
1401 | if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r |
1402 | plat_status_msg_busy_first("Writing SRAM/BRAM...");\r |
1403 | emu_save_load_game(0, 1);\r |
1404 | SRam.changed = 0;\r |
1405 | }\r |
1406 | \r |
1407 | // do menu background to be sure it's right\r |
1408 | pemu_forced_frame(POPT_EN_SOFTSCALE);\r |
1409 | \r |
1410 | pemu_loop_end();\r |
1411 | }\r |
1412 | \r |