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