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