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