emu.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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 "menu.h"
  18. #include "emu.h"
  19. #include "mp3.h"
  20. #include "asm_utils.h"
  21. #include "../common/emu.h"
  22. #include "../common/config.h"
  23. #include "../common/lprintf.h"
  24. #include <pico/pico_int.h>
  25. #include <pico/cd/cue.h>
  26. #define OSD_FPS_X 432
  27. // additional pspaudio imports, credits to crazyc
  28. int sceAudio_38553111(unsigned short samples, unsigned short freq, char unknown); // play with conversion?
  29. int sceAudio_5C37C0AE(void); // end play?
  30. int sceAudio_E0727056(int volume, void *buffer); // blocking output
  31. int sceAudioOutput2GetRestSample();
  32. //unsigned char *Draw2FB = (unsigned char *)VRAM_CACHED_STUFF + 8; // +8 to be able to skip border with 1 quadword..
  33. int engineStateSuspend;
  34. #define PICO_PEN_ADJUST_X 4
  35. #define PICO_PEN_ADJUST_Y 2
  36. static int pico_pen_x = 320/2, pico_pen_y = 240/2;
  37. static void sound_init(void);
  38. static void sound_deinit(void);
  39. static void blit2(const char *fps, const char *notice, int lagging_behind);
  40. static void clearArea(int full);
  41. int plat_get_root_dir(char *dst, int len)
  42. {
  43. if (len > 0) *dst = 0;
  44. return 0;
  45. }
  46. static void osd_text(int x, const char *text, int is_active, int clear_all)
  47. {
  48. unsigned short *screen = is_active ? psp_video_get_active_fb() : psp_screen;
  49. int len = clear_all ? (480 / 2) : (strlen(text) * 8 / 2);
  50. int *p, h;
  51. void *tmp;
  52. for (h = 0; h < 8; h++) {
  53. p = (int *) (screen+x+512*(264+h));
  54. p = (int *) ((int)p & ~3); // align
  55. memset32_uncached(p, 0, len);
  56. }
  57. if (is_active) { tmp = psp_screen; psp_screen = screen; } // nasty pointer tricks
  58. emu_text_out16(x, 264, text);
  59. if (is_active) psp_screen = tmp;
  60. }
  61. void emu_msg_cb(const char *msg)
  62. {
  63. osd_text(4, msg, 1, 1);
  64. noticeMsgTime = sceKernelGetSystemTimeLow() - 2000000;
  65. /* assumption: emu_msg_cb gets called only when something slow is about to happen */
  66. reset_timing = 1;
  67. }
  68. /* FIXME: move to plat */
  69. void emu_Init(void)
  70. {
  71. sound_init();
  72. }
  73. void emu_Deinit(void)
  74. {
  75. sound_deinit();
  76. }
  77. void pemu_prep_defconfig(void)
  78. {
  79. defaultConfig.s_PsndRate = 22050;
  80. defaultConfig.s_PicoCDBuffers = 64;
  81. defaultConfig.CPUclock = 333;
  82. defaultConfig.KeyBinds[ 4] = 1<<0; // SACB RLDU
  83. defaultConfig.KeyBinds[ 6] = 1<<1;
  84. defaultConfig.KeyBinds[ 7] = 1<<2;
  85. defaultConfig.KeyBinds[ 5] = 1<<3;
  86. defaultConfig.KeyBinds[14] = 1<<4;
  87. defaultConfig.KeyBinds[13] = 1<<5;
  88. defaultConfig.KeyBinds[15] = 1<<6;
  89. defaultConfig.KeyBinds[ 3] = 1<<7;
  90. defaultConfig.KeyBinds[12] = 1<<26; // switch rnd
  91. defaultConfig.KeyBinds[ 8] = 1<<27; // save state
  92. defaultConfig.KeyBinds[ 9] = 1<<28; // load state
  93. defaultConfig.KeyBinds[28] = 1<<0; // num "buttons"
  94. defaultConfig.KeyBinds[30] = 1<<1;
  95. defaultConfig.KeyBinds[31] = 1<<2;
  96. defaultConfig.KeyBinds[29] = 1<<3;
  97. defaultConfig.scaling = 1; // bilinear filtering for psp
  98. defaultConfig.scale = 1.20; // fullscreen
  99. defaultConfig.hscale40 = 1.25;
  100. defaultConfig.hscale32 = 1.56;
  101. }
  102. extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
  103. extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
  104. static void (*amips_clut_f)(unsigned short *dst, unsigned char *src, unsigned short *pal, int count) = NULL;
  105. struct Vertex
  106. {
  107. short u,v;
  108. short x,y,z;
  109. };
  110. static struct Vertex __attribute__((aligned(4))) g_vertices[2];
  111. static unsigned short __attribute__((aligned(16))) localPal[0x100];
  112. static int dynamic_palette = 0, need_pal_upload = 0, blit_16bit_mode = 0;
  113. static int fbimg_offs = 0;
  114. static void set_scaling_params(void)
  115. {
  116. int src_width, fbimg_width, fbimg_height, fbimg_xoffs, fbimg_yoffs, border_hack = 0;
  117. g_vertices[0].x = g_vertices[0].y =
  118. g_vertices[0].z = g_vertices[1].z = 0;
  119. fbimg_height = (int)(240.0 * currentConfig.scale + 0.5);
  120. if (Pico.video.reg[12] & 1) {
  121. fbimg_width = (int)(320.0 * currentConfig.scale * currentConfig.hscale40 + 0.5);
  122. src_width = 320;
  123. } else {
  124. fbimg_width = (int)(256.0 * currentConfig.scale * currentConfig.hscale32 + 0.5);
  125. src_width = 256;
  126. }
  127. if (fbimg_width & 1) fbimg_width++; // make even
  128. if (fbimg_height & 1) fbimg_height++;
  129. if (fbimg_width >= 480) {
  130. g_vertices[0].u = (fbimg_width-480)/2;
  131. g_vertices[1].u = src_width - (fbimg_width-480)/2 - 1;
  132. fbimg_width = 480;
  133. fbimg_xoffs = 0;
  134. } else {
  135. g_vertices[0].u = 0;
  136. g_vertices[1].u = src_width;
  137. fbimg_xoffs = 240 - fbimg_width/2;
  138. }
  139. if (fbimg_width > 320 && fbimg_width <= 480) border_hack = 1;
  140. if (fbimg_height >= 272) {
  141. g_vertices[0].v = (fbimg_height-272)/2;
  142. g_vertices[1].v = 240 - (fbimg_height-272)/2;
  143. fbimg_height = 272;
  144. fbimg_yoffs = 0;
  145. } else {
  146. g_vertices[0].v = 0;
  147. g_vertices[1].v = 240;
  148. fbimg_yoffs = 136 - fbimg_height/2;
  149. }
  150. g_vertices[1].x = fbimg_width;
  151. g_vertices[1].y = fbimg_height;
  152. if (fbimg_xoffs < 0) fbimg_xoffs = 0;
  153. if (fbimg_yoffs < 0) fbimg_yoffs = 0;
  154. if (border_hack) {
  155. g_vertices[0].u++;
  156. g_vertices[0].x++;
  157. g_vertices[1].u--;
  158. g_vertices[1].x--;
  159. }
  160. fbimg_offs = (fbimg_yoffs*512 + fbimg_xoffs) * 2; // dst is always 16bit
  161. /*
  162. lprintf("set_scaling_params:\n");
  163. lprintf("offs: %i, %i\n", fbimg_xoffs, fbimg_yoffs);
  164. lprintf("xy0, xy1: %i, %i; %i, %i\n", g_vertices[0].x, g_vertices[0].y, g_vertices[1].x, g_vertices[1].y);
  165. lprintf("uv0, uv1: %i, %i; %i, %i\n", g_vertices[0].u, g_vertices[0].v, g_vertices[1].u, g_vertices[1].v);
  166. */
  167. }
  168. static void do_pal_update(int allow_sh, int allow_as)
  169. {
  170. unsigned int *dpal=(void *)localPal;
  171. int i;
  172. //for (i = 0x3f/2; i >= 0; i--)
  173. // dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
  174. do_pal_convert(localPal, Pico.cram, currentConfig.gamma, currentConfig.gamma2);
  175. Pico.m.dirtyPal = 0;
  176. need_pal_upload = 1;
  177. if (allow_sh && (Pico.video.reg[0xC]&8)) // shadow/hilight?
  178. {
  179. // shadowed pixels
  180. for (i = 0x3f/2; i >= 0; i--)
  181. dpal[0x20|i] = dpal[0x60|i] = (dpal[i]>>1)&0x7bcf7bcf;
  182. // hilighted pixels
  183. for (i = 0x3f; i >= 0; i--) {
  184. int t=localPal[i]&0xf79e;t+=0x4208;
  185. if (t&0x20) t|=0x1e;
  186. if (t&0x800) t|=0x780;
  187. if (t&0x10000) t|=0xf000;
  188. t&=0xf79e;
  189. localPal[0x80|i]=(unsigned short)t;
  190. }
  191. localPal[0xe0] = 0;
  192. localPal[0xf0] = 0x001f;
  193. }
  194. else if (allow_as && (Pico.est.rendstatus & PDRAW_SPR_LO_ON_HI))
  195. {
  196. memcpy(dpal + 0x80/2, localPal, 0x40*2);
  197. }
  198. }
  199. static void do_slowmode_lines(int line_to)
  200. {
  201. int line = 0, line_len = (Pico.video.reg[12]&1) ? 320 : 256;
  202. unsigned short *dst = (unsigned short *)VRAM_STUFF + 512*240/2;
  203. unsigned char *src = (unsigned char *)VRAM_CACHED_STUFF + 16;
  204. if (!(Pico.video.reg[1]&8)) { line = 8; dst += 512*8; src += 512*8; }
  205. for (; line < line_to; line++, dst+=512, src+=512)
  206. amips_clut_f(dst, src, localPal, line_len);
  207. }
  208. static void EmuScanPrepare(void)
  209. {
  210. Pico.est.HighCol = (unsigned char *)VRAM_CACHED_STUFF + 8;
  211. if (!(Pico.video.reg[1]&8)) Pico.est.HighCol += 8*512;
  212. if (dynamic_palette > 0)
  213. dynamic_palette--;
  214. if (Pico.m.dirtyPal)
  215. do_pal_update(1, 1);
  216. if ((Pico.est.rendstatus & PDRAW_SPR_LO_ON_HI) && !(Pico.video.reg[0xC]&8))
  217. amips_clut_f = amips_clut_6bit;
  218. else amips_clut_f = amips_clut;
  219. }
  220. static int EmuScanSlowBegin(unsigned int num)
  221. {
  222. if (!dynamic_palette)
  223. Pico.est.HighCol = (unsigned char *)VRAM_CACHED_STUFF + num * 512 + 8;
  224. return 0;
  225. }
  226. static int EmuScanSlowEnd(unsigned int num)
  227. {
  228. if (Pico.m.dirtyPal) {
  229. if (!dynamic_palette) {
  230. do_slowmode_lines(num);
  231. dynamic_palette = 3; // last for 2 more frames
  232. }
  233. do_pal_update(1, 1);
  234. }
  235. if (dynamic_palette) {
  236. int line_len = (Pico.video.reg[12]&1) ? 320 : 256;
  237. void *dst = (char *)VRAM_STUFF + 512*240 + 512*2*num;
  238. amips_clut_f(dst, Pico.est.HighCol + 8, localPal, line_len);
  239. }
  240. return 0;
  241. }
  242. static void blitscreen_clut(void)
  243. {
  244. int offs = fbimg_offs;
  245. offs += (psp_screen == VRAM_FB0) ? VRAMOFFS_FB0 : VRAMOFFS_FB1;
  246. sceGuSync(0,0); // sync with prev
  247. sceGuStart(GU_DIRECT, guCmdList);
  248. sceGuDrawBuffer(GU_PSM_5650, (void *)offs, 512); // point to back buffer
  249. if (dynamic_palette)
  250. {
  251. if (!blit_16bit_mode) { // the current mode is not 16bit
  252. sceGuTexMode(GU_PSM_5650, 0, 0, 0);
  253. sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 512*240);
  254. blit_16bit_mode = 1;
  255. }
  256. }
  257. else
  258. {
  259. if (blit_16bit_mode) {
  260. sceGuClutMode(GU_PSM_5650,0,0xff,0);
  261. sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
  262. sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
  263. blit_16bit_mode = 0;
  264. }
  265. if ((PicoOpt&0x10) && Pico.m.dirtyPal)
  266. do_pal_update(0, 0);
  267. sceKernelDcacheWritebackAll();
  268. if (need_pal_upload) {
  269. need_pal_upload = 0;
  270. sceGuClutLoad((256/8), localPal); // upload 32*8 entries (256)
  271. }
  272. }
  273. #if 1
  274. if (g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x)
  275. {
  276. struct Vertex* vertices;
  277. int x;
  278. #define SLICE_WIDTH 32
  279. for (x = 0; x < g_vertices[1].x; x += SLICE_WIDTH)
  280. {
  281. // render sprite
  282. vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
  283. memcpy(vertices, g_vertices, 2 * sizeof(struct Vertex));
  284. vertices[0].u = vertices[0].x = x;
  285. vertices[1].u = vertices[1].x = x + SLICE_WIDTH;
  286. sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vertices);
  287. }
  288. // lprintf("listlen: %iB\n", sceGuCheckList()); // ~480 only
  289. }
  290. else
  291. #endif
  292. sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,g_vertices);
  293. sceGuFinish();
  294. }
  295. static void cd_leds(void)
  296. {
  297. unsigned int reg, col_g, col_r, *p;
  298. reg = Pico_mcd->s68k_regs[0];
  299. p = (unsigned int *)((short *)psp_screen + 512*2+4+2);
  300. col_g = (reg & 2) ? 0x06000600 : 0;
  301. col_r = (reg & 1) ? 0x00180018 : 0;
  302. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
  303. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
  304. *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
  305. }
  306. static void draw_pico_ptr(void)
  307. {
  308. unsigned char *p = (unsigned char *)VRAM_STUFF + 16;
  309. // only if pen enabled and for 8bit mode
  310. if (pico_inp_mode == 0 || blit_16bit_mode) return;
  311. p += 512 * (pico_pen_y + PICO_PEN_ADJUST_Y);
  312. p += pico_pen_x + PICO_PEN_ADJUST_X;
  313. p[ -1] = 0xe0; p[ 0] = 0xf0; p[ 1] = 0xe0;
  314. p[ 511] = 0xf0; p[ 512] = 0xf0; p[ 513] = 0xf0;
  315. p[1023] = 0xe0; p[1024] = 0xf0; p[1025] = 0xe0;
  316. }
  317. #if 0
  318. static void dbg_text(void)
  319. {
  320. int *p, h, len;
  321. char text[128];
  322. sprintf(text, "sl: %i, 16b: %i", g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x, blit_16bit_mode);
  323. len = strlen(text) * 8 / 2;
  324. for (h = 0; h < 8; h++) {
  325. p = (int *) ((unsigned short *) psp_screen+2+512*(256+h));
  326. p = (int *) ((int)p & ~3); // align
  327. memset32_uncached(p, 0, len);
  328. }
  329. emu_text_out16(2, 256, text);
  330. }
  331. #endif
  332. /* called after rendering is done, but frame emulation is not finished */
  333. void blit1(void)
  334. {
  335. if (PicoOpt&0x10)
  336. {
  337. int i;
  338. unsigned char *pd;
  339. // clear top and bottom trash
  340. for (pd = Pico.est.Draw2FB+8, i = 8; i > 0; i--, pd += 512)
  341. memset32((int *)pd, 0xe0e0e0e0, 320/4);
  342. for (pd = Pico.est.Draw2FB+512*232+8, i = 8; i > 0; i--, pd += 512)
  343. memset32((int *)pd, 0xe0e0e0e0, 320/4);
  344. }
  345. if (PicoAHW & PAHW_PICO)
  346. draw_pico_ptr();
  347. blitscreen_clut();
  348. }
  349. static void blit2(const char *fps, const char *notice, int lagging_behind)
  350. {
  351. int vsync = 0, emu_opt = currentConfig.EmuOpt;
  352. if (notice || (emu_opt & 2)) {
  353. if (notice) osd_text(4, notice, 0, 0);
  354. if (emu_opt & 2) osd_text(OSD_FPS_X, fps, 0, 0);
  355. }
  356. //dbg_text();
  357. if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
  358. cd_leds();
  359. if (currentConfig.EmuOpt & 0x2000) { // want vsync
  360. if (!(currentConfig.EmuOpt & 0x10000) || !lagging_behind) vsync = 1;
  361. }
  362. psp_video_flip(vsync);
  363. }
  364. // clears whole screen or just the notice area (in all buffers)
  365. static void clearArea(int full)
  366. {
  367. if (full) {
  368. memset32_uncached(psp_screen, 0, 512*272*2/4);
  369. psp_video_flip(0);
  370. memset32_uncached(psp_screen, 0, 512*272*2/4);
  371. memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*240/4);
  372. memset32((int *)VRAM_CACHED_STUFF+512*240/4, 0, 512*240*2/4);
  373. } else {
  374. void *fb = psp_video_get_active_fb();
  375. memset32_uncached((int *)((char *)psp_screen + 512*264*2), 0, 512*8*2/4);
  376. memset32_uncached((int *)((char *)fb + 512*264*2), 0, 512*8*2/4);
  377. }
  378. }
  379. static void vidResetMode(void)
  380. {
  381. // setup GU
  382. sceGuSync(0,0); // sync with prev
  383. sceGuStart(GU_DIRECT, guCmdList);
  384. sceGuClutMode(GU_PSM_5650,0,0xff,0);
  385. sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
  386. sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
  387. if (currentConfig.scaling)
  388. sceGuTexFilter(GU_LINEAR, GU_LINEAR);
  389. else sceGuTexFilter(GU_NEAREST, GU_NEAREST);
  390. sceGuTexScale(1.0f,1.0f);
  391. sceGuTexOffset(0.0f,0.0f);
  392. sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
  393. // slow rend.
  394. PicoDrawSetOutFormat(PDF_NONE, 0);
  395. PicoDrawSetCallbacks(EmuScanSlowBegin, EmuScanSlowEnd);
  396. localPal[0xe0] = 0;
  397. localPal[0xf0] = 0x001f;
  398. Pico.m.dirtyPal = 1;
  399. blit_16bit_mode = dynamic_palette = 0;
  400. sceGuFinish();
  401. set_scaling_params();
  402. sceGuSync(0,0);
  403. }
  404. void plat_debug_cat(char *str)
  405. {
  406. strcat(str, blit_16bit_mode ? "soft clut\n" : "hard clut\n");
  407. }
  408. /* sound stuff */
  409. #define SOUND_BLOCK_SIZE_NTSC (1470*2) // 1024 // 1152
  410. #define SOUND_BLOCK_SIZE_PAL (1764*2)
  411. #define SOUND_BLOCK_COUNT 8
  412. static short __attribute__((aligned(4))) sndBuffer[SOUND_BLOCK_SIZE_PAL*SOUND_BLOCK_COUNT + 44100/50*2];
  413. static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
  414. static int samples_made = 0, samples_done = 0, samples_block = 0;
  415. static int sound_thread_exit = 0;
  416. static SceUID sound_sem = -1;
  417. static void writeSound(int len);
  418. static int sound_thread(SceSize args, void *argp)
  419. {
  420. int ret = 0;
  421. lprintf("sthr: started, priority %i\n", sceKernelGetThreadCurrentPriority());
  422. while (!sound_thread_exit)
  423. {
  424. if (samples_made - samples_done < samples_block) {
  425. // wait for data (use at least 2 blocks)
  426. //lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
  427. while (samples_made - samples_done <= samples_block*2 && !sound_thread_exit)
  428. ret = sceKernelWaitSema(sound_sem, 1, 0);
  429. if (ret < 0) lprintf("sthr: sceKernelWaitSema: %i\n", ret);
  430. continue;
  431. }
  432. // lprintf("sthr: got data: %i\n", samples_made - samples_done);
  433. ret = sceAudio_E0727056(PSP_AUDIO_VOLUME_MAX, snd_playptr);
  434. samples_done += samples_block;
  435. snd_playptr += samples_block;
  436. if (snd_playptr >= sndBuffer_endptr)
  437. snd_playptr = sndBuffer;
  438. // 1.5 kernel returns 0, newer ones return # of samples queued
  439. if (ret < 0)
  440. lprintf("sthr: sceAudio_E0727056: %08x; pos %i/%i\n", ret, samples_done, samples_made);
  441. // shouln't happen, but just in case
  442. if (samples_made - samples_done >= samples_block*3) {
  443. //lprintf("sthr: block skip (%i)\n", samples_made - samples_done);
  444. samples_done += samples_block; // skip
  445. snd_playptr += samples_block;
  446. }
  447. }
  448. lprintf("sthr: exit\n");
  449. sceKernelExitDeleteThread(0);
  450. return 0;
  451. }
  452. static void sound_init(void)
  453. {
  454. SceUID thid;
  455. int ret;
  456. sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
  457. if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
  458. samples_made = samples_done = 0;
  459. samples_block = SOUND_BLOCK_SIZE_NTSC; // make sure it goes to sema
  460. sound_thread_exit = 0;
  461. thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
  462. if (thid >= 0)
  463. {
  464. ret = sceKernelStartThread(thid, 0, 0);
  465. if (ret < 0) lprintf("sound_init: sceKernelStartThread returned %08x\n", ret);
  466. }
  467. else
  468. lprintf("sceKernelCreateThread failed: %i\n", thid);
  469. }
  470. void pemu_sound_start(void)
  471. {
  472. static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
  473. int ret, stereo;
  474. samples_made = samples_done = 0;
  475. if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
  476. PsndRerate(Pico.m.frame_count ? 1 : 0);
  477. }
  478. stereo=(PicoOpt&8)>>3;
  479. samples_block = Pico.m.pal ? SOUND_BLOCK_SIZE_PAL : SOUND_BLOCK_SIZE_NTSC;
  480. if (PsndRate <= 22050) samples_block /= 2;
  481. sndBuffer_endptr = &sndBuffer[samples_block*SOUND_BLOCK_COUNT];
  482. lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
  483. PsndRate, PsndLen, stereo, Pico.m.pal, samples_block);
  484. // while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
  485. // sceAudio_5C37C0AE();
  486. ret = sceAudio_38553111(samples_block/2, PsndRate, 2); // seems to not need that stupid 64byte alignment
  487. if (ret < 0) {
  488. lprintf("sceAudio_38553111() failed: %i\n", ret);
  489. emu_status_msg("sound init failed (%i), snd disabled", ret);
  490. currentConfig.EmuOpt &= ~EOPT_EN_SOUND;
  491. } else {
  492. PicoWriteSound = writeSound;
  493. memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
  494. snd_playptr = sndBuffer_endptr - samples_block;
  495. samples_made = samples_block; // send 1 empty block first..
  496. PsndOut = sndBuffer;
  497. PsndRate_old = PsndRate;
  498. PicoOpt_old = PicoOpt;
  499. pal_old = Pico.m.pal;
  500. }
  501. }
  502. void pemu_sound_stop(void)
  503. {
  504. int i;
  505. if (samples_done == 0)
  506. {
  507. // if no data is written between sceAudio_38553111 and sceAudio_5C37C0AE calls,
  508. // we get a deadlock on next sceAudio_38553111 call
  509. // so this is yet another workaround:
  510. memset32((int *)(void *)sndBuffer, 0, samples_block*4/4);
  511. samples_made = samples_block * 3;
  512. sceKernelSignalSema(sound_sem, 1);
  513. }
  514. sceKernelDelayThread(100*1000);
  515. samples_made = samples_done = 0;
  516. for (i = 0; sceAudioOutput2GetRestSample() > 0 && i < 16; i++)
  517. psp_msleep(100);
  518. sceAudio_5C37C0AE();
  519. }
  520. /* wait until we can write more sound */
  521. void pemu_sound_wait(void)
  522. {
  523. // TODO: test this
  524. while (!sound_thread_exit && samples_made - samples_done > samples_block * 4)
  525. psp_msleep(10);
  526. }
  527. static void sound_deinit(void)
  528. {
  529. sound_thread_exit = 1;
  530. sceKernelSignalSema(sound_sem, 1);
  531. sceKernelDeleteSema(sound_sem);
  532. sound_sem = -1;
  533. }
  534. static void writeSound(int len)
  535. {
  536. int ret;
  537. PsndOut += len / 2;
  538. /*if (PsndOut > sndBuffer_endptr) {
  539. memcpy32((int *)(void *)sndBuffer, (int *)endptr, (PsndOut - endptr + 1) / 2);
  540. PsndOut = &sndBuffer[PsndOut - endptr];
  541. lprintf("mov\n");
  542. }
  543. else*/
  544. if (PsndOut > sndBuffer_endptr) lprintf("snd oflow %i!\n", PsndOut - sndBuffer_endptr);
  545. if (PsndOut >= sndBuffer_endptr)
  546. PsndOut = sndBuffer;
  547. // signal the snd thread
  548. samples_made += len / 2;
  549. if (samples_made - samples_done > samples_block*2) {
  550. // lprintf("signal, %i/%i\n", samples_done, samples_made);
  551. ret = sceKernelSignalSema(sound_sem, 1);
  552. //if (ret < 0) lprintf("snd signal ret %08x\n", ret);
  553. }
  554. }
  555. static void SkipFrame(void)
  556. {
  557. PicoSkipFrame=1;
  558. PicoFrame();
  559. PicoSkipFrame=0;
  560. }
  561. void pemu_forced_frame(int no_scale, int do_emu)
  562. {
  563. int po_old = PicoOpt;
  564. int eo_old = currentConfig.EmuOpt;
  565. PicoOpt &= ~POPT_ALT_RENDERER;
  566. PicoOpt |= POPT_ACC_SPRITES;
  567. if (!no_scale)
  568. PicoOpt |= POPT_EN_SOFTSCALE;
  569. currentConfig.EmuOpt |= 0x80;
  570. vidResetMode();
  571. memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*8/4); // borders
  572. memset32((int *)VRAM_CACHED_STUFF + 512*232/4, 0xe0e0e0e0, 512*8/4);
  573. memset32_uncached((int *)psp_screen + 512*264*2/4, 0, 512*8*2/4);
  574. PicoDrawSetOutFormat(PDF_NONE, 0);
  575. PicoDrawSetCallbacks(EmuScanSlowBegin, EmuScanSlowEnd);
  576. EmuScanPrepare();
  577. PicoFrameDrawOnly();
  578. blit1();
  579. sceGuSync(0,0);
  580. PicoOpt = po_old;
  581. currentConfig.EmuOpt = eo_old;
  582. }
  583. static void RunEventsPico(unsigned int events, unsigned int keys)
  584. {
  585. emu_RunEventsPico(events);
  586. if (pico_inp_mode != 0)
  587. {
  588. PicoPad[0] &= ~0x0f; // release UDLR
  589. if (keys & PBTN_UP) { pico_pen_y--; if (pico_pen_y < 8) pico_pen_y = 8; }
  590. if (keys & PBTN_DOWN) { pico_pen_y++; if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y; }
  591. if (keys & PBTN_LEFT) { pico_pen_x--; if (pico_pen_x < 0) pico_pen_x = 0; }
  592. if (keys & PBTN_RIGHT) {
  593. int lim = (Pico.video.reg[12]&1) ? 319 : 255;
  594. pico_pen_x++;
  595. if (pico_pen_x > lim-PICO_PEN_ADJUST_X)
  596. pico_pen_x = lim-PICO_PEN_ADJUST_X;
  597. }
  598. PicoPicohw.pen_pos[0] = pico_pen_x;
  599. if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;
  600. PicoPicohw.pen_pos[0] += 0x3c;
  601. PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);
  602. }
  603. }
  604. static void RunEvents(unsigned int which)
  605. {
  606. if (which & 0x1800) // save or load (but not both)
  607. {
  608. int do_it = 1;
  609. if ( emu_check_save_file(state_slot) &&
  610. (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
  611. (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
  612. {
  613. int keys;
  614. sceGuSync(0,0);
  615. blit2("", (which & 0x1000) ? "LOAD STATE? (X=yes, O=no)" : "OVERWRITE SAVE? (X=yes, O=no)", 0);
  616. while( !((keys = psp_pad_read(1)) & (PBTN_X|PBTN_CIRCLE)) )
  617. psp_msleep(50);
  618. if (keys & PBTN_CIRCLE) do_it = 0;
  619. while( ((keys = psp_pad_read(1)) & (PBTN_X|PBTN_CIRCLE)) ) // wait for release
  620. psp_msleep(50);
  621. clearArea(0);
  622. }
  623. if (do_it)
  624. {
  625. osd_text(4, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME", 1, 0);
  626. PicoStateProgressCB = emu_msg_cb;
  627. emu_save_load_game((which & 0x1000) >> 12, 0);
  628. PicoStateProgressCB = NULL;
  629. psp_msleep(0);
  630. }
  631. reset_timing = 1;
  632. }
  633. if (which & 0x0400) // switch renderer
  634. {
  635. if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
  636. else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
  637. vidResetMode();
  638. if (PicoOpt & POPT_ALT_RENDERER)
  639. emu_status_msg("fast renderer");
  640. else if (currentConfig.EmuOpt&0x80)
  641. emu_status_msg("accurate renderer");
  642. }
  643. if (which & 0x0300)
  644. {
  645. if(which&0x0200) {
  646. state_slot -= 1;
  647. if(state_slot < 0) state_slot = 9;
  648. } else {
  649. state_slot += 1;
  650. if(state_slot > 9) state_slot = 0;
  651. }
  652. emu_status_msg("SAVE SLOT %i [%s]", state_slot,
  653. emu_check_save_file(state_slot) ? "USED" : "FREE");
  654. }
  655. }
  656. static void updateKeys(void)
  657. {
  658. unsigned int keys, allActions[2] = { 0, 0 }, events;
  659. static unsigned int prevEvents = 0;
  660. int i;
  661. /* FIXME: port to input fw, merge with emu.c:emu_update_input() */
  662. keys = psp_pad_read(0);
  663. if (keys & PSP_CTRL_HOME)
  664. sceDisplayWaitVblankStart();
  665. if (keys & PBTN_SELECT)
  666. engineState = PGS_Menu;
  667. keys &= CONFIGURABLE_KEYS;
  668. PicoPad[0] = allActions[0] & 0xfff;
  669. PicoPad[1] = allActions[1] & 0xfff;
  670. if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
  671. if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
  672. events = (allActions[0] | allActions[1]) >> 16;
  673. if ((events ^ prevEvents) & 0x40) {
  674. emu_set_fastforward(events & 0x40);
  675. reset_timing = 1;
  676. }
  677. events &= ~prevEvents;
  678. if (PicoAHW == PAHW_PICO)
  679. RunEventsPico(events, keys);
  680. if (events) RunEvents(events);
  681. if (movie_data) emu_updateMovie();
  682. prevEvents = (allActions[0] | allActions[1]) >> 16;
  683. }
  684. static void simpleWait(unsigned int until)
  685. {
  686. unsigned int tval;
  687. int diff;
  688. tval = sceKernelGetSystemTimeLow();
  689. diff = (int)until - (int)tval;
  690. if (diff >= 512 && diff < 100*1024)
  691. sceKernelDelayThread(diff);
  692. }
  693. void pemu_loop(void)
  694. {
  695. static int mp3_init_done = 0;
  696. char fpsbuff[24]; // fps count c string
  697. unsigned int tval, tval_thissec = 0; // timing
  698. int target_fps, target_frametime, lim_time, tval_diff, i, oldmodes = 0;
  699. int pframes_done, pframes_shown; // "period" frames, used for sync
  700. int frames_done, frames_shown, tval_fpsc = 0; // actual frames
  701. char *notice = NULL;
  702. lprintf("entered emu_Loop()\n");
  703. fpsbuff[0] = 0;
  704. if (currentConfig.CPUclock != psp_get_cpu_clock()) {
  705. lprintf("setting cpu clock to %iMHz... ", currentConfig.CPUclock);
  706. i = psp_set_cpu_clock(currentConfig.CPUclock);
  707. lprintf(i ? "failed\n" : "done\n");
  708. currentConfig.CPUclock = psp_get_cpu_clock();
  709. }
  710. // make sure we are in correct mode
  711. vidResetMode();
  712. clearArea(1);
  713. Pico.m.dirtyPal = 1;
  714. oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
  715. // pal/ntsc might have changed, reset related stuff
  716. target_fps = Pico.m.pal ? 50 : 60;
  717. target_frametime = Pico.m.pal ? (1000000<<8)/50 : (1000000<<8)/60+1;
  718. reset_timing = 1;
  719. if (PicoAHW & PAHW_MCD) {
  720. // prepare CD buffer
  721. PicoCDBufferInit();
  722. // mp3...
  723. if (!mp3_init_done) {
  724. i = mp3_init();
  725. mp3_init_done = 1;
  726. if (i) { engineState = PGS_Menu; return; }
  727. }
  728. }
  729. // prepare sound stuff
  730. PsndOut = NULL;
  731. if (currentConfig.EmuOpt & EOPT_EN_SOUND)
  732. {
  733. pemu_sound_start();
  734. }
  735. sceDisplayWaitVblankStart();
  736. pframes_shown = pframes_done =
  737. frames_shown = frames_done = 0;
  738. tval_fpsc = sceKernelGetSystemTimeLow();
  739. // loop?
  740. while (engineState == PGS_Running)
  741. {
  742. int modes;
  743. tval = sceKernelGetSystemTimeLow();
  744. if (reset_timing || tval < tval_fpsc) {
  745. //stdbg("timing reset");
  746. reset_timing = 0;
  747. tval_thissec = tval;
  748. pframes_shown = pframes_done = 0;
  749. }
  750. // show notice message?
  751. if (noticeMsgTime) {
  752. static int noticeMsgSum;
  753. if (tval - noticeMsgTime > 2000000) { // > 2.0 sec
  754. noticeMsgTime = 0;
  755. clearArea(0);
  756. notice = 0;
  757. } else {
  758. int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
  759. if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
  760. notice = noticeMsg;
  761. }
  762. }
  763. // check for mode changes
  764. modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
  765. if (modes != oldmodes) {
  766. oldmodes = modes;
  767. clearArea(1);
  768. set_scaling_params();
  769. }
  770. // second passed?
  771. if (tval - tval_fpsc >= 1000000)
  772. {
  773. if (currentConfig.EmuOpt & 2)
  774. sprintf(fpsbuff, "%02i/%02i ", frames_shown, frames_done);
  775. frames_done = frames_shown = 0;
  776. tval_fpsc += 1000000;
  777. }
  778. if (tval - tval_thissec >= 1000000)
  779. {
  780. // missing 1 frame?
  781. if (currentConfig.Frameskip < 0 && pframes_done < target_fps) {
  782. SkipFrame(); pframes_done++; frames_done++;
  783. }
  784. tval_thissec += 1000000;
  785. if (currentConfig.Frameskip < 0) {
  786. pframes_done -= target_fps; if (pframes_done < 0) pframes_done = 0;
  787. pframes_shown -= target_fps; if (pframes_shown < 0) pframes_shown = 0;
  788. if (pframes_shown > pframes_done) pframes_shown = pframes_done;
  789. } else {
  790. pframes_done = pframes_shown = 0;
  791. }
  792. }
  793. #ifdef PFRAMES
  794. sprintf(fpsbuff, "%i", Pico.m.frame_count);
  795. #endif
  796. lim_time = (pframes_done+1) * target_frametime;
  797. if (currentConfig.Frameskip >= 0) // frameskip enabled
  798. {
  799. for (i = 0; i < currentConfig.Frameskip; i++) {
  800. updateKeys();
  801. SkipFrame(); pframes_done++; frames_done++;
  802. if (!(currentConfig.EmuOpt&0x40000)) { // do framelimitting if needed
  803. int tval_diff;
  804. tval = sceKernelGetSystemTimeLow();
  805. tval_diff = (int)(tval - tval_thissec) << 8;
  806. if (tval_diff < lim_time) // we are too fast
  807. simpleWait(tval + ((lim_time - tval_diff)>>8));
  808. }
  809. lim_time += target_frametime;
  810. }
  811. }
  812. else // auto frameskip
  813. {
  814. int tval_diff;
  815. tval = sceKernelGetSystemTimeLow();
  816. tval_diff = (int)(tval - tval_thissec) << 8;
  817. if (tval_diff > lim_time && (pframes_done/16 < pframes_shown))
  818. {
  819. // no time left for this frame - skip
  820. if (tval_diff - lim_time >= (300000<<8)) {
  821. reset_timing = 1;
  822. continue;
  823. }
  824. updateKeys();
  825. SkipFrame(); pframes_done++; frames_done++;
  826. continue;
  827. }
  828. }
  829. updateKeys();
  830. if (!(PicoOpt&0x10))
  831. EmuScanPrepare();
  832. PicoFrame();
  833. sceGuSync(0,0);
  834. // check time
  835. tval = sceKernelGetSystemTimeLow();
  836. tval_diff = (int)(tval - tval_thissec) << 8;
  837. blit2(fpsbuff, notice, tval_diff > lim_time);
  838. if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300000<<8)) { // slowdown detection
  839. reset_timing = 1;
  840. }
  841. else if (!(currentConfig.EmuOpt&0x40000) || currentConfig.Frameskip < 0)
  842. {
  843. // sleep if we are still too fast
  844. if (tval_diff < lim_time)
  845. {
  846. // we are too fast
  847. simpleWait(tval + ((lim_time - tval_diff) >> 8));
  848. }
  849. }
  850. pframes_done++; pframes_shown++;
  851. frames_done++; frames_shown++;
  852. }
  853. emu_set_fastforward(0);
  854. if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
  855. if (PsndOut != NULL) {
  856. pemu_sound_stop();
  857. PsndOut = NULL;
  858. }
  859. // save SRAM
  860. if ((currentConfig.EmuOpt & 1) && SRam.changed) {
  861. emu_msg_cb("Writing SRAM/BRAM..");
  862. emu_save_load_game(0, 1);
  863. SRam.changed = 0;
  864. }
  865. // clear fps counters and stuff
  866. memset32_uncached((int *)psp_video_get_active_fb() + 512*264*2/4, 0, 512*8*2/4);
  867. }
  868. void emu_HandleResume(void)
  869. {
  870. if (!(PicoAHW & PAHW_MCD)) return;
  871. // reopen first CD track
  872. if (Pico_mcd->TOC.Tracks[0].F != NULL)
  873. {
  874. char *fname = rom_fname_reload;
  875. int len = strlen(rom_fname_reload);
  876. cue_data_t *cue_data = NULL;
  877. if (len > 4 && strcasecmp(fname + len - 4, ".cue") == 0)
  878. {
  879. cue_data = cue_parse(rom_fname_reload);
  880. if (cue_data != NULL)
  881. fname = cue_data->tracks[1].fname;
  882. }
  883. lprintf("emu_HandleResume: reopen %s\n", fname);
  884. pm_close(Pico_mcd->TOC.Tracks[0].F);
  885. Pico_mcd->TOC.Tracks[0].F = pm_open(fname);
  886. lprintf("reopen %s\n", Pico_mcd->TOC.Tracks[0].F != NULL ? "ok" : "failed");
  887. if (cue_data != NULL) cue_destroy(cue_data);
  888. }
  889. mp3_reopen_file();
  890. if (!(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
  891. cdda_start_play();
  892. }