4 #include "kgsdk/Framework2D.h"
5 #include "kgsdk/FrameworkAudio.h"
6 #include "../common/emu.h"
7 #include "../common/lprintf.h"
8 #include "../common/arm_utils.h"
12 #include "asm_utils.h"
14 #include <Pico/PicoInt.h>
22 // main 300K gfx-related buffer. Used by menu and renderers.
23 unsigned char gfx_buffer[321*240*2*2];
24 char romFileName[MAX_PATH];
27 unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
30 static DWORD noticeMsgTime = 0;
34 static void blit(const char *fps, const char *notice);
35 static void clearArea(int full);
37 void emu_noticeMsgUpdated(void)
39 noticeMsgTime = GetTickCount();
42 void emu_getMainDir(char *dst, int len)
44 if (len > 0) *dst = 0;
47 static void emu_msg_cb(const char *msg)
49 if (giz_screen == NULL)
50 giz_screen = Framework2D_LockBuffer();
52 memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
53 emu_textOut16(4, 232, msg);
54 noticeMsgTime = GetTickCount() - 2000;
56 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
60 static void emu_state_cb(const char *str)
66 static void emu_msg_tray_open(void)
68 strcpy(noticeMsg, "CD tray opened");
69 noticeMsgTime = GetTickCount();
75 // make dirs for saves, cfgs, etc.
76 CreateDirectory(L"mds", NULL);
77 CreateDirectory(L"srm", NULL);
78 CreateDirectory(L"brm", NULL);
79 CreateDirectory(L"cfg", NULL);
82 PicoMessage = emu_msg_cb;
83 PicoMCDopenTray = emu_msg_tray_open;
84 PicoMCDcloseTray = menu_loop_tray;
90 if((currentConfig.EmuOpt & 1) && SRam.changed) {
91 emu_SaveLoadGame(0, 1);
95 if (!(currentConfig.EmuOpt & 0x20)) {
96 FILE *f = fopen(PicoConfigFile, "r+b");
97 if (!f) emu_WriteConfig(0);
99 // if we already have config, reload it, except last ROM
100 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
101 fread(¤tConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
102 fseek(f, 0, SEEK_SET);
103 fwrite(¤tConfig, 1, sizeof(currentConfig), f);
112 void emu_setDefaultConfig(void)
114 memset(¤tConfig, 0, sizeof(currentConfig));
115 currentConfig.lastRomFile[0] = 0;
116 currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds
117 currentConfig.PicoOpt = 0x0f | 0xc00; // | cd_pcm, cd_cdda
118 currentConfig.PsndRate = 22050;
119 currentConfig.PicoRegion = 0; // auto
120 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
121 currentConfig.Frameskip = -1; // auto
122 currentConfig.volume = 50;
123 currentConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
124 currentConfig.KeyBinds[ 3] = 1<<1;
125 currentConfig.KeyBinds[ 0] = 1<<2;
126 currentConfig.KeyBinds[ 1] = 1<<3;
127 currentConfig.KeyBinds[ 5] = 1<<4;
128 currentConfig.KeyBinds[ 6] = 1<<5;
129 currentConfig.KeyBinds[ 7] = 1<<6;
130 currentConfig.KeyBinds[ 8] = 1<<7;
131 currentConfig.KeyBinds[ 4] = 1<<26; // switch rend
132 currentConfig.KeyBinds[ 8] = 1<<27; // save state
133 currentConfig.KeyBinds[ 9] = 1<<28; // load state
134 currentConfig.KeyBinds[12] = 1<<29; // vol up
135 currentConfig.KeyBinds[11] = 1<<30; // vol down
136 currentConfig.PicoCDBuffers = 64;
137 currentConfig.scaling = 0;
141 static int EmuScan16(unsigned int num, void *sdata)
143 if (!(Pico.video.reg[1]&8)) num += 8;
144 DrawLineDest = (unsigned short *) giz_screen + 321*(num+1);
149 static int EmuScan8(unsigned int num, void *sdata)
151 // draw like the fast renderer
152 if (!(Pico.video.reg[1]&8)) num += 8;
153 HighCol = gfx_buffer + 328*8 + 328*(num+1);
158 static void osd_text(int x, int y, const char *text)
160 int len = strlen(text) * 8;
163 for (h = 0; h < 8; h++) {
164 p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
165 p = (int *) ((int)p & ~3); // align
166 for (i = len; i; i--, p++) *p = 0;
168 emu_textOut16(x, y, text);
172 short localPal[0x100];
173 static void (*vidCpy8to16)(void *dest, void *src, short *pal, int lines) = NULL;
175 static void blit(const char *fps, const char *notice)
177 int emu_opt = currentConfig.EmuOpt;
180 // 8bit fast renderer
181 if (Pico.m.dirtyPal) {
183 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
185 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224);
186 } else if (!(emu_opt&0x80)) {
187 // 8bit accurate renderer
188 if (Pico.m.dirtyPal) {
190 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
191 if(Pico.video.reg[0xC]&8) { // shadow/hilight mode
192 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
193 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO
194 blockcpy(localPal+0xc0, localPal+0x40, 0x40*4);
195 localPal[0xc0] = 0x0600;
196 localPal[0xd0] = 0xc000;
197 localPal[0xe0] = 0x0000; // reserved pixels for OSD
198 localPal[0xf0] = 0xffff;
201 else if (rendstatus & 0x20) { // mid-frame palette changes
202 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
203 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
207 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224);
210 if (notice || (emu_opt & 2)) {
212 if (notice) osd_text(4, h, notice);
213 if (emu_opt & 2) osd_text(osd_fps_x, h, fps);
215 // if ((emu_opt & 0x400) && (PicoMCD & 1))
218 //gp2x_video_wait_vsync();
220 if (!(PicoOpt&0x10)) {
221 if (Pico.video.reg[1] & 8) {
222 if (currentConfig.EmuOpt&0x80)
223 DrawLineDest = (unsigned short *) giz_screen;
225 HighCol = gfx_buffer;
227 if (currentConfig.EmuOpt&0x80)
228 DrawLineDest = (unsigned short *) giz_screen + 320*8;
230 HighCol = gfx_buffer + 328*8;
235 // clears whole screen or just the notice area (in all buffers)
236 static void clearArea(int full)
238 if (giz_screen == NULL)
239 giz_screen = Framework2D_LockBuffer();
240 if (full) memset32(giz_screen, 0, 320*240*2/4);
241 else memset32((int *)((char *)giz_screen + 320*232*2), 0, 320*8*2/4);
244 static void vidResetMode(void)
248 } else if (currentConfig.EmuOpt&0x80) {
249 PicoDrawSetColorFormat(1);
250 PicoScan = EmuScan16;
252 PicoDrawSetColorFormat(0);
255 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
256 // setup pal for 8-bit modes
257 localPal[0xc0] = 0x0600;
258 localPal[0xd0] = 0xc000;
259 localPal[0xe0] = 0x0000; // reserved pixels for OSD
260 localPal[0xf0] = 0xffff;
263 screen = Framework2D_LockBuffer();
264 memset32(screen, 0, 320*240*2/4);
265 Framework2D_UnlockBuffer();
270 static void SkipFrame(int do_audio)
272 PicoSkipFrame=do_audio ? 1 : 2;
277 void emu_forcedFrame(void)
282 static void updateKeys(void)
286 static void simpleWait(DWORD until)
292 //static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;
293 char fpsbuff[24]; // fps count c string
294 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
295 int frames_done = 0, frames_shown = 0, oldmodes = 0;
296 int target_fps, target_frametime, lim_time, tval_diff, i;
299 lprintf("entered emu_Loop()\n");
303 // make sure we are in correct mode
305 if (currentConfig.scaling) PicoOpt|=0x4000;
306 else PicoOpt&=~0x4000;
308 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
309 //find_combos(); // TODO
311 // pal/ntsc might have changed, reset related stuff
312 target_fps = Pico.m.pal ? 50 : 60;
313 target_frametime = (1000<<8)/target_fps;
316 // prepare sound stuff
317 /* if (currentConfig.EmuOpt & 4) {
319 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
320 sound_rerate(Pico.m.frame_count ? 1 : 0);
322 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
323 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
324 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
325 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
326 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
327 PicoWriteSound = updateSound;
328 memset(sndBuffer, 0, sizeof(sndBuffer));
330 PsndRate_old = PsndRate;
331 PsndLen_real = PsndLen;
332 PicoOpt_old = PicoOpt;
333 pal_old = Pico.m.pal;
339 if (PicoMCD & 1) PicoCDBufferInit();
342 while (engineState == PGS_Running)
346 tval = GetTickCount();
347 if (reset_timing || tval < tval_prev) {
350 frames_shown = frames_done = 0;
353 // show notice message?
355 static int noticeMsgSum;
356 if (tval - noticeMsgTime > 2000) { // > 2.0 sec
361 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
362 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
367 // check for mode changes
368 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
369 if (modes != oldmodes) {
370 osd_fps_x = OSD_FPS_X;
372 vidCpy8to16 = vidCpy8to16_40;
374 // vidCpy8to16 = vidCpy8to16_32col;
380 if (tval - tval_thissec >= 1000)
383 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
386 bench_fps_s = bench_fps;
387 bf[bfp++ & 3] = bench_fps;
390 bench_fps += frames_shown;
391 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
393 if(currentConfig.EmuOpt & 2)
394 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
398 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
399 frames_done = frames_shown = 0;
401 // it is quite common for this implementation to leave 1 fame unfinished
402 // when second changes, but we don't want buffer to starve.
403 if (PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
405 SkipFrame(1); frames_done++;
408 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
409 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
410 if (frames_shown > frames_done) frames_shown = frames_done;
414 sprintf(fpsbuff, "%i", Pico.m.frame_count);
418 lim_time = (frames_done+1) * target_frametime;
419 if (currentConfig.Frameskip >= 0) // frameskip enabled
421 for (i = 0; i < currentConfig.Frameskip; i++) {
423 SkipFrame(1); frames_done++;
424 if (PsndOut) { // do framelimitting if sound is enabled
426 tval = GetTickCount();
427 tval_diff = (int)(tval - tval_thissec) << 8;
428 if (tval_diff < lim_time) // we are too fast
429 simpleWait(tval + ((lim_time - tval_diff)>>8));
431 lim_time += target_frametime;
434 else // auto frameskip
437 tval = GetTickCount();
438 tval_diff = (int)(tval - tval_thissec) << 8;
439 if (tval_diff > lim_time)
441 // no time left for this frame - skip
442 if (tval_diff - lim_time >= (300<<8)) {
443 /* something caused a slowdown for us (disk access? cache flush?)
444 * try to recover by resetting timing... */
449 SkipFrame(tval_diff < lim_time+target_frametime*2); frames_done++;
456 if (giz_screen == NULL)
457 giz_screen = Framework2D_LockBuffer();
460 blit(fpsbuff, notice);
462 if (giz_screen != NULL) {
463 Framework2D_UnlockBuffer();
468 tval = GetTickCount();
469 tval_diff = (int)(tval - tval_thissec) << 8;
471 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
473 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
475 // sleep if we are still too fast
476 if (tval_diff < lim_time)
479 simpleWait(tval + ((lim_time - tval_diff) >> 8));
483 frames_done++; frames_shown++;
487 if (PicoMCD & 1) PicoCDBufferFree();
490 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
491 emu_state_cb("Writing SRAM/BRAM..");
492 emu_SaveLoadGame(0, 1);
498 void emu_ResetGame(void)