6cadc2da |
1 | // (c) Copyright 2006-2007 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 <linux/limits.h>\r |
12 | #include <ctype.h>\r |
13 | #include <unistd.h>\r |
14 | \r |
15 | #include <stdarg.h>\r |
16 | \r |
17 | #include "emu.h"\r |
18 | #include "gp2x.h"\r |
19 | #include "usbjoy.h"\r |
20 | #include "menu.h"\r |
e5f426aa |
21 | #include "../common/arm_utils.h"\r |
22 | #include "../common/fonts.h"\r |
ea8c405f |
23 | #include "../common/emu.h"\r |
58c86d00 |
24 | #include "../common/config.h"\r |
cc68a136 |
25 | #include "cpuctrl.h"\r |
26 | \r |
b67ef287 |
27 | #include <Pico/PicoInt.h>\r |
28 | #include <Pico/Patch.h>\r |
4a32f01f |
29 | #include <Pico/sound/mix.h>\r |
b67ef287 |
30 | #include <zlib/zlib.h>\r |
cc68a136 |
31 | \r |
1dceadae |
32 | //#define PFRAMES\r |
cc68a136 |
33 | \r |
34 | #ifdef BENCHMARK\r |
35 | #define OSD_FPS_X 220\r |
36 | #else\r |
37 | #define OSD_FPS_X 260\r |
38 | #endif\r |
39 | \r |
cc68a136 |
40 | \r |
41 | int engineState;\r |
42 | int select_exits = 0;\r |
cc68a136 |
43 | \r |
44 | char romFileName[PATH_MAX];\r |
cc68a136 |
45 | \r |
46 | extern int crashed_940;\r |
47 | \r |
ca61ee42 |
48 | static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r |
cc68a136 |
49 | static struct timeval noticeMsgTime = { 0, 0 }; // when started showing\r |
00bd648e |
50 | static int osd_fps_x;\r |
cc68a136 |
51 | static int gp2x_old_gamma = 100;\r |
e5f426aa |
52 | unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer\r |
00bd648e |
53 | int reset_timing = 0;\r |
cc68a136 |
54 | \r |
213c16ad |
55 | #define PICO_PEN_ADJUST_X 4\r |
56 | #define PICO_PEN_ADJUST_Y 2\r |
c060a9ab |
57 | static int pico_pen_x = 320/2, pico_pen_y = 240/2;\r |
406c96c5 |
58 | \r |
ea8c405f |
59 | static void emu_msg_cb(const char *msg);\r |
60 | static void emu_msg_tray_open(void);\r |
cc68a136 |
61 | \r |
cc68a136 |
62 | \r |
ea8c405f |
63 | void emu_noticeMsgUpdated(void)\r |
cc68a136 |
64 | {\r |
ea8c405f |
65 | gettimeofday(¬iceMsgTime, 0);\r |
cc68a136 |
66 | }\r |
67 | \r |
ca482e5d |
68 | int emu_getMainDir(char *dst, int len)\r |
8c1952f0 |
69 | {\r |
ea8c405f |
70 | extern char **g_argv;\r |
71 | int j;\r |
8c1952f0 |
72 | \r |
ea8c405f |
73 | strncpy(dst, g_argv[0], len);\r |
74 | len -= 32; // reserve\r |
75 | if (len < 0) len = 0;\r |
76 | dst[len] = 0;\r |
77 | for (j = strlen(dst); j > 0; j--)\r |
78 | if (dst[j] == '/') { dst[j+1] = 0; break; }\r |
ca482e5d |
79 | \r |
80 | return j + 1;\r |
8c1952f0 |
81 | }\r |
82 | \r |
cc68a136 |
83 | void emu_Init(void)\r |
84 | {\r |
85 | // make temp buffer for alt renderer\r |
e5f426aa |
86 | PicoDraw2FB = malloc((8+320)*(8+240+8));\r |
87 | if (!PicoDraw2FB)\r |
cc68a136 |
88 | {\r |
e5f426aa |
89 | printf("PicoDraw2FB == 0\n");\r |
cc68a136 |
90 | }\r |
91 | \r |
860c6322 |
92 | // make dirs for saves, cfgs, etc.\r |
93 | mkdir("mds", 0777);\r |
94 | mkdir("srm", 0777);\r |
95 | mkdir("brm", 0777);\r |
96 | mkdir("cfg", 0777);\r |
97 | \r |
cc68a136 |
98 | PicoInit();\r |
66fdc0f0 |
99 | PicoMessage = emu_msg_cb;\r |
721cd396 |
100 | PicoMCDopenTray = emu_msg_tray_open;\r |
101 | PicoMCDcloseTray = menu_loop_tray;\r |
cc68a136 |
102 | }\r |
103 | \r |
104 | \r |
ea8c405f |
105 | static void scaling_update(void)\r |
2433f409 |
106 | {\r |
107 | PicoOpt &= ~0x4100;\r |
108 | switch (currentConfig.scaling) {\r |
109 | default: break; // off\r |
110 | case 1: // hw hor\r |
111 | case 2: PicoOpt |= 0x0100; break; // hw hor+vert\r |
112 | case 3: PicoOpt |= 0x4000; break; // sw hor\r |
113 | }\r |
114 | }\r |
115 | \r |
116 | \r |
cc68a136 |
117 | void emu_Deinit(void)\r |
118 | {\r |
119 | // save SRAM\r |
120 | if((currentConfig.EmuOpt & 1) && SRam.changed) {\r |
121 | emu_SaveLoadGame(0, 1);\r |
122 | SRam.changed = 0;\r |
123 | }\r |
124 | \r |
2433f409 |
125 | if (!(currentConfig.EmuOpt & 0x20)) {\r |
58c86d00 |
126 | config_writelrom(PicoConfigFile);\r |
2433f409 |
127 | #ifndef NO_SYNC\r |
58c86d00 |
128 | sync();\r |
2433f409 |
129 | #endif\r |
2433f409 |
130 | }\r |
131 | \r |
e5f426aa |
132 | free(PicoDraw2FB);\r |
cc68a136 |
133 | \r |
134 | PicoExit();\r |
cc68a136 |
135 | \r |
136 | // restore gamma\r |
137 | if (gp2x_old_gamma != 100)\r |
82bc9cdd |
138 | set_gamma(100, 0);\r |
cc68a136 |
139 | }\r |
140 | \r |
58c86d00 |
141 | void emu_prepareDefaultConfig(void)\r |
142 | {\r |
143 | memset(&defaultConfig, 0, sizeof(defaultConfig));\r |
4aed476f |
144 | defaultConfig.EmuOpt = 0x9d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds\r |
0fc0e241 |
145 | defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|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 |
151 | defaultConfig.CPUclock = 200;\r |
152 | defaultConfig.volume = 50;\r |
153 | defaultConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU\r |
154 | defaultConfig.KeyBinds[ 4] = 1<<1;\r |
155 | defaultConfig.KeyBinds[ 2] = 1<<2;\r |
156 | defaultConfig.KeyBinds[ 6] = 1<<3;\r |
157 | defaultConfig.KeyBinds[14] = 1<<4;\r |
158 | defaultConfig.KeyBinds[13] = 1<<5;\r |
159 | defaultConfig.KeyBinds[12] = 1<<6;\r |
160 | defaultConfig.KeyBinds[ 8] = 1<<7;\r |
161 | defaultConfig.KeyBinds[15] = 1<<26; // switch rend\r |
162 | defaultConfig.KeyBinds[10] = 1<<27; // save state\r |
163 | defaultConfig.KeyBinds[11] = 1<<28; // load state\r |
164 | defaultConfig.KeyBinds[23] = 1<<29; // vol up\r |
165 | defaultConfig.KeyBinds[22] = 1<<30; // vol down\r |
166 | defaultConfig.gamma = 100;\r |
167 | defaultConfig.scaling = 0;\r |
f0f0d2df |
168 | defaultConfig.turbo_rate = 15;\r |
58c86d00 |
169 | }\r |
170 | \r |
76276b0b |
171 | void osd_text(int x, int y, const char *text)\r |
cc68a136 |
172 | {\r |
173 | int len = strlen(text)*8;\r |
174 | \r |
175 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
bf098bc5 |
176 | int *p, i, h;\r |
cc68a136 |
177 | x &= ~3; // align x\r |
178 | len = (len+3) >> 2;\r |
179 | for (h = 0; h < 8; h++) {\r |
180 | p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));\r |
bf098bc5 |
181 | for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r |
cc68a136 |
182 | }\r |
ea8c405f |
183 | emu_textOut8(x, y, text);\r |
cc68a136 |
184 | } else {\r |
185 | int *p, i, h;\r |
186 | x &= ~1; // align x\r |
187 | len = (len+1) >> 1;\r |
188 | for (h = 0; h < 8; h++) {\r |
189 | p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));\r |
190 | for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;\r |
191 | }\r |
ea8c405f |
192 | emu_textOut16(x, y, text);\r |
cc68a136 |
193 | }\r |
194 | }\r |
195 | \r |
213c16ad |
196 | static void draw_cd_leds(void)\r |
bf098bc5 |
197 | {\r |
b837b69b |
198 | // static\r |
199 | int old_reg;\r |
9839d126 |
200 | // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?\r |
bf098bc5 |
201 | old_reg = Pico_mcd->s68k_regs[0];\r |
202 | \r |
203 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
204 | // 8-bit modes\r |
205 | unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r |
206 | unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r |
4f265db7 |
207 | *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =\r |
208 | *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =\r |
209 | *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;\r |
210 | *(unsigned int *)((char *)gp2x_screen + 320*2+12) =\r |
211 | *(unsigned int *)((char *)gp2x_screen + 320*3+12) =\r |
212 | *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;\r |
bf098bc5 |
213 | } else {\r |
214 | // 16-bit modes\r |
4f265db7 |
215 | unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);\r |
bf098bc5 |
216 | unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;\r |
217 | unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;\r |
4f265db7 |
218 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r |
219 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;\r |
9839d126 |
220 | *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;\r |
bf098bc5 |
221 | }\r |
222 | }\r |
223 | \r |
213c16ad |
224 | static void draw_pico_ptr(void)\r |
225 | {\r |
226 | unsigned short *p = (unsigned short *)gp2x_screen;\r |
227 | \r |
228 | // only if pen enabled and for 16bit modes\r |
229 | if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;\r |
230 | \r |
231 | if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))\r |
232 | p += 32;\r |
233 | \r |
234 | p += 320 * (pico_pen_y + PICO_PEN_ADJUST_Y);\r |
235 | p += pico_pen_x + PICO_PEN_ADJUST_X;\r |
236 | p[0] ^= 0xffff;\r |
237 | p[319] ^= 0xffff;\r |
238 | p[320] ^= 0xffff;\r |
239 | p[321] ^= 0xffff;\r |
240 | p[640] ^= 0xffff;\r |
241 | }\r |
242 | \r |
602133e1 |
243 | static int EmuScanBegin16(unsigned int num)\r |
cc68a136 |
244 | {\r |
245 | if (!(Pico.video.reg[1]&8)) num += 8;\r |
602133e1 |
246 | DrawLineDest = (unsigned short *) gp2x_screen + 320 * num;\r |
cc68a136 |
247 | \r |
248 | return 0;\r |
249 | }\r |
250 | \r |
602133e1 |
251 | static int EmuScanBegin8(unsigned int num)\r |
cc68a136 |
252 | {\r |
253 | if (!(Pico.video.reg[1]&8)) num += 8;\r |
602133e1 |
254 | DrawLineDest = (unsigned char *) gp2x_screen + 320 * num;\r |
cc68a136 |
255 | \r |
256 | return 0;\r |
257 | }\r |
258 | \r |
e11c5548 |
259 | int localPal[0x100];\r |
cc68a136 |
260 | static void (*vidCpyM2)(void *dest, void *src) = NULL;\r |
261 | \r |
76276b0b |
262 | static void blit(const char *fps, const char *notice)\r |
cc68a136 |
263 | {\r |
bf098bc5 |
264 | int emu_opt = currentConfig.EmuOpt;\r |
265 | \r |
fad24893 |
266 | if (PicoOpt&0x10)\r |
267 | {\r |
cc68a136 |
268 | // 8bit fast renderer\r |
269 | if (Pico.m.dirtyPal) {\r |
270 | Pico.m.dirtyPal = 0;\r |
271 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
272 | // feed new palette to our device\r |
273 | gp2x_video_setpalette(localPal, 0x40);\r |
274 | }\r |
fad24893 |
275 | // a hack for VR\r |
276 | if (PicoRead16Hook == PicoSVPRead16)\r |
277 | memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);\r |
278 | // do actual copy\r |
e5f426aa |
279 | vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r |
fad24893 |
280 | }\r |
281 | else if (!(emu_opt&0x80))\r |
282 | {\r |
cc68a136 |
283 | // 8bit accurate renderer\r |
602133e1 |
284 | if (Pico.m.dirtyPal)\r |
285 | {\r |
e5fa9817 |
286 | int pallen = 0x40;\r |
cc68a136 |
287 | Pico.m.dirtyPal = 0;\r |
e5fa9817 |
288 | if (Pico.video.reg[0xC]&8) // shadow/hilight mode\r |
289 | {\r |
cc68a136 |
290 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
291 | vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);\r |
292 | vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);\r |
7b802576 |
293 | memcpy32(localPal+0xc0, localPal+0x40, 0x40);\r |
e5fa9817 |
294 | pallen = 0x100;\r |
295 | }\r |
296 | else if (rendstatus & PDRAW_ACC_SPRITES) {\r |
297 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
298 | memcpy32(localPal+0x40, localPal, 0x40);\r |
299 | memcpy32(localPal+0x80, localPal, 0x40);\r |
300 | memcpy32(localPal+0xc0, localPal, 0x40);\r |
301 | pallen = 0x100;\r |
302 | }\r |
303 | else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r |
cc68a136 |
304 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
305 | vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r |
306 | vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r |
e5fa9817 |
307 | pallen = 0xc0;\r |
308 | }\r |
309 | else {\r |
cc68a136 |
310 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
cc68a136 |
311 | }\r |
e5fa9817 |
312 | if (pallen > 0xc0) {\r |
313 | localPal[0xc0] = 0x0000c000;\r |
314 | localPal[0xd0] = 0x00c00000;\r |
315 | localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r |
316 | localPal[0xf0] = 0x00ffffff;\r |
317 | }\r |
318 | gp2x_video_setpalette(localPal, pallen);\r |
cc68a136 |
319 | }\r |
320 | }\r |
321 | \r |
2433f409 |
322 | if (notice || (emu_opt & 2)) {\r |
323 | int h = 232;\r |
324 | if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;\r |
325 | if (notice) osd_text(4, h, notice);\r |
326 | if (emu_opt & 2)\r |
327 | osd_text(osd_fps_x, h, fps);\r |
328 | }\r |
602133e1 |
329 | if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r |
213c16ad |
330 | draw_cd_leds();\r |
331 | if (PicoAHW & PAHW_PICO)\r |
332 | draw_pico_ptr();\r |
cc68a136 |
333 | \r |
334 | //gp2x_video_wait_vsync();\r |
335 | gp2x_video_flip();\r |
336 | \r |
337 | if (!(PicoOpt&0x10)) {\r |
338 | if (!(Pico.video.reg[1]&8)) {\r |
339 | if (currentConfig.EmuOpt&0x80) {\r |
340 | DrawLineDest = (unsigned short *) gp2x_screen + 320*8;\r |
341 | } else {\r |
342 | DrawLineDest = (unsigned char *) gp2x_screen + 320*8;\r |
343 | }\r |
344 | } else {\r |
345 | DrawLineDest = gp2x_screen;\r |
346 | }\r |
347 | }\r |
348 | }\r |
349 | \r |
350 | \r |
351 | // clears whole screen or just the notice area (in all buffers)\r |
352 | static void clearArea(int full)\r |
353 | {\r |
bf098bc5 |
354 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
355 | // 8-bit renderers\r |
356 | if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
357 | else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r |
358 | } else {\r |
cc68a136 |
359 | // 16bit accurate renderer\r |
360 | if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);\r |
361 | else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r |
cc68a136 |
362 | }\r |
363 | }\r |
364 | \r |
365 | \r |
366 | static void vidResetMode(void)\r |
367 | {\r |
368 | if (PicoOpt&0x10) {\r |
cc68a136 |
369 | gp2x_video_changemode(8);\r |
cc68a136 |
370 | } else if (currentConfig.EmuOpt&0x80) {\r |
a12e0116 |
371 | gp2x_video_changemode(16);\r |
cc68a136 |
372 | PicoDrawSetColorFormat(1);\r |
602133e1 |
373 | PicoScanBegin = EmuScanBegin16;\r |
cc68a136 |
374 | } else {\r |
bf098bc5 |
375 | gp2x_video_changemode(8);\r |
376 | PicoDrawSetColorFormat(2);\r |
602133e1 |
377 | PicoScanBegin = EmuScanBegin8;\r |
bf098bc5 |
378 | }\r |
379 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
380 | // setup pal for 8-bit modes\r |
381 | localPal[0xc0] = 0x0000c000; // MCD LEDs\r |
382 | localPal[0xd0] = 0x00c00000;\r |
cc68a136 |
383 | localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r |
384 | localPal[0xf0] = 0x00ffffff;\r |
cc68a136 |
385 | gp2x_video_setpalette(localPal, 0x100);\r |
386 | gp2x_memset_all_buffers(0, 0xe0, 320*240);\r |
387 | gp2x_video_flip();\r |
cc68a136 |
388 | }\r |
389 | Pico.m.dirtyPal = 1;\r |
390 | // reset scaling\r |
2433f409 |
391 | if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))\r |
392 | gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);\r |
393 | else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r |
cc68a136 |
394 | }\r |
395 | \r |
396 | \r |
66fdc0f0 |
397 | static void emu_msg_cb(const char *msg)\r |
398 | {\r |
399 | if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r |
400 | // 8-bit renderers\r |
401 | gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r |
402 | osd_text(4, 232, msg);\r |
403 | gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);\r |
404 | } else {\r |
405 | // 16bit accurate renderer\r |
406 | gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r |
407 | osd_text(4, 232, msg);\r |
408 | gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);\r |
409 | }\r |
410 | gettimeofday(¬iceMsgTime, 0);\r |
411 | noticeMsgTime.tv_sec -= 2;\r |
7a1f6e45 |
412 | \r |
413 | /* assumption: emu_msg_cb gets called only when something slow is about to happen */\r |
414 | reset_timing = 1;\r |
66fdc0f0 |
415 | }\r |
416 | \r |
fa1e5e29 |
417 | static void emu_state_cb(const char *str)\r |
418 | {\r |
419 | clearArea(0);\r |
420 | blit("", str);\r |
421 | }\r |
422 | \r |
721cd396 |
423 | static void emu_msg_tray_open(void)\r |
424 | {\r |
425 | strcpy(noticeMsg, "CD tray opened");\r |
426 | gettimeofday(¬iceMsgTime, 0);\r |
427 | }\r |
428 | \r |
406c96c5 |
429 | static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)\r |
430 | {\r |
f0f0d2df |
431 | int ret, px, py, lim_x;\r |
85a4b5a4 |
432 | static int pdown_frames = 0;\r |
433 | \r |
c060a9ab |
434 | emu_RunEventsPico(events);\r |
435 | \r |
f0f0d2df |
436 | if (pico_inp_mode == 0) return;\r |
85a4b5a4 |
437 | \r |
438 | // for F200\r |
439 | ret = gp2x_touchpad_read(&px, &py);\r |
f0f0d2df |
440 | if (ret >= 0)\r |
441 | {\r |
442 | if (ret > 35000)\r |
443 | {\r |
85a4b5a4 |
444 | if (pdown_frames++ > 5)\r |
445 | PicoPad[0] |= 0x20;\r |
446 | \r |
447 | pico_pen_x = px;\r |
448 | pico_pen_y = py;\r |
449 | if (!(Pico.video.reg[12]&1)) {\r |
450 | pico_pen_x -= 32;\r |
451 | if (pico_pen_x < 0) pico_pen_x = 0;\r |
452 | if (pico_pen_x > 248) pico_pen_x = 248;\r |
453 | }\r |
454 | if (pico_pen_y > 224) pico_pen_y = 224;\r |
455 | }\r |
456 | else\r |
f0f0d2df |
457 | pdown_frames = 0;\r |
85a4b5a4 |
458 | \r |
459 | //if (ret == 0)\r |
460 | // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r |
461 | }\r |
f0f0d2df |
462 | \r |
463 | PicoPad[0] &= ~0x0f; // release UDLR\r |
464 | if (gp2x_keys & GP2X_UP) pico_pen_y--;\r |
465 | if (gp2x_keys & GP2X_DOWN) pico_pen_y++;\r |
466 | if (gp2x_keys & GP2X_LEFT) pico_pen_x--;\r |
467 | if (gp2x_keys & GP2X_RIGHT) pico_pen_x++;\r |
468 | \r |
469 | lim_x = (Pico.video.reg[12]&1) ? 319 : 255;\r |
470 | if (pico_pen_y < 8) pico_pen_y = 8;\r |
471 | if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y;\r |
472 | if (pico_pen_x < 0) pico_pen_x = 0;\r |
473 | if (pico_pen_x > lim_x-PICO_PEN_ADJUST_X) pico_pen_x = lim_x-PICO_PEN_ADJUST_X;\r |
474 | \r |
475 | PicoPicohw.pen_pos[0] = pico_pen_x;\r |
476 | if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;\r |
477 | PicoPicohw.pen_pos[0] += 0x3c;\r |
478 | PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r |
406c96c5 |
479 | }\r |
480 | \r |
4a32f01f |
481 | static void update_volume(int has_changed, int is_up)\r |
482 | {\r |
483 | static int prev_frame = 0, wait_frames = 0;\r |
484 | int vol = currentConfig.volume;\r |
485 | \r |
486 | if (has_changed)\r |
487 | {\r |
488 | if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r |
489 | wait_frames++;\r |
490 | else {\r |
491 | if (is_up) {\r |
492 | if (vol < 99) vol++;\r |
493 | } else {\r |
494 | if (vol > 0) vol--;\r |
495 | }\r |
496 | wait_frames = 0;\r |
497 | gp2x_sound_volume(vol, vol);\r |
498 | currentConfig.volume = vol;\r |
499 | }\r |
500 | sprintf(noticeMsg, "VOL: %02i", vol);\r |
501 | gettimeofday(¬iceMsgTime, 0);\r |
502 | prev_frame = Pico.m.frame_count;\r |
503 | }\r |
504 | \r |
505 | // set the right mixer func\r |
506 | if (!(PicoOpt&8)) return; // just use defaults for mono\r |
507 | if (vol >= 5)\r |
508 | PsndMix_32_to_16l = mix_32_to_16l_stereo;\r |
509 | else {\r |
510 | mix_32_to_16l_level = 5 - vol;\r |
511 | PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;\r |
512 | }\r |
513 | }\r |
514 | \r |
cc68a136 |
515 | static void RunEvents(unsigned int which)\r |
516 | {\r |
4a32f01f |
517 | if (which & 0x1800) // save or load (but not both)\r |
518 | {\r |
cc68a136 |
519 | int do_it = 1;\r |
ea8c405f |
520 | if ( emu_checkSaveFile(state_slot) &&\r |
46969540 |
521 | (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load\r |
522 | (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save\r |
cc68a136 |
523 | unsigned long keys;\r |
46969540 |
524 | blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");\r |
f9d3ee9d |
525 | while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r |
cc68a136 |
526 | usleep(50*1024);\r |
527 | if (keys & GP2X_X) do_it = 0;\r |
f9d3ee9d |
528 | while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release\r |
529 | usleep(50*1024);\r |
cc68a136 |
530 | clearArea(0);\r |
531 | }\r |
532 | if (do_it) {\r |
76276b0b |
533 | osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r |
fa1e5e29 |
534 | PicoStateProgressCB = emu_state_cb;\r |
535 | gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);\r |
76276b0b |
536 | emu_SaveLoadGame((which & 0x1000) >> 12, 0);\r |
fa1e5e29 |
537 | PicoStateProgressCB = NULL;\r |
cc68a136 |
538 | }\r |
539 | \r |
540 | reset_timing = 1;\r |
541 | }\r |
4a32f01f |
542 | if (which & 0x0400) // switch renderer\r |
543 | {\r |
cc68a136 |
544 | if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r |
545 | else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r |
546 | else currentConfig.EmuOpt &= ~0x80;\r |
547 | \r |
548 | vidResetMode();\r |
549 | \r |
550 | if (PicoOpt&0x10) {\r |
551 | strcpy(noticeMsg, " 8bit fast renderer");\r |
552 | } else if (currentConfig.EmuOpt&0x80) {\r |
553 | strcpy(noticeMsg, "16bit accurate renderer");\r |
554 | } else {\r |
555 | strcpy(noticeMsg, " 8bit accurate renderer");\r |
556 | }\r |
557 | \r |
558 | gettimeofday(¬iceMsgTime, 0);\r |
559 | }\r |
4a32f01f |
560 | if (which & 0x0300)\r |
561 | {\r |
cc68a136 |
562 | if(which&0x0200) {\r |
563 | state_slot -= 1;\r |
564 | if(state_slot < 0) state_slot = 9;\r |
565 | } else {\r |
566 | state_slot += 1;\r |
567 | if(state_slot > 9) state_slot = 0;\r |
568 | }\r |
ea8c405f |
569 | sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r |
cc68a136 |
570 | gettimeofday(¬iceMsgTime, 0);\r |
571 | }\r |
4a32f01f |
572 | if (which & 0x0080) {\r |
cc68a136 |
573 | engineState = PGS_Menu;\r |
574 | }\r |
575 | }\r |
576 | \r |
cc68a136 |
577 | static void updateKeys(void)\r |
578 | {\r |
406c96c5 |
579 | unsigned int keys, keys2, allActions[2] = { 0, 0 }, events;\r |
580 | static unsigned int prevEvents = 0;\r |
cc68a136 |
581 | int joy, i;\r |
582 | \r |
583 | keys = gp2x_joystick_read(0);\r |
584 | if (keys & GP2X_SELECT) {\r |
585 | engineState = select_exits ? PGS_Quit : PGS_Menu;\r |
586 | // wait until select is released, so menu would not resume game\r |
587 | while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);\r |
588 | }\r |
589 | \r |
590 | keys &= CONFIGURABLE_KEYS;\r |
406c96c5 |
591 | keys2 = keys;\r |
cc68a136 |
592 | \r |
593 | for (i = 0; i < 32; i++)\r |
594 | {\r |
406c96c5 |
595 | if (keys2 & (1 << i))\r |
bdec53c9 |
596 | {\r |
cc68a136 |
597 | int pl, acts = currentConfig.KeyBinds[i];\r |
598 | if (!acts) continue;\r |
599 | pl = (acts >> 16) & 1;\r |
bdec53c9 |
600 | if (kb_combo_keys & (1 << i))\r |
601 | {\r |
da310283 |
602 | int u = i+1, acts_c = acts & kb_combo_acts;\r |
cc68a136 |
603 | // let's try to find the other one\r |
bdec53c9 |
604 | if (acts_c) {\r |
da310283 |
605 | for (; u < 32; u++)\r |
406c96c5 |
606 | if ( (keys2 & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {\r |
bdec53c9 |
607 | allActions[pl] |= acts_c & currentConfig.KeyBinds[u];\r |
406c96c5 |
608 | keys2 &= ~((1 << i) | (1 << u));\r |
cc68a136 |
609 | break;\r |
610 | }\r |
bdec53c9 |
611 | }\r |
cc68a136 |
612 | // add non-combo actions if combo ones were not found\r |
613 | if (!acts_c || u == 32)\r |
bdec53c9 |
614 | allActions[pl] |= acts & ~kb_combo_acts;\r |
cc68a136 |
615 | } else {\r |
616 | allActions[pl] |= acts;\r |
617 | }\r |
618 | }\r |
619 | }\r |
620 | \r |
621 | // add joy inputs\r |
622 | if (num_of_joys > 0)\r |
623 | {\r |
624 | gp2x_usbjoy_update();\r |
625 | for (joy = 0; joy < num_of_joys; joy++) {\r |
406c96c5 |
626 | int btns = gp2x_usbjoy_check2(joy);\r |
cc68a136 |
627 | for (i = 0; i < 32; i++) {\r |
406c96c5 |
628 | if (btns & (1 << i)) {\r |
cc68a136 |
629 | int acts = currentConfig.JoyBinds[joy][i];\r |
630 | int pl = (acts >> 16) & 1;\r |
631 | allActions[pl] |= acts;\r |
632 | }\r |
633 | }\r |
634 | }\r |
635 | }\r |
636 | \r |
f0f0d2df |
637 | PicoPad[0] = allActions[0] & 0xfff;\r |
638 | PicoPad[1] = allActions[1] & 0xfff;\r |
639 | \r |
640 | if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);\r |
641 | if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);\r |
cc68a136 |
642 | \r |
643 | events = (allActions[0] | allActions[1]) >> 16;\r |
644 | \r |
645 | // volume is treated in special way and triggered every frame\r |
4a32f01f |
646 | if (events & 0x6000)\r |
647 | update_volume(1, events & 0x2000);\r |
648 | \r |
c060a9ab |
649 | if ((events ^ prevEvents) & 0x40) {\r |
650 | emu_changeFastForward(events & 0x40);\r |
651 | update_volume(0, 0);\r |
652 | reset_timing = 1;\r |
653 | }\r |
cc68a136 |
654 | \r |
655 | events &= ~prevEvents;\r |
406c96c5 |
656 | \r |
657 | if (PicoAHW == PAHW_PICO)\r |
658 | RunEventsPico(events, keys);\r |
cc68a136 |
659 | if (events) RunEvents(events);\r |
ea8c405f |
660 | if (movie_data) emu_updateMovie();\r |
cc68a136 |
661 | \r |
662 | prevEvents = (allActions[0] | allActions[1]) >> 16;\r |
663 | }\r |
664 | \r |
cc68a136 |
665 | \r |
7a93adeb |
666 | static void updateSound(int len)\r |
cc68a136 |
667 | {\r |
7a93adeb |
668 | if (PicoOpt&8) len<<=1;\r |
cc68a136 |
669 | \r |
01bc6b19 |
670 | /* avoid writing audio when lagging behind to prevent audio lag */\r |
671 | if (PicoSkipFrame != 2)\r |
672 | gp2x_sound_write(PsndOut, len<<1);\r |
cc68a136 |
673 | }\r |
674 | \r |
675 | \r |
01bc6b19 |
676 | static void SkipFrame(int do_audio)\r |
cc68a136 |
677 | {\r |
01bc6b19 |
678 | PicoSkipFrame=do_audio ? 1 : 2;\r |
cc68a136 |
679 | PicoFrame();\r |
680 | PicoSkipFrame=0;\r |
cc68a136 |
681 | }\r |
682 | \r |
683 | \r |
0fc0e241 |
684 | void emu_forcedFrame(int opts)\r |
860c6322 |
685 | {\r |
686 | int po_old = PicoOpt;\r |
a12e0116 |
687 | int eo_old = currentConfig.EmuOpt;\r |
688 | \r |
0fc0e241 |
689 | PicoOpt &= ~0x10;\r |
690 | PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites\r |
a12e0116 |
691 | currentConfig.EmuOpt |= 0x80;\r |
860c6322 |
692 | \r |
a12e0116 |
693 | //vidResetMode();\r |
694 | PicoDrawSetColorFormat(1);\r |
602133e1 |
695 | PicoScanBegin = EmuScanBegin16;\r |
a12e0116 |
696 | Pico.m.dirtyPal = 1;\r |
697 | PicoFrameDrawOnly();\r |
860c6322 |
698 | \r |
a12e0116 |
699 | /*\r |
860c6322 |
700 | if (!(Pico.video.reg[12]&1)) {\r |
68cba51e |
701 | vidCpyM2 = vidCpyM2_32col;\r |
860c6322 |
702 | clearArea(1);\r |
68cba51e |
703 | } else vidCpyM2 = vidCpyM2_40col;\r |
860c6322 |
704 | \r |
e5f426aa |
705 | vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);\r |
860c6322 |
706 | vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r |
707 | gp2x_video_setpalette(localPal, 0x40);\r |
a12e0116 |
708 | */\r |
48e8482f |
709 | PicoOpt = po_old;\r |
a12e0116 |
710 | currentConfig.EmuOpt = eo_old;\r |
860c6322 |
711 | }\r |
712 | \r |
fcf94fcc |
713 | void emu_platformDebugCat(char *str)\r |
714 | {\r |
715 | // nothing\r |
716 | }\r |
717 | \r |
cc68a136 |
718 | static void simpleWait(int thissec, int lim_time)\r |
719 | {\r |
720 | struct timeval tval;\r |
721 | \r |
722 | spend_cycles(1024);\r |
723 | gettimeofday(&tval, 0);\r |
da310283 |
724 | if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
cc68a136 |
725 | \r |
da310283 |
726 | while (tval.tv_usec < lim_time)\r |
cc68a136 |
727 | {\r |
728 | spend_cycles(1024);\r |
729 | gettimeofday(&tval, 0);\r |
da310283 |
730 | if (thissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
cc68a136 |
731 | }\r |
732 | }\r |
733 | \r |
734 | \r |
da310283 |
735 | #if 0\r |
736 | static void tga_dump(void)\r |
737 | {\r |
738 | #define BYTE unsigned char\r |
739 | #define WORD unsigned short\r |
740 | struct\r |
741 | {\r |
742 | BYTE IDLength; /* 00h Size of Image ID field */\r |
743 | BYTE ColorMapType; /* 01h Color map type */\r |
744 | BYTE ImageType; /* 02h Image type code */\r |
745 | WORD CMapStart; /* 03h Color map origin */\r |
746 | WORD CMapLength; /* 05h Color map length */\r |
747 | BYTE CMapDepth; /* 07h Depth of color map entries */\r |
748 | WORD XOffset; /* 08h X origin of image */\r |
749 | WORD YOffset; /* 0Ah Y origin of image */\r |
750 | WORD Width; /* 0Ch Width of image */\r |
751 | WORD Height; /* 0Eh Height of image */\r |
752 | BYTE PixelDepth; /* 10h Image pixel size */\r |
753 | BYTE ImageDescriptor; /* 11h Image descriptor byte */\r |
754 | } __attribute__((packed)) TGAHEAD;\r |
755 | static unsigned short oldscr[320*240];\r |
756 | FILE *f; char name[128]; int i;\r |
757 | \r |
758 | memset(&TGAHEAD, 0, sizeof(TGAHEAD));\r |
759 | TGAHEAD.ImageType = 2;\r |
760 | TGAHEAD.Width = 320;\r |
761 | TGAHEAD.Height = 240;\r |
762 | TGAHEAD.PixelDepth = 16;\r |
763 | TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left\r |
764 | \r |
765 | #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?\r |
766 | \r |
767 | for (i = 0; i < 320*240; i++)\r |
768 | if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;\r |
769 | if (i < 320*240)\r |
770 | {\r |
771 | for (i = 0; i < 320*240; i++)\r |
772 | oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);\r |
773 | sprintf(name, "%05i.tga", Pico.m.frame_count);\r |
774 | f = fopen(name, "wb");\r |
775 | if (!f) { printf("!f\n"); exit(1); }\r |
776 | fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);\r |
777 | fwrite(oldscr, 1, 320*240*2, f);\r |
778 | fclose(f);\r |
779 | }\r |
780 | }\r |
781 | #endif\r |
782 | \r |
783 | \r |
cc68a136 |
784 | void emu_Loop(void)\r |
785 | {\r |
786 | static int gp2x_old_clock = 200;\r |
fa283c9a |
787 | static int PsndRate_old = 0, PicoOpt_old = 0, EmuOpt_old = 0, pal_old = 0;\r |
cc68a136 |
788 | char fpsbuff[24]; // fps count c string\r |
789 | struct timeval tval; // timing\r |
da310283 |
790 | int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r |
791 | int frames_done, frames_shown, thissec; // actual frames\r |
792 | int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;\r |
cc68a136 |
793 | char *notice = 0;\r |
794 | \r |
795 | printf("entered emu_Loop()\n");\r |
796 | \r |
797 | if (gp2x_old_clock != currentConfig.CPUclock) {\r |
798 | printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r |
799 | set_FCLK(currentConfig.CPUclock);\r |
800 | gp2x_old_clock = currentConfig.CPUclock;\r |
801 | printf(" done\n");\r |
802 | }\r |
803 | \r |
82bc9cdd |
804 | if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r |
805 | set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r |
cc68a136 |
806 | gp2x_old_gamma = currentConfig.gamma;\r |
82bc9cdd |
807 | printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r |
cc68a136 |
808 | }\r |
809 | \r |
aae35e84 |
810 | if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {\r |
811 | if (currentConfig.EmuOpt&0x2000)\r |
812 | set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);\r |
813 | else unset_LCD_custom_rate();\r |
814 | }\r |
815 | \r |
816 | EmuOpt_old = currentConfig.EmuOpt;\r |
cc68a136 |
817 | fpsbuff[0] = 0;\r |
818 | \r |
819 | // make sure we are in correct mode\r |
820 | vidResetMode();\r |
ea8c405f |
821 | scaling_update();\r |
e11c5548 |
822 | Pico.m.dirtyPal = 1;\r |
cc68a136 |
823 | oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r |
bdec53c9 |
824 | emu_findKeyBindCombos();\r |
cc68a136 |
825 | \r |
826 | // pal/ntsc might have changed, reset related stuff\r |
827 | target_fps = Pico.m.pal ? 50 : 60;\r |
828 | target_frametime = 1000000/target_fps;\r |
829 | reset_timing = 1;\r |
830 | \r |
831 | // prepare sound stuff\r |
4a32f01f |
832 | if (currentConfig.EmuOpt & 4)\r |
833 | {\r |
7a93adeb |
834 | int snd_excess_add;\r |
1dceadae |
835 | if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||\r |
836 | ((PicoOpt&0x200) && crashed_940)) {\r |
9d917eea |
837 | PsndRerate(Pico.m.frame_count ? 1 : 0);\r |
cc68a136 |
838 | }\r |
cc68a136 |
839 | snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r |
1dceadae |
840 | printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r |
841 | PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r |
cc68a136 |
842 | gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r |
843 | gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r |
844 | PicoWriteSound = updateSound;\r |
4a32f01f |
845 | update_volume(0, 0);\r |
7a93adeb |
846 | memset(sndBuffer, 0, sizeof(sndBuffer));\r |
847 | PsndOut = sndBuffer;\r |
cc68a136 |
848 | PsndRate_old = PsndRate;\r |
cc68a136 |
849 | PicoOpt_old = PicoOpt;\r |
850 | pal_old = Pico.m.pal;\r |
851 | } else {\r |
4a32f01f |
852 | PsndOut = NULL;\r |
cc68a136 |
853 | }\r |
854 | \r |
0a051f55 |
855 | // prepare CD buffer\r |
602133e1 |
856 | if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r |
0a051f55 |
857 | \r |
aae35e84 |
858 | // calc vsync offset to sync timing code with vsync\r |
859 | if (currentConfig.EmuOpt&0x2000) {\r |
860 | gettimeofday(&tval, 0);\r |
861 | gp2x_video_wait_vsync();\r |
862 | gettimeofday(&tval, 0);\r |
863 | vsync_offset = tval.tv_usec;\r |
864 | while (vsync_offset >= target_frametime)\r |
865 | vsync_offset -= target_frametime;\r |
866 | if (!vsync_offset) vsync_offset++;\r |
867 | printf("vsync_offset: %i\n", vsync_offset);\r |
868 | } else\r |
869 | vsync_offset = 0;\r |
870 | \r |
da310283 |
871 | frames_done = frames_shown = thissec =\r |
872 | pframes_done = pframes_shown = pthissec = 0;\r |
873 | \r |
874 | // loop\r |
cc68a136 |
875 | while (engineState == PGS_Running)\r |
876 | {\r |
877 | int modes;\r |
878 | \r |
879 | gettimeofday(&tval, 0);\r |
4a32f01f |
880 | if (reset_timing) {\r |
cc68a136 |
881 | reset_timing = 0;\r |
da310283 |
882 | pthissec = tval.tv_sec;\r |
883 | pframes_shown = pframes_done = tval.tv_usec/target_frametime;\r |
cc68a136 |
884 | }\r |
885 | \r |
886 | // show notice message?\r |
4a32f01f |
887 | if (noticeMsgTime.tv_sec)\r |
888 | {\r |
cc68a136 |
889 | static int noticeMsgSum;\r |
890 | if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec\r |
891 | noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r |
892 | clearArea(0);\r |
893 | notice = 0;\r |
894 | } else {\r |
895 | int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];\r |
896 | if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }\r |
897 | notice = noticeMsg;\r |
898 | }\r |
899 | }\r |
900 | \r |
901 | // check for mode changes\r |
902 | modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);\r |
4a32f01f |
903 | if (modes != oldmodes)\r |
904 | {\r |
cc68a136 |
905 | int scalex = 320;\r |
906 | osd_fps_x = OSD_FPS_X;\r |
907 | if (modes & 4) {\r |
908 | vidCpyM2 = vidCpyM2_40col;\r |
909 | } else {\r |
910 | if (PicoOpt & 0x100) {\r |
911 | vidCpyM2 = vidCpyM2_32col_nobord;\r |
912 | scalex = 256;\r |
913 | osd_fps_x = OSD_FPS_X - 64;\r |
914 | } else {\r |
915 | vidCpyM2 = vidCpyM2_32col;\r |
916 | }\r |
917 | }\r |
2433f409 |
918 | if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r |
919 | gp2x_video_RGB_setscaling(8, scalex, 224);\r |
920 | else gp2x_video_RGB_setscaling(0, scalex, 240);\r |
cc68a136 |
921 | oldmodes = modes;\r |
922 | clearArea(1);\r |
923 | }\r |
924 | \r |
925 | // second changed?\r |
4a32f01f |
926 | if (thissec != tval.tv_sec)\r |
927 | {\r |
cc68a136 |
928 | #ifdef BENCHMARK\r |
929 | static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r |
4a32f01f |
930 | if (++bench == 10) {\r |
cc68a136 |
931 | bench = 0;\r |
932 | bench_fps_s = bench_fps;\r |
933 | bf[bfp++ & 3] = bench_fps;\r |
934 | bench_fps = 0;\r |
935 | }\r |
936 | bench_fps += frames_shown;\r |
937 | sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r |
938 | #else\r |
1b13dae0 |
939 | if (currentConfig.EmuOpt & 2) {\r |
cc68a136 |
940 | sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r |
1b13dae0 |
941 | if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r |
942 | }\r |
cc68a136 |
943 | #endif\r |
da310283 |
944 | frames_shown = frames_done = 0;\r |
cc68a136 |
945 | thissec = tval.tv_sec;\r |
da310283 |
946 | }\r |
947 | #ifdef PFRAMES\r |
948 | sprintf(fpsbuff, "%i", Pico.m.frame_count);\r |
949 | #endif\r |
cc68a136 |
950 | \r |
da310283 |
951 | if (pthissec != tval.tv_sec)\r |
952 | {\r |
4a32f01f |
953 | if (PsndOut == 0 && currentConfig.Frameskip >= 0) {\r |
da310283 |
954 | pframes_done = pframes_shown = 0;\r |
cc68a136 |
955 | } else {\r |
956 | // it is quite common for this implementation to leave 1 fame unfinished\r |
957 | // when second changes, but we don't want buffer to starve.\r |
da310283 |
958 | if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r |
cc68a136 |
959 | updateKeys();\r |
da310283 |
960 | SkipFrame(1); pframes_done++;\r |
cc68a136 |
961 | }\r |
962 | \r |
da310283 |
963 | pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;\r |
964 | pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;\r |
965 | if (pframes_shown > pframes_done) pframes_shown = pframes_done;\r |
cc68a136 |
966 | }\r |
da310283 |
967 | pthissec = tval.tv_sec;\r |
cc68a136 |
968 | }\r |
aae35e84 |
969 | \r |
da310283 |
970 | lim_time = (pframes_done+1) * target_frametime + vsync_offset;\r |
971 | if (currentConfig.Frameskip >= 0) // frameskip enabled\r |
972 | {\r |
cc68a136 |
973 | for(i = 0; i < currentConfig.Frameskip; i++) {\r |
974 | updateKeys();\r |
da310283 |
975 | SkipFrame(1); pframes_done++; frames_done++;\r |
4a32f01f |
976 | if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r |
cc68a136 |
977 | gettimeofday(&tval, 0);\r |
da310283 |
978 | if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
979 | if (tval.tv_usec < lim_time) { // we are too fast\r |
980 | simpleWait(pthissec, lim_time);\r |
cc68a136 |
981 | }\r |
982 | }\r |
983 | lim_time += target_frametime;\r |
984 | }\r |
da310283 |
985 | }\r |
986 | else if (tval.tv_usec > lim_time) // auto frameskip\r |
987 | {\r |
cc68a136 |
988 | // no time left for this frame - skip\r |
ea8c405f |
989 | if (tval.tv_usec - lim_time >= 300000) {\r |
4ff2d527 |
990 | /* something caused a slowdown for us (disk access? cache flush?)\r |
991 | * try to recover by resetting timing... */\r |
992 | reset_timing = 1;\r |
993 | continue;\r |
994 | }\r |
cc68a136 |
995 | updateKeys();\r |
da310283 |
996 | SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r |
cc68a136 |
997 | continue;\r |
998 | }\r |
999 | \r |
1000 | updateKeys();\r |
1001 | PicoFrame();\r |
1002 | \r |
1003 | // check time\r |
1004 | gettimeofday(&tval, 0);\r |
da310283 |
1005 | if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;\r |
cc68a136 |
1006 | \r |
ea8c405f |
1007 | if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r |
4ff2d527 |
1008 | reset_timing = 1;\r |
1009 | else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r |
cc68a136 |
1010 | {\r |
aae35e84 |
1011 | // sleep or vsync if we are still too fast\r |
cc68a136 |
1012 | // usleep sleeps for ~20ms minimum, so it is not a solution here\r |
4a32f01f |
1013 | if (!reset_timing && tval.tv_usec < lim_time)\r |
cc68a136 |
1014 | {\r |
1015 | // we are too fast\r |
aae35e84 |
1016 | if (vsync_offset) {\r |
1017 | if (lim_time - tval.tv_usec > target_frametime/2)\r |
da310283 |
1018 | simpleWait(pthissec, lim_time - target_frametime/4);\r |
aae35e84 |
1019 | gp2x_video_wait_vsync();\r |
1020 | } else {\r |
da310283 |
1021 | simpleWait(pthissec, lim_time);\r |
aae35e84 |
1022 | }\r |
cc68a136 |
1023 | }\r |
1024 | }\r |
1025 | \r |
1026 | blit(fpsbuff, notice);\r |
1027 | \r |
da310283 |
1028 | pframes_done++; pframes_shown++;\r |
1029 | frames_done++; frames_shown++;\r |
cc68a136 |
1030 | }\r |
1031 | \r |
c060a9ab |
1032 | emu_changeFastForward(0);\r |
0a051f55 |
1033 | \r |
602133e1 |
1034 | if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r |
0a051f55 |
1035 | \r |
cc68a136 |
1036 | // save SRAM\r |
1037 | if((currentConfig.EmuOpt & 1) && SRam.changed) {\r |
6cadc2da |
1038 | emu_state_cb("Writing SRAM/BRAM..");\r |
cc68a136 |
1039 | emu_SaveLoadGame(0, 1);\r |
1040 | SRam.changed = 0;\r |
1041 | }\r |
e11c5548 |
1042 | \r |
a12e0116 |
1043 | // if in 8bit mode, generate 16bit image for menu background\r |
1044 | if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r |
0fc0e241 |
1045 | emu_forcedFrame(POPT_EN_SOFTSCALE);\r |
cc68a136 |
1046 | }\r |
1047 | \r |
1048 | \r |
1049 | void emu_ResetGame(void)\r |
1050 | {\r |
1cb1584b |
1051 | PicoReset();\r |
cc68a136 |
1052 | reset_timing = 1;\r |
1053 | }\r |
1054 | \r |