emu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2006-2010
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <sys/mman.h>
  11. #include "../libpicofe/menu.h"
  12. #include "../libpicofe/plat.h"
  13. #include "../common/emu.h"
  14. #include "../common/arm_utils.h"
  15. #include "../common/upscale.h"
  16. #include "../common/version.h"
  17. #include <pico/pico_int.h>
  18. const char *renderer_names[] = { "16bit accurate", " 8bit accurate", " 8bit fast", NULL };
  19. const char *renderer_names32x[] = { "accurate", "faster", "fastest", NULL };
  20. enum renderer_types { RT_16BIT, RT_8BIT_ACC, RT_8BIT_FAST, RT_COUNT };
  21. static int out_x, out_y, out_w, out_h; // renderer output in render buffer
  22. static int screen_x, screen_y, screen_w, screen_h; // final render destination
  23. static int render_bg; // force 16bit mode for bg render
  24. static u16 *ghost_buf; // backbuffer to simulate LCD ghosting
  25. void pemu_prep_defconfig(void)
  26. {
  27. }
  28. void pemu_validate_config(void)
  29. {
  30. #if !defined(__arm__) && !defined(__aarch64__) && !defined(__mips__) && !defined(__riscv__) && !defined(__riscv) && !defined(__powerpc__) && !defined(__ppc__) && !defined(__i386__) && !defined(__x86_64__)
  31. PicoIn.opt &= ~POPT_EN_DRC;
  32. #endif
  33. }
  34. #define is_16bit_mode() \
  35. (currentConfig.renderer == RT_16BIT || (PicoIn.AHW & PAHW_32X) || render_bg)
  36. static int get_renderer(void)
  37. {
  38. if (PicoIn.AHW & PAHW_32X)
  39. return currentConfig.renderer32x;
  40. else
  41. return currentConfig.renderer;
  42. }
  43. static void change_renderer(int diff)
  44. {
  45. int *r;
  46. if (PicoIn.AHW & PAHW_32X)
  47. r = &currentConfig.renderer32x;
  48. else
  49. r = &currentConfig.renderer;
  50. *r += diff;
  51. if (*r >= RT_COUNT)
  52. *r = 0;
  53. else if (*r < 0)
  54. *r = RT_COUNT - 1;
  55. }
  56. static void draw_cd_leds(void)
  57. {
  58. int led_reg, pitch, scr_offs, led_offs;
  59. led_reg = Pico_mcd->s68k_regs[0];
  60. pitch = g_screen_ppitch;
  61. led_offs = 4;
  62. scr_offs = pitch * 2 + 4;
  63. #define p(x) px[(x)*2 >> 2] = px[((x)*2 >> 2) + 1]
  64. // 16-bit modes
  65. uint32_t *px = (uint32_t *)((short *)g_screen_ptr + scr_offs);
  66. uint32_t col_g = (led_reg & 2) ? 0x06000600 : 0;
  67. uint32_t col_r = (led_reg & 1) ? 0xc000c000 : 0;
  68. p(pitch*0) = p(pitch*1) = p(pitch*2) = col_g;
  69. p(pitch*0 + led_offs) = p(pitch*1 + led_offs) = p(pitch*2 + led_offs) = col_r;
  70. #undef p
  71. }
  72. /* render/screen buffer handling:
  73. * In 16 bit mode, render output is directly placed in the screen buffer.
  74. * SW scaling is handled in renderer (x) and in vscaling callbacks here (y).
  75. * In 8 bit modes, output goes to the internal Draw2FB buffer in alternate
  76. * renderer format (8 pix overscan at left/top/bottom), left aligned (DIS_32C).
  77. * It is converted to 16 bit and SW scaled in pemu_finalize_frame.
  78. *
  79. * HW scaling always aligns the image to the left/top, since selecting an area
  80. * for display isn't always possible.
  81. */
  82. static inline u16 *screen_buffer(u16 *buf)
  83. {
  84. return buf + screen_y * g_screen_ppitch + screen_x -
  85. (out_y * g_screen_ppitch + out_x);
  86. }
  87. void screen_blit(u16 *pd, int pp, u8* ps, int ss, u16 *pal)
  88. {
  89. typedef void (*upscale_t)
  90. (u16 *di,int ds, u8 *si,int ss, int w,int h, u16 *pal);
  91. static const upscale_t upscale_256_224_hv[] = {
  92. upscale_rgb_nn_x_4_5_y_16_17, upscale_rgb_snn_x_4_5_y_16_17,
  93. upscale_rgb_bl2_x_4_5_y_16_17, upscale_rgb_bl4_x_4_5_y_16_17,
  94. };
  95. static const upscale_t upscale_256_____h[] = {
  96. upscale_rgb_nn_x_4_5, upscale_rgb_snn_x_4_5,
  97. upscale_rgb_bl2_x_4_5, upscale_rgb_bl4_x_4_5,
  98. };
  99. static const upscale_t upscale_____224_v[] = {
  100. upscale_rgb_nn_y_16_17, upscale_rgb_snn_y_16_17,
  101. upscale_rgb_bl2_y_16_17, upscale_rgb_bl4_y_16_17,
  102. };
  103. static const upscale_t upscale_160_144_hv[] = {
  104. upscale_rgb_nn_x_1_2_y_3_5, upscale_rgb_nn_x_1_2_y_3_5,
  105. upscale_rgb_bl2_x_1_2_y_3_5, upscale_rgb_bl4_x_1_2_y_3_5,
  106. };
  107. static const upscale_t upscale_160_____h[] = {
  108. upscale_rgb_nn_x_1_2, upscale_rgb_nn_x_1_2,
  109. upscale_rgb_bl2_x_1_2, upscale_rgb_bl2_x_1_2,
  110. };
  111. static const upscale_t upscale_____144_v[] = {
  112. upscale_rgb_nn_y_3_5, upscale_rgb_nn_y_3_5,
  113. upscale_rgb_bl2_y_3_5, upscale_rgb_bl4_y_3_5,
  114. };
  115. const upscale_t *upscale;
  116. int y;
  117. // handle software upscaling
  118. upscale = NULL;
  119. if (currentConfig.scaling == EOPT_SCALE_SW && out_w <= 256) {
  120. if (currentConfig.vscaling == EOPT_SCALE_SW && out_h <= 224)
  121. // h+v scaling
  122. upscale = out_w >= 256 ? upscale_256_224_hv: upscale_160_144_hv;
  123. else
  124. // h scaling
  125. upscale = out_w >= 256 ? upscale_256_____h : upscale_160_____h;
  126. } else if (currentConfig.vscaling == EOPT_SCALE_SW && out_h <= 224)
  127. // v scaling
  128. upscale = out_w >= 256 ? upscale_____224_v : upscale_____144_v;
  129. if (!upscale) {
  130. // no scaling
  131. for (y = 0; y < out_h; y++)
  132. h_copy(pd, pp, ps, 328, out_w, f_pal);
  133. return;
  134. }
  135. upscale[currentConfig.filter & 0x3](pd, pp, ps, ss, out_w, out_h, pal);
  136. }
  137. void pemu_finalize_frame(const char *fps, const char *notice)
  138. {
  139. if (!is_16bit_mode()) {
  140. // convert the 8 bit CLUT output to 16 bit RGB
  141. u16 *pd = screen_buffer(g_screen_ptr) +
  142. out_y * g_screen_ppitch + out_x;
  143. u8 *ps = Pico.est.Draw2FB + out_y * 328 + out_x + 8;
  144. PicoDrawUpdateHighPal();
  145. screen_blit(pd, g_screen_ppitch, ps, 328, Pico.est.HighPal);
  146. }
  147. if (currentConfig.ghosting && out_h == 144) {
  148. // GG LCD ghosting emulation
  149. u16 *pd = screen_buffer(g_screen_ptr) +
  150. out_y * g_screen_ppitch + out_x;
  151. u16 *ps = ghost_buf;
  152. int y, h = currentConfig.vscaling == EOPT_SCALE_SW ? 240:out_h;
  153. int w = currentConfig.scaling == EOPT_SCALE_SW ? 320:out_w;
  154. if (currentConfig.ghosting == 1)
  155. for (y = 0; y < h; y++) {
  156. v_blend((u32 *)pd, (u32 *)ps, w/2, p_075_round);
  157. pd += g_screen_ppitch;
  158. ps += w;
  159. }
  160. else
  161. for (y = 0; y < h; y++) {
  162. v_blend((u32 *)pd, (u32 *)ps, w/2, p_05_round);
  163. pd += g_screen_ppitch;
  164. ps += w;
  165. }
  166. }
  167. //#define FUNKEY_AUTHORIZE_TEXT_OVERLAY
  168. #ifndef FUNKEY_AUTHORIZE_TEXT_OVERLAY
  169. return;
  170. #endif //FUNKEY_AUTHORIZE_TEXT_OVERLAY
  171. if (notice)
  172. emu_osd_text16(4, g_screen_height - 8, notice);
  173. if (currentConfig.EmuOpt & EOPT_SHOW_FPS)
  174. emu_osd_text16(g_screen_width - 60, g_screen_height - 8, fps);
  175. if ((PicoIn.AHW & PAHW_MCD) && (currentConfig.EmuOpt & EOPT_EN_CD_LEDS))
  176. draw_cd_leds();
  177. }
  178. void plat_video_set_buffer(void *buf)
  179. {
  180. if (is_16bit_mode())
  181. PicoDrawSetOutBuf(screen_buffer(buf), g_screen_ppitch * 2);
  182. }
  183. static void apply_renderer(void)
  184. {
  185. PicoIn.opt &= ~(POPT_ALT_RENDERER|POPT_EN_SOFTSCALE|POPT_DIS_32C_BORDER);
  186. if (is_16bit_mode()) {
  187. if (currentConfig.scaling == EOPT_SCALE_SW) {
  188. PicoIn.opt |= POPT_EN_SOFTSCALE;
  189. PicoIn.filter = currentConfig.filter;
  190. } else if (currentConfig.scaling == EOPT_SCALE_HW)
  191. // hw scaling, render without any padding
  192. PicoIn.opt |= POPT_DIS_32C_BORDER;
  193. } else
  194. PicoIn.opt |= POPT_DIS_32C_BORDER;
  195. switch (get_renderer()) {
  196. case RT_16BIT:
  197. // 32X uses line mode for vscaling with accurate renderer, since
  198. // the MD VDP layer must be unscaled and merging the scaled 32X
  199. // image data will fail.
  200. PicoDrawSetOutFormat(PDF_RGB555,
  201. (PicoIn.AHW & PAHW_32X) && currentConfig.vscaling);
  202. PicoDrawSetOutBuf(screen_buffer(g_screen_ptr), g_screen_ppitch * 2);
  203. break;
  204. case RT_8BIT_ACC:
  205. // for simplification the 8 bit accurate renderer uses the same
  206. // storage format as the fast renderer
  207. PicoDrawSetOutFormat(PDF_8BIT, 0);
  208. PicoDrawSetOutBuf(Pico.est.Draw2FB, 328);
  209. break;
  210. case RT_8BIT_FAST:
  211. PicoIn.opt |= POPT_ALT_RENDERER;
  212. PicoDrawSetOutFormat(PDF_NONE, 0);
  213. break;
  214. }
  215. if (PicoIn.AHW & PAHW_32X)
  216. PicoDrawSetOutBuf(screen_buffer(g_screen_ptr), g_screen_ppitch * 2);
  217. Pico.m.dirtyPal = 1;
  218. }
  219. void plat_video_toggle_renderer(int change, int is_menu)
  220. {
  221. change_renderer(change);
  222. if (!is_menu) {
  223. apply_renderer();
  224. if (PicoIn.AHW & PAHW_32X)
  225. emu_status_msg(renderer_names32x[get_renderer()]);
  226. else
  227. emu_status_msg(renderer_names[get_renderer()]);
  228. }
  229. }
  230. void plat_status_msg_clear(void)
  231. {
  232. plat_video_clear_status();
  233. }
  234. void plat_status_msg_busy_next(const char *msg)
  235. {
  236. printf("****** %s: %s\n", __func__, msg);
  237. plat_status_msg_clear();
  238. pemu_finalize_frame("", msg);
  239. plat_video_flip();
  240. emu_status_msg("");
  241. reset_timing = 1;
  242. }
  243. void plat_status_msg_busy_first(const char *msg)
  244. {
  245. plat_status_msg_busy_next(msg);
  246. }
  247. void plat_update_volume(int has_changed, int is_up)
  248. {
  249. }
  250. void pemu_sound_start(void)
  251. {
  252. emu_sound_start();
  253. }
  254. void plat_debug_cat(char *str)
  255. {
  256. }
  257. void pemu_forced_frame(int no_scale, int do_emu)
  258. {
  259. int hs = currentConfig.scaling, vs = currentConfig.vscaling;
  260. // create centered and sw scaled (if scaling enabled) 16 bit output
  261. PicoIn.opt &= ~POPT_DIS_32C_BORDER;
  262. Pico.m.dirtyPal = 1;
  263. if (currentConfig.scaling) currentConfig.scaling = EOPT_SCALE_SW;
  264. if (currentConfig.vscaling) currentConfig.vscaling = EOPT_SCALE_SW;
  265. plat_video_set_size(g_menuscreen_w, g_menuscreen_h);
  266. // render a frame in 16 bit mode
  267. render_bg = 1;
  268. emu_cmn_forced_frame(no_scale, do_emu, screen_buffer(g_screen_ptr));
  269. render_bg = 0;
  270. g_menubg_src_ptr = g_screen_ptr;
  271. currentConfig.scaling = hs, currentConfig.vscaling = vs;
  272. }
  273. /* vertical sw scaling, 16 bit mode */
  274. static int vscale_state;
  275. static int cb_vscaling_begin(unsigned int line)
  276. {
  277. // at start of new frame?
  278. if (line <= out_y) {
  279. // set y frame offset (see emu_video_mode_change)
  280. Pico.est.DrawLineDest = screen_buffer(g_screen_ptr) +
  281. (out_y * g_screen_ppitch /*+ out_x*/);
  282. vscale_state = 0;
  283. return out_y - line;
  284. } else if (line > out_y + out_h)
  285. return 1;
  286. return 0;
  287. }
  288. static int cb_vscaling_nop(unsigned int line)
  289. {
  290. return 0;
  291. }
  292. static int cb_vscaling_end(unsigned int line)
  293. {
  294. u16 *dest = (u16 *)Pico.est.DrawLineDest + out_x;
  295. // helpers for 32 bit operation (2 pixels at once):
  296. u32 *dest32 = (u32 *)dest;
  297. int pp = g_screen_ppitch;
  298. if (out_h == 144)
  299. switch (currentConfig.filter) {
  300. case 0: v_upscale_nn_3_5(dest32, pp/2, out_w/2, vscale_state);
  301. break;
  302. default: v_upscale_snn_3_5(dest32, pp/2, out_w/2, vscale_state);
  303. break;
  304. }
  305. else
  306. switch (currentConfig.filter) {
  307. case 3: v_upscale_bl4_16_17(dest32, pp/2, out_w/2, vscale_state);
  308. break;
  309. case 2: v_upscale_bl2_16_17(dest32, pp/2, out_w/2, vscale_state);
  310. break;
  311. case 1: v_upscale_snn_16_17(dest32, pp/2, out_w/2, vscale_state);
  312. break;
  313. default: v_upscale_nn_16_17(dest32, pp/2, out_w/2, vscale_state);
  314. break;
  315. }
  316. Pico.est.DrawLineDest = (u16 *)dest32 - out_x;
  317. return 0;
  318. }
  319. void emu_video_mode_change(int start_line, int line_count, int start_col, int col_count)
  320. {
  321. // relative position in core fb and screen fb
  322. out_y = start_line; out_x = start_col;
  323. out_h = line_count; out_w = col_count;
  324. PicoDrawSetCallbacks(NULL, NULL);
  325. // center output in screen
  326. screen_w = g_screen_width, screen_x = (screen_w - out_w)/2;
  327. screen_h = g_screen_height, screen_y = (screen_h - out_h)/2;
  328. switch (currentConfig.scaling) {
  329. case EOPT_SCALE_HW:
  330. screen_w = out_w;
  331. screen_x = 0;
  332. break;
  333. case EOPT_SCALE_SW:
  334. screen_x = (screen_w - 320)/2;
  335. break;
  336. }
  337. switch (currentConfig.vscaling) {
  338. case EOPT_SCALE_HW:
  339. screen_h = (out_h < 224 && out_h > 144 ? 224 : out_h);
  340. screen_y = 0;
  341. // NTSC always has 224 visible lines, anything smaller has bars
  342. if (out_h < 224 && out_h > 144)
  343. screen_y += (224 - out_h)/2;
  344. // handle vertical centering for 16 bit mode
  345. if (is_16bit_mode())
  346. PicoDrawSetCallbacks(cb_vscaling_begin,cb_vscaling_nop);
  347. break;
  348. case EOPT_SCALE_SW:
  349. screen_y = (screen_h - 240)/2 + (out_h < 240 && out_h > 144);
  350. // NTSC always has 224 visible lines, anything smaller has bars
  351. if (out_h < 224 && out_h > 144)
  352. screen_y += (224 - out_h)/2;
  353. // in 16 bit mode sw scaling is divided between core and platform
  354. if (is_16bit_mode() && out_h < 240)
  355. PicoDrawSetCallbacks(cb_vscaling_begin,cb_vscaling_end);
  356. break;
  357. }
  358. if (screen_w != g_screen_width || screen_h != g_screen_height)
  359. plat_video_set_size(screen_w, screen_h);
  360. plat_video_set_buffer(g_screen_ptr);
  361. // create a backing buffer for emulating the bad GG lcd display
  362. if (currentConfig.ghosting && out_h == 144) {
  363. int h = currentConfig.vscaling == EOPT_SCALE_SW ? 240:out_h;
  364. int w = currentConfig.scaling == EOPT_SCALE_SW ? 320:out_w;
  365. ghost_buf = realloc(ghost_buf, w * h * 2);
  366. memset(ghost_buf, 0, w * h * 2);
  367. }
  368. // clear whole screen in all buffers
  369. if (!is_16bit_mode())
  370. memset32(Pico.est.Draw2FB, 0xe0e0e0e0, (320+8) * (8+240+8) / 4);
  371. plat_video_clear_buffers();
  372. }
  373. void pemu_loop_prep(void)
  374. {
  375. apply_renderer();
  376. plat_video_clear_buffers();
  377. }
  378. void pemu_loop_end(void)
  379. {
  380. /* do one more frame for menu bg */
  381. pemu_forced_frame(0, 1);
  382. if (ghost_buf) {
  383. free(ghost_buf);
  384. ghost_buf = NULL;
  385. }
  386. }
  387. void plat_wait_till_us(unsigned int us_to)
  388. {
  389. unsigned int now;
  390. now = plat_get_ticks_us();
  391. while ((signed int)(us_to - now) > 512)
  392. {
  393. usleep(1024);
  394. now = plat_get_ticks_us();
  395. }
  396. }
  397. void *plat_mem_get_for_drc(size_t size)
  398. {
  399. #ifdef MAP_JIT
  400. // newer versions of OSX, IOS or TvOS need this
  401. return plat_mmap(0, size, 1, 0);
  402. #else
  403. return NULL;
  404. #endif
  405. }