emu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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 out_x, out_y;
  43. static int out_w, out_h;
  44. static float hscale, vscale;
  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|POPT_EN_SOFTSCALE);
  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 * vscale + 0.5);
  130. fbimg_width = (int)(out_w * hscale + 0.5);
  131. if (fbimg_width & 1) fbimg_width++; // make even
  132. if (fbimg_height & 1) fbimg_height++;
  133. if (fbimg_width >= 480) {
  134. g_vertices[0].u = out_x + (fbimg_width-480)/2;
  135. g_vertices[1].u = out_x + out_w - (fbimg_width-480)/2 - 1;
  136. fbimg_width = 480;
  137. fbimg_xoffs = 0;
  138. } else {
  139. g_vertices[0].u = out_x;
  140. g_vertices[1].u = out_x + out_w;
  141. fbimg_xoffs = 240 - fbimg_width/2;
  142. }
  143. if (fbimg_width > 320 && fbimg_width <= 480) border_hack = 1;
  144. if (fbimg_height >= 272) {
  145. g_vertices[0].v = out_y + (fbimg_height-272)/2;
  146. g_vertices[1].v = out_y + out_h - (fbimg_height-272)/2;
  147. fbimg_height = 272;
  148. fbimg_yoffs = 0;
  149. } else {
  150. g_vertices[0].v = out_y;
  151. g_vertices[1].v = out_y + out_h;
  152. fbimg_yoffs = 136 - fbimg_height/2;
  153. }
  154. if (fbimg_xoffs < 0) fbimg_xoffs = 0;
  155. if (fbimg_yoffs < 0) fbimg_yoffs = 0;
  156. g_vertices[0].x = fbimg_xoffs;
  157. g_vertices[0].y = fbimg_yoffs;
  158. g_vertices[1].x = fbimg_xoffs + fbimg_width;
  159. g_vertices[1].y = fbimg_yoffs + fbimg_height;
  160. if (!is_16bit_mode()) {
  161. // 8-bit modes have an 8 px overlap area on the left
  162. g_vertices[0].u += 8; g_vertices[1].u += 8;
  163. }
  164. if (border_hack) {
  165. g_vertices[0].u++; g_vertices[1].u--;
  166. g_vertices[0].x++; g_vertices[1].x--;
  167. }
  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_sms(void)
  176. {
  177. static u16 tmspal[32] = {
  178. // SMS palette
  179. 0x0000, 0x0000, 0x00a0, 0x00f0, 0x0500, 0x0f00, 0x0005, 0x0ff0,
  180. 0x000a, 0x000f, 0x0055, 0x00ff, 0x0050, 0x0f0f, 0x0555, 0x0fff,
  181. };
  182. int i;
  183. if (!(Pico.video.reg[0] & 0x4)) {
  184. for (i = Pico.est.SonicPalCount; i >= 0; i--)
  185. do_pal_convert(localPal+i*0x40, tmspal, currentConfig.gamma, currentConfig.gamma2);
  186. } else {
  187. for (i = Pico.est.SonicPalCount; i >= 0; i--)
  188. do_pal_convert(localPal+i*0x40, Pico.est.SonicPal+i*0x40, currentConfig.gamma, currentConfig.gamma2);
  189. }
  190. if (Pico.m.dirtyPal == 2)
  191. Pico.m.dirtyPal = 0;
  192. need_pal_upload = 1;
  193. }
  194. static void do_pal_update(void)
  195. {
  196. u32 *dpal=(void *)localPal;
  197. int i;
  198. if (PicoIn.opt & POPT_ALT_RENDERER) {
  199. do_pal_convert(localPal, PicoMem.cram, currentConfig.gamma, currentConfig.gamma2);
  200. Pico.m.dirtyPal = 0;
  201. }
  202. else if (Pico.est.rendstatus & PDRAW_SONIC_MODE)
  203. {
  204. switch (Pico.est.SonicPalCount) {
  205. case 3: do_pal_convert(localPal+0xc0, Pico.est.SonicPal+0xc0, currentConfig.gamma, currentConfig.gamma2);
  206. case 2: do_pal_convert(localPal+0x80, Pico.est.SonicPal+0x80, currentConfig.gamma, currentConfig.gamma2);
  207. case 1: do_pal_convert(localPal+0x40, Pico.est.SonicPal+0x40, currentConfig.gamma, currentConfig.gamma2);
  208. default:do_pal_convert(localPal, Pico.est.SonicPal, currentConfig.gamma, currentConfig.gamma2);
  209. }
  210. }
  211. else if (Pico.video.reg[0xC]&8) // shadow/hilight?
  212. {
  213. do_pal_convert(localPal, Pico.est.SonicPal, currentConfig.gamma, currentConfig.gamma2);
  214. // shadowed pixels
  215. for (i = 0; i < 0x40 / 2; i++) {
  216. dpal[0xc0/2 + i] = dpal[i];
  217. dpal[0x80/2 + i] = (dpal[i] >> 1) & 0x738e738e;
  218. }
  219. // hilighted pixels
  220. for (i = 0; i < 0x40 / 2; i++) {
  221. u32 t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e;
  222. t |= (t >> 4) & 0x08610861;
  223. dpal[0x40/2 + i] = t;
  224. }
  225. }
  226. else
  227. {
  228. do_pal_convert(localPal, Pico.est.SonicPal, currentConfig.gamma, currentConfig.gamma2);
  229. memcpy((int *)dpal+0x40/2, (void *)localPal, 0x40*2);
  230. memcpy((int *)dpal+0x80/2, (void *)localPal, 0x80*2);
  231. }
  232. localPal[0xe0] = 0;
  233. localPal[0xf0] = 0x001f;
  234. if (Pico.m.dirtyPal == 2)
  235. Pico.m.dirtyPal = 0;
  236. need_pal_upload = 1;
  237. }
  238. static void blitscreen_clut(void)
  239. {
  240. sceGuTexMode(is_16bit_mode() ? GU_PSM_5650:GU_PSM_T8,0,0,0);
  241. sceGuTexImage(0,512,512,512,g_screen_ptr);
  242. if (!is_16bit_mode() && Pico.m.dirtyPal) {
  243. if (PicoIn.AHW & PAHW_SMS)
  244. do_pal_update_sms();
  245. else
  246. do_pal_update();
  247. }
  248. if (need_pal_upload) {
  249. need_pal_upload = 0;
  250. sceGuClutLoad((256/8), localPal); // upload 32*8 entries (256)
  251. }
  252. #if 1
  253. if (g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x)
  254. {
  255. struct Vertex* vertices;
  256. int x;
  257. #define SLICE_WIDTH 32
  258. for (x = 0; x < g_vertices[1].x; x += SLICE_WIDTH)
  259. {
  260. // render sprite
  261. vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
  262. memcpy(vertices, g_vertices, 2 * sizeof(struct Vertex));
  263. vertices[0].u = vertices[0].x = x;
  264. vertices[1].u = vertices[1].x = x + SLICE_WIDTH;
  265. sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vertices);
  266. }
  267. // lprintf("listlen: %iB\n", sceGuCheckList()); // ~480 only
  268. }
  269. else
  270. #endif
  271. sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,g_vertices);
  272. }
  273. static void cd_leds(void)
  274. {
  275. struct Vertex* vx;
  276. unsigned int reg, col_g, col_r, *p;
  277. reg = Pico_mcd->s68k_regs[0];
  278. p = (unsigned int *)((short *)osd_buf + 512*2+498);
  279. col_g = (reg & 2) ? 0x06000600 : 0;
  280. col_r = (reg & 1) ? 0x00180018 : 0;
  281. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
  282. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
  283. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
  284. vx = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
  285. vx[0].u = 497, vx[0].v = 1;
  286. vx[1].u = 497+14, vx[1].v = 6;
  287. vx[0].x = 4, vx[0].y = 1;
  288. vx[1].x = 4+14, vx[1].y = 6;
  289. sceGuTexMode(GU_PSM_5650,0,0,0);
  290. sceGuTexImage(0,512,8,512,osd_buf);
  291. sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vx);
  292. }
  293. static void draw_pico_ptr(void)
  294. {
  295. unsigned char *p = (unsigned char *)g_screen_ptr + 8;
  296. // only if pen enabled and for 8bit mode
  297. if (pico_inp_mode == 0 || is_16bit_mode()) return;
  298. p += 512 * (pico_pen_y + PICO_PEN_ADJUST_Y);
  299. p += pico_pen_x + PICO_PEN_ADJUST_X;
  300. if (!(Pico.video.reg[12]&1) && !(PicoIn.opt & POPT_DIS_32C_BORDER))
  301. p += 32;
  302. p[ -1] = 0xe0; p[ 0] = 0xf0; p[ 1] = 0xe0;
  303. p[ 511] = 0xf0; p[ 512] = 0xf0; p[ 513] = 0xf0;
  304. p[1023] = 0xe0; p[1024] = 0xf0; p[1025] = 0xe0;
  305. }
  306. // clears whole screen or just the notice area (in all buffers)
  307. static void clearArea(int full)
  308. {
  309. void *fb = psp_video_get_active_fb();
  310. if (full) {
  311. u32 val = (is_16bit_mode() ? 0x00000000 : 0xe0e0e0e0);
  312. long sz = 512*272*2;
  313. memset32_uncached(psp_screen, 0, 512*272*2/4); // frame buffer
  314. memset32_uncached(fb, 0, 512*272*2/4); // frame buff on display
  315. memset32(VRAM_CACHED_STUFF, val, 2*sz/4); // 2 draw buffers
  316. } else {
  317. memset32_uncached((int *)((char *)psp_screen + 512*264*2), 0, 512*8*2/4);
  318. memset32_uncached((int *)((char *)fb + 512*264*2), 0, 512*8*2/4);
  319. }
  320. }
  321. static void vidResetMode(void)
  322. {
  323. // setup GU
  324. sceGuSync(0,0); // sync with prev
  325. sceGuStart(GU_DIRECT, guCmdList);
  326. sceGuClutMode(GU_PSM_5650,0,0xff,0);
  327. sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
  328. if (currentConfig.filter)
  329. sceGuTexFilter(GU_LINEAR, GU_LINEAR);
  330. else sceGuTexFilter(GU_NEAREST, GU_NEAREST);
  331. sceGuTexScale(1.0f,1.0f);
  332. sceGuTexOffset(0.0f,0.0f);
  333. Pico.m.dirtyPal = 1;
  334. sceGuFinish();
  335. set_scaling_params();
  336. sceGuSync(0,0);
  337. }
  338. /* sound stuff */
  339. #define SOUND_BLOCK_SIZE_NTSC (1470*2) // 1024 // 1152
  340. #define SOUND_BLOCK_SIZE_PAL (1764*2)
  341. #define SOUND_BLOCK_COUNT 8
  342. static short __attribute__((aligned(4))) sndBuffer[SOUND_BLOCK_SIZE_PAL*SOUND_BLOCK_COUNT + 44100/50*2];
  343. static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
  344. static int samples_made = 0, samples_done = 0, samples_block = 0;
  345. static int sound_thread_exit = 0;
  346. static SceUID sound_sem = -1;
  347. static void writeSound(int len);
  348. static int sound_thread(SceSize args, void *argp)
  349. {
  350. int ret = 0;
  351. lprintf("sthr: started, priority %i\n", sceKernelGetThreadCurrentPriority());
  352. while (!sound_thread_exit)
  353. {
  354. if (samples_made - samples_done < samples_block) {
  355. // wait for data (use at least 2 blocks)
  356. //lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
  357. while (samples_made - samples_done <= samples_block*2 && !sound_thread_exit)
  358. ret = sceKernelWaitSema(sound_sem, 1, 0);
  359. if (ret < 0) lprintf("sthr: sceKernelWaitSema: %i\n", ret);
  360. continue;
  361. }
  362. // lprintf("sthr: got data: %i\n", samples_made - samples_done);
  363. ret = sceAudioSRCOutputBlocking(PSP_AUDIO_VOLUME_MAX, snd_playptr);
  364. samples_done += samples_block;
  365. snd_playptr += samples_block;
  366. if (snd_playptr >= sndBuffer_endptr)
  367. snd_playptr = sndBuffer;
  368. // 1.5 kernel returns 0, newer ones return # of samples queued
  369. if (ret < 0)
  370. lprintf("sthr: sceAudioSRCOutputBlocking: %08x; pos %i/%i\n", ret, samples_done, samples_made);
  371. // shouln't happen, but just in case
  372. if (samples_made - samples_done >= samples_block*3) {
  373. //lprintf("sthr: block skip (%i)\n", samples_made - samples_done);
  374. samples_done += samples_block; // skip
  375. snd_playptr += samples_block;
  376. }
  377. }
  378. lprintf("sthr: exit\n");
  379. sceKernelExitDeleteThread(0);
  380. return 0;
  381. }
  382. static void sound_init(void)
  383. {
  384. SceUID thid;
  385. int ret;
  386. sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
  387. if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
  388. samples_made = samples_done = 0;
  389. samples_block = SOUND_BLOCK_SIZE_NTSC; // make sure it goes to sema
  390. sound_thread_exit = 0;
  391. thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
  392. if (thid >= 0)
  393. {
  394. ret = sceKernelStartThread(thid, 0, 0);
  395. if (ret < 0) lprintf("sound_init: sceKernelStartThread returned %08x\n", ret);
  396. }
  397. else
  398. lprintf("sceKernelCreateThread failed: %i\n", thid);
  399. }
  400. void pemu_sound_start(void)
  401. {
  402. static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
  403. static int mp3_init_done;
  404. int ret, stereo;
  405. samples_made = samples_done = 0;
  406. if (PicoIn.AHW & PAHW_MCD) {
  407. // mp3...
  408. if (!mp3_init_done) {
  409. ret = mp3_init();
  410. mp3_init_done = 1;
  411. if (ret) emu_status_msg("mp3 init failed (%i)", ret);
  412. }
  413. }
  414. ret = POPT_EN_FM|POPT_EN_PSG|POPT_EN_STEREO;
  415. if (PicoIn.sndRate != PsndRate_old || (PicoIn.opt&ret) != (PicoOpt_old&ret) || Pico.m.pal != pal_old) {
  416. PsndRerate(Pico.m.frame_count ? 1 : 0);
  417. }
  418. stereo=(PicoIn.opt&8)>>3;
  419. samples_block = Pico.m.pal ? SOUND_BLOCK_SIZE_PAL : SOUND_BLOCK_SIZE_NTSC;
  420. if (PicoIn.sndRate <= 22050) samples_block /= 2;
  421. sndBuffer_endptr = &sndBuffer[samples_block*SOUND_BLOCK_COUNT];
  422. lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
  423. PicoIn.sndRate, Pico.snd.len, stereo, Pico.m.pal, samples_block);
  424. // while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
  425. // sceAudioSRCChRelease();
  426. ret = sceAudioSRCChReserve(samples_block/2, PicoIn.sndRate, 2); // seems to not need that stupid 64byte alignment
  427. if (ret < 0) {
  428. lprintf("sceAudioSRCChReserve() failed: %i\n", ret);
  429. emu_status_msg("sound init failed (%i), snd disabled", ret);
  430. currentConfig.EmuOpt &= ~EOPT_EN_SOUND;
  431. } else {
  432. PicoIn.writeSound = writeSound;
  433. memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
  434. snd_playptr = sndBuffer_endptr - samples_block;
  435. samples_made = samples_block; // send 1 empty block first..
  436. PicoIn.sndOut = sndBuffer;
  437. PsndRate_old = PicoIn.sndRate;
  438. PicoOpt_old = PicoIn.opt;
  439. pal_old = Pico.m.pal;
  440. }
  441. }
  442. void pemu_sound_stop(void)
  443. {
  444. int i;
  445. if (samples_done == 0)
  446. {
  447. // if no data is written between sceAudioSRCChReserve and sceAudioSRCChRelease calls,
  448. // we get a deadlock on next sceAudioSRCChReserve call
  449. // so this is yet another workaround:
  450. memset32((int *)(void *)sndBuffer, 0, samples_block*4/4);
  451. samples_made = samples_block * 3;
  452. sceKernelSignalSema(sound_sem, 1);
  453. }
  454. sceKernelDelayThread(100*1000);
  455. samples_made = samples_done = 0;
  456. for (i = 0; sceAudioOutput2GetRestSample() > 0 && i < 16; i++)
  457. psp_msleep(100);
  458. sceAudioSRCChRelease();
  459. }
  460. /* wait until we can write more sound */
  461. void pemu_sound_wait(void)
  462. {
  463. // TODO: test this
  464. while (!sound_thread_exit && samples_made - samples_done > samples_block * 4)
  465. psp_msleep(10);
  466. }
  467. static void sound_deinit(void)
  468. {
  469. sound_thread_exit = 1;
  470. sceKernelSignalSema(sound_sem, 1);
  471. sceKernelDeleteSema(sound_sem);
  472. sound_sem = -1;
  473. }
  474. static void writeSound(int len)
  475. {
  476. int ret;
  477. PicoIn.sndOut += len / 2;
  478. /*if (PicoIn.sndOut > sndBuffer_endptr) {
  479. memcpy((int *)(void *)sndBuffer, (int *)endptr, (PicoIn.sndOut - endptr + 1) * 2);
  480. PicoIn.sndOut = &sndBuffer[PicoIn.sndOut - endptr];
  481. lprintf("mov\n");
  482. }
  483. else*/
  484. if (PicoIn.sndOut > sndBuffer_endptr) lprintf("snd oflow %i!\n", PicoIn.sndOut - sndBuffer_endptr);
  485. if (PicoIn.sndOut >= sndBuffer_endptr)
  486. PicoIn.sndOut = sndBuffer;
  487. // signal the snd thread
  488. samples_made += len / 2;
  489. if (samples_made - samples_done > samples_block*2) {
  490. // lprintf("signal, %i/%i\n", samples_done, samples_made);
  491. ret = sceKernelSignalSema(sound_sem, 1);
  492. //if (ret < 0) lprintf("snd signal ret %08x\n", ret);
  493. }
  494. }
  495. /* set default configuration values */
  496. void pemu_prep_defconfig(void)
  497. {
  498. defaultConfig.s_PsndRate = 22050;
  499. defaultConfig.s_PicoCDBuffers = 64;
  500. defaultConfig.CPUclock = 333;
  501. defaultConfig.filter = EOPT_FILTER_BILINEAR; // bilinear filtering
  502. defaultConfig.scaling = EOPT_SCALE_43;
  503. defaultConfig.vscaling = EOPT_VSCALE_FULL;
  504. defaultConfig.EmuOpt |= EOPT_SHOW_RTC;
  505. }
  506. /* check configuration for inconsistencies */
  507. void pemu_validate_config(void)
  508. {
  509. if (currentConfig.CPUclock < 33 || currentConfig.CPUclock > 333)
  510. currentConfig.CPUclock = 333;
  511. if (currentConfig.gamma < -4 || currentConfig.gamma > 16)
  512. currentConfig.gamma = 0;
  513. if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2)
  514. currentConfig.gamma2 = 0;
  515. }
  516. /* finalize rendering a frame */
  517. void pemu_finalize_frame(const char *fps, const char *notice)
  518. {
  519. int emu_opt = currentConfig.EmuOpt;
  520. int offs = (psp_screen == VRAM_FB0) ? VRAMOFFS_FB0 : VRAMOFFS_FB1;
  521. if (PicoIn.AHW & PAHW_PICO)
  522. draw_pico_ptr();
  523. sceGuSync(0,0); // sync with prev
  524. sceGuStart(GU_DIRECT, guCmdList);
  525. sceGuDrawBuffer(GU_PSM_5650, (void *)offs, 512); // point to back buffer
  526. blitscreen_clut();
  527. if (notice) osd_text(4, notice);
  528. if (emu_opt & 2) osd_text(OSD_FPS_X, fps);
  529. if ((emu_opt & 0x400) && (PicoIn.AHW & PAHW_MCD))
  530. cd_leds();
  531. sceKernelDcacheWritebackAll();
  532. sceGuFinish();
  533. }
  534. /* FIXME: move plat_* to plat? */
  535. void plat_debug_cat(char *str)
  536. {
  537. }
  538. /* platform dependend emulator initialization */
  539. void plat_init(void)
  540. {
  541. flip_after_sync = 1;
  542. psp_menu_init();
  543. in_psp_init(in_psp_defbinds);
  544. in_probe();
  545. sound_init();
  546. plat_get_data_dir(rom_fname_loaded, sizeof(rom_fname_loaded));
  547. }
  548. /* platform dependend emulator deinitialization */
  549. void plat_finish(void)
  550. {
  551. sound_deinit();
  552. }
  553. /* display emulator status messages before holding emulation */
  554. void plat_status_msg_busy_first(const char *msg)
  555. {
  556. plat_status_msg_busy_next(msg);
  557. }
  558. void plat_status_msg_busy_next(const char *msg)
  559. {
  560. plat_status_msg_clear();
  561. pemu_finalize_frame("", msg);
  562. plat_video_flip();
  563. emu_status_msg("");
  564. reset_timing = 1;
  565. }
  566. /* clear status message area */
  567. void plat_status_msg_clear(void)
  568. {
  569. clearArea(0);
  570. }
  571. /* change the audio volume setting */
  572. void plat_update_volume(int has_changed, int is_up)
  573. {
  574. }
  575. /* prepare for MD screen mode change */
  576. void emu_video_mode_change(int start_line, int line_count, int start_col, int col_count)
  577. {
  578. /* NTSC always has 224 visible lines, anything smaller has bars */
  579. if (line_count < 224 && line_count > 144) {
  580. start_line -= (224-line_count) /2;
  581. line_count = 224;
  582. }
  583. out_y = start_line; out_x = start_col;
  584. out_h = line_count; out_w = col_count;
  585. switch (currentConfig.vscaling) {
  586. case EOPT_VSCALE_PAL:
  587. vscale = (float)270/240;
  588. break;
  589. case EOPT_VSCALE_FULL:
  590. vscale = (float)270/line_count;
  591. break;
  592. default:
  593. vscale = 1;
  594. break;
  595. }
  596. switch (currentConfig.scaling) {
  597. case EOPT_SCALE_43:
  598. hscale = (float)360/col_count;
  599. break;
  600. case EOPT_SCALE_WIDE:
  601. hscale = (float)420/col_count;
  602. break;
  603. case EOPT_SCALE_FULL:
  604. hscale = (float)480/col_count;
  605. break;
  606. default:
  607. hscale = 1;
  608. break;
  609. }
  610. vidResetMode();
  611. if (col_count < 320) // clear borders from h40 remnants
  612. clearArea(1);
  613. }
  614. /* render one frame in RGB */
  615. void pemu_forced_frame(int no_scale, int do_emu)
  616. {
  617. Pico.m.dirtyPal = 1;
  618. if (!no_scale)
  619. no_scale = currentConfig.scaling == EOPT_SCALE_NONE;
  620. emu_cmn_forced_frame(no_scale, do_emu, g_screen_ptr);
  621. }
  622. /* change the platform output rendering */
  623. void plat_video_toggle_renderer(int change, int is_menu_call)
  624. {
  625. change_renderer(change);
  626. if (is_menu_call)
  627. return;
  628. apply_renderer();
  629. vidResetMode();
  630. rendstatus_old = -1;
  631. if (PicoIn.AHW & PAHW_32X)
  632. emu_status_msg(renderer_names32x[get_renderer()]);
  633. else
  634. emu_status_msg(renderer_names[get_renderer()]);
  635. }
  636. /* set the buffer for emulator output rendering */
  637. void plat_video_set_buffer(void *buf)
  638. {
  639. if (is_16bit_mode())
  640. PicoDrawSetOutBuf(g_screen_ptr, g_screen_ppitch * 2);
  641. else
  642. PicoDrawSetOutBuf(g_screen_ptr, g_screen_ppitch);
  643. }
  644. /* prepare for emulator output rendering */
  645. void plat_video_loop_prepare(void)
  646. {
  647. apply_renderer();
  648. vidResetMode();
  649. clearArea(1);
  650. }
  651. /* prepare for entering the emulator loop */
  652. void pemu_loop_prep(void)
  653. {
  654. }
  655. /* terminate the emulator loop */
  656. void pemu_loop_end(void)
  657. {
  658. pemu_sound_stop();
  659. }
  660. /* resume from suspend: change to main menu if emu was running */
  661. void emu_handle_resume(void)
  662. {
  663. if (engineState == PGS_Running)
  664. engineState = PGS_Menu;
  665. }