emu.c 21 KB

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