cff531af |
1 | /*\r |
2 | * PicoDrive\r |
3 | * (C) notaz, 2007-2010\r |
4 | *\r |
5 | * This work is licensed under the terms of MAME license.\r |
6 | * See COPYING file in the top-level directory.\r |
7 | */\r |
ea8c405f |
8 | \r |
9 | #include <stdio.h>\r |
10 | #include <stdlib.h>\r |
4c2e3554 |
11 | #include <string.h>\r |
b24e0f6c |
12 | #include <stdarg.h>\r |
ea8c405f |
13 | #ifndef NO_SYNC\r |
14 | #include <unistd.h>\r |
15 | #endif\r |
16 | \r |
e743be20 |
17 | #include "../libpicofe/posix.h"\r |
18 | #include "../libpicofe/input.h"\r |
19 | #include "../libpicofe/fonts.h"\r |
df92fbd1 |
20 | #include "../libpicofe/sndout.h"\r |
e743be20 |
21 | #include "../libpicofe/lprintf.h"\r |
22 | #include "../libpicofe/plat.h"\r |
ea8c405f |
23 | #include "emu.h"\r |
e743be20 |
24 | #include "input_pico.h"\r |
25 | #include "menu_pico.h"\r |
39e6f973 |
26 | #include "config_file.h"\r |
ea8c405f |
27 | \r |
efcba75f |
28 | #include <pico/pico_int.h>\r |
29 | #include <pico/patch.h>\r |
ea8c405f |
30 | \r |
ea8c405f |
31 | \r |
54646a39 |
32 | #define STATUS_MSG_TIMEOUT 2000\r |
33 | \r |
e2de9939 |
34 | void *g_screen_ptr;\r |
35 | \r |
36 | #if !SCREEN_SIZE_FIXED\r |
37 | int g_screen_width = SCREEN_WIDTH;\r |
38 | int g_screen_height = SCREEN_HEIGHT;\r |
39 | #endif\r |
40 | \r |
c46ffd31 |
41 | char *PicoConfigFile = "config.cfg";\r |
58c86d00 |
42 | currentConfig_t currentConfig, defaultConfig;\r |
ea8c405f |
43 | int state_slot = 0;\r |
44 | int config_slot = 0, config_slot_current = 0;\r |
f2cf8472 |
45 | int pico_pen_x = 320/2, pico_pen_y = 240/2;\r |
c060a9ab |
46 | int pico_inp_mode = 0;\r |
713c9224 |
47 | int engineState = PGS_Menu;\r |
48 | \r |
df92fbd1 |
49 | static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r |
50 | \r |
ae87bffa |
51 | /* tmp buff to reduce stack usage for plats with small stack */\r |
52 | static char static_buff[512];\r |
636d5f25 |
53 | const char *rom_fname_reload;\r |
ae87bffa |
54 | char rom_fname_loaded[512];\r |
f2cf8472 |
55 | int reset_timing = 0;\r |
b24e0f6c |
56 | static unsigned int notice_msg_time; /* when started showing */\r |
57 | static char noticeMsg[40];\r |
ea8c405f |
58 | \r |
59 | unsigned char *movie_data = NULL;\r |
60 | static int movie_size = 0;\r |
61 | \r |
ea8c405f |
62 | \r |
ee2a3bdf |
63 | /* don't use tolower() for easy old glibc binary compatibility */\r |
f8af9634 |
64 | static void strlwr_(char *string)\r |
ea8c405f |
65 | {\r |
f8af9634 |
66 | char *p;\r |
67 | for (p = string; *p; p++)\r |
ee2a3bdf |
68 | if ('A' <= *p && *p <= 'Z')\r |
69 | *p += 'a' - 'A';\r |
ea8c405f |
70 | }\r |
71 | \r |
ca482e5d |
72 | static int try_rfn_cut(char *fname)\r |
ea8c405f |
73 | {\r |
74 | FILE *tmp;\r |
75 | char *p;\r |
76 | \r |
ca482e5d |
77 | p = fname + strlen(fname) - 1;\r |
78 | for (; p > fname; p--)\r |
ea8c405f |
79 | if (*p == '.') break;\r |
80 | *p = 0;\r |
81 | \r |
ca482e5d |
82 | if((tmp = fopen(fname, "rb"))) {\r |
ea8c405f |
83 | fclose(tmp);\r |
84 | return 1;\r |
85 | }\r |
86 | return 0;\r |
87 | }\r |
88 | \r |
35e3031a |
89 | static void get_ext(const char *file, char *ext)\r |
ea8c405f |
90 | {\r |
35e3031a |
91 | const char *p;\r |
ea8c405f |
92 | \r |
93 | p = file + strlen(file) - 4;\r |
94 | if (p < file) p = file;\r |
95 | strncpy(ext, p, 4);\r |
96 | ext[4] = 0;\r |
97 | strlwr_(ext);\r |
98 | }\r |
99 | \r |
fcdefcf6 |
100 | static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext, const char *fname)\r |
101 | {\r |
102 | int prefix_len = 0;\r |
103 | const char *p;\r |
104 | \r |
105 | *dst = 0;\r |
106 | if (prefix) {\r |
107 | int len = plat_get_root_dir(dst, dstlen);\r |
108 | strcpy(dst + len, prefix);\r |
109 | prefix_len = len + strlen(prefix);\r |
110 | }\r |
111 | \r |
112 | p = fname + strlen(fname) - 1;\r |
113 | for (; p >= fname && *p != PATH_SEP_C; p--)\r |
114 | ;\r |
115 | p++;\r |
116 | strncpy(dst + prefix_len, p, dstlen - prefix_len - 1);\r |
117 | \r |
118 | dst[dstlen - 8] = 0;\r |
119 | if (dst[strlen(dst) - 4] == '.')\r |
120 | dst[strlen(dst) - 4] = 0;\r |
121 | if (ext)\r |
122 | strcat(dst, ext);\r |
123 | }\r |
124 | \r |
125 | static void romfname_ext(char *dst, int dstlen, const char *prefix, const char *ext)\r |
126 | {\r |
127 | fname_ext(dst, dstlen, prefix, ext, rom_fname_loaded);\r |
128 | }\r |
129 | \r |
b24e0f6c |
130 | void emu_status_msg(const char *format, ...)\r |
131 | {\r |
132 | va_list vl;\r |
54646a39 |
133 | int ret;\r |
b24e0f6c |
134 | \r |
135 | va_start(vl, format);\r |
54646a39 |
136 | ret = vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);\r |
b24e0f6c |
137 | va_end(vl);\r |
138 | \r |
54646a39 |
139 | /* be sure old text gets overwritten */\r |
140 | for (; ret < 28; ret++)\r |
141 | noticeMsg[ret] = ' ';\r |
142 | noticeMsg[ret] = 0;\r |
143 | \r |
b24e0f6c |
144 | notice_msg_time = plat_get_ticks_ms();\r |
145 | }\r |
146 | \r |
54646a39 |
147 | static const char * const biosfiles_us[] = { "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303" };\r |
148 | static const char * const biosfiles_eu[] = { "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303" };\r |
149 | static const char * const biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };\r |
ea8c405f |
150 | \r |
4c2e3554 |
151 | static const char *find_bios(int *region, const char *cd_fname)\r |
ea8c405f |
152 | {\r |
ea8c405f |
153 | int i, count;\r |
54646a39 |
154 | const char * const *files;\r |
ea8c405f |
155 | FILE *f = NULL;\r |
4c2e3554 |
156 | int ret;\r |
157 | \r |
158 | // we need to have config loaded at this point\r |
159 | ret = emu_read_config(cd_fname, 0);\r |
160 | if (!ret) emu_read_config(NULL, 0);\r |
161 | \r |
162 | if (PicoRegionOverride) {\r |
163 | *region = PicoRegionOverride;\r |
164 | lprintf("override region to %s\n", *region != 4 ?\r |
165 | (*region == 8 ? "EU" : "JAP") : "USA");\r |
166 | }\r |
ea8c405f |
167 | \r |
4c2e3554 |
168 | if (*region == 4) { // US\r |
ea8c405f |
169 | files = biosfiles_us;\r |
170 | count = sizeof(biosfiles_us) / sizeof(char *);\r |
4c2e3554 |
171 | } else if (*region == 8) { // EU\r |
ea8c405f |
172 | files = biosfiles_eu;\r |
173 | count = sizeof(biosfiles_eu) / sizeof(char *);\r |
4c2e3554 |
174 | } else if (*region == 1 || *region == 2) {\r |
ea8c405f |
175 | files = biosfiles_jp;\r |
176 | count = sizeof(biosfiles_jp) / sizeof(char *);\r |
177 | } else {\r |
178 | return 0;\r |
179 | }\r |
180 | \r |
181 | for (i = 0; i < count; i++)\r |
182 | {\r |
ae87bffa |
183 | emu_make_path(static_buff, files[i], sizeof(static_buff) - 4);\r |
184 | strcat(static_buff, ".bin");\r |
185 | f = fopen(static_buff, "rb");\r |
ea8c405f |
186 | if (f) break;\r |
187 | \r |
ae87bffa |
188 | static_buff[strlen(static_buff) - 4] = 0;\r |
189 | strcat(static_buff, ".zip");\r |
190 | f = fopen(static_buff, "rb");\r |
ea8c405f |
191 | if (f) break;\r |
192 | }\r |
193 | \r |
194 | if (f) {\r |
ae87bffa |
195 | lprintf("using bios: %s\n", static_buff);\r |
ea8c405f |
196 | fclose(f);\r |
4c2e3554 |
197 | return static_buff;\r |
ea8c405f |
198 | } else {\r |
ae87bffa |
199 | sprintf(static_buff, "no %s BIOS files found, read docs",\r |
4c2e3554 |
200 | *region != 4 ? (*region == 8 ? "EU" : "JAP") : "USA");\r |
e743be20 |
201 | menu_update_msg(static_buff);\r |
4c2e3554 |
202 | return NULL;\r |
ea8c405f |
203 | }\r |
204 | }\r |
205 | \r |
c6196c0f |
206 | /* check if the name begins with BIOS name */\r |
1ca2ea4f |
207 | /*\r |
c6196c0f |
208 | static int emu_isBios(const char *name)\r |
209 | {\r |
210 | int i;\r |
211 | for (i = 0; i < sizeof(biosfiles_us)/sizeof(biosfiles_us[0]); i++)\r |
212 | if (strstr(name, biosfiles_us[i]) != NULL) return 1;\r |
213 | for (i = 0; i < sizeof(biosfiles_eu)/sizeof(biosfiles_eu[0]); i++)\r |
214 | if (strstr(name, biosfiles_eu[i]) != NULL) return 1;\r |
215 | for (i = 0; i < sizeof(biosfiles_jp)/sizeof(biosfiles_jp[0]); i++)\r |
216 | if (strstr(name, biosfiles_jp[i]) != NULL) return 1;\r |
217 | return 0;\r |
218 | }\r |
1ca2ea4f |
219 | */\r |
c6196c0f |
220 | \r |
7a87643e |
221 | static int extract_text(char *dest, const unsigned char *src, int len, int swab)\r |
58c86d00 |
222 | {\r |
223 | char *p = dest;\r |
224 | int i;\r |
225 | \r |
226 | if (swab) swab = 1;\r |
227 | \r |
228 | for (i = len - 1; i >= 0; i--)\r |
229 | {\r |
230 | if (src[i^swab] != ' ') break;\r |
231 | }\r |
232 | len = i + 1;\r |
233 | \r |
234 | for (i = 0; i < len; i++)\r |
235 | {\r |
236 | unsigned char s = src[i^swab];\r |
237 | if (s >= 0x20 && s < 0x7f && s != '#' && s != '|' &&\r |
238 | s != '[' && s != ']' && s != '\\')\r |
239 | {\r |
240 | *p++ = s;\r |
241 | }\r |
242 | else\r |
243 | {\r |
244 | sprintf(p, "\\%02x", s);\r |
245 | p += 3;\r |
246 | }\r |
247 | }\r |
248 | \r |
249 | return p - dest;\r |
250 | }\r |
251 | \r |
fcdefcf6 |
252 | static char *emu_make_rom_id(const char *fname)\r |
58c86d00 |
253 | {\r |
ca482e5d |
254 | static char id_string[3+0xe*3+0x3*3+0x30*3+3];\r |
bdec53c9 |
255 | int pos, swab = 1;\r |
58c86d00 |
256 | \r |
602133e1 |
257 | if (PicoAHW & PAHW_MCD) {\r |
bdec53c9 |
258 | strcpy(id_string, "CD|");\r |
259 | swab = 0;\r |
260 | }\r |
fcdefcf6 |
261 | else if (PicoAHW & PAHW_SMS)\r |
262 | strcpy(id_string, "MS|");\r |
263 | else strcpy(id_string, "MD|");\r |
58c86d00 |
264 | pos = 3;\r |
265 | \r |
fcdefcf6 |
266 | if (!(PicoAHW & PAHW_SMS)) {\r |
4c2e3554 |
267 | pos += extract_text(id_string + pos, media_id_header + 0x80, 0x0e, swab); // serial\r |
fcdefcf6 |
268 | id_string[pos] = '|'; pos++;\r |
4c2e3554 |
269 | pos += extract_text(id_string + pos, media_id_header + 0xf0, 0x03, swab); // region\r |
fcdefcf6 |
270 | id_string[pos] = '|'; pos++;\r |
4c2e3554 |
271 | pos += extract_text(id_string + pos, media_id_header + 0x50, 0x30, swab); // overseas name\r |
fcdefcf6 |
272 | id_string[pos] = 0;\r |
273 | if (pos > 5)\r |
274 | return id_string;\r |
275 | pos = 3;\r |
276 | }\r |
277 | \r |
278 | // can't find name in ROM, use filename\r |
279 | fname_ext(id_string + 3, sizeof(id_string) - 3, NULL, NULL, fname);\r |
58c86d00 |
280 | \r |
58c86d00 |
281 | return id_string;\r |
282 | }\r |
283 | \r |
ca482e5d |
284 | // buffer must be at least 150 byte long\r |
a47dd663 |
285 | void emu_get_game_name(char *str150)\r |
ca482e5d |
286 | {\r |
287 | int ret, swab = (PicoAHW & PAHW_MCD) ? 0 : 1;\r |
288 | char *s, *d;\r |
289 | \r |
4c2e3554 |
290 | ret = extract_text(str150, media_id_header + 0x50, 0x30, swab); // overseas name\r |
ca482e5d |
291 | \r |
292 | for (s = d = str150 + 1; s < str150+ret; s++)\r |
293 | {\r |
294 | if (*s == 0) break;\r |
295 | if (*s != ' ' || d[-1] != ' ')\r |
296 | *d++ = *s;\r |
297 | }\r |
298 | *d = 0;\r |
299 | }\r |
300 | \r |
974fdb5b |
301 | static void system_announce(void)\r |
302 | {\r |
f3a57b2d |
303 | const char *sys_name, *tv_standard, *extra = "";\r |
974fdb5b |
304 | int fps;\r |
305 | \r |
306 | if (PicoAHW & PAHW_SMS) {\r |
307 | sys_name = "Master System";\r |
f3a57b2d |
308 | #ifdef NO_SMS\r |
309 | extra = " [no support]";\r |
310 | #endif\r |
974fdb5b |
311 | } else if (PicoAHW & PAHW_PICO) {\r |
312 | sys_name = "Pico";\r |
313 | } else if (PicoAHW & PAHW_MCD) {\r |
314 | sys_name = "Mega CD";\r |
315 | if ((Pico.m.hardware & 0xc0) == 0x80)\r |
316 | sys_name = "Sega CD";\r |
317 | } else if (PicoAHW & PAHW_32X) {\r |
318 | sys_name = "32X";\r |
319 | } else {\r |
320 | sys_name = "MegaDrive";\r |
321 | if ((Pico.m.hardware & 0xc0) == 0x80)\r |
322 | sys_name = "Genesis";\r |
323 | }\r |
324 | tv_standard = Pico.m.pal ? "PAL" : "NTSC";\r |
325 | fps = Pico.m.pal ? 50 : 60;\r |
326 | \r |
f3a57b2d |
327 | emu_status_msg("%s %s / %dFPS%s", tv_standard, sys_name, fps, extra);\r |
974fdb5b |
328 | }\r |
329 | \r |
4c2e3554 |
330 | static void do_region_override(const char *media_fname)\r |
331 | {\r |
332 | // we only need to override region if config tells us so\r |
333 | int ret = emu_read_config(media_fname, 0);\r |
334 | if (!ret) emu_read_config(NULL, 0);\r |
335 | }\r |
336 | \r |
636d5f25 |
337 | int emu_reload_rom(const char *rom_fname_in)\r |
ea8c405f |
338 | {\r |
636d5f25 |
339 | char *rom_fname = NULL;\r |
ea8c405f |
340 | char ext[5];\r |
4c2e3554 |
341 | enum media_type_e media_type;\r |
636d5f25 |
342 | int menu_romload_started = 0;\r |
4c2e3554 |
343 | char carthw_path[512];\r |
636d5f25 |
344 | int retval = 0;\r |
ea8c405f |
345 | \r |
636d5f25 |
346 | lprintf("emu_ReloadRom(%s)\n", rom_fname_in);\r |
ea8c405f |
347 | \r |
636d5f25 |
348 | rom_fname = strdup(rom_fname_in);\r |
349 | if (rom_fname == NULL)\r |
350 | return 0;\r |
351 | \r |
ca482e5d |
352 | get_ext(rom_fname, ext);\r |
ea8c405f |
353 | \r |
77189b7d |
354 | // early cleanup\r |
355 | PicoPatchUnload();\r |
eacee137 |
356 | if (movie_data) {\r |
ea8c405f |
357 | free(movie_data);\r |
358 | movie_data = 0;\r |
359 | }\r |
77189b7d |
360 | \r |
2d2247c2 |
361 | if (!strcmp(ext, ".gmv"))\r |
362 | {\r |
ea8c405f |
363 | // check for both gmv and rom\r |
364 | int dummy;\r |
ca482e5d |
365 | FILE *movie_file = fopen(rom_fname, "rb");\r |
b8a1c09a |
366 | if (!movie_file) {\r |
e743be20 |
367 | menu_update_msg("Failed to open movie.");\r |
636d5f25 |
368 | goto out;\r |
ea8c405f |
369 | }\r |
370 | fseek(movie_file, 0, SEEK_END);\r |
371 | movie_size = ftell(movie_file);\r |
372 | fseek(movie_file, 0, SEEK_SET);\r |
b8a1c09a |
373 | if (movie_size < 64+3) {\r |
e743be20 |
374 | menu_update_msg("Invalid GMV file.");\r |
ea8c405f |
375 | fclose(movie_file);\r |
636d5f25 |
376 | goto out;\r |
ea8c405f |
377 | }\r |
378 | movie_data = malloc(movie_size);\r |
b8a1c09a |
379 | if (movie_data == NULL) {\r |
e743be20 |
380 | menu_update_msg("low memory.");\r |
ea8c405f |
381 | fclose(movie_file);\r |
636d5f25 |
382 | goto out;\r |
ea8c405f |
383 | }\r |
b8a1c09a |
384 | dummy = fread(movie_data, 1, movie_size, movie_file);\r |
ea8c405f |
385 | fclose(movie_file);\r |
386 | if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r |
e743be20 |
387 | menu_update_msg("Invalid GMV file.");\r |
636d5f25 |
388 | goto out;\r |
ea8c405f |
389 | }\r |
ca482e5d |
390 | dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r |
ea8c405f |
391 | if (!dummy) {\r |
e743be20 |
392 | menu_update_msg("Could't find a ROM for movie.");\r |
636d5f25 |
393 | goto out;\r |
ea8c405f |
394 | }\r |
ca482e5d |
395 | get_ext(rom_fname, ext);\r |
f8af9634 |
396 | lprintf("gmv loaded for %s\n", rom_fname);\r |
ea8c405f |
397 | }\r |
ca482e5d |
398 | else if (!strcmp(ext, ".pat"))\r |
399 | {\r |
ea8c405f |
400 | int dummy;\r |
ca482e5d |
401 | PicoPatchLoad(rom_fname);\r |
402 | dummy = try_rfn_cut(rom_fname) || try_rfn_cut(rom_fname);\r |
ea8c405f |
403 | if (!dummy) {\r |
e743be20 |
404 | menu_update_msg("Could't find a ROM to patch.");\r |
636d5f25 |
405 | goto out;\r |
ea8c405f |
406 | }\r |
ca482e5d |
407 | get_ext(rom_fname, ext);\r |
ea8c405f |
408 | }\r |
409 | \r |
4c2e3554 |
410 | menu_romload_prepare(rom_fname); // also CD load\r |
411 | menu_romload_started = 1;\r |
ea8c405f |
412 | \r |
4c2e3554 |
413 | emu_make_path(carthw_path, "carthw.cfg", sizeof(carthw_path));\r |
3e49ffd0 |
414 | \r |
4c2e3554 |
415 | media_type = PicoLoadMedia(rom_fname, carthw_path,\r |
416 | find_bios, do_region_override);\r |
ea8c405f |
417 | \r |
4c2e3554 |
418 | switch (media_type) {\r |
419 | case PM_BAD_DETECT:\r |
420 | menu_update_msg("Not a ROM/CD img selected.");\r |
636d5f25 |
421 | goto out;\r |
4c2e3554 |
422 | case PM_BAD_CD:\r |
423 | menu_update_msg("Invalid CD image");\r |
636d5f25 |
424 | goto out;\r |
4c2e3554 |
425 | case PM_BAD_CD_NO_BIOS:\r |
426 | // find_bios() prints a message\r |
636d5f25 |
427 | goto out;\r |
4c2e3554 |
428 | case PM_ERROR:\r |
429 | menu_update_msg("Load error");\r |
636d5f25 |
430 | goto out;\r |
4c2e3554 |
431 | default:\r |
432 | break;\r |
ea8c405f |
433 | }\r |
434 | \r |
4b167c12 |
435 | menu_romload_end();\r |
636d5f25 |
436 | menu_romload_started = 0;\r |
4b167c12 |
437 | \r |
ea8c405f |
438 | if (PicoPatches) {\r |
439 | PicoPatchPrepare();\r |
440 | PicoPatchApply();\r |
441 | }\r |
442 | \r |
443 | // additional movie stuff\r |
f9f40f10 |
444 | if (movie_data)\r |
445 | {\r |
446 | if (movie_data[0x14] == '6')\r |
602133e1 |
447 | PicoOpt |= POPT_6BTN_PAD; // 6 button pad\r |
448 | else PicoOpt &= ~POPT_6BTN_PAD;\r |
2aa27095 |
449 | PicoOpt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing\r |
f9f40f10 |
450 | if (movie_data[0xF] >= 'A') {\r |
451 | if (movie_data[0x16] & 0x80) {\r |
ea8c405f |
452 | PicoRegionOverride = 8;\r |
453 | } else {\r |
454 | PicoRegionOverride = 4;\r |
455 | }\r |
1cb1584b |
456 | PicoReset();\r |
ea8c405f |
457 | // TODO: bits 6 & 5\r |
458 | }\r |
459 | movie_data[0x18+30] = 0;\r |
b24e0f6c |
460 | emu_status_msg("MOVIE: %s", (char *) &movie_data[0x18]);\r |
ea8c405f |
461 | }\r |
462 | else\r |
463 | {\r |
974fdb5b |
464 | system_announce();\r |
602133e1 |
465 | PicoOpt &= ~POPT_DIS_VDP_FIFO;\r |
ea8c405f |
466 | }\r |
ea8c405f |
467 | \r |
bcc9eda0 |
468 | strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);\r |
469 | rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;\r |
bcc9eda0 |
470 | \r |
ea8c405f |
471 | // load SRAM for this ROM\r |
a47dd663 |
472 | if (currentConfig.EmuOpt & EOPT_EN_SRAM)\r |
473 | emu_save_load_game(1, 1);\r |
ea8c405f |
474 | \r |
636d5f25 |
475 | retval = 1;\r |
476 | out:\r |
636d5f25 |
477 | if (menu_romload_started)\r |
478 | menu_romload_end();\r |
479 | free(rom_fname);\r |
480 | return retval;\r |
ea8c405f |
481 | }\r |
482 | \r |
35e3031a |
483 | int emu_swap_cd(const char *fname)\r |
484 | {\r |
485 | cd_img_type cd_type;\r |
486 | int ret = -1;\r |
487 | \r |
4c2e3554 |
488 | cd_type = PicoCdCheck(fname, NULL);\r |
35e3031a |
489 | if (cd_type != CIT_NOT_CD)\r |
490 | ret = Insert_CD(fname, cd_type);\r |
491 | if (ret != 0) {\r |
e743be20 |
492 | menu_update_msg("Load failed, invalid CD image?");\r |
35e3031a |
493 | return 0;\r |
494 | }\r |
495 | \r |
496 | strncpy(rom_fname_loaded, fname, sizeof(rom_fname_loaded)-1);\r |
497 | rom_fname_loaded[sizeof(rom_fname_loaded)-1] = 0;\r |
498 | return 1;\r |
499 | }\r |
500 | \r |
ae87bffa |
501 | // <base dir><end>\r |
27701801 |
502 | void emu_make_path(char *buff, const char *end, int size)\r |
08fe8094 |
503 | {\r |
27701801 |
504 | int pos, end_len;\r |
505 | \r |
506 | end_len = strlen(end);\r |
507 | pos = plat_get_root_dir(buff, size);\r |
508 | strncpy(buff + pos, end, size - pos);\r |
509 | buff[size - 1] = 0;\r |
510 | if (pos + end_len > size - 1)\r |
511 | lprintf("Warning: path truncated: %s\n", buff);\r |
512 | }\r |
513 | \r |
514 | static void make_config_cfg(char *cfg_buff_512)\r |
515 | {\r |
516 | emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);\r |
08fe8094 |
517 | if (config_slot != 0)\r |
518 | {\r |
27701801 |
519 | char *p = strrchr(cfg_buff_512, '.');\r |
520 | if (p == NULL)\r |
521 | p = cfg_buff_512 + strlen(cfg_buff_512);\r |
08fe8094 |
522 | sprintf(p, ".%i.cfg", config_slot);\r |
523 | }\r |
27701801 |
524 | cfg_buff_512[511] = 0;\r |
08fe8094 |
525 | }\r |
526 | \r |
697746df |
527 | void emu_prep_defconfig(void)\r |
528 | {\r |
529 | memset(&defaultConfig, 0, sizeof(defaultConfig));\r |
fcdefcf6 |
530 | defaultConfig.EmuOpt = 0x9d | EOPT_RAM_TIMINGS|EOPT_EN_CD_LEDS;\r |
697746df |
531 | defaultConfig.s_PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |\r |
532 | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES |\r |
533 | POPT_EN_32X|POPT_EN_PWM;\r |
534 | defaultConfig.s_PsndRate = 44100;\r |
535 | defaultConfig.s_PicoRegion = 0; // auto\r |
536 | defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r |
537 | defaultConfig.s_PicoCDBuffers = 0;\r |
fcdefcf6 |
538 | defaultConfig.confirm_save = EOPT_CONFIRM_SAVE;\r |
697746df |
539 | defaultConfig.Frameskip = -1; // auto\r |
540 | defaultConfig.volume = 50;\r |
541 | defaultConfig.gamma = 100;\r |
542 | defaultConfig.scaling = 0;\r |
543 | defaultConfig.turbo_rate = 15;\r |
544 | \r |
545 | // platform specific overrides\r |
546 | pemu_prep_defconfig();\r |
547 | }\r |
548 | \r |
d90f5bd7 |
549 | void emu_set_defconfig(void)\r |
ca482e5d |
550 | {\r |
a47dd663 |
551 | memcpy(¤tConfig, &defaultConfig, sizeof(currentConfig));\r |
ca482e5d |
552 | PicoOpt = currentConfig.s_PicoOpt;\r |
553 | PsndRate = currentConfig.s_PsndRate;\r |
554 | PicoRegionOverride = currentConfig.s_PicoRegion;\r |
555 | PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r |
556 | PicoCDBuffers = currentConfig.s_PicoCDBuffers;\r |
fcdefcf6 |
557 | p32x_msh2_multiplier = MSH2_MULTI_DEFAULT;\r |
558 | p32x_ssh2_multiplier = SSH2_MULTI_DEFAULT;\r |
ca482e5d |
559 | }\r |
560 | \r |
fcdefcf6 |
561 | int emu_read_config(const char *rom_fname, int no_defaults)\r |
ea8c405f |
562 | {\r |
58c86d00 |
563 | char cfg[512];\r |
58c86d00 |
564 | int ret;\r |
ea8c405f |
565 | \r |
490eb480 |
566 | if (!no_defaults)\r |
567 | emu_set_defconfig();\r |
568 | \r |
fcdefcf6 |
569 | if (rom_fname == NULL)\r |
ea8c405f |
570 | {\r |
fcdefcf6 |
571 | // global config\r |
08fe8094 |
572 | make_config_cfg(cfg);\r |
58c86d00 |
573 | ret = config_readsect(cfg, NULL);\r |
ea8c405f |
574 | }\r |
58c86d00 |
575 | else\r |
576 | {\r |
fcdefcf6 |
577 | char *sect = emu_make_rom_id(rom_fname);\r |
ea8c405f |
578 | \r |
58c86d00 |
579 | if (config_slot != 0)\r |
580 | sprintf(cfg, "game.%i.cfg", config_slot);\r |
581 | else strcpy(cfg, "game.cfg");\r |
1ca2ea4f |
582 | \r |
583 | ret = -1;\r |
08fe8094 |
584 | if (config_havesect(cfg, sect))\r |
585 | {\r |
586 | // read user's config\r |
7b802576 |
587 | int vol = currentConfig.volume;\r |
d90f5bd7 |
588 | emu_set_defconfig();\r |
1ca2ea4f |
589 | ret = config_readsect(cfg, sect);\r |
7b802576 |
590 | currentConfig.volume = vol; // make vol global (bah)\r |
1ca2ea4f |
591 | }\r |
a39b7867 |
592 | else\r |
08fe8094 |
593 | {\r |
594 | // read global config, and apply game_def.cfg on top\r |
595 | make_config_cfg(cfg);\r |
596 | config_readsect(cfg, NULL);\r |
d8afe7b8 |
597 | emu_make_path(cfg, "game_def.cfg", sizeof(cfg));\r |
598 | ret = config_readsect(cfg, sect);\r |
08fe8094 |
599 | }\r |
58c86d00 |
600 | \r |
ca482e5d |
601 | if (ret == 0)\r |
58c86d00 |
602 | {\r |
1ca2ea4f |
603 | lprintf("loaded cfg from sect \"%s\"\n", sect);\r |
58c86d00 |
604 | }\r |
ea8c405f |
605 | }\r |
58c86d00 |
606 | \r |
697746df |
607 | pemu_validate_config();\r |
ee2a3bdf |
608 | \r |
ea8c405f |
609 | // some sanity checks\r |
2445b7cb |
610 | #ifdef PSP\r |
ee2a3bdf |
611 | /* TODO: mv to plat_validate_config() */\r |
612 | if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r |
2445b7cb |
613 | if (currentConfig.gamma < -4 || currentConfig.gamma > 16) currentConfig.gamma = 0;\r |
6fc57144 |
614 | if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2) currentConfig.gamma2 = 0;\r |
2445b7cb |
615 | #endif\r |
ee2a3bdf |
616 | if (currentConfig.volume < 0 || currentConfig.volume > 99)\r |
617 | currentConfig.volume = 50;\r |
618 | \r |
619 | if (ret == 0)\r |
620 | config_slot_current = config_slot;\r |
991473ad |
621 | \r |
58c86d00 |
622 | return (ret == 0);\r |
ea8c405f |
623 | }\r |
624 | \r |
625 | \r |
a47dd663 |
626 | int emu_write_config(int is_game)\r |
ea8c405f |
627 | {\r |
58c86d00 |
628 | char cfg[512], *game_sect = NULL;\r |
629 | int ret, write_lrom = 0;\r |
ea8c405f |
630 | \r |
58c86d00 |
631 | if (!is_game)\r |
ea8c405f |
632 | {\r |
ca482e5d |
633 | make_config_cfg(cfg);\r |
58c86d00 |
634 | write_lrom = 1;\r |
ea8c405f |
635 | } else {\r |
636 | if (config_slot != 0)\r |
58c86d00 |
637 | sprintf(cfg, "game.%i.cfg", config_slot);\r |
638 | else strcpy(cfg, "game.cfg");\r |
fcdefcf6 |
639 | game_sect = emu_make_rom_id(rom_fname_loaded);\r |
a47dd663 |
640 | lprintf("emu_write_config: sect \"%s\"\n", game_sect);\r |
ea8c405f |
641 | }\r |
642 | \r |
a47dd663 |
643 | lprintf("emu_write_config: %s ", cfg);\r |
58c86d00 |
644 | ret = config_writesect(cfg, game_sect);\r |
1ca2ea4f |
645 | if (write_lrom) config_writelrom(cfg);\r |
ea8c405f |
646 | #ifndef NO_SYNC\r |
58c86d00 |
647 | sync();\r |
ea8c405f |
648 | #endif\r |
58c86d00 |
649 | lprintf((ret == 0) ? "(ok)\n" : "(failed)\n");\r |
ea8c405f |
650 | \r |
58c86d00 |
651 | if (ret == 0) config_slot_current = config_slot;\r |
652 | return ret == 0;\r |
ea8c405f |
653 | }\r |
654 | \r |
655 | \r |
e2de9939 |
656 | /* always using built-in font */\r |
657 | \r |
cc41eb4f |
658 | #define mk_text_out(name, type, val, topleft, step_x, step_y) \\r |
e2de9939 |
659 | void name(int x, int y, const char *text) \\r |
660 | { \\r |
661 | int i, l, len = strlen(text); \\r |
cc41eb4f |
662 | type *screen = (type *)(topleft) + x * step_x + y * step_y; \\r |
e2de9939 |
663 | \\r |
cc41eb4f |
664 | for (i = 0; i < len; i++, screen += 8 * step_x) \\r |
e2de9939 |
665 | { \\r |
666 | for (l = 0; l < 8; l++) \\r |
667 | { \\r |
668 | unsigned char fd = fontdata8x8[text[i] * 8 + l];\\r |
cc41eb4f |
669 | type *s = screen + l * step_y; \\r |
670 | if (fd&0x80) s[step_x * 0] = val; \\r |
671 | if (fd&0x40) s[step_x * 1] = val; \\r |
672 | if (fd&0x20) s[step_x * 2] = val; \\r |
673 | if (fd&0x10) s[step_x * 3] = val; \\r |
674 | if (fd&0x08) s[step_x * 4] = val; \\r |
675 | if (fd&0x04) s[step_x * 5] = val; \\r |
676 | if (fd&0x02) s[step_x * 6] = val; \\r |
677 | if (fd&0x01) s[step_x * 7] = val; \\r |
e2de9939 |
678 | } \\r |
679 | } \\r |
ea8c405f |
680 | }\r |
681 | \r |
cc41eb4f |
682 | mk_text_out(emu_text_out8, unsigned char, 0xf0, g_screen_ptr, 1, g_screen_width)\r |
683 | mk_text_out(emu_text_out16, unsigned short, 0xffff, g_screen_ptr, 1, g_screen_width)\r |
684 | mk_text_out(emu_text_out8_rot, unsigned char, 0xf0,\r |
685 | (char *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)\r |
686 | mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,\r |
687 | (short *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)\r |
ea8c405f |
688 | \r |
e2de9939 |
689 | #undef mk_text_out\r |
ea8c405f |
690 | \r |
691 | \r |
d34a42f9 |
692 | void update_movie(void)\r |
ea8c405f |
693 | {\r |
694 | int offs = Pico.m.frame_count*3 + 0x40;\r |
695 | if (offs+3 > movie_size) {\r |
696 | free(movie_data);\r |
697 | movie_data = 0;\r |
b24e0f6c |
698 | emu_status_msg("END OF MOVIE.");\r |
ea8c405f |
699 | lprintf("END OF MOVIE.\n");\r |
ea8c405f |
700 | } else {\r |
701 | // MXYZ SACB RLDU\r |
702 | PicoPad[0] = ~movie_data[offs] & 0x8f; // ! SCBA RLDU\r |
f8af9634 |
703 | if(!(movie_data[offs] & 0x10)) PicoPad[0] |= 0x40; // C\r |
704 | if(!(movie_data[offs] & 0x20)) PicoPad[0] |= 0x10; // A\r |
705 | if(!(movie_data[offs] & 0x40)) PicoPad[0] |= 0x20; // B\r |
ea8c405f |
706 | PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r |
f8af9634 |
707 | if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // C\r |
708 | if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // A\r |
709 | if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // B\r |
ea8c405f |
710 | PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r |
711 | if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r |
712 | if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r |
713 | PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r |
714 | if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r |
715 | if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r |
716 | }\r |
717 | }\r |
718 | \r |
ea8c405f |
719 | static int try_ropen_file(const char *fname)\r |
720 | {\r |
721 | FILE *f;\r |
722 | \r |
723 | f = fopen(fname, "rb");\r |
724 | if (f) {\r |
725 | fclose(f);\r |
726 | return 1;\r |
727 | }\r |
728 | return 0;\r |
729 | }\r |
730 | \r |
a47dd663 |
731 | char *emu_get_save_fname(int load, int is_sram, int slot)\r |
ea8c405f |
732 | {\r |
ae87bffa |
733 | char *saveFname = static_buff;\r |
ea8c405f |
734 | char ext[16];\r |
735 | \r |
736 | if (is_sram)\r |
737 | {\r |
6bc00695 |
738 | strcpy(ext, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");\r |
fcdefcf6 |
739 | romfname_ext(saveFname, sizeof(static_buff),\r |
740 | (PicoAHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext);\r |
6bc00695 |
741 | if (!load)\r |
742 | return saveFname;\r |
743 | \r |
744 | if (try_ropen_file(saveFname))\r |
745 | return saveFname;\r |
746 | \r |
fcdefcf6 |
747 | romfname_ext(saveFname, sizeof(static_buff), NULL, ext);\r |
6bc00695 |
748 | if (try_ropen_file(saveFname))\r |
749 | return saveFname;\r |
ea8c405f |
750 | }\r |
751 | else\r |
752 | {\r |
6bc00695 |
753 | const char *ext_main = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds.gz" : ".mds";\r |
754 | const char *ext_othr = (currentConfig.EmuOpt & EOPT_GZIP_SAVES) ? ".mds" : ".mds.gz";\r |
ea8c405f |
755 | ext[0] = 0;\r |
6bc00695 |
756 | if (slot > 0 && slot < 10)\r |
757 | sprintf(ext, ".%i", slot);\r |
758 | strcat(ext, ext_main);\r |
759 | \r |
760 | if (!load) {\r |
fcdefcf6 |
761 | romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);\r |
6bc00695 |
762 | return saveFname;\r |
763 | }\r |
764 | else {\r |
fcdefcf6 |
765 | romfname_ext(saveFname, sizeof(static_buff), "mds" PATH_SEP, ext);\r |
6bc00695 |
766 | if (try_ropen_file(saveFname))\r |
767 | return saveFname;\r |
ea8c405f |
768 | \r |
fcdefcf6 |
769 | romfname_ext(saveFname, sizeof(static_buff), NULL, ext);\r |
6bc00695 |
770 | if (try_ropen_file(saveFname))\r |
771 | return saveFname;\r |
772 | \r |
773 | // try the other ext\r |
774 | ext[0] = 0;\r |
775 | if (slot > 0 && slot < 10)\r |
776 | sprintf(ext, ".%i", slot);\r |
777 | strcat(ext, ext_othr);\r |
778 | \r |
fcdefcf6 |
779 | romfname_ext(saveFname, sizeof(static_buff), "mds"PATH_SEP, ext);\r |
6bc00695 |
780 | if (try_ropen_file(saveFname))\r |
781 | return saveFname;\r |
ea8c405f |
782 | }\r |
783 | }\r |
784 | \r |
6bc00695 |
785 | return NULL;\r |
ea8c405f |
786 | }\r |
787 | \r |
cca8800d |
788 | int emu_check_save_file(int slot, int *time)\r |
ea8c405f |
789 | {\r |
a47dd663 |
790 | return emu_get_save_fname(1, 0, slot) ? 1 : 0;\r |
ea8c405f |
791 | }\r |
792 | \r |
a47dd663 |
793 | int emu_save_load_game(int load, int sram)\r |
ea8c405f |
794 | {\r |
795 | int ret = 0;\r |
796 | char *saveFname;\r |
797 | \r |
798 | // make save filename\r |
a47dd663 |
799 | saveFname = emu_get_save_fname(load, sram, state_slot);\r |
ea8c405f |
800 | if (saveFname == NULL) {\r |
d34a42f9 |
801 | if (!sram)\r |
b24e0f6c |
802 | emu_status_msg(load ? "LOAD FAILED (missing file)" : "SAVE FAILED");\r |
ea8c405f |
803 | return -1;\r |
804 | }\r |
805 | \r |
806 | lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);\r |
807 | \r |
da42200b |
808 | if (sram)\r |
809 | {\r |
ea8c405f |
810 | FILE *sramFile;\r |
811 | int sram_size;\r |
812 | unsigned char *sram_data;\r |
813 | int truncate = 1;\r |
602133e1 |
814 | if (PicoAHW & PAHW_MCD)\r |
815 | {\r |
45f2f245 |
816 | if (PicoOpt & POPT_EN_MCD_RAMCART) {\r |
ea8c405f |
817 | sram_size = 0x12000;\r |
818 | sram_data = SRam.data;\r |
819 | if (sram_data)\r |
820 | memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);\r |
821 | } else {\r |
822 | sram_size = 0x2000;\r |
823 | sram_data = Pico_mcd->bram;\r |
824 | truncate = 0; // the .brm may contain RAM cart data after normal brm\r |
825 | }\r |
826 | } else {\r |
45f2f245 |
827 | sram_size = SRam.size;\r |
ea8c405f |
828 | sram_data = SRam.data;\r |
829 | }\r |
b8a1c09a |
830 | if (sram_data == NULL)\r |
831 | return 0; // SRam forcefully disabled for this game\r |
ea8c405f |
832 | \r |
602133e1 |
833 | if (load)\r |
834 | {\r |
ea8c405f |
835 | sramFile = fopen(saveFname, "rb");\r |
b8a1c09a |
836 | if (!sramFile)\r |
837 | return -1;\r |
838 | ret = fread(sram_data, 1, sram_size, sramFile);\r |
839 | ret = ret > 0 ? 0 : -1;\r |
ea8c405f |
840 | fclose(sramFile);\r |
602133e1 |
841 | if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))\r |
ea8c405f |
842 | memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);\r |
843 | } else {\r |
844 | // sram save needs some special processing\r |
845 | // see if we have anything to save\r |
846 | for (; sram_size > 0; sram_size--)\r |
847 | if (sram_data[sram_size-1]) break;\r |
848 | \r |
849 | if (sram_size) {\r |
850 | sramFile = fopen(saveFname, truncate ? "wb" : "r+b");\r |
851 | if (!sramFile) sramFile = fopen(saveFname, "wb"); // retry\r |
852 | if (!sramFile) return -1;\r |
853 | ret = fwrite(sram_data, 1, sram_size, sramFile);\r |
854 | ret = (ret != sram_size) ? -1 : 0;\r |
855 | fclose(sramFile);\r |
856 | #ifndef NO_SYNC\r |
857 | sync();\r |
858 | #endif\r |
859 | }\r |
860 | }\r |
861 | return ret;\r |
862 | }\r |
863 | else\r |
864 | {\r |
bcc9eda0 |
865 | ret = PicoState(saveFname, !load);\r |
866 | if (!ret) {\r |
ea8c405f |
867 | #ifndef NO_SYNC\r |
bcc9eda0 |
868 | if (!load) sync();\r |
ea8c405f |
869 | #endif\r |
54646a39 |
870 | emu_status_msg(load ? "STATE LOADED" : "STATE SAVED");\r |
bcc9eda0 |
871 | } else {\r |
b24e0f6c |
872 | emu_status_msg(load ? "LOAD FAILED" : "SAVE FAILED");\r |
ea8c405f |
873 | ret = -1;\r |
874 | }\r |
875 | \r |
ea8c405f |
876 | return ret;\r |
877 | }\r |
878 | }\r |
bdec53c9 |
879 | \r |
a47dd663 |
880 | void emu_set_fastforward(int set_on)\r |
c060a9ab |
881 | {\r |
882 | static void *set_PsndOut = NULL;\r |
883 | static int set_Frameskip, set_EmuOpt, is_on = 0;\r |
884 | \r |
885 | if (set_on && !is_on) {\r |
886 | set_PsndOut = PsndOut;\r |
887 | set_Frameskip = currentConfig.Frameskip;\r |
888 | set_EmuOpt = currentConfig.EmuOpt;\r |
889 | PsndOut = NULL;\r |
890 | currentConfig.Frameskip = 8;\r |
891 | currentConfig.EmuOpt &= ~4;\r |
892 | currentConfig.EmuOpt |= 0x40000;\r |
893 | is_on = 1;\r |
b24e0f6c |
894 | emu_status_msg("FAST FORWARD");\r |
c060a9ab |
895 | }\r |
896 | else if (!set_on && is_on) {\r |
897 | PsndOut = set_PsndOut;\r |
898 | currentConfig.Frameskip = set_Frameskip;\r |
899 | currentConfig.EmuOpt = set_EmuOpt;\r |
900 | PsndRerate(1);\r |
901 | is_on = 0;\r |
902 | }\r |
903 | }\r |
904 | \r |
d687ef50 |
905 | static void emu_tray_open(void)\r |
c060a9ab |
906 | {\r |
d687ef50 |
907 | engineState = PGS_TrayMenu;\r |
908 | }\r |
909 | \r |
910 | static void emu_tray_close(void)\r |
911 | {\r |
912 | emu_status_msg("CD tray closed.");\r |
f2cf8472 |
913 | }\r |
914 | \r |
974fdb5b |
915 | void emu_32x_startup(void)\r |
916 | {\r |
c7eb229a |
917 | plat_video_toggle_renderer(0, 0); // HACK\r |
974fdb5b |
918 | system_announce();\r |
c7eb229a |
919 | \r |
920 | // force mode change event\r |
921 | rendstatus_old = -1;\r |
974fdb5b |
922 | }\r |
923 | \r |
f2cf8472 |
924 | void emu_reset_game(void)\r |
925 | {\r |
926 | PicoReset();\r |
927 | reset_timing = 1;\r |
928 | }\r |
929 | \r |
930 | void run_events_pico(unsigned int events)\r |
931 | {\r |
932 | int lim_x;\r |
933 | \r |
934 | if (events & PEV_PICO_SWINP) {\r |
c060a9ab |
935 | pico_inp_mode++;\r |
d34a42f9 |
936 | if (pico_inp_mode > 2)\r |
937 | pico_inp_mode = 0;\r |
c060a9ab |
938 | switch (pico_inp_mode) {\r |
b24e0f6c |
939 | case 2: emu_status_msg("Input: Pen on Pad"); break;\r |
940 | case 1: emu_status_msg("Input: Pen on Storyware"); break;\r |
941 | case 0: emu_status_msg("Input: Joystick");\r |
c060a9ab |
942 | PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r |
943 | break;\r |
944 | }\r |
c060a9ab |
945 | }\r |
f2cf8472 |
946 | if (events & PEV_PICO_PPREV) {\r |
c060a9ab |
947 | PicoPicohw.page--;\r |
d34a42f9 |
948 | if (PicoPicohw.page < 0)\r |
949 | PicoPicohw.page = 0;\r |
b24e0f6c |
950 | emu_status_msg("Page %i", PicoPicohw.page);\r |
c060a9ab |
951 | }\r |
f2cf8472 |
952 | if (events & PEV_PICO_PNEXT) {\r |
c060a9ab |
953 | PicoPicohw.page++;\r |
d34a42f9 |
954 | if (PicoPicohw.page > 6)\r |
955 | PicoPicohw.page = 6;\r |
b24e0f6c |
956 | emu_status_msg("Page %i", PicoPicohw.page);\r |
c060a9ab |
957 | }\r |
f2cf8472 |
958 | \r |
959 | if (pico_inp_mode == 0)\r |
960 | return;\r |
961 | \r |
962 | /* handle other input modes */\r |
963 | if (PicoPad[0] & 1) pico_pen_y--;\r |
964 | if (PicoPad[0] & 2) pico_pen_y++;\r |
965 | if (PicoPad[0] & 4) pico_pen_x--;\r |
966 | if (PicoPad[0] & 8) pico_pen_x++;\r |
967 | PicoPad[0] &= ~0x0f; // release UDLR\r |
968 | \r |
969 | lim_x = (Pico.video.reg[12]&1) ? 319 : 255;\r |
970 | if (pico_pen_y < 8)\r |
971 | pico_pen_y = 8;\r |
972 | if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y)\r |
973 | pico_pen_y = 224 - PICO_PEN_ADJUST_Y;\r |
974 | if (pico_pen_x < 0)\r |
975 | pico_pen_x = 0;\r |
976 | if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X)\r |
977 | pico_pen_x = lim_x - PICO_PEN_ADJUST_X;\r |
978 | \r |
979 | PicoPicohw.pen_pos[0] = pico_pen_x;\r |
980 | if (!(Pico.video.reg[12] & 1))\r |
981 | PicoPicohw.pen_pos[0] += pico_pen_x / 4;\r |
982 | PicoPicohw.pen_pos[0] += 0x3c;\r |
983 | PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r |
c060a9ab |
984 | }\r |
985 | \r |
d34a42f9 |
986 | static void do_turbo(int *pad, int acts)\r |
f0f0d2df |
987 | {\r |
988 | static int turbo_pad = 0;\r |
989 | static unsigned char turbo_cnt[3] = { 0, 0, 0 };\r |
990 | int inc = currentConfig.turbo_rate * 2;\r |
991 | \r |
992 | if (acts & 0x1000) {\r |
993 | turbo_cnt[0] += inc;\r |
994 | if (turbo_cnt[0] >= 60)\r |
995 | turbo_pad ^= 0x10, turbo_cnt[0] = 0;\r |
996 | }\r |
997 | if (acts & 0x2000) {\r |
998 | turbo_cnt[1] += inc;\r |
999 | if (turbo_cnt[1] >= 60)\r |
1000 | turbo_pad ^= 0x20, turbo_cnt[1] = 0;\r |
1001 | }\r |
1002 | if (acts & 0x4000) {\r |
1003 | turbo_cnt[2] += inc;\r |
1004 | if (turbo_cnt[2] >= 60)\r |
1005 | turbo_pad ^= 0x40, turbo_cnt[2] = 0;\r |
1006 | }\r |
1007 | *pad |= turbo_pad & (acts >> 8);\r |
1008 | }\r |
c060a9ab |
1009 | \r |
f2cf8472 |
1010 | static void run_events_ui(unsigned int which)\r |
d34a42f9 |
1011 | {\r |
1012 | if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))\r |
1013 | {\r |
1014 | int do_it = 1;\r |
cca8800d |
1015 | if ( emu_check_save_file(state_slot, NULL) &&\r |
fcdefcf6 |
1016 | (((which & PEV_STATE_LOAD) && (currentConfig.confirm_save & EOPT_CONFIRM_LOAD)) ||\r |
1017 | ((which & PEV_STATE_SAVE) && (currentConfig.confirm_save & EOPT_CONFIRM_SAVE))) )\r |
d34a42f9 |
1018 | {\r |
1019 | const char *nm;\r |
1020 | char tmp[64];\r |
1021 | int keys, len;\r |
1022 | \r |
1023 | strcpy(tmp, (which & PEV_STATE_LOAD) ? "LOAD STATE?" : "OVERWRITE SAVE?");\r |
1024 | len = strlen(tmp);\r |
1025 | nm = in_get_key_name(-1, -PBTN_MA3);\r |
1026 | snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);\r |
1027 | len = strlen(tmp);\r |
1028 | nm = in_get_key_name(-1, -PBTN_MBACK);\r |
1029 | snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);\r |
1030 | \r |
1031 | plat_status_msg_busy_first(tmp);\r |
1032 | \r |
45285368 |
1033 | in_set_config_int(0, IN_CFG_BLOCKING, 1);\r |
13450307 |
1034 | while (in_menu_wait_any(NULL, 50) & (PBTN_MA3|PBTN_MBACK))\r |
d34a42f9 |
1035 | ;\r |
13450307 |
1036 | while ( !((keys = in_menu_wait_any(NULL, 50)) & (PBTN_MA3|PBTN_MBACK)) )\r |
d34a42f9 |
1037 | ;\r |
1038 | if (keys & PBTN_MBACK)\r |
1039 | do_it = 0;\r |
13450307 |
1040 | while (in_menu_wait_any(NULL, 50) & (PBTN_MA3|PBTN_MBACK))\r |
d34a42f9 |
1041 | ;\r |
45285368 |
1042 | in_set_config_int(0, IN_CFG_BLOCKING, 0);\r |
d34a42f9 |
1043 | }\r |
1044 | if (do_it) {\r |
54646a39 |
1045 | plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE");\r |
d34a42f9 |
1046 | PicoStateProgressCB = plat_status_msg_busy_next;\r |
a47dd663 |
1047 | emu_save_load_game((which & PEV_STATE_LOAD) ? 1 : 0, 0);\r |
d34a42f9 |
1048 | PicoStateProgressCB = NULL;\r |
1049 | }\r |
1050 | }\r |
5a681086 |
1051 | if (which & PEV_SWITCH_RND)\r |
d34a42f9 |
1052 | {\r |
5a681086 |
1053 | plat_video_toggle_renderer(1, 0);\r |
d34a42f9 |
1054 | }\r |
1055 | if (which & (PEV_SSLOT_PREV|PEV_SSLOT_NEXT))\r |
1056 | {\r |
1057 | if (which & PEV_SSLOT_PREV) {\r |
1058 | state_slot -= 1;\r |
1059 | if (state_slot < 0)\r |
1060 | state_slot = 9;\r |
1061 | } else {\r |
1062 | state_slot += 1;\r |
1063 | if (state_slot > 9)\r |
1064 | state_slot = 0;\r |
1065 | }\r |
1066 | \r |
b24e0f6c |
1067 | emu_status_msg("SAVE SLOT %i [%s]", state_slot,\r |
cca8800d |
1068 | emu_check_save_file(state_slot, NULL) ? "USED" : "FREE");\r |
d34a42f9 |
1069 | }\r |
1070 | if (which & PEV_MENU)\r |
1071 | engineState = PGS_Menu;\r |
1072 | }\r |
1073 | \r |
1074 | void emu_update_input(void)\r |
1075 | {\r |
093b8a42 |
1076 | static int prev_events = 0;\r |
1077 | int actions[IN_BINDTYPE_COUNT] = { 0, };\r |
1078 | int pl_actions[2];\r |
1079 | int events;\r |
d34a42f9 |
1080 | \r |
093b8a42 |
1081 | in_update(actions);\r |
d34a42f9 |
1082 | \r |
093b8a42 |
1083 | pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];\r |
1084 | pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;\r |
d34a42f9 |
1085 | \r |
093b8a42 |
1086 | PicoPad[0] = pl_actions[0] & 0xfff;\r |
1087 | PicoPad[1] = pl_actions[1] & 0xfff;\r |
d34a42f9 |
1088 | \r |
093b8a42 |
1089 | if (pl_actions[0] & 0x7000)\r |
1090 | do_turbo(&PicoPad[0], pl_actions[0]);\r |
1091 | if (pl_actions[1] & 0x7000)\r |
1092 | do_turbo(&PicoPad[1], pl_actions[1]);\r |
1093 | \r |
1094 | events = actions[IN_BINDTYPE_EMU] & PEV_MASK;\r |
d34a42f9 |
1095 | \r |
1096 | // volume is treated in special way and triggered every frame\r |
1097 | if (events & (PEV_VOL_DOWN|PEV_VOL_UP))\r |
1098 | plat_update_volume(1, events & PEV_VOL_UP);\r |
1099 | \r |
093b8a42 |
1100 | if ((events ^ prev_events) & PEV_FF) {\r |
a47dd663 |
1101 | emu_set_fastforward(events & PEV_FF);\r |
d34a42f9 |
1102 | plat_update_volume(0, 0);\r |
f2cf8472 |
1103 | reset_timing = 1;\r |
d34a42f9 |
1104 | }\r |
1105 | \r |
093b8a42 |
1106 | events &= ~prev_events;\r |
d34a42f9 |
1107 | \r |
f2cf8472 |
1108 | if (PicoAHW == PAHW_PICO)\r |
1109 | run_events_pico(events);\r |
d34a42f9 |
1110 | if (events)\r |
f2cf8472 |
1111 | run_events_ui(events);\r |
d34a42f9 |
1112 | if (movie_data)\r |
1113 | update_movie();\r |
1114 | \r |
093b8a42 |
1115 | prev_events = actions[IN_BINDTYPE_EMU] & PEV_MASK;\r |
d34a42f9 |
1116 | }\r |
1117 | \r |
f2cf8472 |
1118 | static void mkdir_path(char *path_with_reserve, int pos, const char *name)\r |
1119 | {\r |
1120 | strcpy(path_with_reserve + pos, name);\r |
1121 | if (plat_is_dir(path_with_reserve))\r |
1122 | return;\r |
1123 | if (mkdir(path_with_reserve, 0777) < 0)\r |
1124 | lprintf("failed to create: %s\n", path_with_reserve);\r |
1125 | }\r |
1126 | \r |
a4edca53 |
1127 | void emu_cmn_forced_frame(int no_scale, int do_emu)\r |
1128 | {\r |
1129 | int po_old = PicoOpt;\r |
1130 | \r |
1131 | memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);\r |
1132 | \r |
8340e7c9 |
1133 | PicoOpt &= ~POPT_ALT_RENDERER;\r |
a4edca53 |
1134 | PicoOpt |= POPT_ACC_SPRITES;\r |
1135 | if (!no_scale)\r |
1136 | PicoOpt |= POPT_EN_SOFTSCALE;\r |
1137 | \r |
1138 | PicoDrawSetOutFormat(PDF_RGB555, 1);\r |
1139 | Pico.m.dirtyPal = 1;\r |
1140 | if (do_emu)\r |
1141 | PicoFrame();\r |
1142 | else\r |
1143 | PicoFrameDrawOnly();\r |
1144 | \r |
1145 | PicoOpt = po_old;\r |
1146 | }\r |
1147 | \r |
f2cf8472 |
1148 | void emu_init(void)\r |
1149 | {\r |
27701801 |
1150 | char path[512];\r |
f2cf8472 |
1151 | int pos;\r |
1152 | \r |
83ff19ec |
1153 | #if 0\r |
1154 | // FIXME: handle through menu, etc\r |
1155 | FILE *f;\r |
1156 | f = fopen("32X_M_BIOS.BIN", "rb");\r |
1157 | p32x_bios_m = malloc(2048);\r |
1158 | fread(p32x_bios_m, 1, 2048, f);\r |
1159 | fclose(f);\r |
1160 | f = fopen("32X_S_BIOS.BIN", "rb");\r |
1161 | p32x_bios_s = malloc(1024);\r |
1162 | fread(p32x_bios_s, 1, 1024, f);\r |
1163 | fclose(f);\r |
1164 | #endif\r |
1165 | \r |
f2cf8472 |
1166 | /* make dirs for saves */\r |
27701801 |
1167 | pos = plat_get_root_dir(path, sizeof(path) - 4);\r |
1168 | mkdir_path(path, pos, "mds");\r |
1169 | mkdir_path(path, pos, "srm");\r |
1170 | mkdir_path(path, pos, "brm");\r |
1171 | \r |
f6c49d38 |
1172 | pprof_init();\r |
1173 | \r |
27701801 |
1174 | make_config_cfg(path);\r |
1175 | config_readlrom(path);\r |
f2cf8472 |
1176 | \r |
1177 | PicoInit();\r |
1178 | PicoMessage = plat_status_msg_busy_next;\r |
d687ef50 |
1179 | PicoMCDopenTray = emu_tray_open;\r |
1180 | PicoMCDcloseTray = emu_tray_close;\r |
df92fbd1 |
1181 | \r |
1182 | sndout_init();\r |
f2cf8472 |
1183 | }\r |
1184 | \r |
1185 | void emu_finish(void)\r |
1186 | {\r |
1187 | // save SRAM\r |
a47dd663 |
1188 | if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r |
1189 | emu_save_load_game(0, 1);\r |
f2cf8472 |
1190 | SRam.changed = 0;\r |
1191 | }\r |
1192 | \r |
27701801 |
1193 | if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {\r |
1194 | char cfg[512];\r |
1195 | make_config_cfg(cfg);\r |
1196 | config_writelrom(cfg);\r |
1197 | #ifndef NO_SYNC\r |
1198 | sync();\r |
1199 | #endif\r |
1200 | }\r |
f2cf8472 |
1201 | \r |
f6c49d38 |
1202 | pprof_finish();\r |
1203 | \r |
f2cf8472 |
1204 | PicoExit();\r |
df92fbd1 |
1205 | sndout_exit();\r |
1206 | }\r |
1207 | \r |
1208 | static void snd_write_nonblocking(int len)\r |
1209 | {\r |
1210 | sndout_write_nb(PsndOut, len);\r |
1211 | }\r |
1212 | \r |
1213 | void emu_sound_start(void)\r |
1214 | {\r |
1215 | PsndOut = NULL;\r |
1216 | \r |
1217 | if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r |
1218 | {\r |
1219 | int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r |
1220 | \r |
1221 | PsndRerate(Pico.m.frame_count ? 1 : 0);\r |
1222 | \r |
1223 | printf("starting audio: %i len: %i stereo: %i, pal: %i\n",\r |
1224 | PsndRate, PsndLen, is_stereo, Pico.m.pal);\r |
1225 | sndout_start(PsndRate, is_stereo);\r |
1226 | PicoWriteSound = snd_write_nonblocking;\r |
1227 | plat_update_volume(0, 0);\r |
1228 | memset(sndBuffer, 0, sizeof(sndBuffer));\r |
1229 | PsndOut = sndBuffer;\r |
1230 | }\r |
1231 | }\r |
1232 | \r |
1233 | void emu_sound_stop(void)\r |
1234 | {\r |
1235 | sndout_stop();\r |
1236 | }\r |
1237 | \r |
1238 | void emu_sound_wait(void)\r |
1239 | {\r |
1240 | sndout_wait();\r |
f2cf8472 |
1241 | }\r |
1242 | \r |
d4d62665 |
1243 | static void emu_loop_prep(void)\r |
1244 | {\r |
1245 | static int pal_old = -1;\r |
1246 | static int filter_old = -1;\r |
1247 | \r |
1248 | if (currentConfig.CPUclock != plat_target_cpu_clock_get())\r |
1249 | plat_target_cpu_clock_set(currentConfig.CPUclock);\r |
1250 | \r |
1251 | if (Pico.m.pal != pal_old) {\r |
1252 | plat_target_lcdrate_set(Pico.m.pal);\r |
1253 | pal_old = Pico.m.pal;\r |
1254 | }\r |
1255 | \r |
1256 | if (currentConfig.filter != filter_old) {\r |
1257 | plat_target_hwfilter_set(currentConfig.filter);\r |
1258 | filter_old = currentConfig.filter;\r |
1259 | }\r |
1260 | \r |
1261 | plat_target_gamma_set(currentConfig.gamma, 0);\r |
1262 | \r |
1263 | pemu_loop_prep();\r |
1264 | }\r |
1265 | \r |
b24e0f6c |
1266 | static void skip_frame(int do_audio)\r |
1267 | {\r |
1268 | PicoSkipFrame = do_audio ? 1 : 2;\r |
1269 | PicoFrame();\r |
1270 | PicoSkipFrame = 0;\r |
1271 | }\r |
1272 | \r |
1273 | /* our tick here is 1 us right now */\r |
1274 | #define ms_to_ticks(x) (unsigned int)(x * 1000)\r |
1275 | #define get_ticks() plat_get_ticks_us()\r |
1276 | \r |
1277 | void emu_loop(void)\r |
1278 | {\r |
1279 | int pframes_done; /* "period" frames, used for sync */\r |
1280 | int frames_done, frames_shown; /* actual frames for fps counter */\r |
19954be1 |
1281 | int target_fps, target_frametime;\r |
b24e0f6c |
1282 | unsigned int timestamp_base = 0, timestamp_fps;\r |
1283 | char *notice_msg = NULL;\r |
1284 | char fpsbuff[24];\r |
1285 | int i;\r |
1286 | \r |
1287 | fpsbuff[0] = 0;\r |
1288 | \r |
1289 | /* make sure we are in correct mode */\r |
b24e0f6c |
1290 | Pico.m.dirtyPal = 1;\r |
19954be1 |
1291 | rendstatus_old = -1;\r |
b24e0f6c |
1292 | \r |
46bcb899 |
1293 | PicoLoopPrepare();\r |
1294 | \r |
1295 | // prepare CD buffer\r |
1296 | if (PicoAHW & PAHW_MCD)\r |
1297 | PicoCDBufferInit();\r |
1298 | \r |
636d5f25 |
1299 | plat_video_loop_prepare();\r |
d4d62665 |
1300 | emu_loop_prep();\r |
df92fbd1 |
1301 | pemu_sound_start();\r |
46bcb899 |
1302 | \r |
b24e0f6c |
1303 | /* number of ticks per frame */\r |
1304 | if (Pico.m.pal) {\r |
1305 | target_fps = 50;\r |
1306 | target_frametime = ms_to_ticks(1000) / 50;\r |
1307 | } else {\r |
1308 | target_fps = 60;\r |
1309 | target_frametime = ms_to_ticks(1000) / 60 + 1;\r |
1310 | }\r |
1311 | \r |
b24e0f6c |
1312 | timestamp_fps = get_ticks();\r |
1313 | reset_timing = 1;\r |
1314 | \r |
1315 | frames_done = frames_shown = pframes_done = 0;\r |
1316 | \r |
7b436906 |
1317 | plat_video_wait_vsync();\r |
1318 | \r |
b24e0f6c |
1319 | /* loop with resync every 1 sec. */\r |
1320 | while (engineState == PGS_Running)\r |
1321 | {\r |
1322 | unsigned int timestamp;\r |
1323 | int diff, diff_lim;\r |
b24e0f6c |
1324 | \r |
f6c49d38 |
1325 | pprof_start(main);\r |
1326 | \r |
b24e0f6c |
1327 | timestamp = get_ticks();\r |
1328 | if (reset_timing) {\r |
1329 | reset_timing = 0;\r |
1330 | timestamp_base = timestamp;\r |
1331 | pframes_done = 0;\r |
1332 | }\r |
1333 | \r |
1334 | // show notice_msg message?\r |
1335 | if (notice_msg_time != 0)\r |
1336 | {\r |
1337 | static int noticeMsgSum;\r |
54646a39 |
1338 | if (timestamp - ms_to_ticks(notice_msg_time) > ms_to_ticks(STATUS_MSG_TIMEOUT)) {\r |
b24e0f6c |
1339 | notice_msg_time = 0;\r |
1340 | plat_status_msg_clear();\r |
1341 | notice_msg = NULL;\r |
1342 | } else {\r |
1343 | int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];\r |
1344 | if (sum != noticeMsgSum) {\r |
1345 | plat_status_msg_clear();\r |
1346 | noticeMsgSum = sum;\r |
1347 | }\r |
1348 | notice_msg = noticeMsg;\r |
1349 | }\r |
1350 | }\r |
1351 | \r |
b24e0f6c |
1352 | // second changed?\r |
1353 | if (timestamp - timestamp_fps >= ms_to_ticks(1000))\r |
1354 | {\r |
1355 | #ifdef BENCHMARK\r |
1356 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r |
1357 | if (++bench == 10) {\r |
1358 | bench = 0;\r |
1359 | bench_fps_s = bench_fps;\r |
1360 | bf[bfp++ & 3] = bench_fps;\r |
1361 | bench_fps = 0;\r |
1362 | }\r |
1363 | bench_fps += frames_shown;\r |
1364 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r |
07e5dbab |
1365 | printf("%s\n", fpsbuff);\r |
b24e0f6c |
1366 | #else\r |
1367 | if (currentConfig.EmuOpt & EOPT_SHOW_FPS) {\r |
1368 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r |
1369 | if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r |
1370 | }\r |
1371 | #endif\r |
1372 | frames_shown = frames_done = 0;\r |
1373 | timestamp_fps += ms_to_ticks(1000);\r |
1374 | }\r |
1375 | #ifdef PFRAMES\r |
1376 | sprintf(fpsbuff, "%i", Pico.m.frame_count);\r |
1377 | #endif\r |
1378 | \r |
1379 | if (timestamp - timestamp_base >= ms_to_ticks(1000))\r |
1380 | {\r |
21ecaf23 |
1381 | if ((currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) && currentConfig.Frameskip >= 0)\r |
b24e0f6c |
1382 | pframes_done = 0;\r |
5a681086 |
1383 | else\r |
b24e0f6c |
1384 | pframes_done -= target_fps;\r |
5a681086 |
1385 | if (pframes_done < -2) {\r |
1386 | /* don't drag more than 2 frames behind */\r |
1387 | pframes_done = -2;\r |
1388 | timestamp_base = timestamp - 2 * target_frametime;\r |
21ecaf23 |
1389 | }\r |
5a681086 |
1390 | else\r |
1391 | timestamp_base += ms_to_ticks(1000);\r |
b24e0f6c |
1392 | }\r |
1393 | \r |
1394 | diff = timestamp - timestamp_base;\r |
1395 | diff_lim = (pframes_done + 1) * target_frametime;\r |
1396 | \r |
1397 | if (currentConfig.Frameskip >= 0) // frameskip enabled\r |
1398 | {\r |
1399 | for (i = 0; i < currentConfig.Frameskip; i++) {\r |
1400 | emu_update_input();\r |
1401 | skip_frame(1);\r |
1402 | pframes_done++; frames_done++;\r |
1403 | diff_lim += target_frametime;\r |
1404 | \r |
ae87bffa |
1405 | if (!(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT))) {\r |
b24e0f6c |
1406 | timestamp = get_ticks();\r |
1407 | diff = timestamp - timestamp_base;\r |
0d83abe2 |
1408 | if (!reset_timing && diff < diff_lim) // we are too fast\r |
b24e0f6c |
1409 | plat_wait_till_us(timestamp_base + diff_lim);\r |
1410 | }\r |
1411 | }\r |
1412 | }\r |
1413 | else if (diff > diff_lim)\r |
1414 | {\r |
1415 | /* no time left for this frame - skip */\r |
5a681086 |
1416 | /* limit auto frameskip to 8 */\r |
1417 | if (frames_done / 8 <= frames_shown) {\r |
1418 | emu_update_input();\r |
1419 | skip_frame(diff < diff_lim + target_frametime * 16);\r |
1420 | pframes_done++; frames_done++;\r |
b24e0f6c |
1421 | continue;\r |
1422 | }\r |
b24e0f6c |
1423 | }\r |
1424 | \r |
1425 | emu_update_input();\r |
1426 | PicoFrame();\r |
d08e7326 |
1427 | pemu_finalize_frame(fpsbuff, notice_msg);\r |
1428 | \r |
f6c49d38 |
1429 | // plat_video_flip();\r |
b24e0f6c |
1430 | \r |
1431 | /* frame limiter */\r |
ae87bffa |
1432 | if (!reset_timing && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))\r |
b24e0f6c |
1433 | {\r |
1434 | timestamp = get_ticks();\r |
1435 | diff = timestamp - timestamp_base;\r |
1436 | \r |
1437 | // sleep or vsync if we are still too fast\r |
1438 | if (diff < diff_lim)\r |
1439 | {\r |
1440 | // we are too fast\r |
7b436906 |
1441 | plat_wait_till_us(timestamp_base + diff_lim - target_frametime / 4);\r |
1442 | if (currentConfig.EmuOpt & EOPT_VSYNC)\r |
b24e0f6c |
1443 | plat_video_wait_vsync();\r |
b24e0f6c |
1444 | }\r |
1445 | }\r |
1446 | \r |
d08e7326 |
1447 | // XXX: for some plats it might be better to flip before vsync\r |
1448 | // (due to shadow registers in display hw)\r |
1449 | plat_video_flip();\r |
b24e0f6c |
1450 | \r |
1451 | pframes_done++; frames_done++; frames_shown++;\r |
f6c49d38 |
1452 | \r |
1453 | pprof_end(main);\r |
b24e0f6c |
1454 | }\r |
1455 | \r |
1456 | emu_set_fastforward(0);\r |
1457 | \r |
b24e0f6c |
1458 | // save SRAM\r |
1459 | if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r |
1460 | plat_status_msg_busy_first("Writing SRAM/BRAM...");\r |
1461 | emu_save_load_game(0, 1);\r |
1462 | SRam.changed = 0;\r |
1463 | }\r |
1464 | \r |
b24e0f6c |
1465 | pemu_loop_end();\r |
df92fbd1 |
1466 | emu_sound_stop();\r |
730259b7 |
1467 | \r |
1468 | // pemu_loop_end() might want to do 1 frame for bg image,\r |
1469 | // so free CD buffer here\r |
1470 | if (PicoAHW & PAHW_MCD)\r |
1471 | PicoCDBufferFree();\r |
b24e0f6c |
1472 | }\r |
1473 | \r |