emu.c 20 KB

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