bcd94522 |
1 | // (c) Copyright 2006-2009 notaz, All rights reserved.\r |
cc68a136 |
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 |
8 | #include <sys/time.h>\r |
860c6322 |
9 | #include <sys/stat.h>\r |
10 | #include <sys/types.h>\r |
cc68a136 |
11 | #include <stdarg.h>\r |
12 | \r |
0d9bf4fc |
13 | #include "plat_gp2x.h"\r |
42171343 |
14 | #include "soc.h"\r |
bcd94522 |
15 | #include "../common/plat.h"\r |
991473ad |
16 | #include "../common/menu.h"\r |
e5f426aa |
17 | #include "../common/arm_utils.h"\r |
18 | #include "../common/fonts.h"\r |
ea8c405f |
19 | #include "../common/emu.h"\r |
58c86d00 |
20 | #include "../common/config.h"\r |
e5ab6faf |
21 | #include "../linux/sndout_oss.h"\r |
713c9224 |
22 | #include "version.h"\r |
cc68a136 |
23 | \r |
efcba75f |
24 | #include <pico/pico_int.h>\r |
25 | #include <pico/patch.h>\r |
26 | #include <pico/sound/mix.h>\r |
b67ef287 |
27 | #include <zlib/zlib.h>\r |
cc68a136 |
28 | \r |
1dceadae |
29 | //#define PFRAMES\r |
cc68a136 |
30 | \r |
31 | #ifdef BENCHMARK\r |
32 | #define OSD_FPS_X 220\r |
33 | #else\r |
34 | #define OSD_FPS_X 260\r |
35 | #endif\r |
36 | \r |
cc68a136 |
37 | \r |
cc68a136 |
38 | extern int crashed_940;\r |
39 | \r |
ca61ee42 |
40 | static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r |
cc68a136 |
41 | static struct timeval noticeMsgTime = { 0, 0 }; // when started showing\r |
00bd648e |
42 | static int osd_fps_x;\r |
cc68a136 |
43 | static int gp2x_old_gamma = 100;\r |
d34a42f9 |
44 | static char noticeMsg[40];\r |
e5f426aa |
45 | unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer\r |
00bd648e |
46 | int reset_timing = 0;\r |
cc68a136 |
47 | \r |
213c16ad |
48 | #define PICO_PEN_ADJUST_X 4\r |
49 | #define PICO_PEN_ADJUST_Y 2\r |
c060a9ab |
50 | static int pico_pen_x = 320/2, pico_pen_y = 240/2;\r |
406c96c5 |
51 | \r |
ea8c405f |
52 | static void emu_msg_tray_open(void);\r |
cc68a136 |
53 | \r |
cc68a136 |
54 | \r |
d34a42f9 |
55 | void plat_status_msg(const char *format, ...)\r |
cc68a136 |
56 | {\r |
d34a42f9 |
57 | va_list vl;\r |
58 | \r |
59 | va_start(vl, format);\r |
60 | vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);\r |
61 | va_end(vl);\r |
62 | \r |
ea8c405f |
63 | gettimeofday(¬iceMsgTime, 0);\r |
cc68a136 |
64 | }\r |
65 | \r |
ca482e5d |
66 | int emu_getMainDir(char *dst, int len)\r |
8c1952f0 |
67 | {\r |
ea8c405f |
68 | extern char **g_argv;\r |
69 | int j;\r |
8c1952f0 |
70 | \r |
ea8c405f |
71 | strncpy(dst, g_argv[0], len);\r |
72 | len -= 32; // reserve\r |
73 | if (len < 0) len = 0;\r |
74 | dst[len] = 0;\r |
75 | for (j = strlen(dst); j > 0; j--)\r |
76 | if (dst[j] == '/') { dst[j+1] = 0; break; }\r |
ca482e5d |
77 | \r |
78 | return j + 1;\r |
8c1952f0 |
79 | }\r |
80 | \r |
cc68a136 |
81 | void emu_Init(void)\r |
82 | {\r |
83 | // make temp buffer for alt renderer\r |
e5f426aa |
84 | PicoDraw2FB = malloc((8+320)*(8+240+8));\r |
85 | if (!PicoDraw2FB)\r |
cc68a136 |
86 | {\r |
e5f426aa |
87 | printf("PicoDraw2FB == 0\n");\r |
cc68a136 |
88 | }\r |
89 | \r |
860c6322 |
90 | // make dirs for saves, cfgs, etc.\r |
91 | mkdir("mds", 0777);\r |
92 | mkdir("srm", 0777);\r |
93 | mkdir("brm", 0777);\r |
94 | mkdir("cfg", 0777);\r |
95 | \r |
cc68a136 |
96 | PicoInit();\r |
d34a42f9 |
97 | PicoMessage = plat_status_msg_busy_next;\r |
721cd396 |
98 | PicoMCDopenTray = emu_msg_tray_open;\r |
99 | PicoMCDcloseTray = menu_loop_tray;\r |
cc68a136 |
100 | }\r |
101 | \r |
102 | \r |
ea8c405f |
103 | static void scaling_update(void)\r |
2433f409 |
104 | {\r |
ee2a3bdf |
105 | PicoOpt &= ~(POPT_DIS_32C_BORDER|POPT_EN_SOFTSCALE);\r |
2433f409 |
106 | switch (currentConfig.scaling) {\r |
ee2a3bdf |
107 | default:break;\r |
108 | case EOPT_SCALE_HW_H:\r |
109 | case EOPT_SCALE_HW_HV:\r |
110 | PicoOpt |= POPT_DIS_32C_BORDER;\r |
111 | break;\r |
112 | case EOPT_SCALE_SW_H:\r |
113 | PicoOpt |= POPT_EN_SOFTSCALE;\r |
114 | break;\r |
2433f409 |
115 | }\r |
116 | }\r |
117 | \r |
118 | \r |
cc68a136 |
119 | void emu_Deinit(void)\r |
120 | {\r |
121 | // save SRAM\r |
122 | if((currentConfig.EmuOpt & 1) && SRam.changed) {\r |
123 | emu_SaveLoadGame(0, 1);\r |
124 | SRam.changed = 0;\r |
125 | }\r |
126 | \r |
d7dd4d66 |
127 | if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG))\r |
128 | emu_writelrom();\r |
2433f409 |
129 | \r |
e5f426aa |
130 | free(PicoDraw2FB);\r |
cc68a136 |
131 | \r |
132 | PicoExit();\r |
cc68a136 |
133 | \r |
134 | // restore gamma\r |
135 | if (gp2x_old_gamma != 100)\r |
0d9bf4fc |
136 | set_lcd_gamma(100, 0);\r |
cc68a136 |
137 | }\r |
138 | \r |
58c86d00 |
139 | void emu_prepareDefaultConfig(void)\r |
140 | {\r |
ee2a3bdf |
141 | gp2x_soc_t soc;\r |
142 | \r |
58c86d00 |
143 | memset(&defaultConfig, 0, sizeof(defaultConfig));\r |
ee2a3bdf |
144 | defaultConfig.EmuOpt = 0x9d | EOPT_RAM_TIMINGS | 0x600; // | <- confirm_save, cd_leds\r |
145 | defaultConfig.s_PicoOpt = 0x0f | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;\r |
58c86d00 |
146 | defaultConfig.s_PsndRate = 44100;\r |
147 | defaultConfig.s_PicoRegion = 0; // auto\r |
148 | defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r |
2d2247c2 |
149 | defaultConfig.s_PicoCDBuffers = 0;\r |
58c86d00 |
150 | defaultConfig.Frameskip = -1; // auto\r |
ee2a3bdf |
151 | defaultConfig.CPUclock = default_cpu_clock;\r |
58c86d00 |
152 | defaultConfig.volume = 50;\r |
58c86d00 |
153 | defaultConfig.gamma = 100;\r |
154 | defaultConfig.scaling = 0;\r |
f0f0d2df |
155 | defaultConfig.turbo_rate = 15;\r |
ee2a3bdf |
156 | \r |
157 | soc = soc_detect();\r |
158 | if (soc == SOCID_MMSP2)\r |
159 | defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r |
58c86d00 |
160 | }\r |
161 | \r |
bcd94522 |
162 | static void osd_text(int x, int y, const char *text)\r |
cc68a136 |
163 | {\r |
164 | int len = strlen(text)*8;\r |
e2de9939 |
165 | int *p, i, h, offs;\r |
cc68a136 |
166 | \r |
167 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
cc68a136 |
168 | len = (len+3) >> 2;\r |
169 | for (h = 0; h < 8; h++) {\r |
e2de9939 |
170 | offs = (x + g_screen_width * (y+h)) & ~3;\r |
171 | p = (int *) ((char *)g_screen_ptr + offs);\r |
172 | for (i = len; i; i--, p++)\r |
173 | *p = 0xe0e0e0e0;\r |
cc68a136 |
174 | }\r |
ea8c405f |
175 | emu_textOut8(x, y, text);\r |
cc68a136 |
176 | } else {\r |
cc68a136 |
177 | len = (len+1) >> 1;\r |
178 | for (h = 0; h < 8; h++) {\r |
e2de9939 |
179 | offs = (x + g_screen_width * (y+h)) & ~1;\r |
180 | p = (int *) ((short *)g_screen_ptr + offs);\r |
181 | for (i = len; i; i--, p++)\r |
182 | *p = (*p >> 2) & 0x39e7;\r |
cc68a136 |
183 | }\r |
ea8c405f |
184 | emu_textOut16(x, y, text);\r |
cc68a136 |
185 | }\r |
186 | }\r |
187 | \r |
213c16ad |
188 | static void draw_cd_leds(void)\r |
bf098bc5 |
189 | {\r |
b837b69b |
190 | int old_reg;\r |
bf098bc5 |
191 | old_reg = Pico_mcd->s68k_regs[0];\r |
192 | \r |
d34a42f9 |
193 | if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r |
bf098bc5 |
194 | // 8-bit modes\r |
195 | unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r |
196 | unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r |
e2de9939 |
197 | *(unsigned int *)((char *)g_screen_ptr + 320*2+ 4) =\r |
198 | *(unsigned int *)((char *)g_screen_ptr + 320*3+ 4) =\r |
199 | *(unsigned int *)((char *)g_screen_ptr + 320*4+ 4) = col_g;\r |
200 | *(unsigned int *)((char *)g_screen_ptr + 320*2+12) =\r |
201 | *(unsigned int *)((char *)g_screen_ptr + 320*3+12) =\r |
202 | *(unsigned int *)((char *)g_screen_ptr + 320*4+12) = col_r;\r |
bf098bc5 |
203 | } else {\r |
204 | // 16-bit modes\r |
e2de9939 |
205 | unsigned int *p = (unsigned int *)((short *)g_screen_ptr + 320*2+4);\r |
bf098bc5 |
206 | unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;\r |
207 | unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r |
4f265db7 |
208 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r |
209 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r |
9839d126 |
210 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;\r |
bf098bc5 |
211 | }\r |
212 | }\r |
213 | \r |
213c16ad |
214 | static void draw_pico_ptr(void)\r |
215 | {\r |
e2de9939 |
216 | unsigned short *p = (unsigned short *)g_screen_ptr;\r |
213c16ad |
217 | \r |
218 | // only if pen enabled and for 16bit modes\r |
219 | if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;\r |
220 | \r |
221 | if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))\r |
222 | p += 32;\r |
223 | \r |
224 | p += 320 * (pico_pen_y + PICO_PEN_ADJUST_Y);\r |
225 | p += pico_pen_x + PICO_PEN_ADJUST_X;\r |
226 | p[0] ^= 0xffff;\r |
227 | p[319] ^= 0xffff;\r |
228 | p[320] ^= 0xffff;\r |
229 | p[321] ^= 0xffff;\r |
230 | p[640] ^= 0xffff;\r |
231 | }\r |
232 | \r |
602133e1 |
233 | static int EmuScanBegin16(unsigned int num)\r |
cc68a136 |
234 | {\r |
235 | if (!(Pico.video.reg[1]&8)) num += 8;\r |
e2de9939 |
236 | DrawLineDest = (unsigned short *) g_screen_ptr + g_screen_width * num;\r |
cc68a136 |
237 | \r |
238 | return 0;\r |
239 | }\r |
240 | \r |
602133e1 |
241 | static int EmuScanBegin8(unsigned int num)\r |
cc68a136 |
242 | {\r |
243 | if (!(Pico.video.reg[1]&8)) num += 8;\r |
e2de9939 |
244 | DrawLineDest = (unsigned char *) g_screen_ptr + g_screen_width * num;\r |
cc68a136 |
245 | \r |
246 | return 0;\r |
247 | }\r |
248 | \r |
e11c5548 |
249 | int localPal[0x100];\r |
cc68a136 |
250 | static void (*vidCpyM2)(void *dest, void *src) = NULL;\r |
251 | \r |
76276b0b |
252 | static void blit(const char *fps, const char *notice)\r |
cc68a136 |
253 | {\r |
bf098bc5 |
254 | int emu_opt = currentConfig.EmuOpt;\r |
255 | \r |
d34a42f9 |
256 | if (PicoOpt & POPT_ALT_RENDERER)\r |
fad24893 |
257 | {\r |
cc68a136 |
258 | // 8bit fast renderer\r |
259 | if (Pico.m.dirtyPal) {\r |
260 | Pico.m.dirtyPal = 0;\r |
261 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
262 | // feed new palette to our device\r |
263 | gp2x_video_setpalette(localPal, 0x40);\r |
264 | }\r |
fad24893 |
265 | // a hack for VR\r |
266 | if (PicoRead16Hook == PicoSVPRead16)\r |
267 | memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r |
268 | // do actual copy\r |
e2de9939 |
269 | vidCpyM2((unsigned char *)g_screen_ptr+320*8, PicoDraw2FB+328*8);\r |
fad24893 |
270 | }\r |
d34a42f9 |
271 | else if (!(emu_opt & EOPT_16BPP))\r |
fad24893 |
272 | {\r |
cc68a136 |
273 | // 8bit accurate renderer\r |
602133e1 |
274 | if (Pico.m.dirtyPal)\r |
275 | {\r |
f8af9634 |
276 | int pallen = 0xc0;\r |
cc68a136 |
277 | Pico.m.dirtyPal = 0;\r |
e5fa9817 |
278 | if (Pico.video.reg[0xC]&8) // shadow/hilight mode\r |
279 | {\r |
cc68a136 |
280 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
281 | vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r |
282 | vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\r |
7b802576 |
283 | memcpy32(localPal+0xc0, localPal+0x40, 0x40);\r |
e5fa9817 |
284 | pallen = 0x100;\r |
285 | }\r |
e5fa9817 |
286 | else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r |
cc68a136 |
287 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
288 | vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r |
289 | vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r |
e5fa9817 |
290 | }\r |
291 | else {\r |
cc68a136 |
292 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
f8af9634 |
293 | memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess\r |
cc68a136 |
294 | }\r |
e5fa9817 |
295 | if (pallen > 0xc0) {\r |
296 | localPal[0xc0] = 0x0000c000;\r |
297 | localPal[0xd0] = 0x00c00000;\r |
298 | localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r |
299 | localPal[0xf0] = 0x00ffffff;\r |
300 | }\r |
301 | gp2x_video_setpalette(localPal, pallen);\r |
cc68a136 |
302 | }\r |
303 | }\r |
304 | \r |
2433f409 |
305 | if (notice || (emu_opt & 2)) {\r |
306 | int h = 232;\r |
ee2a3bdf |
307 | if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r |
308 | h -= 8;\r |
309 | if (notice)\r |
310 | osd_text(4, h, notice);\r |
2433f409 |
311 | if (emu_opt & 2)\r |
312 | osd_text(osd_fps_x, h, fps);\r |
313 | }\r |
602133e1 |
314 | if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r |
213c16ad |
315 | draw_cd_leds();\r |
316 | if (PicoAHW & PAHW_PICO)\r |
317 | draw_pico_ptr();\r |
cc68a136 |
318 | \r |
cc68a136 |
319 | gp2x_video_flip();\r |
320 | \r |
d34a42f9 |
321 | if (!(PicoOpt & POPT_ALT_RENDERER)) {\r |
cc68a136 |
322 | if (!(Pico.video.reg[1]&8)) {\r |
d34a42f9 |
323 | if (currentConfig.EmuOpt & EOPT_16BPP)\r |
e2de9939 |
324 | DrawLineDest = (unsigned short *) g_screen_ptr + 320*8;\r |
d34a42f9 |
325 | else\r |
e2de9939 |
326 | DrawLineDest = (unsigned char *) g_screen_ptr + 320*8;\r |
cc68a136 |
327 | } else {\r |
e2de9939 |
328 | DrawLineDest = g_screen_ptr;\r |
cc68a136 |
329 | }\r |
330 | }\r |
331 | }\r |
332 | \r |
cc68a136 |
333 | // clears whole screen or just the notice area (in all buffers)\r |
334 | static void clearArea(int full)\r |
335 | {\r |
bf098bc5 |
336 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
337 | // 8-bit renderers\r |
338 | if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
339 | else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r |
340 | } else {\r |
cc68a136 |
341 | // 16bit accurate renderer\r |
342 | if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
343 | else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r |
cc68a136 |
344 | }\r |
345 | }\r |
346 | \r |
d34a42f9 |
347 | void plat_status_msg_busy_next(const char *msg)\r |
348 | {\r |
349 | clearArea(0);\r |
350 | blit("", msg);\r |
351 | \r |
352 | /* assumption: msg_busy_next gets called only when\r |
353 | * something slow is about to happen */\r |
354 | reset_timing = 1;\r |
355 | }\r |
356 | \r |
357 | void plat_status_msg_busy_first(const char *msg)\r |
358 | {\r |
359 | gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);\r |
360 | plat_status_msg_busy_next(msg);\r |
361 | }\r |
cc68a136 |
362 | \r |
363 | static void vidResetMode(void)\r |
364 | {\r |
d34a42f9 |
365 | if (PicoOpt & POPT_ALT_RENDERER) {\r |
cc68a136 |
366 | gp2x_video_changemode(8);\r |
d34a42f9 |
367 | } else if (currentConfig.EmuOpt & EOPT_16BPP) {\r |
a12e0116 |
368 | gp2x_video_changemode(16);\r |
cc68a136 |
369 | PicoDrawSetColorFormat(1);\r |
602133e1 |
370 | PicoScanBegin = EmuScanBegin16;\r |
cc68a136 |
371 | } else {\r |
bf098bc5 |
372 | gp2x_video_changemode(8);\r |
373 | PicoDrawSetColorFormat(2);\r |
602133e1 |
374 | PicoScanBegin = EmuScanBegin8;\r |
bf098bc5 |
375 | }\r |
d34a42f9 |
376 | if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r |
bf098bc5 |
377 | // setup pal for 8-bit modes\r |
378 | localPal[0xc0] = 0x0000c000; // MCD LEDs\r |
379 | localPal[0xd0] = 0x00c00000;\r |
cc68a136 |
380 | localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r |
381 | localPal[0xf0] = 0x00ffffff;\r |
cc68a136 |
382 | gp2x_video_setpalette(localPal, 0x100);\r |
383 | gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
384 | gp2x_video_flip();\r |
cc68a136 |
385 | }\r |
386 | Pico.m.dirtyPal = 1;\r |
387 | // reset scaling\r |
ee2a3bdf |
388 | if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r |
2433f409 |
389 | gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);\r |
390 | else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r |
cc68a136 |
391 | }\r |
392 | \r |
d34a42f9 |
393 | void plat_video_toggle_renderer(void)\r |
66fdc0f0 |
394 | {\r |
d34a42f9 |
395 | if (PicoOpt & POPT_ALT_RENDERER) {\r |
396 | PicoOpt &= ~POPT_ALT_RENDERER;\r |
397 | currentConfig.EmuOpt |= EOPT_16BPP;\r |
398 | } else if (!(currentConfig.EmuOpt & EOPT_16BPP))\r |
399 | PicoOpt |= POPT_ALT_RENDERER;\r |
400 | else\r |
401 | currentConfig.EmuOpt &= ~EOPT_16BPP;\r |
7a1f6e45 |
402 | \r |
d34a42f9 |
403 | vidResetMode();\r |
66fdc0f0 |
404 | \r |
d34a42f9 |
405 | if (PicoOpt & POPT_ALT_RENDERER) {\r |
406 | plat_status_msg(" 8bit fast renderer");\r |
407 | } else if (currentConfig.EmuOpt & EOPT_16BPP) {\r |
408 | plat_status_msg("16bit accurate renderer");\r |
409 | } else {\r |
410 | plat_status_msg(" 8bit accurate renderer");\r |
411 | }\r |
fa1e5e29 |
412 | }\r |
413 | \r |
721cd396 |
414 | static void emu_msg_tray_open(void)\r |
415 | {\r |
d34a42f9 |
416 | plat_status_msg("CD tray opened");\r |
721cd396 |
417 | }\r |
418 | \r |
991473ad |
419 | static void RunEventsPico(unsigned int events)\r |
406c96c5 |
420 | {\r |
f0f0d2df |
421 | int ret, px, py, lim_x;\r |
85a4b5a4 |
422 | static int pdown_frames = 0;\r |
423 | \r |
c060a9ab |
424 | emu_RunEventsPico(events);\r |
425 | \r |
f0f0d2df |
426 | if (pico_inp_mode == 0) return;\r |
85a4b5a4 |
427 | \r |
428 | // for F200\r |
429 | ret = gp2x_touchpad_read(&px, &py);\r |
f0f0d2df |
430 | if (ret >= 0)\r |
431 | {\r |
432 | if (ret > 35000)\r |
433 | {\r |
85a4b5a4 |
434 | if (pdown_frames++ > 5)\r |
435 | PicoPad[0] |= 0x20;\r |
436 | \r |
437 | pico_pen_x = px;\r |
438 | pico_pen_y = py;\r |
439 | if (!(Pico.video.reg[12]&1)) {\r |
440 | pico_pen_x -= 32;\r |
441 | if (pico_pen_x < 0) pico_pen_x = 0;\r |
442 | if (pico_pen_x > 248) pico_pen_x = 248;\r |
443 | }\r |
444 | if (pico_pen_y > 224) pico_pen_y = 224;\r |
445 | }\r |
446 | else\r |
f0f0d2df |
447 | pdown_frames = 0;\r |
85a4b5a4 |
448 | \r |
449 | //if (ret == 0)\r |
450 | // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r |
451 | }\r |
f0f0d2df |
452 | \r |
991473ad |
453 | if (PicoPad[0] & 1) pico_pen_y--;\r |
454 | if (PicoPad[0] & 2) pico_pen_y++;\r |
455 | if (PicoPad[0] & 4) pico_pen_x--;\r |
456 | if (PicoPad[0] & 8) pico_pen_x++;\r |
f0f0d2df |
457 | PicoPad[0] &= ~0x0f; // release UDLR\r |
f0f0d2df |
458 | \r |
459 | lim_x = (Pico.video.reg[12]&1) ? 319 : 255;\r |
460 | if (pico_pen_y < 8) pico_pen_y = 8;\r |
461 | if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y;\r |
462 | if (pico_pen_x < 0) pico_pen_x = 0;\r |
463 | if (pico_pen_x > lim_x-PICO_PEN_ADJUST_X) pico_pen_x = lim_x-PICO_PEN_ADJUST_X;\r |
464 | \r |
465 | PicoPicohw.pen_pos[0] = pico_pen_x;\r |
466 | if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;\r |
467 | PicoPicohw.pen_pos[0] += 0x3c;\r |
468 | PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r |
406c96c5 |
469 | }\r |
470 | \r |
d34a42f9 |
471 | void plat_update_volume(int has_changed, int is_up)\r |
4a32f01f |
472 | {\r |
473 | static int prev_frame = 0, wait_frames = 0;\r |
474 | int vol = currentConfig.volume;\r |
d34a42f9 |
475 | int need_low_volume = 0;\r |
476 | gp2x_soc_t soc;\r |
477 | \r |
478 | soc = soc_detect();\r |
479 | if ((PicoOpt & POPT_EN_STEREO) && soc == SOCID_MMSP2)\r |
480 | need_low_volume = 1;\r |
4a32f01f |
481 | \r |
482 | if (has_changed)\r |
483 | {\r |
d34a42f9 |
484 | if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r |
4a32f01f |
485 | wait_frames++;\r |
486 | else {\r |
487 | if (is_up) {\r |
488 | if (vol < 99) vol++;\r |
489 | } else {\r |
490 | if (vol > 0) vol--;\r |
491 | }\r |
492 | wait_frames = 0;\r |
e5ab6faf |
493 | sndout_oss_setvol(vol, vol);\r |
4a32f01f |
494 | currentConfig.volume = vol;\r |
495 | }\r |
d34a42f9 |
496 | plat_status_msg("VOL: %02i", vol);\r |
4a32f01f |
497 | prev_frame = Pico.m.frame_count;\r |
498 | }\r |
499 | \r |
d34a42f9 |
500 | if (need_low_volume)\r |
501 | return;\r |
502 | \r |
503 | /* set the right mixer func */\r |
4a32f01f |
504 | if (vol >= 5)\r |
505 | PsndMix_32_to_16l = mix_32_to_16l_stereo;\r |
506 | else {\r |
507 | mix_32_to_16l_level = 5 - vol;\r |
508 | PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r |
509 | }\r |
510 | }\r |
511 | \r |
cc68a136 |
512 | \r |
7a93adeb |
513 | static void updateSound(int len)\r |
cc68a136 |
514 | {\r |
7a93adeb |
515 | if (PicoOpt&8) len<<=1;\r |
cc68a136 |
516 | \r |
01bc6b19 |
517 | /* avoid writing audio when lagging behind to prevent audio lag */\r |
518 | if (PicoSkipFrame != 2)\r |
e5ab6faf |
519 | sndout_oss_write(PsndOut, len<<1);\r |
cc68a136 |
520 | }\r |
521 | \r |
7b3f44c6 |
522 | void emu_startSound(void)\r |
523 | {\r |
524 | static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r |
525 | int target_fps = Pico.m.pal ? 50 : 60;\r |
526 | \r |
527 | PsndOut = NULL;\r |
528 | \r |
529 | // prepare sound stuff\r |
530 | if (currentConfig.EmuOpt & 4)\r |
531 | {\r |
532 | int snd_excess_add;\r |
533 | if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r |
534 | ((PicoOpt&0x200) && crashed_940)) {\r |
535 | PsndRerate(Pico.m.frame_count ? 1 : 0);\r |
536 | }\r |
537 | snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r |
538 | printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r |
539 | PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r |
e5ab6faf |
540 | sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);\r |
541 | sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r |
7b3f44c6 |
542 | PicoWriteSound = updateSound;\r |
d34a42f9 |
543 | plat_update_volume(0, 0);\r |
7b3f44c6 |
544 | memset(sndBuffer, 0, sizeof(sndBuffer));\r |
545 | PsndOut = sndBuffer;\r |
546 | PsndRate_old = PsndRate;\r |
547 | PicoOpt_old = PicoOpt;\r |
548 | pal_old = Pico.m.pal;\r |
549 | }\r |
550 | }\r |
551 | \r |
552 | void emu_endSound(void)\r |
553 | {\r |
554 | }\r |
555 | \r |
556 | /* wait until we can write more sound */\r |
557 | void emu_waitSound(void)\r |
558 | {\r |
559 | // don't need to do anything, writes will block by themselves\r |
560 | }\r |
561 | \r |
cc68a136 |
562 | \r |
01bc6b19 |
563 | static void SkipFrame(int do_audio)\r |
cc68a136 |
564 | {\r |
01bc6b19 |
565 | PicoSkipFrame=do_audio ? 1 : 2;\r |
cc68a136 |
566 | PicoFrame();\r |
567 | PicoSkipFrame=0;\r |
cc68a136 |
568 | }\r |
569 | \r |
570 | \r |
0fc0e241 |
571 | void emu_forcedFrame(int opts)\r |
860c6322 |
572 | {\r |
573 | int po_old = PicoOpt;\r |
a12e0116 |
574 | int eo_old = currentConfig.EmuOpt;\r |
575 | \r |
0fc0e241 |
576 | PicoOpt &= ~0x10;\r |
577 | PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites\r |
a12e0116 |
578 | currentConfig.EmuOpt |= 0x80;\r |
860c6322 |
579 | \r |
a12e0116 |
580 | //vidResetMode();\r |
581 | PicoDrawSetColorFormat(1);\r |
602133e1 |
582 | PicoScanBegin = EmuScanBegin16;\r |
a12e0116 |
583 | Pico.m.dirtyPal = 1;\r |
584 | PicoFrameDrawOnly();\r |
860c6322 |
585 | \r |
a12e0116 |
586 | /*\r |
860c6322 |
587 | if (!(Pico.video.reg[12]&1)) {\r |
68cba51e |
588 | vidCpyM2 = vidCpyM2_32col;\r |
860c6322 |
589 | clearArea(1);\r |
68cba51e |
590 | } else vidCpyM2 = vidCpyM2_40col;\r |
860c6322 |
591 | \r |
e2de9939 |
592 | vidCpyM2((unsigned char *)g_screen_ptr+320*8, PicoDraw2FB+328*8);\r |
860c6322 |
593 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
594 | gp2x_video_setpalette(localPal, 0x40);\r |
a12e0116 |
595 | */\r |
48e8482f |
596 | PicoOpt = po_old;\r |
a12e0116 |
597 | currentConfig.EmuOpt = eo_old;\r |
860c6322 |
598 | }\r |
599 | \r |
fcf94fcc |
600 | void emu_platformDebugCat(char *str)\r |
601 | {\r |
602 | // nothing\r |
603 | }\r |
604 | \r |
cc68a136 |
605 | static void simpleWait(int thissec, int lim_time)\r |
606 | {\r |
607 | struct timeval tval;\r |
608 | \r |
609 | spend_cycles(1024);\r |
610 | gettimeofday(&tval, 0);\r |
da310283 |
611 | if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
cc68a136 |
612 | \r |
da310283 |
613 | while (tval.tv_usec < lim_time)\r |
cc68a136 |
614 | {\r |
615 | spend_cycles(1024);\r |
616 | gettimeofday(&tval, 0);\r |
da310283 |
617 | if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
cc68a136 |
618 | }\r |
619 | }\r |
620 | \r |
621 | \r |
da310283 |
622 | #if 0\r |
623 | static void tga_dump(void)\r |
624 | {\r |
625 | #define BYTE unsigned char\r |
626 | #define WORD unsigned short\r |
627 | struct\r |
628 | {\r |
629 | BYTE IDLength; /* 00h Size of Image ID field */\r |
630 | BYTE ColorMapType; /* 01h Color map type */\r |
631 | BYTE ImageType; /* 02h Image type code */\r |
632 | WORD CMapStart; /* 03h Color map origin */\r |
633 | WORD CMapLength; /* 05h Color map length */\r |
634 | BYTE CMapDepth; /* 07h Depth of color map entries */\r |
635 | WORD XOffset; /* 08h X origin of image */\r |
636 | WORD YOffset; /* 0Ah Y origin of image */\r |
637 | WORD Width; /* 0Ch Width of image */\r |
638 | WORD Height; /* 0Eh Height of image */\r |
639 | BYTE PixelDepth; /* 10h Image pixel size */\r |
640 | BYTE ImageDescriptor; /* 11h Image descriptor byte */\r |
641 | } __attribute__((packed)) TGAHEAD;\r |
642 | static unsigned short oldscr[320*240];\r |
643 | FILE *f; char name[128]; int i;\r |
644 | \r |
645 | memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r |
646 | TGAHEAD.ImageType = 2;\r |
647 | TGAHEAD.Width = 320;\r |
648 | TGAHEAD.Height = 240;\r |
649 | TGAHEAD.PixelDepth = 16;\r |
650 | TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r |
651 | \r |
652 | #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r |
653 | \r |
654 | for (i = 0; i < 320*240; i++)\r |
e2de9939 |
655 | if(oldscr[i] != CONV(((unsigned short *)g_screen_ptr)[i])) break;\r |
da310283 |
656 | if (i < 320*240)\r |
657 | {\r |
658 | for (i = 0; i < 320*240; i++)\r |
e2de9939 |
659 | oldscr[i] = CONV(((unsigned short *)g_screen_ptr)[i]);\r |
da310283 |
660 | sprintf(name, "%05i.tga", Pico.m.frame_count);\r |
661 | f = fopen(name, "wb");\r |
662 | if (!f) { printf("!f\n"); exit(1); }\r |
663 | fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r |
664 | fwrite(oldscr, 1, 320*240*2, f);\r |
665 | fclose(f);\r |
666 | }\r |
667 | }\r |
668 | #endif\r |
669 | \r |
670 | \r |
cc68a136 |
671 | void emu_Loop(void)\r |
672 | {\r |
0d9bf4fc |
673 | static int gp2x_old_clock = -1, EmuOpt_old = 0;\r |
cc68a136 |
674 | char fpsbuff[24]; // fps count c string\r |
675 | struct timeval tval; // timing\r |
da310283 |
676 | int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r |
677 | int frames_done, frames_shown, thissec; // actual frames\r |
678 | int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;\r |
cc68a136 |
679 | char *notice = 0;\r |
680 | \r |
681 | printf("entered emu_Loop()\n");\r |
682 | \r |
0d9bf4fc |
683 | if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {\r |
684 | if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)\r |
ee2a3bdf |
685 | set_ram_timings();\r |
0d9bf4fc |
686 | else\r |
687 | unset_ram_timings();\r |
688 | }\r |
689 | \r |
690 | if (gp2x_old_clock < 0)\r |
691 | gp2x_old_clock = default_cpu_clock;\r |
cc68a136 |
692 | if (gp2x_old_clock != currentConfig.CPUclock) {\r |
693 | printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r |
0d9bf4fc |
694 | gp2x_set_cpuclk(currentConfig.CPUclock);\r |
cc68a136 |
695 | gp2x_old_clock = currentConfig.CPUclock;\r |
696 | printf(" done\n");\r |
697 | }\r |
698 | \r |
82bc9cdd |
699 | if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r |
0d9bf4fc |
700 | set_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r |
cc68a136 |
701 | gp2x_old_gamma = currentConfig.gamma;\r |
82bc9cdd |
702 | printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r |
cc68a136 |
703 | }\r |
704 | \r |
0d9bf4fc |
705 | if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_PSYNC) {\r |
706 | if (currentConfig.EmuOpt & EOPT_PSYNC)\r |
707 | set_lcd_custom_rate(Pico.m.pal);\r |
708 | else\r |
709 | unset_lcd_custom_rate();\r |
aae35e84 |
710 | }\r |
711 | \r |
0d9bf4fc |
712 | if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_MMUHACK)\r |
713 | gp2x_make_fb_bufferable(currentConfig.EmuOpt & EOPT_MMUHACK);\r |
714 | \r |
aae35e84 |
715 | EmuOpt_old = currentConfig.EmuOpt;\r |
cc68a136 |
716 | fpsbuff[0] = 0;\r |
717 | \r |
718 | // make sure we are in correct mode\r |
719 | vidResetMode();\r |
ea8c405f |
720 | scaling_update();\r |
e11c5548 |
721 | Pico.m.dirtyPal = 1;\r |
cc68a136 |
722 | oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r |
cc68a136 |
723 | \r |
724 | // pal/ntsc might have changed, reset related stuff\r |
725 | target_fps = Pico.m.pal ? 50 : 60;\r |
726 | target_frametime = 1000000/target_fps;\r |
727 | reset_timing = 1;\r |
728 | \r |
7b3f44c6 |
729 | emu_startSound();\r |
cc68a136 |
730 | \r |
0a051f55 |
731 | // prepare CD buffer\r |
602133e1 |
732 | if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r |
0a051f55 |
733 | \r |
aae35e84 |
734 | // calc vsync offset to sync timing code with vsync\r |
735 | if (currentConfig.EmuOpt&0x2000) {\r |
736 | gettimeofday(&tval, 0);\r |
737 | gp2x_video_wait_vsync();\r |
738 | gettimeofday(&tval, 0);\r |
739 | vsync_offset = tval.tv_usec;\r |
740 | while (vsync_offset >= target_frametime)\r |
741 | vsync_offset -= target_frametime;\r |
742 | if (!vsync_offset) vsync_offset++;\r |
743 | printf("vsync_offset: %i\n", vsync_offset);\r |
744 | } else\r |
745 | vsync_offset = 0;\r |
746 | \r |
da310283 |
747 | frames_done = frames_shown = thissec =\r |
748 | pframes_done = pframes_shown = pthissec = 0;\r |
749 | \r |
750 | // loop\r |
cc68a136 |
751 | while (engineState == PGS_Running)\r |
752 | {\r |
753 | int modes;\r |
754 | \r |
755 | gettimeofday(&tval, 0);\r |
4a32f01f |
756 | if (reset_timing) {\r |
cc68a136 |
757 | reset_timing = 0;\r |
da310283 |
758 | pthissec = tval.tv_sec;\r |
759 | pframes_shown = pframes_done = tval.tv_usec/target_frametime;\r |
cc68a136 |
760 | }\r |
761 | \r |
762 | // show notice message?\r |
4a32f01f |
763 | if (noticeMsgTime.tv_sec)\r |
764 | {\r |
cc68a136 |
765 | static int noticeMsgSum;\r |
766 | if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r |
767 | noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r |
768 | clearArea(0);\r |
769 | notice = 0;\r |
770 | } else {\r |
771 | int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r |
772 | if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r |
773 | notice = noticeMsg;\r |
774 | }\r |
775 | }\r |
776 | \r |
777 | // check for mode changes\r |
778 | modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r |
4a32f01f |
779 | if (modes != oldmodes)\r |
780 | {\r |
cc68a136 |
781 | int scalex = 320;\r |
782 | osd_fps_x = OSD_FPS_X;\r |
783 | if (modes & 4) {\r |
784 | vidCpyM2 = vidCpyM2_40col;\r |
785 | } else {\r |
786 | if (PicoOpt & 0x100) {\r |
787 | vidCpyM2 = vidCpyM2_32col_nobord;\r |
788 | scalex = 256;\r |
789 | osd_fps_x = OSD_FPS_X - 64;\r |
790 | } else {\r |
791 | vidCpyM2 = vidCpyM2_32col;\r |
792 | }\r |
793 | }\r |
ee2a3bdf |
794 | /* want vertical scaling and game is not in 240 line mode */\r |
795 | if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(modes&8))\r |
2433f409 |
796 | gp2x_video_RGB_setscaling(8, scalex, 224);\r |
797 | else gp2x_video_RGB_setscaling(0, scalex, 240);\r |
cc68a136 |
798 | oldmodes = modes;\r |
799 | clearArea(1);\r |
800 | }\r |
801 | \r |
802 | // second changed?\r |
4a32f01f |
803 | if (thissec != tval.tv_sec)\r |
804 | {\r |
cc68a136 |
805 | #ifdef BENCHMARK\r |
806 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r |
4a32f01f |
807 | if (++bench == 10) {\r |
cc68a136 |
808 | bench = 0;\r |
809 | bench_fps_s = bench_fps;\r |
810 | bf[bfp++ & 3] = bench_fps;\r |
811 | bench_fps = 0;\r |
812 | }\r |
813 | bench_fps += frames_shown;\r |
814 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r |
815 | #else\r |
1b13dae0 |
816 | if (currentConfig.EmuOpt & 2) {\r |
cc68a136 |
817 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r |
1b13dae0 |
818 | if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r |
819 | }\r |
cc68a136 |
820 | #endif\r |
da310283 |
821 | frames_shown = frames_done = 0;\r |
cc68a136 |
822 | thissec = tval.tv_sec;\r |
da310283 |
823 | }\r |
824 | #ifdef PFRAMES\r |
825 | sprintf(fpsbuff, "%i", Pico.m.frame_count);\r |
826 | #endif\r |
cc68a136 |
827 | \r |
da310283 |
828 | if (pthissec != tval.tv_sec)\r |
829 | {\r |
4a32f01f |
830 | if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r |
da310283 |
831 | pframes_done = pframes_shown = 0;\r |
cc68a136 |
832 | } else {\r |
833 | // it is quite common for this implementation to leave 1 fame unfinished\r |
834 | // when second changes, but we don't want buffer to starve.\r |
d34a42f9 |
835 | if (PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r |
836 | emu_update_input();\r |
da310283 |
837 | SkipFrame(1); pframes_done++;\r |
cc68a136 |
838 | }\r |
839 | \r |
da310283 |
840 | pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;\r |
841 | pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;\r |
842 | if (pframes_shown > pframes_done) pframes_shown = pframes_done;\r |
cc68a136 |
843 | }\r |
da310283 |
844 | pthissec = tval.tv_sec;\r |
cc68a136 |
845 | }\r |
aae35e84 |
846 | \r |
da310283 |
847 | lim_time = (pframes_done+1) * target_frametime + vsync_offset;\r |
848 | if (currentConfig.Frameskip >= 0) // frameskip enabled\r |
849 | {\r |
cc68a136 |
850 | for(i = 0; i < currentConfig.Frameskip; i++) {\r |
d34a42f9 |
851 | emu_update_input();\r |
da310283 |
852 | SkipFrame(1); pframes_done++; frames_done++;\r |
4a32f01f |
853 | if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r |
cc68a136 |
854 | gettimeofday(&tval, 0);\r |
da310283 |
855 | if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
856 | if (tval.tv_usec < lim_time) { // we are too fast\r |
857 | simpleWait(pthissec, lim_time);\r |
cc68a136 |
858 | }\r |
859 | }\r |
860 | lim_time += target_frametime;\r |
861 | }\r |
da310283 |
862 | }\r |
863 | else if (tval.tv_usec > lim_time) // auto frameskip\r |
864 | {\r |
cc68a136 |
865 | // no time left for this frame - skip\r |
ea8c405f |
866 | if (tval.tv_usec - lim_time >= 300000) {\r |
4ff2d527 |
867 | /* something caused a slowdown for us (disk access? cache flush?)\r |
868 | * try to recover by resetting timing... */\r |
869 | reset_timing = 1;\r |
870 | continue;\r |
871 | }\r |
d34a42f9 |
872 | emu_update_input();\r |
da310283 |
873 | SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r |
cc68a136 |
874 | continue;\r |
875 | }\r |
876 | \r |
d34a42f9 |
877 | emu_update_input();\r |
cc68a136 |
878 | PicoFrame();\r |
879 | \r |
880 | // check time\r |
881 | gettimeofday(&tval, 0);\r |
da310283 |
882 | if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
cc68a136 |
883 | \r |
ea8c405f |
884 | if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r |
4ff2d527 |
885 | reset_timing = 1;\r |
886 | else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r |
cc68a136 |
887 | {\r |
aae35e84 |
888 | // sleep or vsync if we are still too fast\r |
cc68a136 |
889 | // usleep sleeps for ~20ms minimum, so it is not a solution here\r |
4a32f01f |
890 | if (!reset_timing && tval.tv_usec < lim_time)\r |
cc68a136 |
891 | {\r |
892 | // we are too fast\r |
aae35e84 |
893 | if (vsync_offset) {\r |
894 | if (lim_time - tval.tv_usec > target_frametime/2)\r |
da310283 |
895 | simpleWait(pthissec, lim_time - target_frametime/4);\r |
aae35e84 |
896 | gp2x_video_wait_vsync();\r |
897 | } else {\r |
da310283 |
898 | simpleWait(pthissec, lim_time);\r |
aae35e84 |
899 | }\r |
cc68a136 |
900 | }\r |
901 | }\r |
902 | \r |
903 | blit(fpsbuff, notice);\r |
904 | \r |
da310283 |
905 | pframes_done++; pframes_shown++;\r |
906 | frames_done++; frames_shown++;\r |
cc68a136 |
907 | }\r |
908 | \r |
c060a9ab |
909 | emu_changeFastForward(0);\r |
0a051f55 |
910 | \r |
d34a42f9 |
911 | if (PicoAHW & PAHW_MCD)\r |
912 | PicoCDBufferFree();\r |
0a051f55 |
913 | \r |
cc68a136 |
914 | // save SRAM\r |
d34a42f9 |
915 | if ((currentConfig.EmuOpt & EOPT_USE_SRAM) && SRam.changed) {\r |
916 | plat_status_msg_busy_first("Writing SRAM/BRAM...");\r |
cc68a136 |
917 | emu_SaveLoadGame(0, 1);\r |
918 | SRam.changed = 0;\r |
919 | }\r |
e11c5548 |
920 | \r |
a12e0116 |
921 | // if in 8bit mode, generate 16bit image for menu background\r |
d34a42f9 |
922 | if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP))\r |
0fc0e241 |
923 | emu_forcedFrame(POPT_EN_SOFTSCALE);\r |
cc68a136 |
924 | }\r |
925 | \r |
926 | \r |
927 | void emu_ResetGame(void)\r |
928 | {\r |
1cb1584b |
929 | PicoReset();\r |
cc68a136 |
930 | reset_timing = 1;\r |
931 | }\r |
932 | \r |
713c9224 |
933 | const char *plat_get_credits(void)\r |
934 | {\r |
935 | return "PicoDrive v" VERSION " (c) notaz, 2006-2009\n\n\n"\r |
936 | "Credits:\n"\r |
937 | "fDave: Cyclone 68000 core,\n"\r |
938 | " base code of PicoDrive\n"\r |
939 | "Reesy & FluBBa: DrZ80 core\n"\r |
940 | "MAME devs: YM2612 and SN76496 cores\n"\r |
941 | "rlyeh and others: minimal SDK\n"\r |
42171343 |
942 | "Squidge: mmuhack\n"\r |
713c9224 |
943 | "Dzz: ARM940 sample\n"\r |
944 | "GnoStiC / Puck2099: USB joy code\n"\r |
945 | "craigix: GP2X hardware\n"\r |
946 | "ketchupgun: skin design\n"\r |
947 | "\n"\r |
948 | "special thanks (for docs, ideas):\n"\r |
949 | " Charles MacDonald, Haze,\n"\r |
950 | " Stephane Dallongeville,\n"\r |
951 | " Lordus, Exophase, Rokas,\n"\r |
952 | " Nemesis, Tasco Deluxe";\r |
953 | }\r |