emu.c 20 KB

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