emu.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2007,2008
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <sys/syslimits.h> // PATH_MAX
  11. #include <pspthreadman.h>
  12. #include <pspdisplay.h>
  13. #include <psputils.h>
  14. #include <pspgu.h>
  15. #include <pspaudio.h>
  16. #include "psp.h"
  17. #include "emu.h"
  18. #include "mp3.h"
  19. #include "in_psp.h"
  20. #include "asm_utils.h"
  21. #include "../common/emu.h"
  22. #include "../common/input_pico.h"
  23. #include "platform/libpicofe/input.h"
  24. #include "platform/libpicofe/menu.h"
  25. #include <pico/pico_int.h>
  26. #include <pico/cd/genplus_macros.h>
  27. #include <pico/cd/cdd.h>
  28. #include <pico/cd/cue.h>
  29. #define OSD_FPS_X 432
  30. int engineStateSuspend;
  31. #define PICO_PEN_ADJUST_X 4
  32. #define PICO_PEN_ADJUST_Y 2
  33. struct Vertex
  34. {
  35. short u,v;
  36. short x,y,z;
  37. };
  38. static struct Vertex __attribute__((aligned(4))) g_vertices[2];
  39. static unsigned short __attribute__((aligned(16))) localPal[0x100];
  40. static int need_pal_upload = 0;
  41. static int fbimg_offs = 0;
  42. static int h32_mode = 0;
  43. static const struct in_default_bind in_psp_defbinds[] =
  44. {
  45. { PSP_CTRL_UP, IN_BINDTYPE_PLAYER12, GBTN_UP },
  46. { PSP_CTRL_DOWN, IN_BINDTYPE_PLAYER12, GBTN_DOWN },
  47. { PSP_CTRL_LEFT, IN_BINDTYPE_PLAYER12, GBTN_LEFT },
  48. { PSP_CTRL_RIGHT, IN_BINDTYPE_PLAYER12, GBTN_RIGHT },
  49. { PSP_CTRL_SQUARE, IN_BINDTYPE_PLAYER12, GBTN_A },
  50. { PSP_CTRL_CROSS, IN_BINDTYPE_PLAYER12, GBTN_B },
  51. { PSP_CTRL_CIRCLE, IN_BINDTYPE_PLAYER12, GBTN_C },
  52. { PSP_CTRL_START, IN_BINDTYPE_PLAYER12, GBTN_START },
  53. { PSP_CTRL_TRIANGLE, IN_BINDTYPE_EMU, PEVB_SWITCH_RND },
  54. { PSP_CTRL_LTRIGGER, IN_BINDTYPE_EMU, PEVB_STATE_SAVE },
  55. { PSP_CTRL_RTRIGGER, IN_BINDTYPE_EMU, PEVB_STATE_LOAD },
  56. { PSP_CTRL_SELECT, IN_BINDTYPE_EMU, PEVB_MENU },
  57. { 0, 0, 0 }
  58. };
  59. const char *renderer_names[] = { "16bit accurate", " 8bit accurate", " 8bit fast", NULL };
  60. const char *renderer_names32x[] = { "accurate", "faster", "fastest", NULL };
  61. enum renderer_types { RT_16BIT, RT_8BIT_ACC, RT_8BIT_FAST, RT_COUNT };
  62. #define is_16bit_mode() \
  63. (get_renderer() == RT_16BIT || (PicoIn.AHW & PAHW_32X))
  64. static int get_renderer(void)
  65. {
  66. if (PicoIn.AHW & PAHW_32X)
  67. return currentConfig.renderer32x;
  68. else
  69. return currentConfig.renderer;
  70. }
  71. static void change_renderer(int diff)
  72. {
  73. int *r;
  74. if (PicoIn.AHW & PAHW_32X)
  75. r = &currentConfig.renderer32x;
  76. else
  77. r = &currentConfig.renderer;
  78. *r += diff;
  79. if (*r >= RT_COUNT)
  80. *r = 0;
  81. else if (*r < 0)
  82. *r = RT_COUNT - 1;
  83. }
  84. static void apply_renderer(void)
  85. {
  86. PicoIn.opt &= ~POPT_ALT_RENDERER;
  87. PicoIn.opt |= POPT_DIS_32C_BORDER;
  88. switch (currentConfig.renderer) {
  89. case RT_16BIT:
  90. PicoDrawSetOutFormat(PDF_RGB555, 0);
  91. break;
  92. case RT_8BIT_ACC:
  93. PicoDrawSetOutFormat(PDF_8BIT, 0);
  94. break;
  95. case RT_8BIT_FAST:
  96. PicoIn.opt |= POPT_ALT_RENDERER;
  97. PicoDrawSetOutFormat(PDF_NONE, 0);
  98. break;
  99. }
  100. }
  101. static void osd_text(int x, const char *text, int is_active, int clear_all)
  102. {
  103. unsigned short *screen = is_active ? psp_video_get_active_fb() : psp_screen;
  104. int len = clear_all ? (480 / 2) : (strlen(text) * 8 / 2);
  105. int *p, h;
  106. void *tmp = g_screen_ptr;
  107. for (h = 0; h < 8; h++) {
  108. p = (int *) (screen+x+512*(264+h));
  109. p = (int *) ((int)p & ~3); // align
  110. memset32_uncached(p, 0, len);
  111. }
  112. g_screen_ptr = screen; // nasty pointer tricks
  113. emu_text_out16(x, 264, text);
  114. g_screen_ptr = tmp;
  115. }
  116. static void set_scaling_params(void)
  117. {
  118. int src_width, fbimg_width, fbimg_height, fbimg_xoffs, fbimg_yoffs, border_hack = 0;
  119. g_vertices[0].x = g_vertices[0].y =
  120. g_vertices[0].z = g_vertices[1].z = 0;
  121. fbimg_height = (int)(240.0 * currentConfig.scale + 0.5);
  122. if (!h32_mode) {
  123. fbimg_width = (int)(320.0 * currentConfig.scale * currentConfig.hscale40 + 0.5);
  124. src_width = 320;
  125. } else {
  126. fbimg_width = (int)(256.0 * currentConfig.scale * currentConfig.hscale32 + 0.5);
  127. src_width = 256;
  128. }
  129. if (fbimg_width & 1) fbimg_width++; // make even
  130. if (fbimg_height & 1) fbimg_height++;
  131. if (fbimg_width >= 480) {
  132. g_vertices[0].u = (fbimg_width-480)/2;
  133. g_vertices[1].u = src_width - (fbimg_width-480)/2 - 1;
  134. fbimg_width = 480;
  135. fbimg_xoffs = 0;
  136. } else {
  137. g_vertices[0].u = 0;
  138. g_vertices[1].u = src_width;
  139. fbimg_xoffs = 240 - fbimg_width/2;
  140. }
  141. if (fbimg_width > 320 && fbimg_width <= 480) border_hack = 1;
  142. if (fbimg_height >= 272) {
  143. g_vertices[0].v = (fbimg_height-272)/2;
  144. g_vertices[1].v = 240 - (fbimg_height-272)/2;
  145. fbimg_height = 272;
  146. fbimg_yoffs = 0;
  147. } else {
  148. g_vertices[0].v = 0;
  149. g_vertices[1].v = 240;
  150. fbimg_yoffs = 136 - fbimg_height/2;
  151. }
  152. g_vertices[1].x = fbimg_width;
  153. g_vertices[1].y = fbimg_height;
  154. if (fbimg_xoffs < 0) fbimg_xoffs = 0;
  155. if (fbimg_yoffs < 0) fbimg_yoffs = 0;
  156. if (border_hack) {
  157. g_vertices[0].u++;
  158. g_vertices[0].x++;
  159. g_vertices[1].u--;
  160. g_vertices[1].x--;
  161. }
  162. if (!is_16bit_mode()) {
  163. // 8-bit modes have an 8 px overlap area on the left
  164. g_vertices[0].u += 8;
  165. g_vertices[1].u += 8;
  166. }
  167. fbimg_offs = (fbimg_yoffs*512 + fbimg_xoffs) * 2; // dst is always 16bit
  168. /*
  169. lprintf("set_scaling_params:\n");
  170. lprintf("offs: %i, %i\n", fbimg_xoffs, fbimg_yoffs);
  171. lprintf("xy0, xy1: %i, %i; %i, %i\n", g_vertices[0].x, g_vertices[0].y, g_vertices[1].x, g_vertices[1].y);
  172. lprintf("uv0, uv1: %i, %i; %i, %i\n", g_vertices[0].u, g_vertices[0].v, g_vertices[1].u, g_vertices[1].v);
  173. */
  174. }
  175. static void do_pal_update(void)
  176. {
  177. unsigned int *dpal=(void *)localPal;
  178. int i;
  179. //for (i = 0x3f/2; i >= 0; i--)
  180. // dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
  181. if (PicoIn.opt & POPT_ALT_RENDERER) {
  182. do_pal_convert(localPal, PicoMem.cram, currentConfig.gamma, currentConfig.gamma2);
  183. Pico.m.dirtyPal = 0;
  184. }
  185. else if (Pico.est.rendstatus & PDRAW_SONIC_MODE)
  186. {
  187. switch (Pico.est.SonicPalCount) {
  188. case 3: do_pal_convert(localPal+0xc0/2, Pico.est.SonicPal+0xc0, currentConfig.gamma, currentConfig.gamma2);
  189. case 2: do_pal_convert(localPal+0x80/2, Pico.est.SonicPal+0x80, currentConfig.gamma, currentConfig.gamma2);
  190. case 1: do_pal_convert(localPal+0x40/2, Pico.est.SonicPal+0x40, currentConfig.gamma, currentConfig.gamma2);
  191. default:do_pal_convert(localPal, Pico.est.SonicPal, currentConfig.gamma, currentConfig.gamma2);
  192. }
  193. }
  194. else if (Pico.video.reg[0xC]&8) // shadow/hilight?
  195. {
  196. do_pal_convert(localPal, Pico.est.SonicPal, currentConfig.gamma, currentConfig.gamma2);
  197. // shadowed pixels
  198. for (i = 0; i < 0x40 / 2; i++) {
  199. dpal[0xc0/2 + i] = dpal[i];
  200. dpal[0x80/2 + i] = (dpal[i] >> 1) & 0x738e738e;
  201. }
  202. // hilighted pixels
  203. for (i = 0; i < 0x40 / 2; i++) {
  204. u32 t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e;
  205. t |= (t >> 4) & 0x08610861;
  206. dpal[0x40/2 + i] = t;
  207. }
  208. }
  209. else
  210. {
  211. do_pal_convert(localPal, Pico.est.SonicPal, currentConfig.gamma, currentConfig.gamma2);
  212. memcpy((int *)dpal+0x40/2, (void *)localPal, 0x40*2);
  213. memcpy((int *)dpal+0x80/2, (void *)localPal, 0x80*2);
  214. }
  215. localPal[0xe0] = 0;
  216. localPal[0xf0] = 0x001f;
  217. if (Pico.m.dirtyPal == 2)
  218. Pico.m.dirtyPal = 0;
  219. need_pal_upload = 1;
  220. }
  221. static void blitscreen_clut(void)
  222. {
  223. int offs = fbimg_offs;
  224. offs += (psp_screen == VRAM_FB0) ? VRAMOFFS_FB0 : VRAMOFFS_FB1;
  225. sceGuSync(0,0); // sync with prev
  226. sceGuStart(GU_DIRECT, guCmdList);
  227. sceGuDrawBuffer(GU_PSM_5650, (void *)offs, 512); // point to back buffer
  228. sceGuTexMode(is_16bit_mode() ? GU_PSM_5650:GU_PSM_T8,0,0,0);
  229. sceGuTexImage(0,512,512,512,g_screen_ptr);
  230. if (Pico.m.dirtyPal)
  231. do_pal_update();
  232. sceKernelDcacheWritebackAll();
  233. if (need_pal_upload) {
  234. need_pal_upload = 0;
  235. sceGuClutLoad((256/8), localPal); // upload 32*8 entries (256)
  236. }
  237. #if 1
  238. if (g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x)
  239. {
  240. struct Vertex* vertices;
  241. int x;
  242. #define SLICE_WIDTH 32
  243. for (x = 0; x < g_vertices[1].x; x += SLICE_WIDTH)
  244. {
  245. // render sprite
  246. vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
  247. memcpy(vertices, g_vertices, 2 * sizeof(struct Vertex));
  248. vertices[0].u = vertices[0].x = x;
  249. vertices[1].u = vertices[1].x = x + SLICE_WIDTH;
  250. sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vertices);
  251. }
  252. // lprintf("listlen: %iB\n", sceGuCheckList()); // ~480 only
  253. }
  254. else
  255. #endif
  256. sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,g_vertices);
  257. sceGuFinish();
  258. }
  259. static void cd_leds(void)
  260. {
  261. unsigned int reg, col_g, col_r, *p;
  262. reg = Pico_mcd->s68k_regs[0];
  263. p = (unsigned int *)((short *)psp_screen + 512*2+4+2);
  264. col_g = (reg & 2) ? 0x06000600 : 0;
  265. col_r = (reg & 1) ? 0x00180018 : 0;
  266. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
  267. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
  268. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
  269. }
  270. static void draw_pico_ptr(void)
  271. {
  272. unsigned char *p = (unsigned char *)g_screen_ptr + 8;
  273. // only if pen enabled and for 8bit mode
  274. if (pico_inp_mode == 0 || is_16bit_mode()) return;
  275. p += 512 * (pico_pen_y + PICO_PEN_ADJUST_Y);
  276. p += pico_pen_x + PICO_PEN_ADJUST_X;
  277. p[ -1] = 0xe0; p[ 0] = 0xf0; p[ 1] = 0xe0;
  278. p[ 511] = 0xf0; p[ 512] = 0xf0; p[ 513] = 0xf0;
  279. p[1023] = 0xe0; p[1024] = 0xf0; p[1025] = 0xe0;
  280. }
  281. // clears whole screen or just the notice area (in all buffers)
  282. static void clearArea(int full)
  283. {
  284. void *fb = psp_video_get_active_fb();
  285. if (full) {
  286. memset32_uncached(psp_screen, 0, 512*272*2/4);
  287. memset32_uncached(fb, 0, 512*272*2/4);
  288. memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*240/4);
  289. memset32((int *)VRAM_CACHED_STUFF+512*240/4, 0, 512*240*2/4);
  290. } else {
  291. memset32_uncached((int *)((char *)psp_screen + 512*264*2), 0, 512*8*2/4);
  292. memset32_uncached((int *)((char *)fb + 512*264*2), 0, 512*8*2/4);
  293. }
  294. }
  295. static void vidResetMode(void)
  296. {
  297. // setup GU
  298. sceGuSync(0,0); // sync with prev
  299. sceGuStart(GU_DIRECT, guCmdList);
  300. sceGuClutMode(GU_PSM_5650,0,0xff,0);
  301. sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
  302. if (currentConfig.scaling)
  303. sceGuTexFilter(GU_LINEAR, GU_LINEAR);
  304. else sceGuTexFilter(GU_NEAREST, GU_NEAREST);
  305. sceGuTexScale(1.0f,1.0f);
  306. sceGuTexOffset(0.0f,0.0f);
  307. Pico.m.dirtyPal = 1;
  308. sceGuFinish();
  309. set_scaling_params();
  310. sceGuSync(0,0);
  311. }
  312. /* sound stuff */
  313. #define SOUND_BLOCK_SIZE_NTSC (1470*2) // 1024 // 1152
  314. #define SOUND_BLOCK_SIZE_PAL (1764*2)
  315. #define SOUND_BLOCK_COUNT 8
  316. static short __attribute__((aligned(4))) sndBuffer[SOUND_BLOCK_SIZE_PAL*SOUND_BLOCK_COUNT + 44100/50*2];
  317. static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
  318. static int samples_made = 0, samples_done = 0, samples_block = 0;
  319. static int sound_thread_exit = 0;
  320. static SceUID sound_sem = -1;
  321. static void writeSound(int len);
  322. static int sound_thread(SceSize args, void *argp)
  323. {
  324. int ret = 0;
  325. lprintf("sthr: started, priority %i\n", sceKernelGetThreadCurrentPriority());
  326. while (!sound_thread_exit)
  327. {
  328. if (samples_made - samples_done < samples_block) {
  329. // wait for data (use at least 2 blocks)
  330. //lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
  331. while (samples_made - samples_done <= samples_block*2 && !sound_thread_exit)
  332. ret = sceKernelWaitSema(sound_sem, 1, 0);
  333. if (ret < 0) lprintf("sthr: sceKernelWaitSema: %i\n", ret);
  334. continue;
  335. }
  336. // lprintf("sthr: got data: %i\n", samples_made - samples_done);
  337. ret = sceAudioSRCOutputBlocking(PSP_AUDIO_VOLUME_MAX, snd_playptr);
  338. samples_done += samples_block;
  339. snd_playptr += samples_block;
  340. if (snd_playptr >= sndBuffer_endptr)
  341. snd_playptr = sndBuffer;
  342. // 1.5 kernel returns 0, newer ones return # of samples queued
  343. if (ret < 0)
  344. lprintf("sthr: sceAudioSRCOutputBlocking: %08x; pos %i/%i\n", ret, samples_done, samples_made);
  345. // shouln't happen, but just in case
  346. if (samples_made - samples_done >= samples_block*3) {
  347. //lprintf("sthr: block skip (%i)\n", samples_made - samples_done);
  348. samples_done += samples_block; // skip
  349. snd_playptr += samples_block;
  350. }
  351. }
  352. lprintf("sthr: exit\n");
  353. sceKernelExitDeleteThread(0);
  354. return 0;
  355. }
  356. static void sound_init(void)
  357. {
  358. SceUID thid;
  359. int ret;
  360. sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
  361. if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
  362. samples_made = samples_done = 0;
  363. samples_block = SOUND_BLOCK_SIZE_NTSC; // make sure it goes to sema
  364. sound_thread_exit = 0;
  365. thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
  366. if (thid >= 0)
  367. {
  368. ret = sceKernelStartThread(thid, 0, 0);
  369. if (ret < 0) lprintf("sound_init: sceKernelStartThread returned %08x\n", ret);
  370. }
  371. else
  372. lprintf("sceKernelCreateThread failed: %i\n", thid);
  373. }
  374. void pemu_sound_start(void)
  375. {
  376. static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
  377. static int mp3_init_done;
  378. int ret, stereo;
  379. samples_made = samples_done = 0;
  380. if (PicoIn.AHW & PAHW_MCD) {
  381. // mp3...
  382. if (!mp3_init_done) {
  383. ret = mp3_init();
  384. mp3_init_done = 1;
  385. if (ret) emu_status_msg("mp3 init failed (%i)", ret);
  386. }
  387. }
  388. if (PicoIn.sndRate != PsndRate_old || (PicoIn.opt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
  389. PsndRerate(Pico.m.frame_count ? 1 : 0);
  390. }
  391. stereo=(PicoIn.opt&8)>>3;
  392. samples_block = Pico.m.pal ? SOUND_BLOCK_SIZE_PAL : SOUND_BLOCK_SIZE_NTSC;
  393. if (PicoIn.sndRate <= 22050) samples_block /= 2;
  394. sndBuffer_endptr = &sndBuffer[samples_block*SOUND_BLOCK_COUNT];
  395. lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
  396. PicoIn.sndRate, Pico.snd.len, stereo, Pico.m.pal, samples_block);
  397. // while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
  398. // sceAudioSRCChRelease();
  399. ret = sceAudioSRCChReserve(samples_block/2, PicoIn.sndRate, 2); // seems to not need that stupid 64byte alignment
  400. if (ret < 0) {
  401. lprintf("sceAudioSRCChReserve() failed: %i\n", ret);
  402. emu_status_msg("sound init failed (%i), snd disabled", ret);
  403. currentConfig.EmuOpt &= ~EOPT_EN_SOUND;
  404. } else {
  405. PicoIn.writeSound = writeSound;
  406. memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
  407. snd_playptr = sndBuffer_endptr - samples_block;
  408. samples_made = samples_block; // send 1 empty block first..
  409. PicoIn.sndOut = sndBuffer;
  410. PsndRate_old = PicoIn.sndRate;
  411. PicoOpt_old = PicoIn.opt;
  412. pal_old = Pico.m.pal;
  413. }
  414. }
  415. void pemu_sound_stop(void)
  416. {
  417. int i;
  418. if (samples_done == 0)
  419. {
  420. // if no data is written between sceAudioSRCChReserve and sceAudioSRCChRelease calls,
  421. // we get a deadlock on next sceAudioSRCChReserve call
  422. // so this is yet another workaround:
  423. memset32((int *)(void *)sndBuffer, 0, samples_block*4/4);
  424. samples_made = samples_block * 3;
  425. sceKernelSignalSema(sound_sem, 1);
  426. }
  427. sceKernelDelayThread(100*1000);
  428. samples_made = samples_done = 0;
  429. for (i = 0; sceAudioOutput2GetRestSample() > 0 && i < 16; i++)
  430. psp_msleep(100);
  431. sceAudioSRCChRelease();
  432. }
  433. /* wait until we can write more sound */
  434. void pemu_sound_wait(void)
  435. {
  436. // TODO: test this
  437. while (!sound_thread_exit && samples_made - samples_done > samples_block * 4)
  438. psp_msleep(10);
  439. }
  440. static void sound_deinit(void)
  441. {
  442. sound_thread_exit = 1;
  443. sceKernelSignalSema(sound_sem, 1);
  444. sceKernelDeleteSema(sound_sem);
  445. sound_sem = -1;
  446. }
  447. static void writeSound(int len)
  448. {
  449. int ret;
  450. PicoIn.sndOut += len / 2;
  451. /*if (PicoIn.sndOut > sndBuffer_endptr) {
  452. memcpy((int *)(void *)sndBuffer, (int *)endptr, (PicoIn.sndOut - endptr + 1) * 2);
  453. PicoIn.sndOut = &sndBuffer[PicoIn.sndOut - endptr];
  454. lprintf("mov\n");
  455. }
  456. else*/
  457. if (PicoIn.sndOut > sndBuffer_endptr) lprintf("snd oflow %i!\n", PicoIn.sndOut - sndBuffer_endptr);
  458. if (PicoIn.sndOut >= sndBuffer_endptr)
  459. PicoIn.sndOut = sndBuffer;
  460. // signal the snd thread
  461. samples_made += len / 2;
  462. if (samples_made - samples_done > samples_block*2) {
  463. // lprintf("signal, %i/%i\n", samples_done, samples_made);
  464. ret = sceKernelSignalSema(sound_sem, 1);
  465. //if (ret < 0) lprintf("snd signal ret %08x\n", ret);
  466. }
  467. }
  468. /* set default configuration values */
  469. void pemu_prep_defconfig(void)
  470. {
  471. defaultConfig.s_PsndRate = 22050;
  472. defaultConfig.s_PicoCDBuffers = 64;
  473. defaultConfig.CPUclock = 333;
  474. defaultConfig.scaling = 1; // bilinear filtering for psp
  475. defaultConfig.scale = 1.20; // fullscreen
  476. defaultConfig.hscale40 = 1.25;
  477. defaultConfig.hscale32 = 1.56;
  478. }
  479. /* check configuration for inconsistencies */
  480. void pemu_validate_config(void)
  481. {
  482. if (currentConfig.CPUclock < 33 || currentConfig.CPUclock > 333)
  483. currentConfig.CPUclock = 333;
  484. }
  485. /* finalize rendering a frame */
  486. void pemu_finalize_frame(const char *fps, const char *notice)
  487. {
  488. int emu_opt = currentConfig.EmuOpt;
  489. if (PicoIn.opt&POPT_ALT_RENDERER)
  490. {
  491. int i;
  492. unsigned char *pd;
  493. // clear top and bottom trash
  494. for (pd = Pico.est.Draw2FB, i = 8; i > 0; i--, pd += 512)
  495. memset32((int *)pd, 0xe0e0e0e0, 320/4);
  496. for (pd = Pico.est.Draw2FB+512*232, i = 8; i > 0; i--, pd += 512)
  497. memset32((int *)pd, 0xe0e0e0e0, 320/4);
  498. }
  499. if (PicoIn.AHW & PAHW_PICO)
  500. draw_pico_ptr();
  501. blitscreen_clut();
  502. // XXX move this to flip to give texture renderer more time?
  503. if (notice) osd_text(4, notice, 0, 0);
  504. if (emu_opt & 2) osd_text(OSD_FPS_X, fps, 0, 0);
  505. if ((emu_opt & 0x400) && (PicoIn.AHW & PAHW_MCD))
  506. cd_leds();
  507. }
  508. /* FIXME: move plat_* to plat? */
  509. void plat_debug_cat(char *str)
  510. {
  511. }
  512. /* platform dependend emulator initialization */
  513. void plat_init(void)
  514. {
  515. flip_after_sync = 1;
  516. in_psp_init(in_psp_defbinds);
  517. in_probe();
  518. sound_init();
  519. }
  520. /* platform dependend emulator deinitialization */
  521. void plat_finish(void)
  522. {
  523. sound_deinit();
  524. }
  525. /* display emulator status messages before holding emulation */
  526. void plat_status_msg_busy_first(const char *msg)
  527. {
  528. plat_status_msg_busy_next(msg);
  529. }
  530. void plat_status_msg_busy_next(const char *msg)
  531. {
  532. plat_status_msg_clear();
  533. pemu_finalize_frame("", msg);
  534. plat_video_flip();
  535. emu_status_msg("");
  536. reset_timing = 1;
  537. }
  538. /* clear status message area */
  539. void plat_status_msg_clear(void)
  540. {
  541. clearArea(0);
  542. }
  543. /* change the audio volume setting */
  544. void plat_update_volume(int has_changed, int is_up)
  545. {
  546. }
  547. /* prepare for MD screen mode change */
  548. void emu_video_mode_change(int start_line, int line_count, int is_32cols)
  549. {
  550. h32_mode = is_32cols;
  551. vidResetMode();
  552. if (h32_mode) // clear borders from h40 remnants
  553. clearArea(1);
  554. }
  555. /* render one frame in RGB */
  556. void pemu_forced_frame(int no_scale, int do_emu)
  557. {
  558. PicoIn.opt &= ~POPT_DIS_32C_BORDER;
  559. Pico.m.dirtyPal = 1;
  560. if (!no_scale)
  561. no_scale = currentConfig.scaling == EOPT_SCALE_NONE;
  562. emu_cmn_forced_frame(no_scale, do_emu);
  563. }
  564. /* change the platform output rendering */
  565. void plat_video_toggle_renderer(int change, int is_menu_call)
  566. {
  567. change_renderer(change);
  568. if (is_menu_call)
  569. return;
  570. apply_renderer();
  571. vidResetMode();
  572. rendstatus_old = -1;
  573. if (PicoIn.AHW & PAHW_32X)
  574. emu_status_msg(renderer_names32x[get_renderer()]);
  575. else
  576. emu_status_msg(renderer_names[get_renderer()]);
  577. }
  578. /* set the buffer for emulator output rendering */
  579. void plat_video_set_buffer(void *buf)
  580. {
  581. if (currentConfig.renderer == RT_16BIT || (PicoIn.AHW & PAHW_32X))
  582. PicoDrawSetOutBuf(g_screen_ptr, g_screen_ppitch * 2);
  583. else
  584. PicoDrawSetOutBuf(g_screen_ptr, g_screen_ppitch);
  585. }
  586. /* prepare for emulator output rendering */
  587. void plat_video_loop_prepare(void)
  588. {
  589. apply_renderer();
  590. vidResetMode();
  591. clearArea(1);
  592. }
  593. /* prepare for entering the emulator loop */
  594. void pemu_loop_prep(void)
  595. {
  596. }
  597. /* terminate the emulator loop */
  598. void pemu_loop_end(void)
  599. {
  600. pemu_sound_stop();
  601. }
  602. // TODO
  603. void emu_handle_resume(void)
  604. {
  605. if (!(PicoIn.AHW & PAHW_MCD)) return;
  606. // reopen first CD track
  607. if (cdd.toc.tracks[0].fd != NULL)
  608. {
  609. char *fname = rom_fname_reload;
  610. int len = strlen(rom_fname_reload);
  611. cue_data_t *cue_data = NULL;
  612. if (len > 4 && strcasecmp(fname + len - 4, ".cue") == 0)
  613. {
  614. cue_data = cue_parse(rom_fname_reload);
  615. if (cue_data != NULL)
  616. fname = cue_data->tracks[1].fname;
  617. }
  618. lprintf("emu_HandleResume: reopen %s\n", fname);
  619. pm_close(cdd.toc.tracks[0].fd);
  620. cdd.toc.tracks[0].fd = pm_open(fname);
  621. lprintf("reopen %s\n", cdd.toc.tracks[0].fd != NULL ? "ok" : "failed");
  622. if (cue_data != NULL) cue_destroy(cue_data);
  623. }
  624. mp3_reopen_file();
  625. #if 0 // TODO
  626. if (!(Pico_mcd->s68k_regs[0x36] & 1)/* && (Pico_mcd->scd.Status_CDC & 1)*/)
  627. cdd_change_track(cdd.index, cdd.lba);
  628. #endif
  629. }