plat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2010,2011
  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/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <sys/ioctl.h>
  14. #include <unistd.h>
  15. #include <linux/fb.h>
  16. #include <linux/omapfb.h>
  17. #include <linux/input.h>
  18. #include "../common/emu.h"
  19. #include "../common/arm_utils.h"
  20. #include "../common/input_pico.h"
  21. #include "../common/version.h"
  22. #include "../libpicofe/input.h"
  23. #include "../libpicofe/menu.h"
  24. #include "../libpicofe/plat.h"
  25. #include "../libpicofe/linux/in_evdev.h"
  26. #include "../libpicofe/linux/sndout_oss.h"
  27. #include "../libpicofe/linux/fbdev.h"
  28. #include "../libpicofe/linux/xenv.h"
  29. #include "plat.h"
  30. #include "asm_utils.h"
  31. #include <pico/pico_int.h>
  32. static struct vout_fbdev *main_fb, *layer_fb;
  33. // g_layer_* - in use, g_layer_c* - configured custom
  34. int g_layer_cx, g_layer_cy, g_layer_cw, g_layer_ch;
  35. static int g_layer_x, g_layer_y;
  36. static int g_layer_w = 320, g_layer_h = 240;
  37. static int g_osd_fps_x, g_osd_y, doing_bg_frame;
  38. static unsigned char __attribute__((aligned(4))) fb_copy[320 * 240 * 2];
  39. static void *temp_frame;
  40. const char *renderer_names[] = { NULL };
  41. const char *renderer_names32x[] = { NULL };
  42. static const char * const pandora_gpio_keys[KEY_MAX + 1] = {
  43. [0 ... KEY_MAX] = NULL,
  44. [KEY_UP] = "Up",
  45. [KEY_LEFT] = "Left",
  46. [KEY_RIGHT] = "Right",
  47. [KEY_DOWN] = "Down",
  48. [KEY_HOME] = "A",
  49. [KEY_PAGEDOWN] = "X",
  50. [KEY_END] = "B",
  51. [KEY_PAGEUP] = "Y",
  52. [KEY_RIGHTSHIFT]= "L",
  53. [KEY_RIGHTCTRL] = "R",
  54. [KEY_LEFTALT] = "Start",
  55. [KEY_LEFTCTRL] = "Select",
  56. [KEY_MENU] = "Pandora",
  57. };
  58. static struct in_default_bind in_evdev_defbinds[] =
  59. {
  60. { KEY_UP, IN_BINDTYPE_PLAYER12, GBTN_UP },
  61. { KEY_DOWN, IN_BINDTYPE_PLAYER12, GBTN_DOWN },
  62. { KEY_LEFT, IN_BINDTYPE_PLAYER12, GBTN_LEFT },
  63. { KEY_RIGHT, IN_BINDTYPE_PLAYER12, GBTN_RIGHT },
  64. { KEY_A, IN_BINDTYPE_PLAYER12, GBTN_A },
  65. { KEY_S, IN_BINDTYPE_PLAYER12, GBTN_B },
  66. { KEY_D, IN_BINDTYPE_PLAYER12, GBTN_C },
  67. { KEY_ENTER, IN_BINDTYPE_PLAYER12, GBTN_START },
  68. { KEY_F, IN_BINDTYPE_EMU, PEVB_FF },
  69. { KEY_BACKSPACE,IN_BINDTYPE_EMU, PEVB_FF },
  70. { KEY_BACKSLASH,IN_BINDTYPE_EMU, PEVB_MENU },
  71. { KEY_SPACE, IN_BINDTYPE_EMU, PEVB_MENU },
  72. { KEY_LEFTCTRL, IN_BINDTYPE_EMU, PEVB_MENU },
  73. { KEY_HOME, IN_BINDTYPE_PLAYER12, GBTN_A },
  74. { KEY_PAGEDOWN, IN_BINDTYPE_PLAYER12, GBTN_B },
  75. { KEY_END, IN_BINDTYPE_PLAYER12, GBTN_C },
  76. { KEY_LEFTALT, IN_BINDTYPE_PLAYER12, GBTN_START },
  77. { KEY_1, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
  78. { KEY_2, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
  79. { KEY_3, IN_BINDTYPE_EMU, PEVB_SSLOT_PREV },
  80. { KEY_4, IN_BINDTYPE_EMU, PEVB_SSLOT_NEXT },
  81. { KEY_5, IN_BINDTYPE_EMU, PEVB_PICO_PPREV },
  82. { KEY_6, IN_BINDTYPE_EMU, PEVB_PICO_PNEXT },
  83. { KEY_7, IN_BINDTYPE_EMU, PEVB_PICO_SWINP },
  84. { 0, 0, 0 }
  85. };
  86. void pemu_prep_defconfig(void)
  87. {
  88. defaultConfig.EmuOpt |= EOPT_VSYNC|EOPT_16BPP;
  89. defaultConfig.s_PicoOpt |= POPT_EN_MCD_GFX;
  90. defaultConfig.scaling = SCALE_2x2_3x2;
  91. }
  92. void pemu_validate_config(void)
  93. {
  94. currentConfig.CPUclock = plat_target_cpu_clock_get();
  95. }
  96. static void osd_text(int x, int y, const char *text)
  97. {
  98. int len = strlen(text)*8;
  99. int i, h;
  100. len++;
  101. if (x + len > g_screen_width)
  102. len = g_screen_width - x;
  103. for (h = 0; h < 8; h++) {
  104. unsigned short *p;
  105. p = (unsigned short *)g_screen_ptr + x + g_screen_width*(y + h);
  106. for (i = len; i > 0; i--, p++)
  107. *p = (*p>>2) & 0x39e7;
  108. }
  109. emu_text_out16(x, y, text);
  110. }
  111. static void draw_cd_leds(void)
  112. {
  113. int old_reg;
  114. old_reg = Pico_mcd->s68k_regs[0];
  115. if (0) {
  116. // 8-bit modes
  117. unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;
  118. unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;
  119. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+ 4) =
  120. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+ 4) =
  121. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+ 4) = col_g;
  122. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*2+12) =
  123. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*3+12) =
  124. *(unsigned int *)((char *)g_screen_ptr + g_screen_width*4+12) = col_r;
  125. } else {
  126. // 16-bit modes
  127. unsigned int *p = (unsigned int *)((short *)g_screen_ptr + g_screen_width*2+4);
  128. unsigned int col_g = (old_reg & 2) ? 0x06000600 : 0;
  129. unsigned int col_r = (old_reg & 1) ? 0xc000c000 : 0;
  130. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
  131. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += g_screen_width/2 - 12/2;
  132. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
  133. }
  134. }
  135. void pemu_finalize_frame(const char *fps, const char *notice)
  136. {
  137. if (notice && notice[0])
  138. osd_text(2, g_osd_y, notice);
  139. if (fps && fps[0] && (currentConfig.EmuOpt & EOPT_SHOW_FPS))
  140. osd_text(g_osd_fps_x, g_osd_y, fps);
  141. if ((PicoAHW & PAHW_MCD) && (currentConfig.EmuOpt & EOPT_EN_CD_LEDS))
  142. draw_cd_leds();
  143. }
  144. void plat_video_flip(void)
  145. {
  146. g_screen_ptr = vout_fbdev_flip(layer_fb);
  147. PicoDrawSetOutBuf(g_screen_ptr, g_screen_width * 2);
  148. // XXX: drain OS event queue here, maybe we'll actually use it someday..
  149. xenv_update(NULL, NULL, NULL, NULL);
  150. }
  151. void plat_video_toggle_renderer(int change, int is_menu)
  152. {
  153. }
  154. void plat_video_menu_enter(int is_rom_loaded)
  155. {
  156. }
  157. void plat_video_menu_begin(void)
  158. {
  159. }
  160. void plat_video_menu_end(void)
  161. {
  162. g_menuscreen_ptr = vout_fbdev_flip(main_fb);
  163. }
  164. void plat_video_menu_leave(void)
  165. {
  166. }
  167. void plat_video_wait_vsync(void)
  168. {
  169. vout_fbdev_wait_vsync(main_fb);
  170. }
  171. void plat_status_msg_clear(void)
  172. {
  173. vout_fbdev_clear_lines(layer_fb, g_osd_y, 8);
  174. }
  175. void plat_status_msg_busy_next(const char *msg)
  176. {
  177. plat_status_msg_clear();
  178. pemu_finalize_frame("", msg);
  179. plat_video_flip();
  180. emu_status_msg("");
  181. reset_timing = 1;
  182. }
  183. void plat_status_msg_busy_first(const char *msg)
  184. {
  185. plat_status_msg_busy_next(msg);
  186. }
  187. void plat_update_volume(int has_changed, int is_up)
  188. {
  189. }
  190. void pemu_forced_frame(int no_scale, int do_emu)
  191. {
  192. doing_bg_frame = 1;
  193. emu_cmn_forced_frame(no_scale, do_emu);
  194. doing_bg_frame = 0;
  195. // making a copy because enabling the layer clears it's mem
  196. memcpy((void *)fb_copy, g_screen_ptr, sizeof(fb_copy));
  197. g_menubg_src_ptr = fb_copy;
  198. }
  199. void pemu_sound_start(void)
  200. {
  201. emu_sound_start();
  202. }
  203. void plat_debug_cat(char *str)
  204. {
  205. }
  206. static int pnd_setup_layer_(int fd, int enabled, int x, int y, int w, int h)
  207. {
  208. struct omapfb_plane_info pi;
  209. struct omapfb_mem_info mi;
  210. int ret;
  211. ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
  212. if (ret != 0) {
  213. perror("QUERY_PLANE");
  214. return -1;
  215. }
  216. ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
  217. if (ret != 0) {
  218. perror("QUERY_MEM");
  219. return -1;
  220. }
  221. /* must disable when changing stuff */
  222. if (pi.enabled) {
  223. pi.enabled = 0;
  224. ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
  225. if (ret != 0)
  226. perror("SETUP_PLANE");
  227. }
  228. mi.size = 320*240*2*4;
  229. ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
  230. if (ret != 0) {
  231. perror("SETUP_MEM");
  232. return -1;
  233. }
  234. pi.pos_x = x;
  235. pi.pos_y = y;
  236. pi.out_width = w;
  237. pi.out_height = h;
  238. pi.enabled = enabled;
  239. ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
  240. if (ret != 0) {
  241. perror("SETUP_PLANE");
  242. return -1;
  243. }
  244. return 0;
  245. }
  246. int pnd_setup_layer(int enabled, int x, int y, int w, int h)
  247. {
  248. return pnd_setup_layer_(vout_fbdev_get_fd(layer_fb), enabled, x, y, w, h);
  249. }
  250. void pnd_restore_layer_data(void)
  251. {
  252. short *t = (short *)fb_copy + 320*240 / 2 + 160;
  253. // right now this is used by menu, which wants to preview something
  254. // so try to get something on the layer.
  255. if ((t[0] | t[5] | t[13]) == 0)
  256. memset32((void *)fb_copy, 0x07000700, sizeof(fb_copy) / 4);
  257. memcpy(g_screen_ptr, (void *)fb_copy, 320*240*2);
  258. plat_video_flip();
  259. }
  260. void emu_video_mode_change(int start_line, int line_count, int is_32cols)
  261. {
  262. int fb_w = 320, fb_h = 240, fb_left = 0, fb_right = 0, fb_top = 0, fb_bottom = 0;
  263. if (doing_bg_frame)
  264. return;
  265. if (is_32cols) {
  266. fb_w = 256;
  267. fb_left = fb_right = 32;
  268. }
  269. switch (currentConfig.scaling) {
  270. case SCALE_1x1:
  271. g_layer_w = fb_w;
  272. g_layer_h = fb_h;
  273. break;
  274. case SCALE_2x2_3x2:
  275. g_layer_w = fb_w * (is_32cols ? 3 : 2);
  276. g_layer_h = fb_h * 2;
  277. break;
  278. case SCALE_2x2_2x2:
  279. g_layer_w = fb_w * 2;
  280. g_layer_h = fb_h * 2;
  281. break;
  282. case SCALE_FULLSCREEN:
  283. g_layer_w = 800;
  284. g_layer_h = 480;
  285. break;
  286. case SCALE_CUSTOM:
  287. g_layer_x = g_layer_cx;
  288. g_layer_y = g_layer_cy;
  289. g_layer_w = g_layer_cw;
  290. g_layer_h = g_layer_ch;
  291. break;
  292. }
  293. if (currentConfig.scaling != SCALE_CUSTOM) {
  294. // center the layer
  295. g_layer_x = 800 / 2 - g_layer_w / 2;
  296. g_layer_y = 480 / 2 - g_layer_h / 2;
  297. }
  298. switch (currentConfig.scaling) {
  299. case SCALE_FULLSCREEN:
  300. case SCALE_CUSTOM:
  301. fb_top = start_line;
  302. fb_h = line_count;
  303. break;
  304. }
  305. g_osd_fps_x = is_32cols ? 232 : 264;
  306. g_osd_y = fb_top + fb_h - 8;
  307. pnd_setup_layer(1, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
  308. vout_fbdev_clear(layer_fb);
  309. vout_fbdev_resize(layer_fb, fb_w, fb_h, 16, fb_left, fb_right, fb_top, fb_bottom, 4);
  310. plat_video_flip();
  311. PicoDrawSetOutFormat(PDF_RGB555, 0);
  312. }
  313. void plat_video_loop_prepare(void)
  314. {
  315. // make sure there is no junk left behind the layer
  316. memset(g_menuscreen_ptr, 0, g_menuscreen_w * g_menuscreen_h * 2);
  317. g_menuscreen_ptr = vout_fbdev_flip(main_fb);
  318. // emu_video_mode_change will call pnd_setup_layer()
  319. }
  320. void pemu_loop_prep(void)
  321. {
  322. // dirty buffers better go now than during gameplay
  323. fflush(stdout);
  324. fflush(stderr);
  325. sync();
  326. sleep(0);
  327. }
  328. void pemu_loop_end(void)
  329. {
  330. /* do one more frame for menu bg */
  331. pemu_forced_frame(0, 1);
  332. pnd_setup_layer(0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
  333. }
  334. void plat_wait_till_us(unsigned int us_to)
  335. {
  336. unsigned int now;
  337. signed int diff;
  338. now = plat_get_ticks_us();
  339. // XXX: need to check NOHZ
  340. diff = (signed int)(us_to - now);
  341. if (diff > 10000) {
  342. //printf("sleep %d\n", us_to - now);
  343. usleep(diff * 15 / 16);
  344. now = plat_get_ticks_us();
  345. //printf(" wake %d\n", (signed)(us_to - now));
  346. }
  347. /*
  348. while ((signed int)(us_to - now) > 512) {
  349. spend_cycles(1024);
  350. now = plat_get_ticks_us();
  351. }
  352. */
  353. }
  354. void plat_early_init(void)
  355. {
  356. }
  357. void plat_init(void)
  358. {
  359. const char *main_fb_name, *layer_fb_name;
  360. int fd, ret, w, h;
  361. main_fb_name = getenv("FBDEV_MAIN");
  362. if (main_fb_name == NULL)
  363. main_fb_name = "/dev/fb0";
  364. layer_fb_name = getenv("FBDEV_LAYER");
  365. if (layer_fb_name == NULL)
  366. layer_fb_name = "/dev/fb1";
  367. // must set the layer up first to be able to use it
  368. fd = open(layer_fb_name, O_RDWR);
  369. if (fd == -1) {
  370. fprintf(stderr, "%s: ", layer_fb_name);
  371. perror("open");
  372. exit(1);
  373. }
  374. ret = pnd_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
  375. close(fd);
  376. if (ret != 0) {
  377. fprintf(stderr, "failed to set up layer, exiting.\n");
  378. exit(1);
  379. }
  380. xenv_init(NULL, "PicoDrive " VERSION);
  381. w = h = 0;
  382. main_fb = vout_fbdev_init(main_fb_name, &w, &h, 16, 2);
  383. if (main_fb == NULL) {
  384. fprintf(stderr, "couldn't init fb: %s\n", main_fb_name);
  385. exit(1);
  386. }
  387. g_menuscreen_w = w;
  388. g_menuscreen_h = h;
  389. g_menuscreen_ptr = vout_fbdev_flip(main_fb);
  390. w = 320; h = 240;
  391. layer_fb = vout_fbdev_init(layer_fb_name, &w, &h, 16, 4);
  392. if (layer_fb == NULL) {
  393. fprintf(stderr, "couldn't init fb: %s\n", layer_fb_name);
  394. goto fail0;
  395. }
  396. if (w != g_screen_width || h != g_screen_height) {
  397. fprintf(stderr, "%dx%d not supported on %s\n", w, h, layer_fb_name);
  398. goto fail1;
  399. }
  400. g_screen_ptr = vout_fbdev_flip(layer_fb);
  401. temp_frame = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
  402. if (temp_frame == NULL) {
  403. fprintf(stderr, "OOM\n");
  404. goto fail1;
  405. }
  406. g_menubg_ptr = temp_frame;
  407. g_menubg_src_ptr = temp_frame;
  408. pnd_menu_init();
  409. in_evdev_init(in_evdev_defbinds);
  410. in_probe();
  411. plat_target_setup_input();
  412. sndout_oss_frag_frames = 2;
  413. return;
  414. fail1:
  415. vout_fbdev_finish(layer_fb);
  416. fail0:
  417. vout_fbdev_finish(main_fb);
  418. exit(1);
  419. }
  420. void plat_finish(void)
  421. {
  422. vout_fbdev_finish(main_fb);
  423. xenv_finish();
  424. printf("all done\n");
  425. }