4 #include <sys/stat.h> // mkdir
7 #include "kgsdk/Framework.h"
8 #include "kgsdk/Framework2D.h"
9 #include "kgsdk/FrameworkAudio.h"
10 #include "../common/emu.h"
11 #include "../common/lprintf.h"
12 #include "../common/arm_utils.h"
13 #include "../common/config.h"
17 #include "asm_utils.h"
19 #include <pico/pico_int.h>
27 // main 300K gfx-related buffer. Used by menu and renderers.
28 unsigned char gfx_buffer[321*240*2*2];
29 unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
31 static short *snd_cbuff = NULL;
32 static int snd_cbuf_samples = 0, snd_all_samples = 0;
35 static void blit(const char *fps, const char *notice);
36 static void clearArea(int full);
38 int plat_get_root_dir(char *dst, int len)
40 if (len > 0) *dst = 0;
45 static void emu_msg_cb(const char *msg)
47 if (giz_screen != NULL) fb_unlock();
48 giz_screen = fb_lock(1);
50 memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
51 emu_text_out16(4, 232, msg);
52 noticeMsgTime = GetTickCount() - 2000;
54 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
58 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
61 void emu_stateCb(const char *str)
63 if (giz_screen != NULL) fb_unlock();
64 giz_screen = fb_lock(1);
72 Sleep(0); /* yield the CPU, the system may need it */
75 void pemu_prep_defconfig(void)
77 defaultConfig.s_PsndRate = 22050;
78 defaultConfig.s_PicoCDBuffers = 0;
79 defaultConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
80 defaultConfig.KeyBinds[ 3] = 1<<1;
81 defaultConfig.KeyBinds[ 0] = 1<<2;
82 defaultConfig.KeyBinds[ 1] = 1<<3;
83 defaultConfig.KeyBinds[ 5] = 1<<4;
84 defaultConfig.KeyBinds[ 6] = 1<<5;
85 defaultConfig.KeyBinds[ 7] = 1<<6;
86 defaultConfig.KeyBinds[ 4] = 1<<7;
87 defaultConfig.KeyBinds[13] = 1<<26; // switch rend
88 defaultConfig.KeyBinds[ 8] = 1<<27; // save state
89 defaultConfig.KeyBinds[ 9] = 1<<28; // load state
90 defaultConfig.KeyBinds[12] = 1<<29; // vol up
91 defaultConfig.KeyBinds[11] = 1<<30; // vol down
92 defaultConfig.scaling = 0;
96 static int EmuScanBegin16(unsigned int num)
98 DrawLineDest = (unsigned short *) giz_screen + 321 * num;
100 if ((currentConfig.EmuOpt&0x4000) && (num&1) == 0) // (Pico.m.frame_count&1))
101 return 1; // skip next line
106 static int EmuScanBegin8(unsigned int num)
108 // draw like the fast renderer
109 HighCol = gfx_buffer + 328 * num;
114 static void osd_text(int x, int y, const char *text)
116 int len = strlen(text) * 8 / 2;
118 for (h = 0; h < 8; h++) {
119 p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
120 p = (int *) ((int)p & ~3); // align
123 emu_text_out16(x, y, text);
127 void log1(void *p1, void *p2)
129 lprintf("%p %p %p\n", p1, p2, DrawLineDest);
133 static void cd_leds(void)
135 static int old_reg = 0;
136 unsigned int col_g, col_r, *p;
138 if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
139 old_reg = Pico_mcd->s68k_regs[0];
141 p = (unsigned int *)((short *)giz_screen + 321*2+4+2);
142 col_g = (old_reg & 2) ? 0x06000600 : 0;
143 col_r = (old_reg & 1) ? 0xc000c000 : 0;
144 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1;
145 *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2;
146 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
150 static short localPal[0x100];
152 static void blit(const char *fps, const char *notice)
154 int emu_opt = currentConfig.EmuOpt;
158 int lines_flags = 224;
159 // 8bit fast renderer
160 if (Pico.m.dirtyPal) {
162 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
165 if (PicoAHW & PAHW_SVP)
166 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
167 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
168 if (currentConfig.EmuOpt&0x4000)
169 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
170 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
172 else if (!(emu_opt&0x80))
175 // 8bit accurate renderer
176 if (Pico.m.dirtyPal) {
178 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
179 if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
180 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
181 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO?
182 memcpy32((void *)(localPal+0xc0), (void *)(localPal+0x40), 0x40*2/4);
183 localPal[0xc0] = 0x0600;
184 localPal[0xd0] = 0xc000;
185 localPal[0xe0] = 0x0000; // reserved pixels for OSD
186 localPal[0xf0] = 0xffff;
189 else if (rendstatus & 0x20) { // mid-frame palette changes
190 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
191 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
194 lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
195 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
196 if (currentConfig.EmuOpt&0x4000)
197 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
198 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
201 if (notice || (emu_opt & 2)) {
203 if (notice) osd_text(4, h, notice);
204 if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps);
207 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
211 // clears whole screen or just the notice area (in all buffers)
212 static void clearArea(int full)
214 if (giz_screen == NULL)
215 giz_screen = fb_lock(1);
216 if (full) memset32(giz_screen, 0, 320*240*2/4);
217 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
219 if (currentConfig.EmuOpt&0x8000) {
221 giz_screen = fb_lock(0);
222 if (full) memset32(giz_screen, 0, 320*240*2/4);
223 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
227 static void vidResetMode(void)
229 giz_screen = fb_lock(1);
232 } else if (currentConfig.EmuOpt&0x80) {
233 PicoDrawSetOutFormat(PDF_RGB555, 0);
234 PicoDrawSetCallbacks(EmuScanBegin16, NULL);
236 PicoDrawSetOutFormat(PDF_NONE, 0);
237 PicoDrawSetCallbacks(EmuScanBegin8, NULL);
239 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) {
240 // setup pal for 8-bit modes
241 localPal[0xc0] = 0x0600;
242 localPal[0xd0] = 0xc000;
243 localPal[0xe0] = 0x0000; // reserved pixels for OSD
244 localPal[0xf0] = 0xffff;
248 memset32(giz_screen, 0, 321*240*2/4);
249 if (currentConfig.EmuOpt&0x8000) {
251 giz_screen = fb_lock(0);
252 memset32(giz_screen, 0, 321*240*2/4);
260 static void stdbg(const char *fmt, ...)
265 sprintf(noticeMsg, "%x ", cnt++);
267 vsnprintf(noticeMsg+strlen(noticeMsg), sizeof(noticeMsg)-strlen(noticeMsg), fmt, vl);
270 noticeMsgTime = GetTickCount();
274 static void updateSound(int len)
276 snd_all_samples += len / 2;
278 if (PsndOut - snd_cbuff >= snd_cbuf_samples)
280 //if (PsndOut - snd_cbuff != snd_cbuf_samples)
281 // stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
287 static void SkipFrame(void)
294 /* forced frame to front buffer */
295 void pemu_forced_frame(int no_scale, int do_emu)
297 int po_old = PicoOpt;
298 int eo_old = currentConfig.EmuOpt;
301 PicoOpt |= POPT_ACC_SPRITES;
303 PicoOpt |= POPT_EN_SOFTSCALE;
304 currentConfig.EmuOpt |= 0x80;
306 if (giz_screen == NULL)
307 giz_screen = fb_lock(1);
309 PicoDrawSetOutFormat(PDF_RGB555, 0);
310 PicoDrawSetCallbacks(EmuScanBegin16, NULL);
318 currentConfig.EmuOpt = eo_old;
322 static void RunEvents(unsigned int which)
324 if (which & 0x1800) // save or load (but not both)
329 FrameworkAudio_SetPause(1);
330 if (giz_screen == NULL)
331 giz_screen = fb_lock(1);
332 if ( emu_check_save_file(state_slot) &&
333 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
334 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
337 blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)");
338 while( !((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) )
340 if (keys & PBTN_STOP) do_it = 0;
341 while( ((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) ) // wait for release
348 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
349 PicoStateProgressCB = emu_stateCb;
350 emu_save_load_game((which & 0x1000) >> 12, 0);
351 PicoStateProgressCB = NULL;
356 FrameworkAudio_SetPause(0);
359 if (which & 0x0400) // switch renderer
361 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
362 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
367 strcpy(noticeMsg, " 8bit fast renderer");
368 } else if (currentConfig.EmuOpt&0x80) {
369 strcpy(noticeMsg, "16bit accurate renderer");
371 strcpy(noticeMsg, " 8bit accurate renderer");
374 noticeMsgTime = GetTickCount();
380 if(state_slot < 0) state_slot = 9;
383 if(state_slot > 9) state_slot = 0;
385 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE");
386 noticeMsgTime = GetTickCount();
390 static void updateKeys(void)
392 unsigned int keys, allActions[2] = { 0, 0 }, events;
393 static unsigned int prevEvents = 0;
396 /* FIXME: port to input fw, merge with emu.c:emu_update_input() */
397 keys = Framework_PollGetButtons();
398 if (keys & PBTN_HOME)
399 engineState = PGS_Menu;
401 keys &= CONFIGURABLE_KEYS;
403 PicoPad[0] = allActions[0] & 0xfff;
404 PicoPad[1] = allActions[1] & 0xfff;
406 if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
407 if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
409 events = (allActions[0] | allActions[1]) >> 16;
411 // volume is treated in special way and triggered every frame
412 if ((events & 0x6000) && PsndOut != NULL)
414 int vol = currentConfig.volume;
415 if (events & 0x2000) {
416 if (vol < 100) vol++;
420 FrameworkAudio_SetVolume(vol, vol);
421 sprintf(noticeMsg, "VOL: %02i ", vol);
422 noticeMsgTime = GetTickCount();
423 currentConfig.volume = vol;
426 events &= ~prevEvents;
427 if (events) RunEvents(events);
428 if (movie_data) emu_updateMovie();
430 prevEvents = (allActions[0] | allActions[1]) >> 16;
433 void plat_debug_cat(char *str)
437 static void simpleWait(DWORD until)
442 tval = GetTickCount();
443 diff = (int)until - (int)tval;
447 while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection
448 spend_cycles(1024*2);
453 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
454 char fpsbuff[24]; // fps count c string
455 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
456 int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000;
457 int target_fps, target_frametime, lim_time, tval_diff, i;
460 lprintf("entered emu_Loop()\n");
464 // make sure we are in correct mode
466 if (currentConfig.scaling) PicoOpt|=0x4000;
467 else PicoOpt&=~0x4000;
469 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
471 // pal/ntsc might have changed, reset related stuff
472 target_fps = Pico.m.pal ? 50 : 60;
473 target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1;
477 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
479 // prepare sound stuff
481 if (currentConfig.EmuOpt & 4)
483 int ret, snd_excess_add, stereo;
484 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
485 PsndRerate(Pico.m.frame_count ? 1 : 0);
487 stereo=(PicoOpt&8)>>3;
488 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
489 snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
490 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
491 PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal);
492 ret = FrameworkAudio_Init(PsndRate, snd_cbuf_samples, stereo);
494 lprintf("FrameworkAudio_Init() failed: %i\n", ret);
495 sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
496 noticeMsgTime = GetTickCount();
497 currentConfig.EmuOpt &= ~4;
499 FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume);
500 PicoWriteSound = updateSound;
501 snd_cbuff = FrameworkAudio_56448Buffer();
502 PsndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle
504 PsndRate_old = PsndRate;
505 PicoOpt_old = PicoOpt;
506 pal_old = Pico.m.pal;
511 while (engineState == PGS_Running)
515 tval = GetTickCount();
516 if (reset_timing || tval < tval_prev) {
517 //stdbg("timing reset");
520 frames_shown = frames_done = 0;
523 // show notice message?
525 static int noticeMsgSum;
526 if (tval - noticeMsgTime > 2000) { // > 2.0 sec
531 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
532 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
537 // check for mode changes
538 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
539 if (modes != oldmodes) {
545 if (tval - tval_thissec >= sec_ms)
548 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
551 bench_fps_s = bench_fps;
552 bf[bfp++ & 3] = bench_fps;
555 bench_fps += frames_shown;
556 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
558 if(currentConfig.EmuOpt & 2)
559 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
561 //tval_thissec += 1000;
562 tval_thissec += sec_ms;
566 /* some code which tries to sync things to audio clock, the dirty way */
567 static int audio_skew_prev = 0;
568 int audio_skew, adj, co = 9, shift = 7;
569 audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos();
570 if (PsndRate == 22050) co = 10;
571 if (PsndRate > 22050) co = 11;
572 if (PicoOpt&8) shift++;
573 if (audio_skew < 0) {
574 adj = -((-audio_skew) >> shift);
575 if (audio_skew > -(6<<co)) adj>>=1;
576 if (audio_skew > -(4<<co)) adj>>=1;
577 if (audio_skew > -(2<<co)) adj>>=1;
578 if (audio_skew > audio_skew_prev) adj>>=2; // going up already
580 adj = audio_skew >> shift;
581 if (audio_skew < (6<<co)) adj>>=1;
582 if (audio_skew < (4<<co)) adj>>=1;
583 if (audio_skew < (2<<co)) adj>>=1;
584 if (audio_skew < audio_skew_prev) adj>>=2;
586 audio_skew_prev = audio_skew;
587 target_frametime += adj;
588 sec_ms = (target_frametime * target_fps) >> 8;
589 //stdbg("%i %i %i", audio_skew, adj, sec_ms);
590 frames_done = frames_shown = 0;
592 else if (currentConfig.Frameskip < 0) {
593 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
594 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
595 if (frames_shown > frames_done) frames_shown = frames_done;
597 frames_done = frames_shown = 0;
601 sprintf(fpsbuff, "%i", Pico.m.frame_count);
605 lim_time = (frames_done+1) * target_frametime;
606 if (currentConfig.Frameskip >= 0) // frameskip enabled
608 for (i = 0; i < currentConfig.Frameskip; i++) {
610 SkipFrame(); frames_done++;
611 if (PsndOut) { // do framelimitting if sound is enabled
613 tval = GetTickCount();
614 tval_diff = (int)(tval - tval_thissec) << 8;
615 if (tval_diff < lim_time) // we are too fast
616 simpleWait(tval + ((lim_time - tval_diff)>>8));
618 lim_time += target_frametime;
621 else // auto frameskip
624 tval = GetTickCount();
625 tval_diff = (int)(tval - tval_thissec) << 8;
626 if (tval_diff > lim_time)
628 // no time left for this frame - skip
629 if (tval_diff - lim_time >= (300<<8)) {
630 /* something caused a slowdown for us (disk access? cache flush?)
631 * try to recover by resetting timing... */
636 SkipFrame(); frames_done++;
643 if (currentConfig.EmuOpt&0x80)
644 /* be sure correct framebuffer is locked */
645 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
649 if (giz_screen == NULL)
650 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
652 blit(fpsbuff, notice);
654 if (giz_screen != NULL) {
659 if (currentConfig.EmuOpt&0x2000)
660 Framework2D_WaitVSync();
662 if (currentConfig.EmuOpt&0x8000)
666 tval = GetTickCount();
667 tval_diff = (int)(tval - tval_thissec) << 8;
669 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
671 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
673 // sleep if we are still too fast
674 if (tval_diff < lim_time)
677 simpleWait(tval + ((lim_time - tval_diff) >> 8));
681 frames_done++; frames_shown++;
685 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
687 if (PsndOut != NULL) {
688 PsndOut = snd_cbuff = NULL;
689 FrameworkAudio_Close();
693 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
694 emu_stateCb("Writing SRAM/BRAM..");
695 emu_save_load_game(0, 1);