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