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];
30 static short *snd_cbuff = NULL;
31 static int snd_cbuf_samples = 0, snd_all_samples = 0;
34 static void blit(const char *fps, const char *notice);
35 static void clearArea(int full);
37 int plat_get_root_dir(char *dst, int len)
39 if (len > 0) *dst = 0;
44 static void emu_msg_cb(const char *msg)
46 if (giz_screen != NULL) fb_unlock();
47 giz_screen = fb_lock(1);
49 memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
50 emu_text_out16(4, 232, msg);
51 noticeMsgTime = GetTickCount() - 2000;
53 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
57 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
60 void emu_stateCb(const char *str)
62 if (giz_screen != NULL) fb_unlock();
63 giz_screen = fb_lock(1);
71 Sleep(0); /* yield the CPU, the system may need it */
74 void pemu_prep_defconfig(void)
76 defaultConfig.s_PsndRate = 22050;
77 defaultConfig.s_PicoCDBuffers = 0;
78 defaultConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
79 defaultConfig.KeyBinds[ 3] = 1<<1;
80 defaultConfig.KeyBinds[ 0] = 1<<2;
81 defaultConfig.KeyBinds[ 1] = 1<<3;
82 defaultConfig.KeyBinds[ 5] = 1<<4;
83 defaultConfig.KeyBinds[ 6] = 1<<5;
84 defaultConfig.KeyBinds[ 7] = 1<<6;
85 defaultConfig.KeyBinds[ 4] = 1<<7;
86 defaultConfig.KeyBinds[13] = 1<<26; // switch rend
87 defaultConfig.KeyBinds[ 8] = 1<<27; // save state
88 defaultConfig.KeyBinds[ 9] = 1<<28; // load state
89 defaultConfig.KeyBinds[12] = 1<<29; // vol up
90 defaultConfig.KeyBinds[11] = 1<<30; // vol down
91 defaultConfig.scaling = 0;
95 static int EmuScanBegin16(unsigned int num)
97 DrawLineDest = (unsigned short *) giz_screen + 321 * num;
99 if ((currentConfig.EmuOpt&0x4000) && (num&1) == 0) // (Pico.m.frame_count&1))
100 return 1; // skip next line
105 static int EmuScanBegin8(unsigned int num)
107 // draw like the fast renderer
108 HighCol = gfx_buffer + 328 * num;
113 static void osd_text(int x, int y, const char *text)
115 int len = strlen(text) * 8 / 2;
117 for (h = 0; h < 8; h++) {
118 p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
119 p = (int *) ((int)p & ~3); // align
122 emu_text_out16(x, y, text);
126 void log1(void *p1, void *p2)
128 lprintf("%p %p %p\n", p1, p2, DrawLineDest);
132 static void cd_leds(void)
134 static int old_reg = 0;
135 unsigned int col_g, col_r, *p;
137 if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
138 old_reg = Pico_mcd->s68k_regs[0];
140 p = (unsigned int *)((short *)giz_screen + 321*2+4+2);
141 col_g = (old_reg & 2) ? 0x06000600 : 0;
142 col_r = (old_reg & 1) ? 0xc000c000 : 0;
143 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1;
144 *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2;
145 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
149 static short localPal[0x100];
151 static void blit(const char *fps, const char *notice)
153 int emu_opt = currentConfig.EmuOpt;
157 int lines_flags = 224;
158 // 8bit fast renderer
159 if (Pico.m.dirtyPal) {
161 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
164 if (PicoAHW & PAHW_SVP)
165 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
166 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
167 if (currentConfig.EmuOpt&0x4000)
168 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
169 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
171 else if (!(emu_opt&0x80))
174 // 8bit accurate renderer
175 if (Pico.m.dirtyPal) {
177 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
178 if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
179 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
180 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO?
181 memcpy32((void *)(localPal+0xc0), (void *)(localPal+0x40), 0x40*2/4);
182 localPal[0xc0] = 0x0600;
183 localPal[0xd0] = 0xc000;
184 localPal[0xe0] = 0x0000; // reserved pixels for OSD
185 localPal[0xf0] = 0xffff;
188 else if (rendstatus & 0x20) { // mid-frame palette changes
189 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
190 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
193 lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
194 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
195 if (currentConfig.EmuOpt&0x4000)
196 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
197 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
200 if (notice || (emu_opt & 2)) {
202 if (notice) osd_text(4, h, notice);
203 if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps);
206 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
210 // clears whole screen or just the notice area (in all buffers)
211 static void clearArea(int full)
213 if (giz_screen == NULL)
214 giz_screen = fb_lock(1);
215 if (full) memset32(giz_screen, 0, 320*240*2/4);
216 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
218 if (currentConfig.EmuOpt&0x8000) {
220 giz_screen = fb_lock(0);
221 if (full) memset32(giz_screen, 0, 320*240*2/4);
222 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
226 static void vidResetMode(void)
228 giz_screen = fb_lock(1);
231 } else if (currentConfig.EmuOpt&0x80) {
232 PicoDrawSetOutFormat(PDF_RGB555, 0);
233 PicoDrawSetCallbacks(EmuScanBegin16, NULL);
235 PicoDrawSetOutFormat(PDF_NONE, 0);
236 PicoDrawSetCallbacks(EmuScanBegin8, NULL);
238 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) {
239 // setup pal for 8-bit modes
240 localPal[0xc0] = 0x0600;
241 localPal[0xd0] = 0xc000;
242 localPal[0xe0] = 0x0000; // reserved pixels for OSD
243 localPal[0xf0] = 0xffff;
247 memset32(giz_screen, 0, 321*240*2/4);
248 if (currentConfig.EmuOpt&0x8000) {
250 giz_screen = fb_lock(0);
251 memset32(giz_screen, 0, 321*240*2/4);
259 static void stdbg(const char *fmt, ...)
264 sprintf(noticeMsg, "%x ", cnt++);
266 vsnprintf(noticeMsg+strlen(noticeMsg), sizeof(noticeMsg)-strlen(noticeMsg), fmt, vl);
269 noticeMsgTime = GetTickCount();
273 static void updateSound(int len)
275 snd_all_samples += len / 2;
277 if (PsndOut - snd_cbuff >= snd_cbuf_samples)
279 //if (PsndOut - snd_cbuff != snd_cbuf_samples)
280 // stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
286 static void SkipFrame(void)
293 /* forced frame to front buffer */
294 void pemu_forced_frame(int no_scale, int do_emu)
296 int po_old = PicoOpt;
297 int eo_old = currentConfig.EmuOpt;
300 PicoOpt |= POPT_ACC_SPRITES;
302 PicoOpt |= POPT_EN_SOFTSCALE;
303 currentConfig.EmuOpt |= 0x80;
305 if (giz_screen == NULL)
306 giz_screen = fb_lock(1);
308 PicoDrawSetOutFormat(PDF_RGB555, 0);
309 PicoDrawSetCallbacks(EmuScanBegin16, NULL);
317 currentConfig.EmuOpt = eo_old;
321 static void RunEvents(unsigned int which)
323 if (which & 0x1800) // save or load (but not both)
328 FrameworkAudio_SetPause(1);
329 if (giz_screen == NULL)
330 giz_screen = fb_lock(1);
331 if ( emu_check_save_file(state_slot) &&
332 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
333 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
336 blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)");
337 while( !((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) )
339 if (keys & PBTN_STOP) do_it = 0;
340 while( ((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) ) // wait for release
347 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
348 PicoStateProgressCB = emu_stateCb;
349 emu_save_load_game((which & 0x1000) >> 12, 0);
350 PicoStateProgressCB = NULL;
355 FrameworkAudio_SetPause(0);
358 if (which & 0x0400) // switch renderer
360 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
361 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
366 strcpy(noticeMsg, " 8bit fast renderer");
367 } else if (currentConfig.EmuOpt&0x80) {
368 strcpy(noticeMsg, "16bit accurate renderer");
370 strcpy(noticeMsg, " 8bit accurate renderer");
373 noticeMsgTime = GetTickCount();
379 if(state_slot < 0) state_slot = 9;
382 if(state_slot > 9) state_slot = 0;
384 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE");
385 noticeMsgTime = GetTickCount();
389 static void updateKeys(void)
391 unsigned int keys, allActions[2] = { 0, 0 }, events;
392 static unsigned int prevEvents = 0;
395 /* FIXME: port to input fw, merge with emu.c:emu_update_input() */
396 keys = Framework_PollGetButtons();
397 if (keys & PBTN_HOME)
398 engineState = PGS_Menu;
400 keys &= CONFIGURABLE_KEYS;
402 PicoPad[0] = allActions[0] & 0xfff;
403 PicoPad[1] = allActions[1] & 0xfff;
405 if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
406 if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
408 events = (allActions[0] | allActions[1]) >> 16;
410 // volume is treated in special way and triggered every frame
411 if ((events & 0x6000) && PsndOut != NULL)
413 int vol = currentConfig.volume;
414 if (events & 0x2000) {
415 if (vol < 100) vol++;
419 FrameworkAudio_SetVolume(vol, vol);
420 sprintf(noticeMsg, "VOL: %02i ", vol);
421 noticeMsgTime = GetTickCount();
422 currentConfig.volume = vol;
425 events &= ~prevEvents;
426 if (events) RunEvents(events);
427 if (movie_data) emu_updateMovie();
429 prevEvents = (allActions[0] | allActions[1]) >> 16;
432 void plat_debug_cat(char *str)
436 static void simpleWait(DWORD until)
441 tval = GetTickCount();
442 diff = (int)until - (int)tval;
446 while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection
447 spend_cycles(1024*2);
452 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
453 char fpsbuff[24]; // fps count c string
454 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
455 int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000;
456 int target_fps, target_frametime, lim_time, tval_diff, i;
459 lprintf("entered emu_Loop()\n");
463 // make sure we are in correct mode
465 if (currentConfig.scaling) PicoOpt|=0x4000;
466 else PicoOpt&=~0x4000;
468 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
470 // pal/ntsc might have changed, reset related stuff
471 target_fps = Pico.m.pal ? 50 : 60;
472 target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1;
476 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
478 // prepare sound stuff
480 if (currentConfig.EmuOpt & 4)
482 int ret, snd_excess_add, stereo;
483 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
484 PsndRerate(Pico.m.frame_count ? 1 : 0);
486 stereo=(PicoOpt&8)>>3;
487 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
488 snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
489 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
490 PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal);
491 ret = FrameworkAudio_Init(PsndRate, snd_cbuf_samples, stereo);
493 lprintf("FrameworkAudio_Init() failed: %i\n", ret);
494 sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
495 noticeMsgTime = GetTickCount();
496 currentConfig.EmuOpt &= ~4;
498 FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume);
499 PicoWriteSound = updateSound;
500 snd_cbuff = FrameworkAudio_56448Buffer();
501 PsndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle
503 PsndRate_old = PsndRate;
504 PicoOpt_old = PicoOpt;
505 pal_old = Pico.m.pal;
510 while (engineState == PGS_Running)
514 tval = GetTickCount();
515 if (reset_timing || tval < tval_prev) {
516 //stdbg("timing reset");
519 frames_shown = frames_done = 0;
522 // show notice message?
524 static int noticeMsgSum;
525 if (tval - noticeMsgTime > 2000) { // > 2.0 sec
530 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
531 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
536 // check for mode changes
537 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
538 if (modes != oldmodes) {
544 if (tval - tval_thissec >= sec_ms)
547 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
550 bench_fps_s = bench_fps;
551 bf[bfp++ & 3] = bench_fps;
554 bench_fps += frames_shown;
555 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
557 if(currentConfig.EmuOpt & 2)
558 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
560 //tval_thissec += 1000;
561 tval_thissec += sec_ms;
565 /* some code which tries to sync things to audio clock, the dirty way */
566 static int audio_skew_prev = 0;
567 int audio_skew, adj, co = 9, shift = 7;
568 audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos();
569 if (PsndRate == 22050) co = 10;
570 if (PsndRate > 22050) co = 11;
571 if (PicoOpt&8) shift++;
572 if (audio_skew < 0) {
573 adj = -((-audio_skew) >> shift);
574 if (audio_skew > -(6<<co)) adj>>=1;
575 if (audio_skew > -(4<<co)) adj>>=1;
576 if (audio_skew > -(2<<co)) adj>>=1;
577 if (audio_skew > audio_skew_prev) adj>>=2; // going up already
579 adj = audio_skew >> shift;
580 if (audio_skew < (6<<co)) adj>>=1;
581 if (audio_skew < (4<<co)) adj>>=1;
582 if (audio_skew < (2<<co)) adj>>=1;
583 if (audio_skew < audio_skew_prev) adj>>=2;
585 audio_skew_prev = audio_skew;
586 target_frametime += adj;
587 sec_ms = (target_frametime * target_fps) >> 8;
588 //stdbg("%i %i %i", audio_skew, adj, sec_ms);
589 frames_done = frames_shown = 0;
591 else if (currentConfig.Frameskip < 0) {
592 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
593 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
594 if (frames_shown > frames_done) frames_shown = frames_done;
596 frames_done = frames_shown = 0;
600 sprintf(fpsbuff, "%i", Pico.m.frame_count);
604 lim_time = (frames_done+1) * target_frametime;
605 if (currentConfig.Frameskip >= 0) // frameskip enabled
607 for (i = 0; i < currentConfig.Frameskip; i++) {
609 SkipFrame(); frames_done++;
610 if (PsndOut) { // do framelimitting if sound is enabled
612 tval = GetTickCount();
613 tval_diff = (int)(tval - tval_thissec) << 8;
614 if (tval_diff < lim_time) // we are too fast
615 simpleWait(tval + ((lim_time - tval_diff)>>8));
617 lim_time += target_frametime;
620 else // auto frameskip
623 tval = GetTickCount();
624 tval_diff = (int)(tval - tval_thissec) << 8;
625 if (tval_diff > lim_time)
627 // no time left for this frame - skip
628 if (tval_diff - lim_time >= (300<<8)) {
629 /* something caused a slowdown for us (disk access? cache flush?)
630 * try to recover by resetting timing... */
635 SkipFrame(); frames_done++;
642 if (currentConfig.EmuOpt&0x80)
643 /* be sure correct framebuffer is locked */
644 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
648 if (giz_screen == NULL)
649 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
651 blit(fpsbuff, notice);
653 if (giz_screen != NULL) {
658 if (currentConfig.EmuOpt&0x2000)
659 Framework2D_WaitVSync();
661 if (currentConfig.EmuOpt&0x8000)
665 tval = GetTickCount();
666 tval_diff = (int)(tval - tval_thissec) << 8;
668 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
670 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
672 // sleep if we are still too fast
673 if (tval_diff < lim_time)
676 simpleWait(tval + ((lim_time - tval_diff) >> 8));
680 frames_done++; frames_shown++;
684 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
686 if (PsndOut != NULL) {
687 PsndOut = snd_cbuff = NULL;
688 FrameworkAudio_Close();
692 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
693 emu_stateCb("Writing SRAM/BRAM..");
694 emu_save_load_game(0, 1);