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