emu.c 27 KB

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