emu.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * (c) Copyright 2006-2010 notaz, All rights reserved.
  3. *
  4. * For performance reasons 3 renderers are exported for both MD and 32x modes:
  5. * - 16bpp line renderer
  6. * - 8bpp line renderer (slightly faster)
  7. * - 8bpp tile renderer
  8. * In 32x mode:
  9. * - 32x layer is overlayed on top of 16bpp one
  10. * - line internal one done on PicoDraw2FB, then mixed with 32x
  11. * - tile internal one done on PicoDraw2FB, then mixed with 32x
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include "../libpicofe/gp2x/plat_gp2x.h"
  17. #include "../libpicofe/gp2x/soc.h"
  18. #include "../libpicofe/input.h"
  19. #include "../libpicofe/plat.h"
  20. #include "../libpicofe/gp2x/soc_pollux.h"
  21. #include "../common/menu_pico.h"
  22. #include "../common/arm_utils.h"
  23. #include "../common/emu.h"
  24. #include "../common/config_file.h"
  25. #include "../common/version.h"
  26. #include "plat.h"
  27. #include <pico/pico_int.h>
  28. #include <pico/patch.h>
  29. #include <pico/sound/mix.h>
  30. #include <zlib.h>
  31. #ifdef BENCHMARK
  32. #define OSD_FPS_X 220
  33. #else
  34. #define OSD_FPS_X 260
  35. #endif
  36. //extern int crashed_940;
  37. static int osd_fps_x, osd_y, doing_bg_frame;
  38. const char *renderer_names[] = { "16bit accurate", " 8bit accurate", " 8bit fast", NULL };
  39. const char *renderer_names32x[] = { "accurate", "faster", "fastest", NULL };
  40. enum renderer_types { RT_16BIT, RT_8BIT_ACC, RT_8BIT_FAST, RT_COUNT };
  41. static int (*emu_scan_begin)(unsigned int num) = NULL;
  42. static int (*emu_scan_end)(unsigned int num) = NULL;
  43. void pemu_prep_defconfig(void)
  44. {
  45. gp2x_soc_t soc;
  46. defaultConfig.CPUclock = default_cpu_clock;
  47. defaultConfig.renderer32x = RT_8BIT_FAST;
  48. defaultConfig.analog_deadzone = 50;
  49. soc = soc_detect();
  50. if (soc == SOCID_MMSP2)
  51. defaultConfig.s_PicoOpt |= POPT_EXT_FM;
  52. else if (soc == SOCID_POLLUX) {
  53. defaultConfig.EmuOpt |= EOPT_WIZ_TEAR_FIX|EOPT_SHOW_RTC;
  54. defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX;
  55. }
  56. }
  57. void pemu_validate_config(void)
  58. {
  59. if (gp2x_dev_id != GP2X_DEV_GP2X)
  60. PicoOpt &= ~POPT_EXT_FM;
  61. if (gp2x_dev_id != GP2X_DEV_WIZ)
  62. currentConfig.EmuOpt &= ~EOPT_WIZ_TEAR_FIX;
  63. if (currentConfig.gamma < 10 || currentConfig.gamma > 300)
  64. currentConfig.gamma = 100;
  65. if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 1024)
  66. currentConfig.CPUclock = default_cpu_clock;
  67. }
  68. static int get_renderer(void)
  69. {
  70. if (PicoAHW & PAHW_32X)
  71. return currentConfig.renderer32x;
  72. else
  73. return currentConfig.renderer;
  74. }
  75. static void change_renderer(int diff)
  76. {
  77. int *r;
  78. if (PicoAHW & PAHW_32X)
  79. r = &currentConfig.renderer32x;
  80. else
  81. r = &currentConfig.renderer;
  82. *r += diff;
  83. // 8bpp fast is not there (yet?)
  84. if ((PicoAHW & PAHW_SMS) && *r == RT_8BIT_FAST)
  85. (*r)++;
  86. if (*r >= RT_COUNT)
  87. *r = 0;
  88. else if (*r < 0)
  89. *r = RT_COUNT - 1;
  90. }
  91. #define is_16bit_mode() \
  92. (get_renderer() == RT_16BIT || (PicoAHW & PAHW_32X))
  93. static void (*osd_text)(int x, int y, const char *text);
  94. static void osd_text8(int x, int y, const char *text)
  95. {
  96. int len = strlen(text)*8;
  97. int *p, i, h, offs;
  98. len = (len+3) >> 2;
  99. for (h = 0; h < 8; h++) {
  100. offs = (x + g_screen_width * (y+h)) & ~3;
  101. p = (int *) ((char *)g_screen_ptr + offs);
  102. for (i = len; i; i--, p++)
  103. *p = 0xe0e0e0e0;
  104. }
  105. emu_text_out8(x, y, text);
  106. }
  107. static void osd_text8_rot(int x, int y, const char *text)
  108. {
  109. int len = strlen(text) * 8;
  110. char *p = (char *)g_screen_ptr + 240*(320-x) + y;
  111. while (len--) {
  112. memset(p, 0xe0, 8);
  113. p -= 240;
  114. }
  115. emu_text_out8_rot(x, y, text);
  116. }
  117. static void osd_text16_rot(int x, int y, const char *text)
  118. {
  119. int len = strlen(text) * 8;
  120. short *p = (short *)g_screen_ptr + 240*(320-x) + y;
  121. while (len--) {
  122. memset(p, 0, 8*2);
  123. p -= 240;
  124. }
  125. emu_text_out16_rot(x, y, text);
  126. }
  127. static void draw_cd_leds(void)
  128. {
  129. int led_reg, pitch, scr_offs, led_offs;
  130. led_reg = Pico_mcd->s68k_regs[0];
  131. if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {
  132. pitch = 240;
  133. led_offs = -pitch * 6;
  134. scr_offs = pitch * (320 - 4);
  135. } else {
  136. pitch = 320;
  137. led_offs = 4;
  138. scr_offs = pitch * 2 + 4;
  139. }
  140. if (!is_16bit_mode()) {
  141. #define p(x) px[(x) >> 2]
  142. // 8-bit modes
  143. unsigned int *px = (unsigned int *)((char *)g_screen_ptr + scr_offs);
  144. unsigned int col_g = (led_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
  145. unsigned int col_r = (led_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
  146. p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;
  147. p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;
  148. #undef p
  149. } else {
  150. #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]
  151. // 16-bit modes
  152. unsigned int *px = (unsigned int *)((short *)g_screen_ptr + scr_offs);
  153. unsigned int col_g = (led_reg & 2) ? 0x06000600 : 0;
  154. unsigned int col_r = (led_reg & 1) ? 0xc000c000 : 0;
  155. p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;
  156. p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;
  157. #undef p
  158. }
  159. }
  160. static void draw_pico_ptr(void)
  161. {
  162. unsigned short *p = (unsigned short *)g_screen_ptr;
  163. int x, y, pitch = 320;
  164. // only if pen enabled and for 16bit modes
  165. if (pico_inp_mode == 0 || currentConfig.EmuOpt != RT_16BIT)
  166. return;
  167. x = pico_pen_x + PICO_PEN_ADJUST_X;
  168. y = pico_pen_y + PICO_PEN_ADJUST_Y;
  169. if (!(Pico.video.reg[12]&1) && !(PicoOpt & POPT_DIS_32C_BORDER))
  170. x += 32;
  171. if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {
  172. pitch = 240;
  173. p += (319 - x) * pitch + y;
  174. } else
  175. p += x + y * pitch;
  176. p[0] ^= 0xffff;
  177. p[pitch-1] ^= 0xffff;
  178. p[pitch] ^= 0xffff;
  179. p[pitch+1] ^= 0xffff;
  180. p[pitch*2] ^= 0xffff;
  181. }
  182. /* rot thing for Wiz */
  183. static unsigned char __attribute__((aligned(4))) rot_buff[320*4*2];
  184. static int EmuScanBegin16_rot(unsigned int num)
  185. {
  186. DrawLineDest = rot_buff + (num & 3) * 320 * 2;
  187. return 0;
  188. }
  189. static int EmuScanEnd16_rot(unsigned int num)
  190. {
  191. if ((num & 3) != 3)
  192. return 0;
  193. rotated_blit16(g_screen_ptr, rot_buff, num + 1,
  194. !(Pico.video.reg[12] & 1) && !(PicoOpt & POPT_EN_SOFTSCALE));
  195. return 0;
  196. }
  197. static int EmuScanBegin8_rot(unsigned int num)
  198. {
  199. DrawLineDest = rot_buff + (num & 3) * 320;
  200. return 0;
  201. }
  202. static int EmuScanEnd8_rot(unsigned int num)
  203. {
  204. if ((num & 3) != 3)
  205. return 0;
  206. rotated_blit8(g_screen_ptr, rot_buff, num + 1,
  207. !(Pico.video.reg[12] & 1));
  208. return 0;
  209. }
  210. /* line doublers */
  211. static unsigned int ld_counter;
  212. static int ld_left, ld_lines;
  213. static int EmuScanBegin16_ld(unsigned int num)
  214. {
  215. if ((signed int)(ld_counter - num) > 100)
  216. ld_counter = 0;
  217. if (emu_scan_begin)
  218. return emu_scan_begin(ld_counter);
  219. else
  220. DrawLineDest = (char *)g_screen_ptr + 320 * ld_counter * gp2x_current_bpp / 8;
  221. return 0;
  222. }
  223. static int EmuScanEnd16_ld(unsigned int num)
  224. {
  225. void *oldline = DrawLineDest;
  226. if (emu_scan_end)
  227. emu_scan_end(ld_counter);
  228. ld_counter++;
  229. ld_left--;
  230. if (ld_left <= 0) {
  231. ld_left = ld_lines;
  232. EmuScanBegin16_ld(num);
  233. memcpy32(DrawLineDest, oldline, 320 * gp2x_current_bpp / 8 / 4);
  234. if (emu_scan_end)
  235. emu_scan_end(ld_counter);
  236. ld_counter++;
  237. }
  238. return 0;
  239. }
  240. static int localPal[0x100];
  241. static void (*vidcpyM2)(void *dest, void *src, int m32col, int with_32c_border);
  242. static int (*make_local_pal)(int fast_mode);
  243. static int make_local_pal_md(int fast_mode)
  244. {
  245. int pallen = 0xc0;
  246. bgr444_to_rgb32(localPal, Pico.cram);
  247. if (fast_mode)
  248. return 0x40;
  249. if (Pico.video.reg[0xC] & 8) { // shadow/hilight mode
  250. bgr444_to_rgb32_sh(localPal, Pico.cram);
  251. localPal[0xc0] = 0x0000c000;
  252. localPal[0xd0] = 0x00c00000;
  253. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  254. localPal[0xf0] = 0x00ffffff;
  255. pallen = 0x100;
  256. }
  257. else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes
  258. bgr444_to_rgb32(localPal+0x40, HighPal);
  259. bgr444_to_rgb32(localPal+0x80, HighPal+0x40);
  260. }
  261. else
  262. memcpy32(localPal+0x80, localPal, 0x40); // for spr prio mess
  263. return pallen;
  264. }
  265. static int make_local_pal_sms(int fast_mode)
  266. {
  267. unsigned short *spal = Pico.cram;
  268. unsigned int *dpal = (void *)localPal;
  269. unsigned int i, t;
  270. for (i = 0x40; i > 0; i--) {
  271. t = *spal++;
  272. t = ((t & 0x0003) << 22) | ((t & 0x000c) << 12) | ((t & 0x0030) << 2);
  273. t |= t >> 2;
  274. t |= t >> 4;
  275. *dpal++ = t;
  276. }
  277. return 0x40;
  278. }
  279. void pemu_finalize_frame(const char *fps, const char *notice)
  280. {
  281. int emu_opt = currentConfig.EmuOpt;
  282. int ret;
  283. if (PicoAHW & PAHW_32X)
  284. ; // nothing to do
  285. else if (get_renderer() == RT_8BIT_FAST)
  286. {
  287. // 8bit fast renderer
  288. if (Pico.m.dirtyPal) {
  289. Pico.m.dirtyPal = 0;
  290. ret = make_local_pal(1);
  291. // feed new palette to our device
  292. gp2x_video_setpalette(localPal, ret);
  293. }
  294. // a hack for VR
  295. if (PicoAHW & PAHW_SVP)
  296. memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
  297. // do actual copy
  298. vidcpyM2(g_screen_ptr, PicoDraw2FB+328*8,
  299. !(Pico.video.reg[12] & 1), !(PicoOpt & POPT_DIS_32C_BORDER));
  300. }
  301. else if (get_renderer() == RT_8BIT_ACC)
  302. {
  303. // 8bit accurate renderer
  304. if (Pico.m.dirtyPal)
  305. {
  306. Pico.m.dirtyPal = 0;
  307. ret = make_local_pal(0);
  308. gp2x_video_setpalette(localPal, ret);
  309. }
  310. }
  311. if (notice)
  312. osd_text(4, osd_y, notice);
  313. if (emu_opt & EOPT_SHOW_FPS)
  314. osd_text(osd_fps_x, osd_y, fps);
  315. if ((PicoAHW & PAHW_MCD) && (emu_opt & EOPT_EN_CD_LEDS))
  316. draw_cd_leds();
  317. if (PicoAHW & PAHW_PICO)
  318. draw_pico_ptr();
  319. }
  320. void plat_video_flip(void)
  321. {
  322. int stride = g_screen_width;
  323. gp2x_video_flip();
  324. if (is_16bit_mode())
  325. stride *= 2;
  326. PicoDrawSetOutBuf(g_screen_ptr, stride);
  327. }
  328. /* XXX */
  329. unsigned int plat_get_ticks_ms(void)
  330. {
  331. return gp2x_get_ticks_ms();
  332. }
  333. unsigned int plat_get_ticks_us(void)
  334. {
  335. return gp2x_get_ticks_us();
  336. }
  337. void plat_wait_till_us(unsigned int us_to)
  338. {
  339. unsigned int now;
  340. spend_cycles(1024);
  341. now = plat_get_ticks_us();
  342. while ((signed int)(us_to - now) > 512)
  343. {
  344. spend_cycles(1024);
  345. now = plat_get_ticks_us();
  346. }
  347. }
  348. void plat_video_wait_vsync(void)
  349. {
  350. gp2x_video_wait_vsync();
  351. }
  352. void plat_status_msg_clear(void)
  353. {
  354. int is_8bit = !is_16bit_mode();
  355. if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {
  356. /* ugh.. */
  357. int i, u, *p;
  358. if (is_8bit) {
  359. for (i = 0; i < 4; i++) {
  360. p = (int *)gp2x_screens[i] + (240-8) / 4;
  361. for (u = 320; u > 0; u--, p += 240/4)
  362. p[0] = p[1] = 0xe0e0e0e0;
  363. }
  364. } else {
  365. for (i = 0; i < 4; i++) {
  366. p = (int *)gp2x_screens[i] + (240-8)*2 / 4;
  367. for (u = 320; u > 0; u--, p += 240*2/4)
  368. p[0] = p[1] = p[2] = p[3] = 0;
  369. }
  370. }
  371. return;
  372. }
  373. if (is_8bit)
  374. gp2x_memset_all_buffers(320*232, 0xe0, 320*8);
  375. else
  376. gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);
  377. }
  378. void plat_status_msg_busy_next(const char *msg)
  379. {
  380. plat_status_msg_clear();
  381. pemu_finalize_frame("", msg);
  382. plat_video_flip();
  383. emu_status_msg("");
  384. /* assumption: msg_busy_next gets called only when
  385. * something slow is about to happen */
  386. reset_timing = 1;
  387. }
  388. void plat_status_msg_busy_first(const char *msg)
  389. {
  390. gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);
  391. plat_status_msg_busy_next(msg);
  392. }
  393. static void vid_reset_mode(void)
  394. {
  395. int gp2x_mode = 16;
  396. int renderer = get_renderer();
  397. PicoOpt &= ~POPT_ALT_RENDERER;
  398. emu_scan_begin = NULL;
  399. emu_scan_end = NULL;
  400. switch (renderer) {
  401. case RT_16BIT:
  402. PicoDrawSetOutFormat(PDF_RGB555, 0);
  403. PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
  404. break;
  405. case RT_8BIT_ACC:
  406. PicoDrawSetOutFormat(PDF_8BIT, 0);
  407. PicoDrawSetOutBuf(g_screen_ptr, g_screen_width);
  408. gp2x_mode = 8;
  409. break;
  410. case RT_8BIT_FAST:
  411. PicoOpt |= POPT_ALT_RENDERER;
  412. PicoDrawSetOutFormat(PDF_NONE, 0);
  413. vidcpyM2 = vidcpy_m2;
  414. gp2x_mode = 8;
  415. break;
  416. default:
  417. printf("bad renderer\n");
  418. break;
  419. }
  420. if (PicoAHW & PAHW_32X) {
  421. // Wiz 16bit is an exception, uses line rendering due to rotation mess
  422. if (renderer == RT_16BIT && (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)) {
  423. PicoDrawSetOutFormat(PDF_RGB555, 1);
  424. }
  425. else {
  426. PicoDrawSetOutFormat(PDF_NONE, 0);
  427. }
  428. PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
  429. gp2x_mode = 16;
  430. }
  431. if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {
  432. if ((PicoAHW & PAHW_32X) || renderer == RT_16BIT) {
  433. emu_scan_begin = EmuScanBegin16_rot;
  434. emu_scan_end = EmuScanEnd16_rot;
  435. }
  436. else if (renderer == RT_8BIT_ACC) {
  437. emu_scan_begin = EmuScanBegin8_rot;
  438. emu_scan_end = EmuScanEnd8_rot;
  439. }
  440. else if (renderer == RT_8BIT_FAST)
  441. vidcpyM2 = vidcpy_m2_rot;
  442. }
  443. PicoDrawSetCallbacks(emu_scan_begin, emu_scan_end);
  444. if (is_16bit_mode())
  445. osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text16_rot : emu_osd_text16;
  446. else
  447. osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text8_rot : osd_text8;
  448. gp2x_video_wait_vsync();
  449. if (!is_16bit_mode()) {
  450. // setup pal for 8-bit modes
  451. localPal[0xc0] = 0x0000c000; // MCD LEDs
  452. localPal[0xd0] = 0x00c00000;
  453. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  454. localPal[0xf0] = 0x00ffffff;
  455. gp2x_video_setpalette(localPal, 0x100);
  456. gp2x_memset_all_buffers(0, 0xe0, 320*240);
  457. }
  458. else
  459. gp2x_memset_all_buffers(0, 0, 320*240*2);
  460. if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)
  461. gp2x_mode = -gp2x_mode;
  462. gp2x_video_changemode(gp2x_mode, Pico.m.pal);
  463. Pico.m.dirtyPal = 1;
  464. PicoOpt &= ~POPT_EN_SOFTSCALE;
  465. if (currentConfig.scaling == EOPT_SCALE_SW)
  466. PicoOpt |= POPT_EN_SOFTSCALE;
  467. // palette converters for 8bit modes
  468. make_local_pal = (PicoAHW & PAHW_SMS) ? make_local_pal_sms : make_local_pal_md;
  469. }
  470. void emu_video_mode_change(int start_line, int line_count, int is_32cols)
  471. {
  472. int scalex = 320, scaley = 240;
  473. int ln_offs = 0;
  474. if (doing_bg_frame)
  475. return;
  476. osd_fps_x = OSD_FPS_X;
  477. osd_y = 232;
  478. /* set up hwscaling here */
  479. PicoOpt &= ~POPT_DIS_32C_BORDER;
  480. if (is_32cols && currentConfig.scaling == EOPT_SCALE_HW) {
  481. scalex = 256;
  482. PicoOpt |= POPT_DIS_32C_BORDER;
  483. osd_fps_x = OSD_FPS_X - 64;
  484. }
  485. if (currentConfig.vscaling == EOPT_SCALE_HW) {
  486. ln_offs = start_line;
  487. scaley = line_count;
  488. osd_y = start_line + line_count - 8;
  489. }
  490. gp2x_video_RGB_setscaling(ln_offs, scalex, scaley);
  491. /* line doubling */
  492. if (currentConfig.vscaling == EOPT_SCALE_SW && line_count < 240) {
  493. ld_lines = ld_left = line_count / (240 - line_count);
  494. PicoDrawSetCallbacks(EmuScanBegin16_ld, EmuScanEnd16_ld);
  495. }
  496. // clear whole screen in all buffers
  497. if (!is_16bit_mode())
  498. gp2x_memset_all_buffers(0, 0xe0, 320*240);
  499. else
  500. gp2x_memset_all_buffers(0, 0, 320*240*2);
  501. }
  502. void plat_video_toggle_renderer(int change, int is_menu_call)
  503. {
  504. change_renderer(change);
  505. if (is_menu_call)
  506. return;
  507. vid_reset_mode();
  508. rendstatus_old = -1;
  509. if (PicoAHW & PAHW_32X)
  510. emu_status_msg(renderer_names32x[get_renderer()]);
  511. else
  512. emu_status_msg(renderer_names[get_renderer()]);
  513. }
  514. #if 0 // TODO
  515. static void RunEventsPico(unsigned int events)
  516. {
  517. int ret, px, py, lim_x;
  518. static int pdown_frames = 0;
  519. // for F200
  520. ret = gp2x_touchpad_read(&px, &py);
  521. if (ret >= 0)
  522. {
  523. if (ret > 35000)
  524. {
  525. if (pdown_frames++ > 5)
  526. PicoPad[0] |= 0x20;
  527. pico_pen_x = px;
  528. pico_pen_y = py;
  529. if (!(Pico.video.reg[12]&1)) {
  530. pico_pen_x -= 32;
  531. if (pico_pen_x < 0) pico_pen_x = 0;
  532. if (pico_pen_x > 248) pico_pen_x = 248;
  533. }
  534. if (pico_pen_y > 224) pico_pen_y = 224;
  535. }
  536. else
  537. pdown_frames = 0;
  538. //if (ret == 0)
  539. // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
  540. }
  541. }
  542. #endif
  543. void plat_update_volume(int has_changed, int is_up)
  544. {
  545. static int prev_frame = 0, wait_frames = 0;
  546. int need_low_volume = 0;
  547. int vol = currentConfig.volume;
  548. gp2x_soc_t soc;
  549. soc = soc_detect();
  550. if ((PicoOpt & POPT_EN_STEREO) && soc == SOCID_MMSP2)
  551. need_low_volume = 1;
  552. if (has_changed)
  553. {
  554. if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
  555. wait_frames++;
  556. else {
  557. wait_frames = 0;
  558. plat_target_step_volume(&currentConfig.volume, is_up ? 1 : -1);
  559. vol = currentConfig.volume;
  560. }
  561. emu_status_msg("VOL: %02i", vol);
  562. prev_frame = Pico.m.frame_count;
  563. }
  564. if (!need_low_volume)
  565. return;
  566. /* set the right mixer func */
  567. if (vol >= 5)
  568. PsndMix_32_to_16l = mix_32_to_16l_stereo;
  569. else {
  570. mix_32_to_16l_level = 5 - vol;
  571. PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
  572. }
  573. }
  574. void pemu_sound_start(void)
  575. {
  576. gp2x_soc_t soc;
  577. emu_sound_start();
  578. if (currentConfig.EmuOpt & EOPT_EN_SOUND)
  579. {
  580. soc = soc_detect();
  581. if (soc == SOCID_POLLUX) {
  582. PsndRate = pollux_get_real_snd_rate(PsndRate);
  583. PsndRerate(Pico.m.frame_count ? 1 : 0);
  584. }
  585. plat_target_step_volume(&currentConfig.volume, 0);
  586. }
  587. }
  588. static const int sound_rates[] = { 44100, 32000, 22050, 16000, 11025, 8000 };
  589. void pemu_sound_stop(void)
  590. {
  591. int i;
  592. /* get back from Pollux pain */
  593. PsndRate += 1000;
  594. for (i = 0; i < ARRAY_SIZE(sound_rates); i++) {
  595. if (PsndRate >= sound_rates[i]) {
  596. PsndRate = sound_rates[i];
  597. break;
  598. }
  599. }
  600. }
  601. void pemu_forced_frame(int no_scale, int do_emu)
  602. {
  603. doing_bg_frame = 1;
  604. PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
  605. PicoDrawSetCallbacks(NULL, NULL);
  606. Pico.m.dirtyPal = 1;
  607. emu_cmn_forced_frame(no_scale, do_emu);
  608. g_menubg_src_ptr = g_screen_ptr;
  609. doing_bg_frame = 0;
  610. }
  611. void plat_debug_cat(char *str)
  612. {
  613. }
  614. void plat_video_loop_prepare(void)
  615. {
  616. // make sure we are in correct mode
  617. change_renderer(0);
  618. vid_reset_mode();
  619. }
  620. void pemu_loop_prep(void)
  621. {
  622. if (gp2x_dev_id == GP2X_DEV_CAANOO)
  623. in_set_config_int(in_name_to_id("evdev:pollux-analog"),
  624. IN_CFG_ABS_DEAD_ZONE,
  625. currentConfig.analog_deadzone);
  626. // dirty buffers better go now than during gameplay
  627. sync();
  628. sleep(0);
  629. }
  630. void pemu_loop_end(void)
  631. {
  632. pemu_sound_stop();
  633. /* do one more frame for menu bg */
  634. pemu_forced_frame(0, 1);
  635. }