emu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. (currentConfig.renderer == RT_16BIT || (PicoIn.AHW & PAHW_32X) || doing_bg_frame)
  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 || !is_16bit_mode())
  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; // numbers in Q1 format
  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 -= 2;
  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. memcpy(localPal+0xc0, localPal, 0x40*4); // for spr prio mess
  262. }
  263. else {
  264. bgr444_to_rgb32(localPal, Pico.est.SonicPal);
  265. memcpy(localPal+0x40, localPal, 0x40*4); // for spr prio mess
  266. memcpy(localPal+0x80, localPal, 0x80*4); // for spr prio mess
  267. }
  268. localPal[0xc0] = 0x0000c000;
  269. localPal[0xd0] = 0x00c00000;
  270. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  271. localPal[0xf0] = 0x00ffffff;
  272. if (Pico.m.dirtyPal == 2)
  273. Pico.m.dirtyPal = 0;
  274. return pallen;
  275. }
  276. static int make_local_pal_sms(int fast_mode)
  277. {
  278. bgr444_to_rgb32(localPal, PicoMem.cram);
  279. Pico.m.dirtyPal = 0;
  280. return 0x40;
  281. }
  282. void pemu_finalize_frame(const char *fps, const char *notice)
  283. {
  284. int emu_opt = currentConfig.EmuOpt;
  285. if (PicoIn.AHW & PAHW_32X)
  286. localPalSize = 0; // nothing to do
  287. else if (get_renderer() == RT_8BIT_FAST)
  288. {
  289. // 8bit fast renderer
  290. if (Pico.m.dirtyPal)
  291. localPalSize = make_local_pal(1);
  292. // a hack for VR
  293. if (PicoIn.AHW & PAHW_SVP)
  294. memset32((int *)(Pico.est.Draw2FB+328*8+328*223), 0xe0e0e0e0, 328/4);
  295. // clear top and bottom of overlap trash
  296. if (!(Pico.est.rendstatus & PDRAW_30_ROWS)) {
  297. memset32((int *)(Pico.est.Draw2FB+8*(224+8)), 0xe0e0e0e0, 328*8/4);
  298. memset32((int *)(Pico.est.Draw2FB), 0xe0e0e0e0, 328*8/4);
  299. }
  300. // do actual copy
  301. vidcpyM2(g_screen_ptr, Pico.est.Draw2FB+328*8,
  302. !(Pico.video.reg[12] & 1), !(PicoIn.opt & POPT_DIS_32C_BORDER));
  303. }
  304. else if (get_renderer() == RT_8BIT_ACC)
  305. {
  306. // 8bit accurate renderer
  307. if (Pico.m.dirtyPal)
  308. localPalSize = make_local_pal(0);
  309. }
  310. else localPalSize = 0; // no palette in 16bit mode
  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 ((PicoIn.AHW & PAHW_MCD) && (emu_opt & EOPT_EN_CD_LEDS))
  316. draw_cd_leds();
  317. if (PicoIn.AHW & 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. // switching the palette takes immediate effect, whilst flipping only
  325. // takes effect with the next vsync; unavoidable flicker may occur!
  326. if (localPalSize)
  327. gp2x_video_setpalette(localPal, localPalSize);
  328. if (is_16bit_mode())
  329. stride *= 2;
  330. // the fast renderer has overlap areas and can't directly render to
  331. // screen buffers. Its output is copied to screen in finalize_frame
  332. if (currentConfig.renderer != RT_8BIT_FAST || (PicoIn.AHW & PAHW_32X))
  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, 0xe0e0e0e0, 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. if (doing_bg_frame)
  405. renderer = RT_16BIT;
  406. PicoIn.opt &= ~POPT_ALT_RENDERER;
  407. emu_scan_begin = NULL;
  408. emu_scan_end = NULL;
  409. switch (renderer) {
  410. case RT_16BIT:
  411. PicoDrawSetOutFormat(PDF_RGB555, 0);
  412. PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
  413. break;
  414. case RT_8BIT_ACC:
  415. PicoDrawSetOutFormat(PDF_8BIT, 0);
  416. PicoDrawSetOutBuf(g_screen_ptr, g_screen_width);
  417. gp2x_mode = 8;
  418. break;
  419. case RT_8BIT_FAST:
  420. PicoIn.opt |= POPT_ALT_RENDERER;
  421. PicoDrawSetOutFormat(PDF_NONE, 0);
  422. vidcpyM2 = vidcpy_m2;
  423. gp2x_mode = 8;
  424. break;
  425. default:
  426. printf("bad renderer\n");
  427. break;
  428. }
  429. if (PicoIn.AHW & PAHW_32X) {
  430. // Wiz 16bit is an exception, uses line rendering due to rotation mess
  431. if (renderer == RT_16BIT && (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)) {
  432. PicoDrawSetOutFormat(PDF_RGB555, 1);
  433. }
  434. PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
  435. gp2x_mode = 16;
  436. }
  437. if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) {
  438. if ((PicoIn.AHW & PAHW_32X) || renderer == RT_16BIT) {
  439. emu_scan_begin = EmuScanBegin16_rot;
  440. emu_scan_end = EmuScanEnd16_rot;
  441. }
  442. else if (renderer == RT_8BIT_ACC) {
  443. emu_scan_begin = EmuScanBegin8_rot;
  444. emu_scan_end = EmuScanEnd8_rot;
  445. }
  446. else if (renderer == RT_8BIT_FAST)
  447. vidcpyM2 = vidcpy_m2_rot;
  448. }
  449. PicoDrawSetCallbacks(emu_scan_begin, emu_scan_end);
  450. if (is_16bit_mode())
  451. osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text16_rot : emu_osd_text16;
  452. else
  453. osd_text = (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX) ? osd_text8_rot : osd_text8;
  454. gp2x_video_wait_vsync();
  455. if (!is_16bit_mode()) {
  456. // setup pal for 8-bit modes
  457. localPal[0xc0] = 0x0000c000; // MCD LEDs
  458. localPal[0xd0] = 0x00c00000;
  459. localPal[0xe0] = 0x00000000; // reserved pixels for OSD
  460. localPal[0xf0] = 0x00ffffff;
  461. gp2x_video_setpalette(localPal, 0x100);
  462. }
  463. if (currentConfig.EmuOpt & EOPT_WIZ_TEAR_FIX)
  464. gp2x_mode = -gp2x_mode;
  465. gp2x_video_changemode(gp2x_mode, Pico.m.pal);
  466. Pico.m.dirtyPal = 1;
  467. PicoIn.opt &= ~(POPT_DIS_32C_BORDER|POPT_EN_SOFTSCALE);
  468. if (currentConfig.scaling == EOPT_SCALE_SW) {
  469. PicoIn.opt |= POPT_EN_SOFTSCALE;
  470. PicoIn.filter = EOPT_FILTER_BILINEAR2;
  471. } else if (currentConfig.scaling == EOPT_SCALE_HW && is_16bit_mode())
  472. // hw scaling, render without any padding
  473. PicoIn.opt |= POPT_DIS_32C_BORDER;
  474. // palette converters for 8bit modes
  475. make_local_pal = (PicoIn.AHW & PAHW_SMS) ? make_local_pal_sms : make_local_pal_md;
  476. }
  477. void emu_video_mode_change(int start_line, int line_count, int start_col, int col_count)
  478. {
  479. int scalex = 320, scaley = 240;
  480. int ln_offs = 0;
  481. /* line doubling for swscaling, also needed for bg frames */
  482. if (currentConfig.vscaling == EOPT_SCALE_SW && line_count < 240) {
  483. ld_lines = ld_left = 2*line_count / (240 - line_count);
  484. PicoDrawSetCallbacks(EmuScanBegin16_ld, EmuScanEnd16_ld);
  485. }
  486. if (doing_bg_frame)
  487. return;
  488. osd_fps_x = OSD_FPS_X;
  489. osd_y = 232;
  490. /* set up hwscaling here */
  491. if (col_count < 320 && currentConfig.scaling == EOPT_SCALE_HW) {
  492. scalex = col_count;
  493. osd_fps_x = col_count - (320-OSD_FPS_X);
  494. }
  495. if (currentConfig.vscaling == EOPT_SCALE_HW) {
  496. ln_offs = start_line;
  497. scaley = line_count;
  498. osd_y = start_line + line_count - 8;
  499. }
  500. gp2x_video_RGB_setscaling(ln_offs, scalex, scaley);
  501. // clear whole screen in all buffers
  502. if (!is_16bit_mode())
  503. gp2x_memset_all_buffers(0, 0xe0, 320*240);
  504. else
  505. gp2x_memset_all_buffers(0, 0, 320*240*2);
  506. }
  507. void plat_video_toggle_renderer(int change, int is_menu_call)
  508. {
  509. change_renderer(change);
  510. if (is_menu_call)
  511. return;
  512. vid_reset_mode();
  513. rendstatus_old = -1;
  514. if (PicoIn.AHW & PAHW_32X)
  515. emu_status_msg(renderer_names32x[get_renderer()]);
  516. else
  517. emu_status_msg(renderer_names[get_renderer()]);
  518. }
  519. #if 0 // TODO
  520. static void RunEventsPico(unsigned int events)
  521. {
  522. int ret, px, py, lim_x;
  523. static int pdown_frames = 0;
  524. // for F200
  525. ret = gp2x_touchpad_read(&px, &py);
  526. if (ret >= 0)
  527. {
  528. if (ret > 35000)
  529. {
  530. if (pdown_frames++ > 5)
  531. PicoIn.pad[0] |= 0x20;
  532. pico_pen_x = px;
  533. pico_pen_y = py;
  534. if (!(Pico.video.reg[12]&1)) {
  535. pico_pen_x -= 32;
  536. if (pico_pen_x < 0) pico_pen_x = 0;
  537. if (pico_pen_x > 248) pico_pen_x = 248;
  538. }
  539. if (pico_pen_y > 224) pico_pen_y = 224;
  540. }
  541. else
  542. pdown_frames = 0;
  543. //if (ret == 0)
  544. // PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;
  545. }
  546. }
  547. #endif
  548. void plat_update_volume(int has_changed, int is_up)
  549. {
  550. static int prev_frame = 0, wait_frames = 0;
  551. int need_low_volume = 0;
  552. int vol = currentConfig.volume;
  553. gp2x_soc_t soc;
  554. soc = soc_detect();
  555. if ((PicoIn.opt & POPT_EN_STEREO) && soc == SOCID_MMSP2)
  556. need_low_volume = 1;
  557. if (has_changed)
  558. {
  559. if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)
  560. wait_frames++;
  561. else {
  562. wait_frames = 0;
  563. plat_target_step_volume(&currentConfig.volume, is_up ? 1 : -1);
  564. vol = currentConfig.volume;
  565. }
  566. emu_status_msg("VOL: %02i", vol);
  567. prev_frame = Pico.m.frame_count;
  568. }
  569. if (!need_low_volume)
  570. return;
  571. /* set the right mixer func */
  572. if (vol >= 5)
  573. PsndMix_32_to_16l = mix_32_to_16l_stereo;
  574. else {
  575. mix_32_to_16l_level = 5 - vol;
  576. PsndMix_32_to_16l = mix_32_to_16l_stereo_lvl;
  577. }
  578. }
  579. void pemu_sound_start(void)
  580. {
  581. gp2x_soc_t soc;
  582. emu_sound_start();
  583. if (currentConfig.EmuOpt & EOPT_EN_SOUND)
  584. {
  585. soc = soc_detect();
  586. if (soc == SOCID_POLLUX) {
  587. PicoIn.sndRate = pollux_get_real_snd_rate(PicoIn.sndRate);
  588. PsndRerate(Pico.m.frame_count ? 1 : 0);
  589. }
  590. plat_target_step_volume(&currentConfig.volume, 0);
  591. }
  592. }
  593. static const int sound_rates[] = { 44100, 32000, 22050, 16000, 11025, 8000 };
  594. void pemu_sound_stop(void)
  595. {
  596. int i;
  597. /* get back from Pollux pain */
  598. PicoIn.sndRate += 1000;
  599. for (i = 0; i < ARRAY_SIZE(sound_rates); i++) {
  600. if (PicoIn.sndRate >= sound_rates[i]) {
  601. PicoIn.sndRate = sound_rates[i];
  602. break;
  603. }
  604. }
  605. }
  606. void pemu_forced_frame(int no_scale, int do_emu)
  607. {
  608. doing_bg_frame = 1;
  609. PicoDrawSetCallbacks(NULL, NULL);
  610. Pico.m.dirtyPal = 1;
  611. PicoIn.opt &= ~POPT_DIS_32C_BORDER;
  612. gp2x_current_bpp = 16;
  613. if (!no_scale)
  614. no_scale = currentConfig.scaling == EOPT_SCALE_NONE;
  615. emu_cmn_forced_frame(no_scale, do_emu, g_screen_ptr);
  616. g_menubg_src_ptr = g_screen_ptr;
  617. doing_bg_frame = 0;
  618. }
  619. void plat_debug_cat(char *str)
  620. {
  621. }
  622. void plat_video_loop_prepare(void)
  623. {
  624. // make sure we are in correct mode
  625. change_renderer(0);
  626. vid_reset_mode();
  627. rendstatus_old = -1;
  628. }
  629. void pemu_loop_prep(void)
  630. {
  631. if (gp2x_dev_id == GP2X_DEV_CAANOO)
  632. in_set_config_int(in_name_to_id("evdev:pollux-analog"),
  633. IN_CFG_ABS_DEAD_ZONE,
  634. currentConfig.analog_deadzone);
  635. // dirty buffers better go now than during gameplay
  636. sync();
  637. sleep(0);
  638. }
  639. void pemu_loop_end(void)
  640. {
  641. pemu_sound_stop();
  642. /* do one more frame for menu bg */
  643. pemu_forced_frame(0, 1);
  644. }