emu.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. // (c) Copyright 2006-2007 notaz, All rights reserved.
  2. // Free for non-commercial use.
  3. // For commercial use, separate licencing terms must be obtained.
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/time.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <linux/limits.h>
  10. #include <ctype.h>
  11. #include <unistd.h>
  12. #include <stdarg.h>
  13. #include "emu.h"
  14. #include "gp2x.h"
  15. #include "menu.h"
  16. #include "../linux/usbjoy.h"
  17. #include "../common/arm_utils.h"
  18. #include "../common/fonts.h"
  19. #include "../common/emu.h"
  20. #include "../common/config.h"
  21. #include "../linux/sndout_oss.h"
  22. #include "cpuctrl.h"
  23. #include <pico/pico_int.h>
  24. #include <pico/patch.h>
  25. #include <pico/sound/mix.h>
  26. #include <zlib/zlib.h>
  27. //#define PFRAMES
  28. #ifdef BENCHMARK
  29. #define OSD_FPS_X 220
  30. #else
  31. #define OSD_FPS_X 260
  32. #endif
  33. int engineState;
  34. int select_exits = 0;
  35. char romFileName[PATH_MAX];
  36. extern int crashed_940;
  37. static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
  38. static struct timeval noticeMsgTime = { 0, 0 }; // when started showing
  39. static int osd_fps_x;
  40. static int gp2x_old_gamma = 100;
  41. unsigned char *PicoDraw2FB = NULL; // temporary buffer for alt renderer
  42. int reset_timing = 0;
  43. #define PICO_PEN_ADJUST_X 4
  44. #define PICO_PEN_ADJUST_Y 2
  45. static int pico_pen_x = 320/2, pico_pen_y = 240/2;
  46. static void emu_msg_cb(const char *msg);
  47. static void emu_msg_tray_open(void);
  48. void emu_noticeMsgUpdated(void)
  49. {
  50. gettimeofday(&noticeMsgTime, 0);
  51. }
  52. int emu_getMainDir(char *dst, int len)
  53. {
  54. extern char **g_argv;
  55. int j;
  56. strncpy(dst, g_argv[0], len);
  57. len -= 32; // reserve
  58. if (len < 0) len = 0;
  59. dst[len] = 0;
  60. for (j = strlen(dst); j > 0; j--)
  61. if (dst[j] == '/') { dst[j+1] = 0; break; }
  62. return j + 1;
  63. }
  64. void emu_Init(void)
  65. {
  66. // make temp buffer for alt renderer
  67. PicoDraw2FB = malloc((8+320)*(8+240+8));
  68. if (!PicoDraw2FB)
  69. {
  70. printf("PicoDraw2FB == 0\n");
  71. }
  72. // make dirs for saves, cfgs, etc.
  73. mkdir("mds", 0777);
  74. mkdir("srm", 0777);
  75. mkdir("brm", 0777);
  76. mkdir("cfg", 0777);
  77. PicoInit();
  78. PicoMessage = emu_msg_cb;
  79. PicoMCDopenTray = emu_msg_tray_open;
  80. PicoMCDcloseTray = menu_loop_tray;
  81. }
  82. static void scaling_update(void)
  83. {
  84. PicoOpt &= ~0x4100;
  85. switch (currentConfig.scaling) {
  86. default: break; // off
  87. case 1: // hw hor
  88. case 2: PicoOpt |= 0x0100; break; // hw hor+vert
  89. case 3: PicoOpt |= 0x4000; break; // sw hor
  90. }
  91. }
  92. void emu_Deinit(void)
  93. {
  94. // save SRAM
  95. if((currentConfig.EmuOpt & 1) && SRam.changed) {
  96. emu_SaveLoadGame(0, 1);
  97. SRam.changed = 0;
  98. }
  99. if (!(currentConfig.EmuOpt & 0x20)) {
  100. config_writelrom(PicoConfigFile);
  101. #ifndef NO_SYNC
  102. sync();
  103. #endif
  104. }
  105. free(PicoDraw2FB);
  106. PicoExit();
  107. // restore gamma
  108. if (gp2x_old_gamma != 100)
  109. set_gamma(100, 0);
  110. }
  111. void emu_prepareDefaultConfig(void)
  112. {
  113. memset(&defaultConfig, 0, sizeof(defaultConfig));
  114. defaultConfig.EmuOpt = 0x9d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds
  115. defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;
  116. defaultConfig.s_PsndRate = 44100;
  117. defaultConfig.s_PicoRegion = 0; // auto
  118. defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
  119. defaultConfig.s_PicoCDBuffers = 0;
  120. defaultConfig.Frameskip = -1; // auto
  121. defaultConfig.CPUclock = 200;
  122. defaultConfig.volume = 50;
  123. defaultConfig.KeyBinds[ 0] = 1<<0; // SACB RLDU
  124. defaultConfig.KeyBinds[ 4] = 1<<1;
  125. defaultConfig.KeyBinds[ 2] = 1<<2;
  126. defaultConfig.KeyBinds[ 6] = 1<<3;
  127. defaultConfig.KeyBinds[14] = 1<<4;
  128. defaultConfig.KeyBinds[13] = 1<<5;
  129. defaultConfig.KeyBinds[12] = 1<<6;
  130. defaultConfig.KeyBinds[ 8] = 1<<7;
  131. defaultConfig.KeyBinds[15] = 1<<26; // switch rend
  132. defaultConfig.KeyBinds[10] = 1<<27; // save state
  133. defaultConfig.KeyBinds[11] = 1<<28; // load state
  134. defaultConfig.KeyBinds[23] = 1<<29; // vol up
  135. defaultConfig.KeyBinds[22] = 1<<30; // vol down
  136. defaultConfig.gamma = 100;
  137. defaultConfig.scaling = 0;
  138. defaultConfig.turbo_rate = 15;
  139. }
  140. void osd_text(int x, int y, const char *text)
  141. {
  142. int len = strlen(text)*8;
  143. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  144. int *p, i, h;
  145. x &= ~3; // align x
  146. len = (len+3) >> 2;
  147. for (h = 0; h < 8; h++) {
  148. p = (int *) ((unsigned char *) gp2x_screen+x+320*(y+h));
  149. for (i = len; i; i--, p++) *p = 0xe0e0e0e0;
  150. }
  151. emu_textOut8(x, y, text);
  152. } else {
  153. int *p, i, h;
  154. x &= ~1; // align x
  155. len = (len+1) >> 1;
  156. for (h = 0; h < 8; h++) {
  157. p = (int *) ((unsigned short *) gp2x_screen+x+320*(y+h));
  158. for (i = len; i; i--, p++) *p = (*p>>2)&0x39e7;
  159. }
  160. emu_textOut16(x, y, text);
  161. }
  162. }
  163. static void draw_cd_leds(void)
  164. {
  165. // static
  166. int old_reg;
  167. // if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?
  168. old_reg = Pico_mcd->s68k_regs[0];
  169. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  170. // 8-bit modes
  171. unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
  172. unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
  173. *(unsigned int *)((char *)gp2x_screen + 320*2+ 4) =
  174. *(unsigned int *)((char *)gp2x_screen + 320*3+ 4) =
  175. *(unsigned int *)((char *)gp2x_screen + 320*4+ 4) = col_g;
  176. *(unsigned int *)((char *)gp2x_screen + 320*2+12) =
  177. *(unsigned int *)((char *)gp2x_screen + 320*3+12) =
  178. *(unsigned int *)((char *)gp2x_screen + 320*4+12) = col_r;
  179. } else {
  180. // 16-bit modes
  181. unsigned int *p = (unsigned int *)((short *)gp2x_screen + 320*2+4);
  182. unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
  183. unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
  184. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
  185. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 320/2 - 12/2;
  186. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
  187. }
  188. }
  189. static void draw_pico_ptr(void)
  190. {
  191. unsigned short *p = (unsigned short *)gp2x_screen;
  192. // only if pen enabled and for 16bit modes
  193. if (pico_inp_mode == 0 || (PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) return;
  194. if (!(Pico.video.reg[12]&1) && !(PicoOpt&POPT_DIS_32C_BORDER))
  195. p += 32;
  196. p += 320 * (pico_pen_y + PICO_PEN_ADJUST_Y);
  197. p += pico_pen_x + PICO_PEN_ADJUST_X;
  198. p[0] ^= 0xffff;
  199. p[319] ^= 0xffff;
  200. p[320] ^= 0xffff;
  201. p[321] ^= 0xffff;
  202. p[640] ^= 0xffff;
  203. }
  204. static int EmuScanBegin16(unsigned int num)
  205. {
  206. if (!(Pico.video.reg[1]&8)) num += 8;
  207. DrawLineDest = (unsigned short *) gp2x_screen + 320 * num;
  208. return 0;
  209. }
  210. static int EmuScanBegin8(unsigned int num)
  211. {
  212. if (!(Pico.video.reg[1]&8)) num += 8;
  213. DrawLineDest = (unsigned char *) gp2x_screen + 320 * num;
  214. return 0;
  215. }
  216. int localPal[0x100];
  217. static void (*vidCpyM2)(void *dest, void *src) = NULL;
  218. static void blit(const char *fps, const char *notice)
  219. {
  220. int emu_opt = currentConfig.EmuOpt;
  221. if (PicoOpt&0x10)
  222. {
  223. // 8bit fast renderer
  224. if (Pico.m.dirtyPal) {
  225. Pico.m.dirtyPal = 0;
  226. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  227. // feed new palette to our device
  228. gp2x_video_setpalette(localPal, 0x40);
  229. }
  230. // a hack for VR
  231. if (PicoRead16Hook == PicoSVPRead16)
  232. memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
  233. // do actual copy
  234. vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
  235. }
  236. else if (!(emu_opt&0x80))
  237. {
  238. // 8bit accurate renderer
  239. if (Pico.m.dirtyPal)
  240. {
  241. int pallen = 0xc0;
  242. Pico.m.dirtyPal = 0;
  243. if (Pico.video.reg[0xC]&8) // shadow/hilight mode
  244. {
  245. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  246. vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
  247. vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40);
  248. memcpy32(localPal+0xc0, localPal+0x40, 0x40);
  249. pallen = 0x100;
  250. }
  251. else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes
  252. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  253. vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);
  254. vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);
  255. }
  256. else {
  257. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  258. memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess
  259. }
  260. if (pallen > 0xc0) {
  261. localPal[0xc0] = 0x0000c000;
  262. localPal[0xd0] = 0x00c00000;
  263. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  264. localPal[0xf0] = 0x00ffffff;
  265. }
  266. gp2x_video_setpalette(localPal, pallen);
  267. }
  268. }
  269. if (notice || (emu_opt & 2)) {
  270. int h = 232;
  271. if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;
  272. if (notice) osd_text(4, h, notice);
  273. if (emu_opt & 2)
  274. osd_text(osd_fps_x, h, fps);
  275. }
  276. if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
  277. draw_cd_leds();
  278. if (PicoAHW & PAHW_PICO)
  279. draw_pico_ptr();
  280. //gp2x_video_wait_vsync();
  281. gp2x_video_flip();
  282. if (!(PicoOpt&0x10)) {
  283. if (!(Pico.video.reg[1]&8)) {
  284. if (currentConfig.EmuOpt&0x80) {
  285. DrawLineDest = (unsigned short *) gp2x_screen + 320*8;
  286. } else {
  287. DrawLineDest = (unsigned char *) gp2x_screen + 320*8;
  288. }
  289. } else {
  290. DrawLineDest = gp2x_screen;
  291. }
  292. }
  293. }
  294. // clears whole screen or just the notice area (in all buffers)
  295. static void clearArea(int full)
  296. {
  297. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  298. // 8-bit renderers
  299. if (full) gp2x_memset_all_buffers(0, 0xe0, 320*240);
  300. else gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
  301. } else {
  302. // 16bit accurate renderer
  303. if (full) gp2x_memset_all_buffers(0, 0, 320*240*2);
  304. else gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
  305. }
  306. }
  307. static void vidResetMode(void)
  308. {
  309. if (PicoOpt&0x10) {
  310. gp2x_video_changemode(8);
  311. } else if (currentConfig.EmuOpt&0x80) {
  312. gp2x_video_changemode(16);
  313. PicoDrawSetColorFormat(1);
  314. PicoScanBegin = EmuScanBegin16;
  315. } else {
  316. gp2x_video_changemode(8);
  317. PicoDrawSetColorFormat(2);
  318. PicoScanBegin = EmuScanBegin8;
  319. }
  320. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  321. // setup pal for 8-bit modes
  322. localPal[0xc0] = 0x0000c000; // MCD LEDs
  323. localPal[0xd0] = 0x00c00000;
  324. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  325. localPal[0xf0] = 0x00ffffff;
  326. gp2x_video_setpalette(localPal, 0x100);
  327. gp2x_memset_all_buffers(0, 0xe0, 320*240);
  328. gp2x_video_flip();
  329. }
  330. Pico.m.dirtyPal = 1;
  331. // reset scaling
  332. if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))
  333. gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);
  334. else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);
  335. }
  336. static void emu_msg_cb(const char *msg)
  337. {
  338. if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
  339. // 8-bit renderers
  340. gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
  341. osd_text(4, 232, msg);
  342. gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232, 320*232, 320*8);
  343. } else {
  344. // 16bit accurate renderer
  345. gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
  346. osd_text(4, 232, msg);
  347. gp2x_memcpy_all_buffers((char *)gp2x_screen+320*232*2, 320*232*2, 320*8*2);
  348. }
  349. gettimeofday(&noticeMsgTime, 0);
  350. noticeMsgTime.tv_sec -= 2;
  351. /* assumption: emu_msg_cb gets called only when something slow is about to happen */
  352. reset_timing = 1;
  353. }
  354. static void emu_state_cb(const char *str)
  355. {
  356. clearArea(0);
  357. blit("", str);
  358. }
  359. static void emu_msg_tray_open(void)
  360. {
  361. strcpy(noticeMsg, "CD tray opened");
  362. gettimeofday(&noticeMsgTime, 0);
  363. }
  364. static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)
  365. {
  366. int ret, px, py, lim_x;
  367. static int pdown_frames = 0;
  368. emu_RunEventsPico(events);
  369. if (pico_inp_mode == 0) return;
  370. // for F200
  371. ret = gp2x_touchpad_read(&px, &py);
  372. if (ret >= 0)
  373. {
  374. if (ret > 35000)
  375. {
  376. if (pdown_frames++ > 5)
  377. PicoPad[0] |= 0x20;
  378. pico_pen_x = px;
  379. pico_pen_y = py;
  380. if (!(Pico.video.reg[12]&1)) {
  381. pico_pen_x -= 32;
  382. if (pico_pen_x < 0) pico_pen_x = 0;
  383. if (pico_pen_x > 248) pico_pen_x = 248;
  384. }
  385. if (pico_pen_y > 224) pico_pen_y = 224;
  386. }
  387. else
  388. pdown_frames = 0;
  389. //if (ret == 0)
  390. // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
  391. }
  392. PicoPad[0] &= ~0x0f; // release UDLR
  393. if (gp2x_keys & GP2X_UP) pico_pen_y--;
  394. if (gp2x_keys & GP2X_DOWN) pico_pen_y++;
  395. if (gp2x_keys & GP2X_LEFT) pico_pen_x--;
  396. if (gp2x_keys & GP2X_RIGHT) pico_pen_x++;
  397. lim_x = (Pico.video.reg[12]&1) ? 319 : 255;
  398. if (pico_pen_y < 8) pico_pen_y = 8;
  399. if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y;
  400. if (pico_pen_x < 0) pico_pen_x = 0;
  401. if (pico_pen_x > lim_x-PICO_PEN_ADJUST_X) pico_pen_x = lim_x-PICO_PEN_ADJUST_X;
  402. PicoPicohw.pen_pos[0] = pico_pen_x;
  403. if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;
  404. PicoPicohw.pen_pos[0] += 0x3c;
  405. PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
  406. }
  407. static void update_volume(int has_changed, int is_up)
  408. {
  409. static int prev_frame = 0, wait_frames = 0;
  410. int vol = currentConfig.volume;
  411. if (has_changed)
  412. {
  413. if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
  414. wait_frames++;
  415. else {
  416. if (is_up) {
  417. if (vol < 99) vol++;
  418. } else {
  419. if (vol > 0) vol--;
  420. }
  421. wait_frames = 0;
  422. sndout_oss_setvol(vol, vol);
  423. currentConfig.volume = vol;
  424. }
  425. sprintf(noticeMsg, "VOL: %02i", vol);
  426. gettimeofday(&noticeMsgTime, 0);
  427. prev_frame = Pico.m.frame_count;
  428. }
  429. // set the right mixer func
  430. if (!(PicoOpt&8)) return; // just use defaults for mono
  431. if (vol >= 5)
  432. PsndMix_32_to_16l = mix_32_to_16l_stereo;
  433. else {
  434. mix_32_to_16l_level = 5 - vol;
  435. PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
  436. }
  437. }
  438. static void RunEvents(unsigned int which)
  439. {
  440. if (which & 0x1800) // save or load (but not both)
  441. {
  442. int do_it = 1;
  443. if ( emu_checkSaveFile(state_slot) &&
  444. (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
  445. (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save
  446. unsigned long keys;
  447. blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");
  448. while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )
  449. usleep(50*1024);
  450. if (keys & GP2X_X) do_it = 0;
  451. while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release
  452. usleep(50*1024);
  453. clearArea(0);
  454. }
  455. if (do_it) {
  456. osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
  457. PicoStateProgressCB = emu_state_cb;
  458. gp2x_memcpy_all_buffers(gp2x_screen, 0, 320*240*2);
  459. emu_SaveLoadGame((which & 0x1000) >> 12, 0);
  460. PicoStateProgressCB = NULL;
  461. }
  462. reset_timing = 1;
  463. }
  464. if (which & 0x0400) // switch renderer
  465. {
  466. if ( PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
  467. else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;
  468. else currentConfig.EmuOpt &= ~0x80;
  469. vidResetMode();
  470. if (PicoOpt&0x10) {
  471. strcpy(noticeMsg, " 8bit fast renderer");
  472. } else if (currentConfig.EmuOpt&0x80) {
  473. strcpy(noticeMsg, "16bit accurate renderer");
  474. } else {
  475. strcpy(noticeMsg, " 8bit accurate renderer");
  476. }
  477. gettimeofday(&noticeMsgTime, 0);
  478. }
  479. if (which & 0x0300)
  480. {
  481. if(which&0x0200) {
  482. state_slot -= 1;
  483. if(state_slot < 0) state_slot = 9;
  484. } else {
  485. state_slot += 1;
  486. if(state_slot > 9) state_slot = 0;
  487. }
  488. sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
  489. gettimeofday(&noticeMsgTime, 0);
  490. }
  491. if (which & 0x0080) {
  492. engineState = PGS_Menu;
  493. }
  494. }
  495. static void updateKeys(void)
  496. {
  497. unsigned int keys, keys2, allActions[2] = { 0, 0 }, events;
  498. static unsigned int prevEvents = 0;
  499. int joy, i;
  500. keys = gp2x_joystick_read(0);
  501. if (keys & GP2X_SELECT) {
  502. engineState = select_exits ? PGS_Quit : PGS_Menu;
  503. // wait until select is released, so menu would not resume game
  504. while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);
  505. }
  506. keys &= CONFIGURABLE_KEYS;
  507. keys2 = keys;
  508. for (i = 0; i < 32; i++)
  509. {
  510. if (keys2 & (1 << i))
  511. {
  512. int pl, acts = currentConfig.KeyBinds[i];
  513. if (!acts) continue;
  514. pl = (acts >> 16) & 1;
  515. if (kb_combo_keys & (1 << i))
  516. {
  517. int u = i+1, acts_c = acts & kb_combo_acts;
  518. // let's try to find the other one
  519. if (acts_c) {
  520. for (; u < 32; u++)
  521. if ( (keys2 & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {
  522. allActions[pl] |= acts_c & currentConfig.KeyBinds[u];
  523. keys2 &= ~((1 << i) | (1 << u));
  524. break;
  525. }
  526. }
  527. // add non-combo actions if combo ones were not found
  528. if (!acts_c || u == 32)
  529. allActions[pl] |= acts & ~kb_combo_acts;
  530. } else {
  531. allActions[pl] |= acts;
  532. }
  533. }
  534. }
  535. // add joy inputs
  536. if (num_of_joys > 0)
  537. {
  538. usbjoy_update();
  539. for (joy = 0; joy < num_of_joys; joy++) {
  540. int btns = usbjoy_check2(joy);
  541. for (i = 0; i < 32; i++) {
  542. if (btns & (1 << i)) {
  543. int acts = currentConfig.JoyBinds[joy][i];
  544. int pl = (acts >> 16) & 1;
  545. allActions[pl] |= acts;
  546. }
  547. }
  548. }
  549. }
  550. PicoPad[0] = allActions[0] & 0xfff;
  551. PicoPad[1] = allActions[1] & 0xfff;
  552. if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
  553. if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
  554. events = (allActions[0] | allActions[1]) >> 16;
  555. // volume is treated in special way and triggered every frame
  556. if (events & 0x6000)
  557. update_volume(1, events & 0x2000);
  558. if ((events ^ prevEvents) & 0x40) {
  559. emu_changeFastForward(events & 0x40);
  560. update_volume(0, 0);
  561. reset_timing = 1;
  562. }
  563. events &= ~prevEvents;
  564. if (PicoAHW == PAHW_PICO)
  565. RunEventsPico(events, keys);
  566. if (events) RunEvents(events);
  567. if (movie_data) emu_updateMovie();
  568. prevEvents = (allActions[0] | allActions[1]) >> 16;
  569. }
  570. static void updateSound(int len)
  571. {
  572. if (PicoOpt&8) len<<=1;
  573. /* avoid writing audio when lagging behind to prevent audio lag */
  574. if (PicoSkipFrame != 2)
  575. sndout_oss_write(PsndOut, len<<1);
  576. }
  577. void emu_startSound(void)
  578. {
  579. static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
  580. int target_fps = Pico.m.pal ? 50 : 60;
  581. PsndOut = NULL;
  582. // prepare sound stuff
  583. if (currentConfig.EmuOpt & 4)
  584. {
  585. int snd_excess_add;
  586. if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old ||
  587. ((PicoOpt&0x200) && crashed_940)) {
  588. PsndRerate(Pico.m.frame_count ? 1 : 0);
  589. }
  590. snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
  591. printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
  592. PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
  593. sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);
  594. sndout_oss_setvol(currentConfig.volume, currentConfig.volume);
  595. PicoWriteSound = updateSound;
  596. update_volume(0, 0);
  597. memset(sndBuffer, 0, sizeof(sndBuffer));
  598. PsndOut = sndBuffer;
  599. PsndRate_old = PsndRate;
  600. PicoOpt_old = PicoOpt;
  601. pal_old = Pico.m.pal;
  602. }
  603. }
  604. void emu_endSound(void)
  605. {
  606. }
  607. /* wait until we can write more sound */
  608. void emu_waitSound(void)
  609. {
  610. // don't need to do anything, writes will block by themselves
  611. }
  612. static void SkipFrame(int do_audio)
  613. {
  614. PicoSkipFrame=do_audio ? 1 : 2;
  615. PicoFrame();
  616. PicoSkipFrame=0;
  617. }
  618. void emu_forcedFrame(int opts)
  619. {
  620. int po_old = PicoOpt;
  621. int eo_old = currentConfig.EmuOpt;
  622. PicoOpt &= ~0x10;
  623. PicoOpt |= opts|POPT_ACC_SPRITES; // acc_sprites
  624. currentConfig.EmuOpt |= 0x80;
  625. //vidResetMode();
  626. PicoDrawSetColorFormat(1);
  627. PicoScanBegin = EmuScanBegin16;
  628. Pico.m.dirtyPal = 1;
  629. PicoFrameDrawOnly();
  630. /*
  631. if (!(Pico.video.reg[12]&1)) {
  632. vidCpyM2 = vidCpyM2_32col;
  633. clearArea(1);
  634. } else vidCpyM2 = vidCpyM2_40col;
  635. vidCpyM2((unsigned char *)gp2x_screen+320*8, PicoDraw2FB+328*8);
  636. vidConvCpyRGB32(localPal, Pico.cram, 0x40);
  637. gp2x_video_setpalette(localPal, 0x40);
  638. */
  639. PicoOpt = po_old;
  640. currentConfig.EmuOpt = eo_old;
  641. }
  642. void emu_platformDebugCat(char *str)
  643. {
  644. // nothing
  645. }
  646. static void simpleWait(int thissec, int lim_time)
  647. {
  648. struct timeval tval;
  649. spend_cycles(1024);
  650. gettimeofday(&tval, 0);
  651. if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
  652. while (tval.tv_usec < lim_time)
  653. {
  654. spend_cycles(1024);
  655. gettimeofday(&tval, 0);
  656. if (thissec != tval.tv_sec) tval.tv_usec+=1000000;
  657. }
  658. }
  659. #if 0
  660. static void tga_dump(void)
  661. {
  662. #define BYTE unsigned char
  663. #define WORD unsigned short
  664. struct
  665. {
  666. BYTE IDLength; /* 00h Size of Image ID field */
  667. BYTE ColorMapType; /* 01h Color map type */
  668. BYTE ImageType; /* 02h Image type code */
  669. WORD CMapStart; /* 03h Color map origin */
  670. WORD CMapLength; /* 05h Color map length */
  671. BYTE CMapDepth; /* 07h Depth of color map entries */
  672. WORD XOffset; /* 08h X origin of image */
  673. WORD YOffset; /* 0Ah Y origin of image */
  674. WORD Width; /* 0Ch Width of image */
  675. WORD Height; /* 0Eh Height of image */
  676. BYTE PixelDepth; /* 10h Image pixel size */
  677. BYTE ImageDescriptor; /* 11h Image descriptor byte */
  678. } __attribute__((packed)) TGAHEAD;
  679. static unsigned short oldscr[320*240];
  680. FILE *f; char name[128]; int i;
  681. memset(&TGAHEAD, 0, sizeof(TGAHEAD));
  682. TGAHEAD.ImageType = 2;
  683. TGAHEAD.Width = 320;
  684. TGAHEAD.Height = 240;
  685. TGAHEAD.PixelDepth = 16;
  686. TGAHEAD.ImageDescriptor = 2<<4; // image starts at top-left
  687. #define CONV(X) (((X>>1)&0x7fe0)|(X&0x1f)) // 555?
  688. for (i = 0; i < 320*240; i++)
  689. if(oldscr[i] != CONV(((unsigned short *)gp2x_screen)[i])) break;
  690. if (i < 320*240)
  691. {
  692. for (i = 0; i < 320*240; i++)
  693. oldscr[i] = CONV(((unsigned short *)gp2x_screen)[i]);
  694. sprintf(name, "%05i.tga", Pico.m.frame_count);
  695. f = fopen(name, "wb");
  696. if (!f) { printf("!f\n"); exit(1); }
  697. fwrite(&TGAHEAD, 1, sizeof(TGAHEAD), f);
  698. fwrite(oldscr, 1, 320*240*2, f);
  699. fclose(f);
  700. }
  701. }
  702. #endif
  703. void emu_Loop(void)
  704. {
  705. static int gp2x_old_clock = 200, EmuOpt_old = 0;
  706. char fpsbuff[24]; // fps count c string
  707. struct timeval tval; // timing
  708. int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync
  709. int frames_done, frames_shown, thissec; // actual frames
  710. int oldmodes = 0, target_fps, target_frametime, lim_time, vsync_offset, i;
  711. char *notice = 0;
  712. printf("entered emu_Loop()\n");
  713. if (gp2x_old_clock != currentConfig.CPUclock) {
  714. printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);
  715. set_FCLK(currentConfig.CPUclock);
  716. gp2x_old_clock = currentConfig.CPUclock;
  717. printf(" done\n");
  718. }
  719. if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {
  720. set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
  721. gp2x_old_gamma = currentConfig.gamma;
  722. printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));
  723. }
  724. if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {
  725. if (currentConfig.EmuOpt&0x2000)
  726. set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);
  727. else unset_LCD_custom_rate();
  728. }
  729. EmuOpt_old = currentConfig.EmuOpt;
  730. fpsbuff[0] = 0;
  731. // make sure we are in correct mode
  732. vidResetMode();
  733. scaling_update();
  734. Pico.m.dirtyPal = 1;
  735. oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
  736. emu_findKeyBindCombos();
  737. // pal/ntsc might have changed, reset related stuff
  738. target_fps = Pico.m.pal ? 50 : 60;
  739. target_frametime = 1000000/target_fps;
  740. reset_timing = 1;
  741. emu_startSound();
  742. // prepare CD buffer
  743. if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
  744. // calc vsync offset to sync timing code with vsync
  745. if (currentConfig.EmuOpt&0x2000) {
  746. gettimeofday(&tval, 0);
  747. gp2x_video_wait_vsync();
  748. gettimeofday(&tval, 0);
  749. vsync_offset = tval.tv_usec;
  750. while (vsync_offset >= target_frametime)
  751. vsync_offset -= target_frametime;
  752. if (!vsync_offset) vsync_offset++;
  753. printf("vsync_offset: %i\n", vsync_offset);
  754. } else
  755. vsync_offset = 0;
  756. frames_done = frames_shown = thissec =
  757. pframes_done = pframes_shown = pthissec = 0;
  758. // loop
  759. while (engineState == PGS_Running)
  760. {
  761. int modes;
  762. gettimeofday(&tval, 0);
  763. if (reset_timing) {
  764. reset_timing = 0;
  765. pthissec = tval.tv_sec;
  766. pframes_shown = pframes_done = tval.tv_usec/target_frametime;
  767. }
  768. // show notice message?
  769. if (noticeMsgTime.tv_sec)
  770. {
  771. static int noticeMsgSum;
  772. if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) { // > 2.0 sec
  773. noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
  774. clearArea(0);
  775. notice = 0;
  776. } else {
  777. int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
  778. if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
  779. notice = noticeMsg;
  780. }
  781. }
  782. // check for mode changes
  783. modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
  784. if (modes != oldmodes)
  785. {
  786. int scalex = 320;
  787. osd_fps_x = OSD_FPS_X;
  788. if (modes & 4) {
  789. vidCpyM2 = vidCpyM2_40col;
  790. } else {
  791. if (PicoOpt & 0x100) {
  792. vidCpyM2 = vidCpyM2_32col_nobord;
  793. scalex = 256;
  794. osd_fps_x = OSD_FPS_X - 64;
  795. } else {
  796. vidCpyM2 = vidCpyM2_32col;
  797. }
  798. }
  799. if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode
  800. gp2x_video_RGB_setscaling(8, scalex, 224);
  801. else gp2x_video_RGB_setscaling(0, scalex, 240);
  802. oldmodes = modes;
  803. clearArea(1);
  804. }
  805. // second changed?
  806. if (thissec != tval.tv_sec)
  807. {
  808. #ifdef BENCHMARK
  809. static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
  810. if (++bench == 10) {
  811. bench = 0;
  812. bench_fps_s = bench_fps;
  813. bf[bfp++ & 3] = bench_fps;
  814. bench_fps = 0;
  815. }
  816. bench_fps += frames_shown;
  817. sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
  818. #else
  819. if (currentConfig.EmuOpt & 2) {
  820. sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
  821. if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }
  822. }
  823. #endif
  824. frames_shown = frames_done = 0;
  825. thissec = tval.tv_sec;
  826. }
  827. #ifdef PFRAMES
  828. sprintf(fpsbuff, "%i", Pico.m.frame_count);
  829. #endif
  830. if (pthissec != tval.tv_sec)
  831. {
  832. if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
  833. pframes_done = pframes_shown = 0;
  834. } else {
  835. // it is quite common for this implementation to leave 1 fame unfinished
  836. // when second changes, but we don't want buffer to starve.
  837. if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {
  838. updateKeys();
  839. SkipFrame(1); pframes_done++;
  840. }
  841. pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
  842. pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
  843. if (pframes_shown > pframes_done) pframes_shown = pframes_done;
  844. }
  845. pthissec = tval.tv_sec;
  846. }
  847. lim_time = (pframes_done+1) * target_frametime + vsync_offset;
  848. if (currentConfig.Frameskip >= 0) // frameskip enabled
  849. {
  850. for(i = 0; i < currentConfig.Frameskip; i++) {
  851. updateKeys();
  852. SkipFrame(1); pframes_done++; frames_done++;
  853. if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled
  854. gettimeofday(&tval, 0);
  855. if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
  856. if (tval.tv_usec < lim_time) { // we are too fast
  857. simpleWait(pthissec, lim_time);
  858. }
  859. }
  860. lim_time += target_frametime;
  861. }
  862. }
  863. else if (tval.tv_usec > lim_time) // auto frameskip
  864. {
  865. // no time left for this frame - skip
  866. if (tval.tv_usec - lim_time >= 300000) {
  867. /* something caused a slowdown for us (disk access? cache flush?)
  868. * try to recover by resetting timing... */
  869. reset_timing = 1;
  870. continue;
  871. }
  872. updateKeys();
  873. SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;
  874. continue;
  875. }
  876. updateKeys();
  877. PicoFrame();
  878. // check time
  879. gettimeofday(&tval, 0);
  880. if (pthissec != tval.tv_sec) tval.tv_usec+=1000000;
  881. if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection
  882. reset_timing = 1;
  883. else if (PsndOut != NULL || currentConfig.Frameskip < 0)
  884. {
  885. // sleep or vsync if we are still too fast
  886. // usleep sleeps for ~20ms minimum, so it is not a solution here
  887. if (!reset_timing && tval.tv_usec < lim_time)
  888. {
  889. // we are too fast
  890. if (vsync_offset) {
  891. if (lim_time - tval.tv_usec > target_frametime/2)
  892. simpleWait(pthissec, lim_time - target_frametime/4);
  893. gp2x_video_wait_vsync();
  894. } else {
  895. simpleWait(pthissec, lim_time);
  896. }
  897. }
  898. }
  899. blit(fpsbuff, notice);
  900. pframes_done++; pframes_shown++;
  901. frames_done++; frames_shown++;
  902. }
  903. emu_changeFastForward(0);
  904. if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
  905. // save SRAM
  906. if((currentConfig.EmuOpt & 1) && SRam.changed) {
  907. emu_state_cb("Writing SRAM/BRAM..");
  908. emu_SaveLoadGame(0, 1);
  909. SRam.changed = 0;
  910. }
  911. // if in 8bit mode, generate 16bit image for menu background
  912. if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))
  913. emu_forcedFrame(POPT_EN_SOFTSCALE);
  914. }
  915. void emu_ResetGame(void)
  916. {
  917. PicoReset();
  918. reset_timing = 1;
  919. }