emu.c 29 KB

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