emu.c 28 KB

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