emu.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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/lprintf.h"
  10. #include "../common/arm_utils.h"
  11. #include "../common/config.h"
  12. #include "emu.h"
  13. #include "menu.h"
  14. #include "giz.h"
  15. #include "asm_utils.h"
  16. #include <Pico/PicoInt.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. char romFileName[MAX_PATH];
  25. int engineState;
  26. unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
  27. int reset_timing = 0;
  28. static DWORD noticeMsgTime = 0;
  29. static short *snd_cbuff = NULL;
  30. static int snd_cbuf_samples = 0, snd_all_samples = 0;
  31. static void blit(const char *fps, const char *notice);
  32. static void clearArea(int full);
  33. void emu_noticeMsgUpdated(void)
  34. {
  35. noticeMsgTime = GetTickCount();
  36. }
  37. int emu_getMainDir(char *dst, int len)
  38. {
  39. if (len > 0) *dst = 0;
  40. return 0;
  41. }
  42. static void emu_msg_cb(const char *msg)
  43. {
  44. if (giz_screen != NULL) fb_unlock();
  45. giz_screen = fb_lock(1);
  46. memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
  47. emu_textOut16(4, 232, msg);
  48. noticeMsgTime = GetTickCount() - 2000;
  49. /* assumption: emu_msg_cb gets called only when something slow is about to happen */
  50. reset_timing = 1;
  51. fb_unlock();
  52. giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
  53. }
  54. void emu_stateCb(const char *str)
  55. {
  56. if (giz_screen != NULL) fb_unlock();
  57. giz_screen = fb_lock(1);
  58. clearArea(0);
  59. blit("", str);
  60. fb_unlock();
  61. giz_screen = NULL;
  62. Sleep(0); /* yield the CPU, the system may need it */
  63. }
  64. static void emu_msg_tray_open(void)
  65. {
  66. strcpy(noticeMsg, "CD tray opened");
  67. noticeMsgTime = GetTickCount();
  68. }
  69. void emu_Init(void)
  70. {
  71. // make dirs for saves, cfgs, etc.
  72. mkdir("mds", 0777);
  73. mkdir("srm", 0777);
  74. mkdir("brm", 0777);
  75. mkdir("cfg", 0777);
  76. PicoInit();
  77. PicoMessage = emu_msg_cb;
  78. PicoMCDopenTray = emu_msg_tray_open;
  79. PicoMCDcloseTray = menu_loop_tray;
  80. }
  81. void emu_Deinit(void)
  82. {
  83. // save SRAM
  84. if ((currentConfig.EmuOpt & 1) && SRam.changed) {
  85. emu_SaveLoadGame(0, 1);
  86. SRam.changed = 0;
  87. }
  88. if (!(currentConfig.EmuOpt & 0x20))
  89. config_writelrom(PicoConfigFile);
  90. PicoExit();
  91. }
  92. void emu_prepareDefaultConfig(void)
  93. {
  94. memset(&defaultConfig, 0, sizeof(defaultConfig));
  95. defaultConfig.EmuOpt = 0x1d | 0x680; // | confirm_save, cd_leds, 16bit rend
  96. defaultConfig.s_PicoOpt = 0x0f | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;
  97. defaultConfig.s_PsndRate = 22050;
  98. defaultConfig.s_PicoRegion = 0; // auto
  99. defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
  100. defaultConfig.s_PicoCDBuffers = 0;
  101. defaultConfig.Frameskip = -1; // auto
  102. defaultConfig.volume = 50;
  103. defaultConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
  104. defaultConfig.KeyBinds[ 3] = 1<<1;
  105. defaultConfig.KeyBinds[ 0] = 1<<2;
  106. defaultConfig.KeyBinds[ 1] = 1<<3;
  107. defaultConfig.KeyBinds[ 5] = 1<<4;
  108. defaultConfig.KeyBinds[ 6] = 1<<5;
  109. defaultConfig.KeyBinds[ 7] = 1<<6;
  110. defaultConfig.KeyBinds[ 4] = 1<<7;
  111. defaultConfig.KeyBinds[13] = 1<<26; // switch rend
  112. defaultConfig.KeyBinds[ 8] = 1<<27; // save state
  113. defaultConfig.KeyBinds[ 9] = 1<<28; // load state
  114. defaultConfig.KeyBinds[12] = 1<<29; // vol up
  115. defaultConfig.KeyBinds[11] = 1<<30; // vol down
  116. defaultConfig.scaling = 0;
  117. defaultConfig.turbo_rate = 15;
  118. }
  119. static int EmuScanBegin16(unsigned int num)
  120. {
  121. if (!(Pico.video.reg[1]&8)) num += 8;
  122. DrawLineDest = (unsigned short *) giz_screen + 321 * num;
  123. if ((currentConfig.EmuOpt&0x4000) && (num&1) == 0) // (Pico.m.frame_count&1))
  124. return 1; // skip next line
  125. return 0;
  126. }
  127. static int EmuScanBegin8(unsigned int num)
  128. {
  129. // draw like the fast renderer
  130. if (!(Pico.video.reg[1]&8)) num += 8;
  131. HighCol = gfx_buffer + 328 * num;
  132. return 0;
  133. }
  134. static void osd_text(int x, int y, const char *text)
  135. {
  136. int len = strlen(text) * 8 / 2;
  137. int *p, h;
  138. for (h = 0; h < 8; h++) {
  139. p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
  140. p = (int *) ((int)p & ~3); // align
  141. memset32(p, 0, len);
  142. }
  143. emu_textOut16(x, y, text);
  144. }
  145. /*
  146. void log1(void *p1, void *p2)
  147. {
  148. lprintf("%p %p %p\n", p1, p2, DrawLineDest);
  149. }
  150. */
  151. static void cd_leds(void)
  152. {
  153. static int old_reg = 0;
  154. unsigned int col_g, col_r, *p;
  155. if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
  156. old_reg = Pico_mcd->s68k_regs[0];
  157. p = (unsigned int *)((short *)giz_screen + 321*2+4+2);
  158. col_g = (old_reg & 2) ? 0x06000600 : 0;
  159. col_r = (old_reg & 1) ? 0xc000c000 : 0;
  160. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1;
  161. *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2;
  162. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
  163. }
  164. static short localPal[0x100];
  165. static void blit(const char *fps, const char *notice)
  166. {
  167. int emu_opt = currentConfig.EmuOpt;
  168. if (PicoOpt&0x10)
  169. {
  170. int lines_flags = 224;
  171. // 8bit fast renderer
  172. if (Pico.m.dirtyPal) {
  173. Pico.m.dirtyPal = 0;
  174. vidConvCpyRGB565(localPal, Pico.cram, 0x40);
  175. }
  176. // a hack for VR
  177. if (PicoRead16Hook == PicoSVPRead16)
  178. memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
  179. if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
  180. if (currentConfig.EmuOpt&0x4000)
  181. lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
  182. vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
  183. }
  184. else if (!(emu_opt&0x80))
  185. {
  186. int lines_flags;
  187. // 8bit accurate renderer
  188. if (Pico.m.dirtyPal) {
  189. Pico.m.dirtyPal = 0;
  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. memcpy32((void *)(localPal+0xc0), (void *)(localPal+0x40), 0x40*2/4);
  195. localPal[0xc0] = 0x0600;
  196. localPal[0xd0] = 0xc000;
  197. localPal[0xe0] = 0x0000; // reserved pixels for OSD
  198. localPal[0xf0] = 0xffff;
  199. }
  200. /* no support
  201. else if (rendstatus & 0x20) { // mid-frame palette changes
  202. vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
  203. vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
  204. } */
  205. }
  206. lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
  207. if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
  208. if (currentConfig.EmuOpt&0x4000)
  209. lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
  210. vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
  211. }
  212. if (notice || (emu_opt & 2)) {
  213. int h = 232;
  214. if (notice) osd_text(4, h, notice);
  215. if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps);
  216. }
  217. if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
  218. cd_leds();
  219. }
  220. // clears whole screen or just the notice area (in all buffers)
  221. static void clearArea(int full)
  222. {
  223. if (giz_screen == NULL)
  224. giz_screen = fb_lock(1);
  225. if (full) memset32(giz_screen, 0, 320*240*2/4);
  226. else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
  227. if (currentConfig.EmuOpt&0x8000) {
  228. fb_unlock();
  229. giz_screen = fb_lock(0);
  230. if (full) memset32(giz_screen, 0, 320*240*2/4);
  231. else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
  232. }
  233. }
  234. static void vidResetMode(void)
  235. {
  236. giz_screen = fb_lock(1);
  237. if (PicoOpt&0x10) {
  238. } else if (currentConfig.EmuOpt&0x80) {
  239. PicoDrawSetColorFormat(1);
  240. PicoScanBegin = EmuScanBegin16;
  241. } else {
  242. PicoDrawSetColorFormat(-1);
  243. PicoScanBegin = EmuScanBegin8;
  244. }
  245. if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) {
  246. // setup pal for 8-bit modes
  247. localPal[0xc0] = 0x0600;
  248. localPal[0xd0] = 0xc000;
  249. localPal[0xe0] = 0x0000; // reserved pixels for OSD
  250. localPal[0xf0] = 0xffff;
  251. }
  252. Pico.m.dirtyPal = 1;
  253. memset32(giz_screen, 0, 321*240*2/4);
  254. if (currentConfig.EmuOpt&0x8000) {
  255. fb_unlock();
  256. giz_screen = fb_lock(0);
  257. memset32(giz_screen, 0, 321*240*2/4);
  258. }
  259. fb_unlock();
  260. giz_screen = NULL;
  261. }
  262. /*
  263. #include <stdarg.h>
  264. static void stdbg(const char *fmt, ...)
  265. {
  266. static int cnt = 0;
  267. va_list vl;
  268. sprintf(noticeMsg, "%x ", cnt++);
  269. va_start(vl, fmt);
  270. vsnprintf(noticeMsg+strlen(noticeMsg), sizeof(noticeMsg)-strlen(noticeMsg), fmt, vl);
  271. va_end(vl);
  272. noticeMsgTime = GetTickCount();
  273. }
  274. */
  275. static void updateSound(int len)
  276. {
  277. if (PicoOpt&8) len<<=1;
  278. snd_all_samples += len;
  279. PsndOut += len;
  280. if (PsndOut - snd_cbuff >= snd_cbuf_samples)
  281. {
  282. //if (PsndOut - snd_cbuff != snd_cbuf_samples)
  283. // stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
  284. PsndOut = snd_cbuff;
  285. }
  286. }
  287. static void SkipFrame(void)
  288. {
  289. PicoSkipFrame=1;
  290. PicoFrame();
  291. PicoSkipFrame=0;
  292. }
  293. /* forced frame to front buffer */
  294. void emu_forcedFrame(int opts)
  295. {
  296. int po_old = PicoOpt;
  297. int eo_old = currentConfig.EmuOpt;
  298. PicoOpt &= ~0x10;
  299. PicoOpt |= opts|POPT_ACC_SPRITES;
  300. currentConfig.EmuOpt |= 0x80;
  301. if (giz_screen == NULL)
  302. giz_screen = fb_lock(1);
  303. PicoDrawSetColorFormat(1);
  304. PicoScanBegin = EmuScanBegin16;
  305. Pico.m.dirtyPal = 1;
  306. PicoFrameDrawOnly();
  307. fb_unlock();
  308. giz_screen = NULL;
  309. PicoOpt = po_old;
  310. currentConfig.EmuOpt = eo_old;
  311. }
  312. static void RunEvents(unsigned int which)
  313. {
  314. if (which & 0x1800) // save or load (but not both)
  315. {
  316. int do_it = 1;
  317. if (PsndOut != NULL)
  318. FrameworkAudio_SetPause(1);
  319. if (giz_screen == NULL)
  320. giz_screen = fb_lock(1);
  321. if ( emu_checkSaveFile(state_slot) &&
  322. (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
  323. (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
  324. {
  325. int keys;
  326. blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)");
  327. while( !((keys = Framework_PollGetButtons()) & (BTN_PLAY|BTN_STOP)) )
  328. Sleep(50);
  329. if (keys & BTN_STOP) do_it = 0;
  330. while( ((keys = Framework_PollGetButtons()) & (BTN_PLAY|BTN_STOP)) ) // wait for release
  331. Sleep(50);
  332. clearArea(0);
  333. }
  334. if (do_it)
  335. {
  336. osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
  337. PicoStateProgressCB = emu_stateCb;
  338. emu_SaveLoadGame((which & 0x1000) >> 12, 0);
  339. PicoStateProgressCB = NULL;
  340. Sleep(0);
  341. }
  342. if (PsndOut != NULL)
  343. FrameworkAudio_SetPause(0);
  344. reset_timing = 1;
  345. }
  346. if (which & 0x0400) // switch renderer
  347. {
  348. if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
  349. else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
  350. vidResetMode();
  351. if (PicoOpt&0x10) {
  352. strcpy(noticeMsg, " 8bit fast renderer");
  353. } else if (currentConfig.EmuOpt&0x80) {
  354. strcpy(noticeMsg, "16bit accurate renderer");
  355. } else {
  356. strcpy(noticeMsg, " 8bit accurate renderer");
  357. }
  358. noticeMsgTime = GetTickCount();
  359. }
  360. if (which & 0x0300)
  361. {
  362. if(which&0x0200) {
  363. state_slot -= 1;
  364. if(state_slot < 0) state_slot = 9;
  365. } else {
  366. state_slot += 1;
  367. if(state_slot > 9) state_slot = 0;
  368. }
  369. sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
  370. noticeMsgTime = GetTickCount();
  371. }
  372. }
  373. static void updateKeys(void)
  374. {
  375. unsigned int keys, allActions[2] = { 0, 0 }, events;
  376. static unsigned int prevEvents = 0;
  377. int i;
  378. keys = Framework_PollGetButtons();
  379. if (keys & BTN_HOME)
  380. engineState = PGS_Menu;
  381. keys &= CONFIGURABLE_KEYS;
  382. for (i = 0; i < 32; i++)
  383. {
  384. if (keys & (1 << i))
  385. {
  386. int pl, acts = currentConfig.KeyBinds[i];
  387. if (!acts) continue;
  388. pl = (acts >> 16) & 1;
  389. if (kb_combo_keys & (1 << i))
  390. {
  391. int u = i+1, acts_c = acts & kb_combo_acts;
  392. // let's try to find the other one
  393. if (acts_c) {
  394. for (; u < 32; u++)
  395. if ( (keys & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {
  396. allActions[pl] |= acts_c & currentConfig.KeyBinds[u];
  397. keys &= ~((1 << i) | (1 << u));
  398. break;
  399. }
  400. }
  401. // add non-combo actions if combo ones were not found
  402. if (!acts_c || u == 32)
  403. allActions[pl] |= acts & ~kb_combo_acts;
  404. } else {
  405. allActions[pl] |= acts;
  406. }
  407. }
  408. }
  409. PicoPad[0] = allActions[0] & 0xfff;
  410. PicoPad[1] = allActions[1] & 0xfff;
  411. if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
  412. if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
  413. events = (allActions[0] | allActions[1]) >> 16;
  414. // volume is treated in special way and triggered every frame
  415. if ((events & 0x6000) && PsndOut != NULL)
  416. {
  417. int vol = currentConfig.volume;
  418. if (events & 0x2000) {
  419. if (vol < 100) vol++;
  420. } else {
  421. if (vol > 0) vol--;
  422. }
  423. FrameworkAudio_SetVolume(vol, vol);
  424. sprintf(noticeMsg, "VOL: %02i ", vol);
  425. noticeMsgTime = GetTickCount();
  426. currentConfig.volume = vol;
  427. }
  428. events &= ~prevEvents;
  429. if (events) RunEvents(events);
  430. if (movie_data) emu_updateMovie();
  431. prevEvents = (allActions[0] | allActions[1]) >> 16;
  432. }
  433. void emu_platformDebugCat(char *str)
  434. {
  435. // nothing
  436. }
  437. static void simpleWait(DWORD until)
  438. {
  439. DWORD tval;
  440. int diff;
  441. tval = GetTickCount();
  442. diff = (int)until - (int)tval;
  443. if (diff >= 2)
  444. Sleep(diff - 1);
  445. while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection
  446. spend_cycles(1024*2);
  447. }
  448. void emu_Loop(void)
  449. {
  450. static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
  451. char fpsbuff[24]; // fps count c string
  452. DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
  453. int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000;
  454. int target_fps, target_frametime, lim_time, tval_diff, i;
  455. char *notice = NULL;
  456. lprintf("entered emu_Loop()\n");
  457. fpsbuff[0] = 0;
  458. // make sure we are in correct mode
  459. vidResetMode();
  460. if (currentConfig.scaling) PicoOpt|=0x4000;
  461. else PicoOpt&=~0x4000;
  462. Pico.m.dirtyPal = 1;
  463. oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
  464. emu_findKeyBindCombos();
  465. // pal/ntsc might have changed, reset related stuff
  466. target_fps = Pico.m.pal ? 50 : 60;
  467. target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1;
  468. reset_timing = 1;
  469. // prepare CD buffer
  470. if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
  471. // prepare sound stuff
  472. PsndOut = NULL;
  473. if (currentConfig.EmuOpt & 4)
  474. {
  475. int ret, snd_excess_add, stereo;
  476. if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
  477. PsndRerate(Pico.m.frame_count ? 1 : 0);
  478. }
  479. stereo=(PicoOpt&8)>>3;
  480. snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
  481. snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
  482. lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
  483. PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal);
  484. ret = FrameworkAudio_Init(PsndRate, snd_cbuf_samples, stereo);
  485. if (ret != 0) {
  486. lprintf("FrameworkAudio_Init() failed: %i\n", ret);
  487. sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
  488. noticeMsgTime = GetTickCount();
  489. currentConfig.EmuOpt &= ~4;
  490. } else {
  491. FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume);
  492. PicoWriteSound = updateSound;
  493. snd_cbuff = FrameworkAudio_56448Buffer();
  494. PsndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle
  495. snd_all_samples = 0;
  496. PsndRate_old = PsndRate;
  497. PicoOpt_old = PicoOpt;
  498. pal_old = Pico.m.pal;
  499. }
  500. }
  501. // loop?
  502. while (engineState == PGS_Running)
  503. {
  504. int modes;
  505. tval = GetTickCount();
  506. if (reset_timing || tval < tval_prev) {
  507. //stdbg("timing reset");
  508. reset_timing = 0;
  509. tval_thissec = tval;
  510. frames_shown = frames_done = 0;
  511. }
  512. // show notice message?
  513. if (noticeMsgTime) {
  514. static int noticeMsgSum;
  515. if (tval - noticeMsgTime > 2000) { // > 2.0 sec
  516. noticeMsgTime = 0;
  517. clearArea(0);
  518. notice = 0;
  519. } else {
  520. int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
  521. if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
  522. notice = noticeMsg;
  523. }
  524. }
  525. // check for mode changes
  526. modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
  527. if (modes != oldmodes) {
  528. oldmodes = modes;
  529. clearArea(1);
  530. }
  531. // second passed?
  532. if (tval - tval_thissec >= sec_ms)
  533. {
  534. #ifdef BENCHMARK
  535. static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
  536. if(++bench == 10) {
  537. bench = 0;
  538. bench_fps_s = bench_fps;
  539. bf[bfp++ & 3] = bench_fps;
  540. bench_fps = 0;
  541. }
  542. bench_fps += frames_shown;
  543. sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
  544. #else
  545. if(currentConfig.EmuOpt & 2)
  546. sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
  547. #endif
  548. //tval_thissec += 1000;
  549. tval_thissec += sec_ms;
  550. if (PsndOut != NULL)
  551. {
  552. /* some code which tries to sync things to audio clock, the dirty way */
  553. static int audio_skew_prev = 0;
  554. int audio_skew, adj, co = 9, shift = 7;
  555. audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos();
  556. if (PsndRate == 22050) co = 10;
  557. if (PsndRate > 22050) co = 11;
  558. if (PicoOpt&8) shift++;
  559. if (audio_skew < 0) {
  560. adj = -((-audio_skew) >> shift);
  561. if (audio_skew > -(6<<co)) adj>>=1;
  562. if (audio_skew > -(4<<co)) adj>>=1;
  563. if (audio_skew > -(2<<co)) adj>>=1;
  564. if (audio_skew > audio_skew_prev) adj>>=2; // going up already
  565. } else {
  566. adj = audio_skew >> shift;
  567. if (audio_skew < (6<<co)) adj>>=1;
  568. if (audio_skew < (4<<co)) adj>>=1;
  569. if (audio_skew < (2<<co)) adj>>=1;
  570. if (audio_skew < audio_skew_prev) adj>>=2;
  571. }
  572. audio_skew_prev = audio_skew;
  573. target_frametime += adj;
  574. sec_ms = (target_frametime * target_fps) >> 8;
  575. //stdbg("%i %i %i", audio_skew, adj, sec_ms);
  576. frames_done = frames_shown = 0;
  577. }
  578. else if (currentConfig.Frameskip < 0) {
  579. frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
  580. frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
  581. if (frames_shown > frames_done) frames_shown = frames_done;
  582. } else {
  583. frames_done = frames_shown = 0;
  584. }
  585. }
  586. #ifdef PFRAMES
  587. sprintf(fpsbuff, "%i", Pico.m.frame_count);
  588. #endif
  589. tval_prev = tval;
  590. lim_time = (frames_done+1) * target_frametime;
  591. if (currentConfig.Frameskip >= 0) // frameskip enabled
  592. {
  593. for (i = 0; i < currentConfig.Frameskip; i++) {
  594. updateKeys();
  595. SkipFrame(); frames_done++;
  596. if (PsndOut) { // do framelimitting if sound is enabled
  597. int tval_diff;
  598. tval = GetTickCount();
  599. tval_diff = (int)(tval - tval_thissec) << 8;
  600. if (tval_diff < lim_time) // we are too fast
  601. simpleWait(tval + ((lim_time - tval_diff)>>8));
  602. }
  603. lim_time += target_frametime;
  604. }
  605. }
  606. else // auto frameskip
  607. {
  608. int tval_diff;
  609. tval = GetTickCount();
  610. tval_diff = (int)(tval - tval_thissec) << 8;
  611. if (tval_diff > lim_time)
  612. {
  613. // no time left for this frame - skip
  614. if (tval_diff - lim_time >= (300<<8)) {
  615. /* something caused a slowdown for us (disk access? cache flush?)
  616. * try to recover by resetting timing... */
  617. reset_timing = 1;
  618. continue;
  619. }
  620. updateKeys();
  621. SkipFrame(); frames_done++;
  622. continue;
  623. }
  624. }
  625. updateKeys();
  626. if (currentConfig.EmuOpt&0x80)
  627. /* be sure correct framebuffer is locked */
  628. giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
  629. PicoFrame();
  630. if (giz_screen == NULL)
  631. giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
  632. blit(fpsbuff, notice);
  633. if (giz_screen != NULL) {
  634. fb_unlock();
  635. giz_screen = NULL;
  636. }
  637. if (currentConfig.EmuOpt&0x2000)
  638. Framework2D_WaitVSync();
  639. if (currentConfig.EmuOpt&0x8000)
  640. fb_flip();
  641. // check time
  642. tval = GetTickCount();
  643. tval_diff = (int)(tval - tval_thissec) << 8;
  644. if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
  645. reset_timing = 1;
  646. else if (PsndOut != NULL || currentConfig.Frameskip < 0)
  647. {
  648. // sleep if we are still too fast
  649. if (tval_diff < lim_time)
  650. {
  651. // we are too fast
  652. simpleWait(tval + ((lim_time - tval_diff) >> 8));
  653. }
  654. }
  655. frames_done++; frames_shown++;
  656. }
  657. if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
  658. if (PsndOut != NULL) {
  659. PsndOut = snd_cbuff = NULL;
  660. FrameworkAudio_Close();
  661. }
  662. // save SRAM
  663. if ((currentConfig.EmuOpt & 1) && SRam.changed) {
  664. emu_stateCb("Writing SRAM/BRAM..");
  665. emu_SaveLoadGame(0, 1);
  666. SRam.changed = 0;
  667. }
  668. }
  669. void emu_ResetGame(void)
  670. {
  671. PicoReset();
  672. reset_timing = 1;
  673. }