emu.c 12 KB

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