emu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. #include <windows.h>
  2. #include <string.h>
  3. #include <sys/stat.h> // mkdir
  4. #include <sys/types.h>
  5. #include "kgsdk/Framework.h"
  6. #include "kgsdk/Framework2D.h"
  7. #include "kgsdk/FrameworkAudio.h"
  8. #include "../common/emu.h"
  9. #include "../common/arm_utils.h"
  10. #include "../common/config.h"
  11. #include "../libpicofe/lprintf.h"
  12. #include "emu.h"
  13. #include "menu.h"
  14. #include "giz.h"
  15. #include "asm_utils.h"
  16. #include <pico/pico_int.h>
  17. #ifdef BENCHMARK
  18. #define OSD_FPS_X 220
  19. #else
  20. #define OSD_FPS_X 260
  21. #endif
  22. // main 300K gfx-related buffer. Used by menu and renderers.
  23. unsigned char gfx_buffer[321*240*2*2];
  24. static short *snd_cbuff = NULL;
  25. static int snd_cbuf_samples = 0, snd_all_samples = 0;
  26. static void blit(const char *fps, const char *notice);
  27. static void clearArea(int full);
  28. int plat_get_root_dir(char *dst, int len)
  29. {
  30. if (len > 0) *dst = 0;
  31. return 0;
  32. }
  33. static void emu_msg_cb(const char *msg)
  34. {
  35. if (giz_screen != NULL) fb_unlock();
  36. giz_screen = fb_lock(1);
  37. memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
  38. emu_text_out16(4, 232, msg);
  39. noticeMsgTime = GetTickCount() - 2000;
  40. /* assumption: emu_msg_cb gets called only when something slow is about to happen */
  41. reset_timing = 1;
  42. fb_unlock();
  43. giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
  44. }
  45. void emu_stateCb(const char *str)
  46. {
  47. if (giz_screen != NULL) fb_unlock();
  48. giz_screen = fb_lock(1);
  49. clearArea(0);
  50. blit("", str);
  51. fb_unlock();
  52. giz_screen = NULL;
  53. Sleep(0); /* yield the CPU, the system may need it */
  54. }
  55. void pemu_prep_defconfig(void)
  56. {
  57. defaultConfig.s_PsndRate = 22050;
  58. defaultConfig.s_PicoCDBuffers = 0;
  59. defaultConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
  60. defaultConfig.KeyBinds[ 3] = 1<<1;
  61. defaultConfig.KeyBinds[ 0] = 1<<2;
  62. defaultConfig.KeyBinds[ 1] = 1<<3;
  63. defaultConfig.KeyBinds[ 5] = 1<<4;
  64. defaultConfig.KeyBinds[ 6] = 1<<5;
  65. defaultConfig.KeyBinds[ 7] = 1<<6;
  66. defaultConfig.KeyBinds[ 4] = 1<<7;
  67. defaultConfig.KeyBinds[13] = 1<<26; // switch rend
  68. defaultConfig.KeyBinds[ 8] = 1<<27; // save state
  69. defaultConfig.KeyBinds[ 9] = 1<<28; // load state
  70. defaultConfig.KeyBinds[12] = 1<<29; // vol up
  71. defaultConfig.KeyBinds[11] = 1<<30; // vol down
  72. defaultConfig.scaling = 0;
  73. }
  74. static int EmuScanBegin16(unsigned int num)
  75. {
  76. Pico.est.DrawLineDest = (unsigned short *) giz_screen + 321 * num;
  77. if ((currentConfig.EmuOpt&0x4000) && (num&1) == 0) // (Pico.m.frame_count&1))
  78. return 1; // skip next line
  79. return 0;
  80. }
  81. static int EmuScanBegin8(unsigned int num)
  82. {
  83. // draw like the fast renderer
  84. Pico.est.HighCol = gfx_buffer + 328 * num;
  85. return 0;
  86. }
  87. static void osd_text(int x, int y, const char *text)
  88. {
  89. int len = strlen(text) * 8 / 2;
  90. int *p, h;
  91. for (h = 0; h < 8; h++) {
  92. p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
  93. p = (int *) ((int)p & ~3); // align
  94. memset32(p, 0, len);
  95. }
  96. emu_text_out16(x, y, text);
  97. }
  98. static void cd_leds(void)
  99. {
  100. static int old_reg = 0;
  101. unsigned int col_g, col_r, *p;
  102. if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
  103. old_reg = Pico_mcd->s68k_regs[0];
  104. p = (unsigned int *)((short *)giz_screen + 321*2+4+2);
  105. col_g = (old_reg & 2) ? 0x06000600 : 0;
  106. col_r = (old_reg & 1) ? 0xc000c000 : 0;
  107. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1;
  108. *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2;
  109. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
  110. }
  111. static short localPal[0x100];
  112. static void blit(const char *fps, const char *notice)
  113. {
  114. int emu_opt = currentConfig.EmuOpt;
  115. if (PicoIn.opt&0x10)
  116. {
  117. int lines_flags = 224;
  118. // 8bit fast renderer
  119. if (Pico.m.dirtyPal) {
  120. Pico.m.dirtyPal = 0;
  121. vidConvCpyRGB565(localPal, Pico.cram, 0x40);
  122. }
  123. // a hack for VR
  124. if (PicoIn.AHW & PAHW_SVP)
  125. memset((int *)(Pico.est.Draw2FB+328*8+328*223), 0xe0e0e0e0, 328*4);
  126. if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
  127. if (currentConfig.EmuOpt&0x4000)
  128. lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
  129. vidCpy8to16((unsigned short *)giz_screen+321*8, Pico.est.Draw2FB+328*8, localPal, lines_flags);
  130. }
  131. else if (!(emu_opt&0x80))
  132. {
  133. int lines_flags;
  134. // 8bit accurate renderer
  135. if (Pico.m.dirtyPal) {
  136. if (Pico.m.dirtyPal == 2)
  137. Pico.m.dirtyPal = 0;
  138. /* no support
  139. switch (Pico.est.SonicPalCount) {
  140. case 3: vidConvCpyRGB565(localPal+0xc0, Pico.est.SonicPal+0xc0, 0x40);
  141. case 2: vidConvCpyRGB565(localPal+0x80, Pico.est.SonicPal+0x80, 0x40);
  142. case 1: vidConvCpyRGB565(localPal+0x40, Pico.est.SonicPal+0x40, 0x40);
  143. default://vidConvCpyRGB565(localPal, Pico.est.SonicPal, 0x40);
  144. } */
  145. vidConvCpyRGB565(localPal, Pico.est.SonicPal, 0x40);
  146. if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
  147. //vidConvCpyRGB32sh(localPal+0x40, Pico.est.SonicPal, 0x40);
  148. //vidConvCpyRGB32hi(localPal+0x80, Pico.est.SonicPal, 0x40); // TODO?
  149. memcpy((void *)(localPal+0xc0), (void *)(localPal+0x40), 0x40*2);
  150. localPal[0xc0] = 0x0600;
  151. localPal[0xd0] = 0xc000;
  152. localPal[0xe0] = 0x0000; // reserved pixels for OSD
  153. localPal[0xf0] = 0xffff;
  154. }
  155. }
  156. lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
  157. if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
  158. if (currentConfig.EmuOpt&0x4000)
  159. lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
  160. vidCpy8to16((unsigned short *)giz_screen+321*8, Pico.est.Draw2FB+328*8, localPal, lines_flags);
  161. }
  162. if (notice || (emu_opt & 2)) {
  163. int h = 232;
  164. if (notice) osd_text(4, h, notice);
  165. if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps);
  166. }
  167. if ((emu_opt & 0x400) && (PicoIn.AHW & PAHW_MCD))
  168. cd_leds();
  169. }
  170. // clears whole screen or just the notice area (in all buffers)
  171. static void clearArea(int full)
  172. {
  173. if (giz_screen == NULL)
  174. giz_screen = fb_lock(1);
  175. if (full) memset32(giz_screen, 0, 320*240*2/4);
  176. else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
  177. if (currentConfig.EmuOpt&0x8000) {
  178. fb_unlock();
  179. giz_screen = fb_lock(0);
  180. if (full) memset32(giz_screen, 0, 320*240*2/4);
  181. else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
  182. }
  183. }
  184. static void vidResetMode(void)
  185. {
  186. giz_screen = fb_lock(1);
  187. if (PicoIn.opt&0x10) {
  188. } else if (currentConfig.EmuOpt&0x80) {
  189. PicoDrawSetOutFormat(PDF_RGB555, 0);
  190. PicoDrawSetCallbacks(EmuScanBegin16, NULL);
  191. } else {
  192. PicoDrawSetOutFormat(PDF_NONE, 0);
  193. PicoDrawSetCallbacks(EmuScanBegin8, NULL);
  194. }
  195. if ((PicoIn.opt&0x10) || !(currentConfig.EmuOpt&0x80)) {
  196. // setup pal for 8-bit modes
  197. localPal[0xc0] = 0x0600;
  198. localPal[0xd0] = 0xc000;
  199. localPal[0xe0] = 0x0000; // reserved pixels for OSD
  200. localPal[0xf0] = 0xffff;
  201. }
  202. Pico.m.dirtyPal = 1;
  203. memset32(giz_screen, 0, 321*240*2/4);
  204. if (currentConfig.EmuOpt&0x8000) {
  205. fb_unlock();
  206. giz_screen = fb_lock(0);
  207. memset32(giz_screen, 0, 321*240*2/4);
  208. }
  209. fb_unlock();
  210. giz_screen = NULL;
  211. }
  212. /*
  213. #include <stdarg.h>
  214. static void stdbg(const char *fmt, ...)
  215. {
  216. static int cnt = 0;
  217. va_list vl;
  218. sprintf(noticeMsg, "%x ", cnt++);
  219. va_start(vl, fmt);
  220. vsnprintf(noticeMsg+strlen(noticeMsg), sizeof(noticeMsg)-strlen(noticeMsg), fmt, vl);
  221. va_end(vl);
  222. noticeMsgTime = GetTickCount();
  223. }
  224. */
  225. static void updateSound(int len)
  226. {
  227. snd_all_samples += len / 2;
  228. PicoIn.sndOut += len / 2;
  229. if (PicoIn.sndOut - snd_cbuff >= snd_cbuf_samples)
  230. {
  231. //if (PicoIn.sndOut - snd_cbuff != snd_cbuf_samples)
  232. // stdbg("snd diff is %i, not %i", PicoIn.sndOut - snd_cbuff, snd_cbuf_samples);
  233. PicoIn.sndOut = snd_cbuff;
  234. }
  235. }
  236. static void SkipFrame(void)
  237. {
  238. PicoIn.skipFrame=1;
  239. PicoFrame();
  240. PicoIn.skipFrame=0;
  241. }
  242. /* forced frame to front buffer */
  243. void pemu_forced_frame(int no_scale, int do_emu)
  244. {
  245. int po_old = PicoIn.opt;
  246. int eo_old = currentConfig.EmuOpt;
  247. PicoIn.opt &= ~0x10;
  248. PicoIn.opt |= POPT_ACC_SPRITES;
  249. if (!no_scale && currentConfig.scaling)
  250. PicoIn.opt |= POPT_EN_SOFTSCALE;
  251. currentConfig.EmuOpt |= 0x80;
  252. if (giz_screen == NULL)
  253. giz_screen = fb_lock(1);
  254. PicoDrawSetOutFormat(PDF_RGB555, 0);
  255. PicoDrawSetCallbacks(EmuScanBegin16, NULL);
  256. Pico.m.dirtyPal = 1;
  257. PicoFrameDrawOnly();
  258. fb_unlock();
  259. giz_screen = NULL;
  260. PicoIn.opt = po_old;
  261. currentConfig.EmuOpt = eo_old;
  262. }
  263. static void RunEvents(unsigned int which)
  264. {
  265. if (which & 0x1800) // save or load (but not both)
  266. {
  267. int do_it = 1;
  268. if (PicoIn.sndOut != NULL)
  269. FrameworkAudio_SetPause(1);
  270. if (giz_screen == NULL)
  271. giz_screen = fb_lock(1);
  272. if ( emu_check_save_file(state_slot) &&
  273. (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
  274. (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
  275. {
  276. int keys;
  277. blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)");
  278. while( !((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) )
  279. Sleep(50);
  280. if (keys & PBTN_STOP) do_it = 0;
  281. while( ((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) ) // wait for release
  282. Sleep(50);
  283. clearArea(0);
  284. }
  285. if (do_it)
  286. {
  287. osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
  288. PicoStateProgressCB = emu_stateCb;
  289. emu_save_load_game((which & 0x1000) >> 12, 0);
  290. PicoStateProgressCB = NULL;
  291. Sleep(0);
  292. }
  293. if (PicoIn.sndOut != NULL)
  294. FrameworkAudio_SetPause(0);
  295. reset_timing = 1;
  296. }
  297. if (which & 0x0400) // switch renderer
  298. {
  299. if (PicoIn.opt&0x10) { PicoIn.opt&=~0x10; currentConfig.EmuOpt |= 0x80; }
  300. else { PicoIn.opt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
  301. vidResetMode();
  302. if (PicoIn.opt&0x10) {
  303. strcpy(noticeMsg, " 8bit fast renderer");
  304. } else if (currentConfig.EmuOpt&0x80) {
  305. strcpy(noticeMsg, "16bit accurate renderer");
  306. } else {
  307. strcpy(noticeMsg, " 8bit accurate renderer");
  308. }
  309. noticeMsgTime = GetTickCount();
  310. }
  311. if (which & 0x0300)
  312. {
  313. if(which&0x0200) {
  314. state_slot -= 1;
  315. if(state_slot < 0) state_slot = 9;
  316. } else {
  317. state_slot += 1;
  318. if(state_slot > 9) state_slot = 0;
  319. }
  320. sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_check_save_file(state_slot) ? "USED" : "FREE");
  321. noticeMsgTime = GetTickCount();
  322. }
  323. }
  324. static void updateKeys(void)
  325. {
  326. unsigned int keys, allActions[2] = { 0, 0 }, events;
  327. static unsigned int prevEvents = 0;
  328. int i;
  329. /* FIXME: port to input fw, merge with emu.c:emu_update_input() */
  330. keys = Framework_PollGetButtons();
  331. if (keys & PBTN_HOME)
  332. engineState = PGS_Menu;
  333. keys &= CONFIGURABLE_KEYS;
  334. PicoIn.pad[0] = allActions[0] & 0xfff;
  335. PicoIn.pad[1] = allActions[1] & 0xfff;
  336. if (allActions[0] & 0x7000) emu_DoTurbo(&PicoIn.pad[0], allActions[0]);
  337. if (allActions[1] & 0x7000) emu_DoTurbo(&PicoIn.pad[1], allActions[1]);
  338. events = (allActions[0] | allActions[1]) >> 16;
  339. // volume is treated in special way and triggered every frame
  340. if ((events & 0x6000) && PicoIn.sndOut != NULL)
  341. {
  342. int vol = currentConfig.volume;
  343. if (events & 0x2000) {
  344. if (vol < 100) vol++;
  345. } else {
  346. if (vol > 0) vol--;
  347. }
  348. FrameworkAudio_SetVolume(vol, vol);
  349. sprintf(noticeMsg, "VOL: %02i ", vol);
  350. noticeMsgTime = GetTickCount();
  351. currentConfig.volume = vol;
  352. }
  353. events &= ~prevEvents;
  354. if (events) RunEvents(events);
  355. if (movie_data) emu_updateMovie();
  356. prevEvents = (allActions[0] | allActions[1]) >> 16;
  357. }
  358. void plat_debug_cat(char *str)
  359. {
  360. }
  361. static void simpleWait(DWORD until)
  362. {
  363. DWORD tval;
  364. int diff;
  365. tval = GetTickCount();
  366. diff = (int)until - (int)tval;
  367. if (diff >= 2)
  368. Sleep(diff - 1);
  369. while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection
  370. spend_cycles(1024*2);
  371. }
  372. void pemu_loop(void)
  373. {
  374. static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
  375. char fpsbuff[24]; // fps count c string
  376. DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
  377. int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000;
  378. int target_fps, target_frametime, lim_time, tval_diff, i;
  379. char *notice = NULL;
  380. lprintf("entered emu_Loop()\n");
  381. fpsbuff[0] = 0;
  382. // make sure we are in correct mode
  383. vidResetMode();
  384. if (currentConfig.scaling) PicoIn.opt|=0x4000;
  385. else PicoIn.opt&=~0x4000;
  386. Pico.m.dirtyPal = 1;
  387. oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
  388. // pal/ntsc might have changed, reset related stuff
  389. target_fps = Pico.m.pal ? 50 : 60;
  390. target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1;
  391. reset_timing = 1;
  392. // prepare CD buffer
  393. if (PicoIn.AHW & PAHW_MCD) PicoCDBufferInit();
  394. // prepare sound stuff
  395. PicoIn.sndOut = NULL;
  396. if (currentConfig.EmuOpt & 4)
  397. {
  398. int ret, snd_excess_add, stereo;
  399. if (PicoIn.sndRate != PsndRate_old || (PicoIn.opt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
  400. PsndRerate(Pico.m.frame_count ? 1 : 0);
  401. }
  402. stereo=(PicoIn.opt&8)>>3;
  403. snd_excess_add = ((PicoIn.sndRate - Pico.snd.len*target_fps)<<16) / target_fps;
  404. snd_cbuf_samples = (PicoIn.sndRate<<stereo) * 16 / target_fps;
  405. lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
  406. PicoIn.sndRate, Pico.snd.len, snd_excess_add, stereo, Pico.m.pal);
  407. ret = FrameworkAudio_Init(PicoIn.sndRate, snd_cbuf_samples, stereo);
  408. if (ret != 0) {
  409. lprintf("FrameworkAudio_Init() failed: %i\n", ret);
  410. sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
  411. noticeMsgTime = GetTickCount();
  412. currentConfig.EmuOpt &= ~4;
  413. } else {
  414. FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume);
  415. PicoIn.writeSound = updateSound;
  416. snd_cbuff = FrameworkAudio_56448Buffer();
  417. PicoIn.sndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle
  418. snd_all_samples = 0;
  419. PsndRate_old = PicoIn.sndRate;
  420. PicoOpt_old = PicoIn.opt;
  421. pal_old = Pico.m.pal;
  422. }
  423. }
  424. // loop?
  425. while (engineState == PGS_Running)
  426. {
  427. int modes;
  428. tval = GetTickCount();
  429. if (reset_timing || tval < tval_prev) {
  430. //stdbg("timing reset");
  431. reset_timing = 0;
  432. tval_thissec = tval;
  433. frames_shown = frames_done = 0;
  434. }
  435. // show notice message?
  436. if (noticeMsgTime) {
  437. static int noticeMsgSum;
  438. if (tval - noticeMsgTime > 2000) { // > 2.0 sec
  439. noticeMsgTime = 0;
  440. clearArea(0);
  441. notice = 0;
  442. } else {
  443. int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
  444. if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
  445. notice = noticeMsg;
  446. }
  447. }
  448. // check for mode changes
  449. modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
  450. if (modes != oldmodes) {
  451. oldmodes = modes;
  452. clearArea(1);
  453. }
  454. // second passed?
  455. if (tval - tval_thissec >= sec_ms)
  456. {
  457. #ifdef BENCHMARK
  458. static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
  459. if(++bench == 10) {
  460. bench = 0;
  461. bench_fps_s = bench_fps;
  462. bf[bfp++ & 3] = bench_fps;
  463. bench_fps = 0;
  464. }
  465. bench_fps += frames_shown;
  466. sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
  467. #else
  468. if(currentConfig.EmuOpt & 2)
  469. sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
  470. #endif
  471. //tval_thissec += 1000;
  472. tval_thissec += sec_ms;
  473. if (PicoIn.sndOut != NULL)
  474. {
  475. /* some code which tries to sync things to audio clock, the dirty way */
  476. static int audio_skew_prev = 0;
  477. int audio_skew, adj, co = 9, shift = 7;
  478. audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos();
  479. if (PicoIn.sndRate == 22050) co = 10;
  480. if (PicoIn.sndRate > 22050) co = 11;
  481. if (PicoIn.opt&8) shift++;
  482. if (audio_skew < 0) {
  483. adj = -((-audio_skew) >> shift);
  484. if (audio_skew > -(6<<co)) adj>>=1;
  485. if (audio_skew > -(4<<co)) adj>>=1;
  486. if (audio_skew > -(2<<co)) adj>>=1;
  487. if (audio_skew > audio_skew_prev) adj>>=2; // going up already
  488. } else {
  489. adj = audio_skew >> shift;
  490. if (audio_skew < (6<<co)) adj>>=1;
  491. if (audio_skew < (4<<co)) adj>>=1;
  492. if (audio_skew < (2<<co)) adj>>=1;
  493. if (audio_skew < audio_skew_prev) adj>>=2;
  494. }
  495. audio_skew_prev = audio_skew;
  496. target_frametime += adj;
  497. sec_ms = (target_frametime * target_fps) >> 8;
  498. //stdbg("%i %i %i", audio_skew, adj, sec_ms);
  499. frames_done = frames_shown = 0;
  500. }
  501. else if (currentConfig.Frameskip < 0) {
  502. frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
  503. frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
  504. if (frames_shown > frames_done) frames_shown = frames_done;
  505. } else {
  506. frames_done = frames_shown = 0;
  507. }
  508. }
  509. #ifdef PFRAMES
  510. sprintf(fpsbuff, "%i", Pico.m.frame_count);
  511. #endif
  512. tval_prev = tval;
  513. lim_time = (frames_done+1) * target_frametime;
  514. if (currentConfig.Frameskip >= 0) // frameskip enabled
  515. {
  516. for (i = 0; i < currentConfig.Frameskip; i++) {
  517. updateKeys();
  518. SkipFrame(); frames_done++;
  519. if (PicoIn.sndOut) { // do framelimitting if sound is enabled
  520. int tval_diff;
  521. tval = GetTickCount();
  522. tval_diff = (int)(tval - tval_thissec) << 8;
  523. if (tval_diff < lim_time) // we are too fast
  524. simpleWait(tval + ((lim_time - tval_diff)>>8));
  525. }
  526. lim_time += target_frametime;
  527. }
  528. }
  529. else // auto frameskip
  530. {
  531. int tval_diff;
  532. tval = GetTickCount();
  533. tval_diff = (int)(tval - tval_thissec) << 8;
  534. if (tval_diff > lim_time)
  535. {
  536. // no time left for this frame - skip
  537. if (tval_diff - lim_time >= (300<<8)) {
  538. /* something caused a slowdown for us (disk access? cache flush?)
  539. * try to recover by resetting timing... */
  540. reset_timing = 1;
  541. continue;
  542. }
  543. updateKeys();
  544. SkipFrame(); frames_done++;
  545. continue;
  546. }
  547. }
  548. updateKeys();
  549. if (currentConfig.EmuOpt&0x80)
  550. /* be sure correct framebuffer is locked */
  551. giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
  552. PicoFrame();
  553. if (giz_screen == NULL)
  554. giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
  555. blit(fpsbuff, notice);
  556. if (giz_screen != NULL) {
  557. fb_unlock();
  558. giz_screen = NULL;
  559. }
  560. if (currentConfig.EmuOpt&0x2000)
  561. Framework2D_WaitVSync();
  562. if (currentConfig.EmuOpt&0x8000)
  563. fb_flip();
  564. // check time
  565. tval = GetTickCount();
  566. tval_diff = (int)(tval - tval_thissec) << 8;
  567. if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
  568. reset_timing = 1;
  569. else if (PicoIn.sndOut != NULL || currentConfig.Frameskip < 0)
  570. {
  571. // sleep if we are still too fast
  572. if (tval_diff < lim_time)
  573. {
  574. // we are too fast
  575. simpleWait(tval + ((lim_time - tval_diff) >> 8));
  576. }
  577. }
  578. frames_done++; frames_shown++;
  579. }
  580. if (PicoIn.AHW & PAHW_MCD) PicoCDBufferFree();
  581. if (PicoIn.sndOut != NULL) {
  582. PicoIn.sndOut = snd_cbuff = NULL;
  583. FrameworkAudio_Close();
  584. }
  585. // save SRAM
  586. if ((currentConfig.EmuOpt & 1) && SRam.changed) {
  587. emu_stateCb("Writing SRAM/BRAM..");
  588. emu_save_load_game(0, 1);
  589. SRam.changed = 0;
  590. }
  591. }