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