emu.c 18 KB

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