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