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