psp snd output improved
[picodrive.git] / platform / psp / emu.c
CommitLineData
1820b5a7 1#include <sys/stat.h>
2#include <sys/types.h>
3#include <sys/syslimits.h> // PATH_MAX
4
7d4906bf 5#include <pspthreadman.h>
70357ce5 6#include <pspdisplay.h>
9112b6ce 7#include <psputils.h>
8#include <pspgu.h>
a4221917 9#include <pspaudio.h>
7d4906bf 10
11#include "psp.h"
12#include "menu.h"
13#include "emu.h"
14#include "../common/emu.h"
15#include "../common/lprintf.h"
1820b5a7 16#include "../../Pico/PicoInt.h"
17
8ab3e3c1 18#define OSD_FPS_X 420
7d4906bf 19
a4221917 20// additional pspaudio imports, credits to crazyc
21int sceAudio_38553111(unsigned short samples, unsigned short freq, char unknown); // play with conversion?
22int sceAudio_5C37C0AE(void); // end play?
23int sceAudio_E0727056(int volume, void *buffer); // blocking output
24int sceAudioOutput2GetRestSample();
25
26
1820b5a7 27char romFileName[PATH_MAX];
9112b6ce 28unsigned char *PicoDraw2FB = (unsigned char *)VRAM_CACHED_STUFF + 8; // +8 to be able to skip border with 1 quadword..
1820b5a7 29int engineState;
30
7d4906bf 31static int combo_keys = 0, combo_acts = 0; // keys and actions which need button combos
32static unsigned int noticeMsgTime = 0;
33int reset_timing = 0; // do we need this?
34
35
a4221917 36static void sound_init(void);
37static void sound_deinit(void);
8ab3e3c1 38static void blit2(const char *fps, const char *notice);
7d4906bf 39static void clearArea(int full);
1820b5a7 40
41void emu_noticeMsgUpdated(void)
42{
7d4906bf 43 noticeMsgTime = sceKernelGetSystemTimeLow();
1820b5a7 44}
45
46void emu_getMainDir(char *dst, int len)
47{
7d4906bf 48 if (len > 0) *dst = 0;
1820b5a7 49}
50
8ab3e3c1 51static void osd_text(int x, const char *text, int is_active)
1820b5a7 52{
8ab3e3c1 53 unsigned short *screen = is_active ? psp_video_get_active_fb() : psp_screen;
54 int len = strlen(text) * 8 / 2;
55 int *p, h;
56 void *tmp;
57 for (h = 0; h < 8; h++) {
58 p = (int *) (screen+x+512*(264+h));
59 p = (int *) ((int)p & ~3); // align
60 memset32(p, 0, len);
61 }
62 if (is_active) { tmp = psp_screen; psp_screen = screen; } // nasty pointer tricks
63 emu_textOut16(x, 264, text);
64 if (is_active) psp_screen = tmp;
65}
7d4906bf 66
8ab3e3c1 67void emu_msg_cb(const char *msg)
68{
69 osd_text(4, msg, 1);
7d4906bf 70 noticeMsgTime = sceKernelGetSystemTimeLow() - 2000000;
71
72 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
73 reset_timing = 1;
1820b5a7 74}
75
7d4906bf 76static void emu_msg_tray_open(void)
1820b5a7 77{
7d4906bf 78 strcpy(noticeMsg, "CD tray opened");
79 noticeMsgTime = sceKernelGetSystemTimeLow();
1820b5a7 80}
81
7d4906bf 82
1820b5a7 83void emu_Init(void)
84{
85 // make dirs for saves, cfgs, etc.
86 mkdir("mds", 0777);
87 mkdir("srm", 0777);
88 mkdir("brm", 0777);
89 mkdir("cfg", 0777);
90
a4221917 91 sound_init();
92
1820b5a7 93 PicoInit();
7d4906bf 94 PicoMessage = emu_msg_cb;
95 PicoMCDopenTray = emu_msg_tray_open;
96 PicoMCDcloseTray = menu_loop_tray;
1820b5a7 97}
98
99void emu_Deinit(void)
100{
101 // save SRAM
7d4906bf 102 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
1820b5a7 103 emu_SaveLoadGame(0, 1);
104 SRam.changed = 0;
105 }
106
107 if (!(currentConfig.EmuOpt & 0x20)) {
108 FILE *f = fopen(PicoConfigFile, "r+b");
109 if (!f) emu_WriteConfig(0);
110 else {
111 // if we already have config, reload it, except last ROM
112 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
113 fread(&currentConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
114 fseek(f, 0, SEEK_SET);
115 fwrite(&currentConfig, 1, sizeof(currentConfig), f);
116 fflush(f);
117 fclose(f);
118 }
119 }
7d4906bf 120
1820b5a7 121 PicoExit();
a4221917 122 sound_deinit();
1820b5a7 123}
124
7d4906bf 125void emu_setDefaultConfig(void)
126{
127 memset(&currentConfig, 0, sizeof(currentConfig));
128 currentConfig.lastRomFile[0] = 0;
129 currentConfig.EmuOpt = 0x1f | 0x680; // | confirm_save, cd_leds, 16bit rend
e41da5ce 130 currentConfig.PicoOpt = 0x0f | 0xc00; // | cd_pcm, cd_cdda
7d4906bf 131 currentConfig.PsndRate = 22050;
132 currentConfig.PicoRegion = 0; // auto
133 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
134 currentConfig.Frameskip = -1; // auto
135 currentConfig.volume = 50;
70357ce5 136 currentConfig.CPUclock = 222;
7d4906bf 137 currentConfig.KeyBinds[ 4] = 1<<0; // SACB RLDU
138 currentConfig.KeyBinds[ 6] = 1<<1;
139 currentConfig.KeyBinds[ 7] = 1<<2;
140 currentConfig.KeyBinds[ 5] = 1<<3;
141 currentConfig.KeyBinds[14] = 1<<4;
142 currentConfig.KeyBinds[13] = 1<<5;
143 currentConfig.KeyBinds[15] = 1<<6;
144 currentConfig.KeyBinds[ 3] = 1<<7;
9d917eea 145 currentConfig.KeyBinds[12] = 1<<26; // switch rnd
7d4906bf 146 currentConfig.KeyBinds[ 8] = 1<<27; // save state
147 currentConfig.KeyBinds[ 9] = 1<<28; // load state
148 currentConfig.PicoCDBuffers = 0;
8ab3e3c1 149 currentConfig.scaling = 1; // bilinear filtering for psp
150 currentConfig.scale = currentConfig.hscale32 = currentConfig.hscale40 = 1.0;
7d4906bf 151}
152
153
8ab3e3c1 154extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
9112b6ce 155
156struct Vertex
157{
158 short u,v;
159 short x,y,z;
160};
161
8ab3e3c1 162static struct Vertex __attribute__((aligned(4))) g_vertices[2];
163static unsigned short __attribute__((aligned(16))) localPal[0x100];
164static int dynamic_palette = 0, need_pal_upload = 0, blit_16bit_mode = 0;
165static int fbimg_offs = 0;
166
167static void set_scaling_params(void)
9112b6ce 168{
8ab3e3c1 169 int src_width, fbimg_width, fbimg_height, fbimg_xoffs, fbimg_yoffs;
170 g_vertices[0].x = g_vertices[0].y =
171 g_vertices[0].z = g_vertices[1].z = 0;
172
173 fbimg_height = (int)(240.0 * currentConfig.scale + 0.5);
174 if (Pico.video.reg[12] & 1) {
175 fbimg_width = (int)(320.0 * currentConfig.scale * currentConfig.hscale40 + 0.5);
176 src_width = 320;
177 } else {
178 fbimg_width = (int)(256.0 * currentConfig.scale * currentConfig.hscale32 + 0.5);
179 src_width = 256;
180 }
9112b6ce 181
8ab3e3c1 182 if (fbimg_width >= 480) {
183 g_vertices[0].u = (fbimg_width-480)/2;
184 g_vertices[1].u = src_width - (fbimg_width-480)/2;
185 fbimg_width = 480;
186 fbimg_xoffs = 0;
187 } else {
188 g_vertices[0].u = 0;
189 g_vertices[1].u = src_width;
190 fbimg_xoffs = 240 - fbimg_width/2;
191 }
192
193 if (fbimg_height >= 272) {
194 g_vertices[0].v = (fbimg_height-272)/2;
195 g_vertices[1].v = 240 - (fbimg_height-272)/2;
196 fbimg_height = 272;
197 fbimg_yoffs = 0;
198 } else {
199 g_vertices[0].v = 0;
200 g_vertices[1].v = 240;
201 fbimg_yoffs = 136 - fbimg_height/2;
202 }
203
204 g_vertices[1].x = fbimg_width;
205 g_vertices[1].y = fbimg_height;
206 if (fbimg_xoffs < 0) fbimg_xoffs = 0;
207 if (fbimg_yoffs < 0) fbimg_yoffs = 0;
208 fbimg_offs = (fbimg_yoffs*512 + fbimg_xoffs) * 2; // dst is always 16bit
209
80db4442 210 /*
8ab3e3c1 211 lprintf("set_scaling_params:\n");
212 lprintf("offs: %i, %i\n", fbimg_xoffs, fbimg_yoffs);
213 lprintf("xy0, xy1: %i, %i; %i, %i\n", g_vertices[0].x, g_vertices[0].y, g_vertices[1].x, g_vertices[1].y);
214 lprintf("uv0, uv1: %i, %i; %i, %i\n", g_vertices[0].u, g_vertices[0].v, g_vertices[1].u, g_vertices[1].v);
80db4442 215 */
9112b6ce 216}
217
80db4442 218static void do_pal_update(int allow_sh)
7d4906bf 219{
8ab3e3c1 220 unsigned int *spal=(void *)Pico.cram;
221 unsigned int *dpal=(void *)localPal;
222 int i;
9112b6ce 223
8ab3e3c1 224 for (i = 0x3f/2; i >= 0; i--)
225 dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
9112b6ce 226
80db4442 227 if (allow_sh && (Pico.video.reg[0xC]&8)) // shadow/hilight?
8ab3e3c1 228 {
229 // shadowed pixels
9112b6ce 230 for (i = 0x3f/2; i >= 0; i--)
80db4442 231 dpal[0x20|i] = dpal[0x60|i] = (dpal[i]>>1)&0x738e738e;
8ab3e3c1 232 // hilighted pixels
233 for (i = 0x3f; i >= 0; i--) {
234 int t=localPal[i]&0xe71c;t+=0x4208;
235 if (t&0x20) t|=0x1c;
236 if (t&0x800) t|=0x700;
237 if (t&0x10000) t|=0xe000;
238 t&=0xe71c;
239 localPal[0x80|i]=(unsigned short)t;
240 }
241 localPal[0xe0] = 0;
9112b6ce 242 }
8ab3e3c1 243 Pico.m.dirtyPal = 0;
244 need_pal_upload = 1;
245}
9112b6ce 246
8ab3e3c1 247static void do_slowmode_lines(int line_to)
248{
249 int line = 0, line_len = (Pico.video.reg[12]&1) ? 320 : 256;
250 unsigned short *dst = (unsigned short *)VRAM_STUFF + 512*240/2;
251 unsigned char *src = (unsigned char *)VRAM_CACHED_STUFF + 16;
252 if (!(Pico.video.reg[1]&8)) { line = 8; dst += 512*8; src += 512*8; }
9112b6ce 253
8ab3e3c1 254 for (; line < line_to; line++, dst+=512, src+=512)
255 amips_clut(dst, src, localPal, line_len);
256}
9112b6ce 257
8ab3e3c1 258static void EmuScanPrepare(void)
259{
260 HighCol = (unsigned char *)VRAM_CACHED_STUFF + 8;
261 if (!(Pico.video.reg[1]&8)) HighCol += 8*512;
9112b6ce 262
8ab3e3c1 263 dynamic_palette = 0;
264 if (Pico.m.dirtyPal)
80db4442 265 do_pal_update(1);
8ab3e3c1 266}
267
268static int EmuScanSlow(unsigned int num, void *sdata)
269{
270 if (!(Pico.video.reg[1]&8)) num += 8;
271
272 if (Pico.m.dirtyPal) {
273 if (!dynamic_palette) {
274 do_slowmode_lines(num);
275 dynamic_palette = 1;
276 }
80db4442 277 do_pal_update(1);
8ab3e3c1 278 }
279
280 if (dynamic_palette) {
281 int line_len = (Pico.video.reg[12]&1) ? 320 : 256;
282 void *dst = (char *)VRAM_STUFF + 512*240 + 512*2*num;
283 amips_clut(dst, HighCol + 8, localPal, line_len);
284 } else
285 HighCol = (unsigned char *)VRAM_CACHED_STUFF + (num+1)*512 + 8;
9112b6ce 286
7d4906bf 287 return 0;
288}
289
8ab3e3c1 290static void blitscreen_clut(void)
9112b6ce 291{
8ab3e3c1 292 int offs = fbimg_offs;
293 offs += (psp_screen == VRAM_FB0) ? VRAMOFFS_FB0 : VRAMOFFS_FB1;
9112b6ce 294
9112b6ce 295 sceGuSync(0,0); // sync with prev
296 sceGuStart(GU_DIRECT, guCmdList);
8ab3e3c1 297 sceGuDrawBuffer(GU_PSM_5650, (void *)offs, 512); // point to back buffer
9112b6ce 298
8ab3e3c1 299 if (dynamic_palette)
300 {
301 if (!blit_16bit_mode) {
302 sceGuTexMode(GU_PSM_5650, 0, 0, 0);
303 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 512*240);
9112b6ce 304
8ab3e3c1 305 blit_16bit_mode = 1;
306 }
9112b6ce 307 }
8ab3e3c1 308 else
9112b6ce 309 {
8ab3e3c1 310 if (blit_16bit_mode) {
311 sceGuClutMode(GU_PSM_5650,0,0xff,0);
312 sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
313 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
314 blit_16bit_mode = 0;
315 }
9112b6ce 316
8ab3e3c1 317 if ((PicoOpt&0x10) && Pico.m.dirtyPal)
80db4442 318 do_pal_update(0);
319
320 sceKernelDcacheWritebackAll();
9112b6ce 321
8ab3e3c1 322 if (need_pal_upload) {
323 need_pal_upload = 0;
324 sceGuClutLoad((256/8), localPal); // upload 32*8 entries (256)
325 }
326 }
7d4906bf 327
8ab3e3c1 328#if 1
329 if (g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x)
330 {
331 struct Vertex* vertices;
332 int x;
7d4906bf 333
8ab3e3c1 334 #define SLICE_WIDTH 32
335 for (x = 0; x < g_vertices[1].x; x += SLICE_WIDTH)
336 {
337 // render sprite
338 vertices = (struct Vertex*)sceGuGetMemory(2 * sizeof(struct Vertex));
339 memcpy(vertices, g_vertices, 2 * sizeof(struct Vertex));
340 vertices[0].u = vertices[0].x = x;
341 vertices[1].u = vertices[1].x = x + SLICE_WIDTH;
342 sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,vertices);
343 }
344 // lprintf("listlen: %iB\n", sceGuCheckList()); // ~480 only
7d4906bf 345 }
8ab3e3c1 346 else
347#endif
348 sceGuDrawArray(GU_SPRITES,GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D,2,0,g_vertices);
349
350 sceGuFinish();
7d4906bf 351}
352
353
354static void cd_leds(void)
355{
356 static int old_reg = 0;
357 unsigned int col_g, col_r, *p;
358
359 if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
360 old_reg = Pico_mcd->s68k_regs[0];
361
362 p = (unsigned int *)((short *)psp_screen + 512*2+4+2);
363 col_g = (old_reg & 2) ? 0x06000600 : 0;
364 col_r = (old_reg & 1) ? 0xc000c000 : 0;
365 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
366 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 512/2 - 12/2;
367 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
368}
369
370
8ab3e3c1 371static void dbg_text(void)
7d4906bf 372{
8ab3e3c1 373 int *p, h, len;
374 char text[128];
7d4906bf 375
8ab3e3c1 376 sprintf(text, "sl: %i, 16b: %i", g_vertices[0].u == 0 && g_vertices[1].u == g_vertices[1].x, blit_16bit_mode);
377 len = strlen(text) * 8 / 2;
378 for (h = 0; h < 8; h++) {
379 p = (int *) ((unsigned short *) psp_screen+2+512*(256+h));
380 p = (int *) ((int)p & ~3); // align
381 memset32(p, 0, len);
7d4906bf 382 }
8ab3e3c1 383 emu_textOut16(2, 256, text);
384}
385
386
387/* called after rendering is done, but frame emulation is not finished */
388void blit1(void)
389{
390 if (PicoOpt&0x10)
7d4906bf 391 {
8ab3e3c1 392 int i;
393 unsigned char *pd;
394 // clear top and bottom trash
395 for (pd = PicoDraw2FB+8, i = 8; i > 0; i--, pd += 512)
396 memset32((int *)pd, 0xe0e0e0e0, 320/4);
397 for (pd = PicoDraw2FB+512*232+8, i = 8; i > 0; i--, pd += 512)
398 memset32((int *)pd, 0xe0e0e0e0, 320/4);
7d4906bf 399 }
8ab3e3c1 400
401 blitscreen_clut();
402}
403
404
405static void blit2(const char *fps, const char *notice)
406{
407 int emu_opt = currentConfig.EmuOpt;
408
409 sceGuSync(0,0);
7d4906bf 410
411 if (notice || (emu_opt & 2)) {
8ab3e3c1 412 if (notice) osd_text(4, notice, 0);
413 if (emu_opt & 2) osd_text(OSD_FPS_X, fps, 0);
7d4906bf 414 }
415
8ab3e3c1 416 dbg_text();
417
7d4906bf 418 if ((emu_opt & 0x400) && (PicoMCD & 1))
419 cd_leds();
420
421 psp_video_flip(0);
422}
423
424// clears whole screen or just the notice area (in all buffers)
425static void clearArea(int full)
426{
427 if (full) {
428 memset32(psp_screen, 0, 512*272*2/4);
429 psp_video_flip(0);
430 memset32(psp_screen, 0, 512*272*2/4);
8ab3e3c1 431 memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*240/4);
432 memset32((int *)VRAM_CACHED_STUFF+512*240/4, 0, 512*240*2/4);
7d4906bf 433 } else {
434 void *fb = psp_video_get_active_fb();
435 memset32((int *)((char *)psp_screen + 512*264*2), 0, 512*8*2/4);
436 memset32((int *)((char *)fb + 512*264*2), 0, 512*8*2/4);
437 }
438}
439
440static void vidResetMode(void)
441{
9112b6ce 442 // setup GU
443 sceGuSync(0,0); // sync with prev
444 sceGuStart(GU_DIRECT, guCmdList);
445
446 sceGuClutMode(GU_PSM_5650,0,0xff,0);
9112b6ce 447 sceGuTexMode(GU_PSM_T8,0,0,0); // 8-bit image
448 sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
8ab3e3c1 449 if (currentConfig.scaling)
450 sceGuTexFilter(GU_LINEAR, GU_LINEAR);
451 else sceGuTexFilter(GU_NEAREST, GU_NEAREST);
9112b6ce 452 sceGuTexScale(1.0f,1.0f);
453 sceGuTexOffset(0.0f,0.0f);
9112b6ce 454
8ab3e3c1 455 sceGuTexImage(0,512,512,512,(char *)VRAM_STUFF + 16);
9112b6ce 456
8ab3e3c1 457 // slow rend.
458 PicoDrawSetColorFormat(-1);
459 PicoScan = EmuScanSlow;
9112b6ce 460
8ab3e3c1 461 localPal[0xe0] = 0;
7d4906bf 462 Pico.m.dirtyPal = 1;
8ab3e3c1 463 blit_16bit_mode = dynamic_palette = 0;
7d4906bf 464
9112b6ce 465 sceGuFinish();
8ab3e3c1 466 set_scaling_params();
9112b6ce 467 sceGuSync(0,0);
7d4906bf 468}
8ab3e3c1 469
a4221917 470
471/* sound stuff */
88b3d7c1 472#define SOUND_BLOCK_SIZE_NTSC (1470*2) // 1024 // 1152
473#define SOUND_BLOCK_SIZE_PAL (1764*2)
a4221917 474#define SOUND_BLOCK_COUNT 4
475
e41da5ce 476static short __attribute__((aligned(4))) sndBuffer[SOUND_BLOCK_SIZE_PAL*SOUND_BLOCK_COUNT + 44100/50*2];
477static short *snd_playptr = NULL, *sndBuffer_endptr = NULL;
478static int samples_made = 0, samples_done = 0, samples_block = 0;
a4221917 479static int sound_thread_exit = 0;
480static SceUID sound_sem = -1;
481
482static void writeSound(int len);
483
484static int sound_thread(SceSize args, void *argp)
7d4906bf 485{
a4221917 486 int ret;
487
c5b61ac2 488 lprintf("sthr: started, priority %i\n", sceKernelGetThreadCurrentPriority());
a4221917 489
490 while (!sound_thread_exit)
491 {
492 if (samples_made - samples_done < samples_block) {
88b3d7c1 493 // wait for data (use at least 2 blocks)
494 //lprintf("sthr: wait... (%i)\n", samples_made - samples_done);
495 while (samples_made - samples_done <= samples_block*2 && !sound_thread_exit)
496 ret = sceKernelWaitSema(sound_sem, 1, 0);
a4221917 497 //lprintf("sthr: sceKernelWaitSema: %i\n", ret);
498 continue;
499 }
500
501 //lprintf("sthr: got data: %i\n", samples_made - samples_done);
502
503 ret = sceAudio_E0727056(PSP_AUDIO_VOLUME_MAX, snd_playptr);
504
505 samples_done += samples_block;
506 snd_playptr += samples_block;
e41da5ce 507 if (snd_playptr >= sndBuffer_endptr)
a4221917 508 snd_playptr = sndBuffer;
509 if (ret)
510 lprintf("sthr: outf: %i; pos %i/%i\n", ret, samples_done, samples_made);
88b3d7c1 511
512 // shouln't happen, but just in case
513 if (samples_made - samples_done >= samples_block*3) {
514 //lprintf("block skip (%i)\n", samples_made - samples_done);
515 samples_done += samples_block; // skip
516 snd_playptr += samples_block;
517 }
518
a4221917 519 }
520
521 lprintf("sthr: exit\n");
522 sceKernelExitDeleteThread(0);
523 return 0;
524}
525
526static void sound_init(void)
527{
528 SceUID thid;
529
530 sound_sem = sceKernelCreateSema("sndsem", 0, 0, 1, NULL);
531 if (sound_sem < 0) lprintf("sceKernelCreateSema() failed: %i\n", sound_sem);
532
c5b61ac2 533 samples_made = samples_done = 0;
534 samples_block = SOUND_BLOCK_SIZE_NTSC; // make sure it goes to sema
a4221917 535 sound_thread_exit = 0;
536 thid = sceKernelCreateThread("sndthread", sound_thread, 0x12, 0x10000, 0, NULL);
537 if (thid >= 0)
538 {
539 sceKernelStartThread(thid, 0, 0);
540 }
541 else
542 lprintf("sceKernelCreateThread failed: %i\n", thid);
543}
544
545static void sound_prepare(void)
546{
547 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
548 int ret, stereo;
549
550 samples_made = samples_done = 0;
551
552 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
9d917eea 553 PsndRerate(Pico.m.frame_count ? 1 : 0);
a4221917 554 }
555 stereo=(PicoOpt&8)>>3;
e41da5ce 556
557 samples_block = Pico.m.pal ? SOUND_BLOCK_SIZE_PAL : SOUND_BLOCK_SIZE_NTSC;
88b3d7c1 558 if (PsndRate <= 22050) samples_block /= 2;
e41da5ce 559 sndBuffer_endptr = &sndBuffer[samples_block*SOUND_BLOCK_COUNT];
a4221917 560
561 lprintf("starting audio: %i, len: %i, stereo: %i, pal: %i, block samples: %i\n",
562 PsndRate, PsndLen, stereo, Pico.m.pal, samples_block);
563
564 while (sceAudioOutput2GetRestSample() > 0) psp_msleep(100);
565 sceAudio_5C37C0AE();
e41da5ce 566 ret = sceAudio_38553111(samples_block/2, PsndRate, 2); // seems to not need that stupid 64byte alignment
a4221917 567 if (ret < 0) {
568 lprintf("sceAudio_38553111() failed: %i\n", ret);
569 sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
570 noticeMsgTime = sceKernelGetSystemTimeLow();
571 currentConfig.EmuOpt &= ~4;
572 } else {
a4221917 573 PicoWriteSound = writeSound;
574 memset32((int *)(void *)sndBuffer, 0, sizeof(sndBuffer)/4);
e41da5ce 575 snd_playptr = sndBuffer_endptr - samples_block;
576 samples_made = samples_block; // send 1 empty block first..
a4221917 577 PsndOut = sndBuffer;
578 PsndRate_old = PsndRate;
579 PicoOpt_old = PicoOpt;
580 pal_old = Pico.m.pal;
581 }
582}
583
584static void sound_end(void)
585{
80db4442 586 samples_made = samples_done = 0;
587 while (sceAudioOutput2GetRestSample() > 0)
e41da5ce 588 psp_msleep(100);
589 sceAudio_5C37C0AE();
a4221917 590}
591
592static void sound_deinit(void)
593{
594 sound_thread_exit = 1;
595 sceKernelSignalSema(sound_sem, 1);
80db4442 596 sceKernelDeleteSema(sound_sem);
597 sound_sem = -1;
a4221917 598}
599
600static void writeSound(int len)
601{
602 int ret;
7d4906bf 603 if (PicoOpt&8) len<<=1;
604
a4221917 605 PsndOut += len;
e41da5ce 606 /*if (PsndOut > sndBuffer_endptr) {
a4221917 607 memcpy32((int *)(void *)sndBuffer, (int *)endptr, (PsndOut - endptr + 1) / 2);
608 PsndOut = &sndBuffer[PsndOut - endptr];
e41da5ce 609 lprintf("mov\n");
a4221917 610 }
e41da5ce 611 else*/
612 if (PsndOut >= sndBuffer_endptr)
613 PsndOut = sndBuffer;
a4221917 614
615 // signal the snd thread
616 samples_made += len;
88b3d7c1 617 if (samples_made - samples_done > samples_block*2) {
e41da5ce 618 // lprintf("signal, %i/%i\n", samples_done, samples_made);
a4221917 619 ret = sceKernelSignalSema(sound_sem, 1);
e41da5ce 620 // lprintf("signal ret %i\n", ret);
a4221917 621 }
7d4906bf 622}
a4221917 623
7d4906bf 624
625static void SkipFrame(void)
626{
627 PicoSkipFrame=1;
628 PicoFrame();
629 PicoSkipFrame=0;
630}
631
632void emu_forcedFrame(void)
633{
634 int po_old = PicoOpt;
635 int eo_old = currentConfig.EmuOpt;
636
637 PicoOpt &= ~0x0010;
638 PicoOpt |= 0x4080; // soft_scale | acc_sprites
639 currentConfig.EmuOpt |= 0x80;
640
8ab3e3c1 641 vidResetMode();
642 memset32(VRAM_CACHED_STUFF, 0xe0e0e0e0, 512*8/4); // borders
643 memset32((int *)VRAM_CACHED_STUFF + 512*232/4, 0xe0e0e0e0, 512*8/4);
a4221917 644 memset32((int *)psp_screen + 512*264*2/4, 0, 512*8*2/4);
8ab3e3c1 645
646 PicoDrawSetColorFormat(-1);
647 PicoScan = EmuScanSlow;
9112b6ce 648 EmuScanPrepare();
7d4906bf 649 PicoFrameDrawOnly();
8ab3e3c1 650 blit1();
651 sceGuSync(0,0);
7d4906bf 652
653 PicoOpt = po_old;
654 currentConfig.EmuOpt = eo_old;
655}
656
657
658static void RunEvents(unsigned int which)
659{
660 if (which & 0x1800) // save or load (but not both)
661 {
662 int do_it = 1;
663
664 if ( emu_checkSaveFile(state_slot) &&
665 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
666 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
667 {
668 int keys;
8ab3e3c1 669 blit2("", (which & 0x1000) ? "LOAD STATE? (X=yes, O=no)" : "OVERWRITE SAVE? (X=yes, O=no)");
7d4906bf 670 while( !((keys = psp_pad_read(1)) & (BTN_X|BTN_CIRCLE)) )
671 psp_msleep(50);
672 if (keys & BTN_CIRCLE) do_it = 0;
673 while( ((keys = psp_pad_read(1)) & (BTN_X|BTN_CIRCLE)) ) // wait for release
674 psp_msleep(50);
675 clearArea(0);
676 }
677
678 if (do_it)
679 {
8ab3e3c1 680 osd_text(4, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME", 1);
681 PicoStateProgressCB = emu_msg_cb;
7d4906bf 682 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
683 PicoStateProgressCB = NULL;
684 psp_msleep(0);
685 }
686
687 reset_timing = 1;
688 }
689 if (which & 0x0400) // switch renderer
690 {
691 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
692 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
693
694 vidResetMode();
695
c5b61ac2 696 if (PicoOpt&0x10)
697 strcpy(noticeMsg, "fast renderer");
698 else if (currentConfig.EmuOpt&0x80)
699 strcpy(noticeMsg, "accurate renderer");
7d4906bf 700
701 noticeMsgTime = sceKernelGetSystemTimeLow();
702 }
703 if (which & 0x0300)
704 {
705 if(which&0x0200) {
706 state_slot -= 1;
707 if(state_slot < 0) state_slot = 9;
708 } else {
709 state_slot += 1;
710 if(state_slot > 9) state_slot = 0;
711 }
712 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
713 noticeMsgTime = sceKernelGetSystemTimeLow();
714 }
715}
716
717static void updateKeys(void)
718{
719 unsigned int keys, allActions[2] = { 0, 0 }, events;
720 static unsigned int prevEvents = 0;
721 int i;
722
723 keys = psp_pad_read(0);
70357ce5 724 if (keys & PSP_CTRL_HOME)
725 sceDisplayWaitVblankStart();
726
7d4906bf 727 if (keys & BTN_SELECT)
728 engineState = PGS_Menu;
729
730 keys &= CONFIGURABLE_KEYS;
731
732 for (i = 0; i < 32; i++)
733 {
734 if (keys & (1 << i))
735 {
736 int pl, acts = currentConfig.KeyBinds[i];
737 if (!acts) continue;
738 pl = (acts >> 16) & 1;
739 if (combo_keys & (1 << i))
740 {
741 int u = i+1, acts_c = acts & combo_acts;
742 // let's try to find the other one
743 if (acts_c)
744 for (; u < 32; u++)
745 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
746 allActions[pl] |= acts_c;
747 keys &= ~((1 << i) | (1 << u));
748 break;
749 }
750 // add non-combo actions if combo ones were not found
751 if (!acts_c || u == 32)
752 allActions[pl] |= acts & ~combo_acts;
753 } else {
754 allActions[pl] |= acts;
755 }
756 }
757 }
758
759 PicoPad[0] = (unsigned short) allActions[0];
760 PicoPad[1] = (unsigned short) allActions[1];
761
762 events = (allActions[0] | allActions[1]) >> 16;
763
764 // volume is treated in special way and triggered every frame
765 if ((events & 0x6000) && PsndOut != NULL)
766 {
767 int vol = currentConfig.volume;
768 if (events & 0x2000) {
769 if (vol < 100) vol++;
770 } else {
771 if (vol > 0) vol--;
772 }
773 // FrameworkAudio_SetVolume(vol, vol); // TODO
774 sprintf(noticeMsg, "VOL: %02i ", vol);
775 noticeMsgTime = sceKernelGetSystemTimeLow();
776 currentConfig.volume = vol;
777 }
778
779 events &= ~prevEvents;
780 if (events) RunEvents(events);
781 if (movie_data) emu_updateMovie();
782
783 prevEvents = (allActions[0] | allActions[1]) >> 16;
784}
785
786static void find_combos(void)
787{
788 int act, u;
789
790 // find out which keys and actions are combos
791 combo_keys = combo_acts = 0;
792 for (act = 0; act < 32; act++)
793 {
794 int keyc = 0;
795 if (act == 16 || act == 17) continue; // player2 flag
796 for (u = 0; u < 32; u++)
797 {
798 if (currentConfig.KeyBinds[u] & (1 << act)) keyc++;
799 }
800 if (keyc > 1)
801 {
802 // loop again and mark those keys and actions as combo
803 for (u = 0; u < 32; u++)
804 {
805 if (currentConfig.KeyBinds[u] & (1 << act)) {
806 combo_keys |= 1 << u;
807 combo_acts |= 1 << act;
808 }
809 }
810 }
811 }
812}
813
814
815static void simpleWait(unsigned int until)
816{
817 unsigned int tval;
818 int diff;
819
820 tval = sceKernelGetSystemTimeLow();
821 diff = (int)until - (int)tval;
822 if (diff >= 512 && diff < 100*1024)
823 sceKernelDelayThread(diff);
824}
825
826void emu_Loop(void)
827{
7d4906bf 828 char fpsbuff[24]; // fps count c string
829 unsigned int tval, tval_prev = 0, tval_thissec = 0; // timing
830 int frames_done = 0, frames_shown = 0, oldmodes = 0;
831 int target_fps, target_frametime, lim_time, tval_diff, i;
832 char *notice = NULL;
833
834 lprintf("entered emu_Loop()\n");
835
836 fpsbuff[0] = 0;
837
70357ce5 838 if (currentConfig.CPUclock != psp_get_cpu_clock()) {
839 lprintf("setting cpu clock to %iMHz... ", currentConfig.CPUclock);
840 i = psp_set_cpu_clock(currentConfig.CPUclock);
841 lprintf(i ? "failed\n" : "done\n");
842 currentConfig.CPUclock = psp_get_cpu_clock();
843 }
844
7d4906bf 845 // make sure we are in correct mode
846 vidResetMode();
8ab3e3c1 847 clearArea(1);
7d4906bf 848 Pico.m.dirtyPal = 1;
849 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
850 find_combos();
851
852 // pal/ntsc might have changed, reset related stuff
853 target_fps = Pico.m.pal ? 50 : 60;
854 target_frametime = Pico.m.pal ? (1000000<<8)/50 : (1000000<<8)/60+1;
855 reset_timing = 1;
856
857 // prepare CD buffer
858 if (PicoMCD & 1) PicoCDBufferInit();
859
860 // prepare sound stuff
861 PsndOut = NULL;
7d4906bf 862 if (currentConfig.EmuOpt & 4)
863 {
a4221917 864 sound_prepare();
7d4906bf 865 }
7d4906bf 866
867 // loop?
868 while (engineState == PGS_Running)
869 {
870 int modes;
871
872 tval = sceKernelGetSystemTimeLow();
873 if (reset_timing || tval < tval_prev) {
874 //stdbg("timing reset");
875 reset_timing = 0;
876 tval_thissec = tval;
877 frames_shown = frames_done = 0;
878 }
879
880 // show notice message?
881 if (noticeMsgTime) {
882 static int noticeMsgSum;
883 if (tval - noticeMsgTime > 2000000) { // > 2.0 sec
884 noticeMsgTime = 0;
885 clearArea(0);
886 notice = 0;
887 } else {
888 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
889 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
890 notice = noticeMsg;
891 }
892 }
893
894 // check for mode changes
895 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
896 if (modes != oldmodes) {
897 oldmodes = modes;
898 clearArea(1);
8ab3e3c1 899 set_scaling_params();
7d4906bf 900 }
901
902 // second passed?
903 if (tval - tval_thissec >= 1000000)
904 {
e41da5ce 905 // missing 1 frame?
906 if (currentConfig.Frameskip < 0 && frames_done < target_fps) {
907 SkipFrame(); frames_done++;
7d4906bf 908 }
e41da5ce 909
910 if (currentConfig.EmuOpt & 2)
911 sprintf(fpsbuff, "%02i/%02i ", frames_shown, frames_done);
912
7d4906bf 913 tval_thissec += 1000000;
914
915 if (currentConfig.Frameskip < 0) {
916 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
917 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
918 if (frames_shown > frames_done) frames_shown = frames_done;
919 } else {
920 frames_done = frames_shown = 0;
921 }
922 }
923#ifdef PFRAMES
924 sprintf(fpsbuff, "%i", Pico.m.frame_count);
925#endif
926
927 tval_prev = tval;
928 lim_time = (frames_done+1) * target_frametime;
929 if (currentConfig.Frameskip >= 0) // frameskip enabled
930 {
931 for (i = 0; i < currentConfig.Frameskip; i++) {
932 updateKeys();
933 SkipFrame(); frames_done++;
934 if (PsndOut) { // do framelimitting if sound is enabled
935 int tval_diff;
936 tval = sceKernelGetSystemTimeLow();
937 tval_diff = (int)(tval - tval_thissec) << 8;
938 if (tval_diff < lim_time) // we are too fast
939 simpleWait(tval + ((lim_time - tval_diff)>>8));
940 }
941 lim_time += target_frametime;
942 }
943 }
944 else // auto frameskip
945 {
946 int tval_diff;
947 tval = sceKernelGetSystemTimeLow();
948 tval_diff = (int)(tval - tval_thissec) << 8;
88b3d7c1 949 if (tval_diff > lim_time && (frames_done/16 < frames_shown))
7d4906bf 950 {
951 // no time left for this frame - skip
952 if (tval_diff - lim_time >= (300000<<8)) {
7d4906bf 953 reset_timing = 1;
954 continue;
955 }
956 updateKeys();
957 SkipFrame(); frames_done++;
958 continue;
959 }
960 }
961
962 updateKeys();
963
964 if (!(PicoOpt&0x10))
9112b6ce 965 EmuScanPrepare();
7d4906bf 966
967 PicoFrame();
968
8ab3e3c1 969 blit2(fpsbuff, notice);
7d4906bf 970
971 // check time
972 tval = sceKernelGetSystemTimeLow();
973 tval_diff = (int)(tval - tval_thissec) << 8;
974
975 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300000<<8)) // slowdown detection
976 reset_timing = 1;
977 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
978 {
979 // sleep if we are still too fast
980 if (tval_diff < lim_time)
981 {
982 // we are too fast
983 simpleWait(tval + ((lim_time - tval_diff) >> 8));
984 }
985 }
986
987 frames_done++; frames_shown++;
988 }
989
990
991 if (PicoMCD & 1) PicoCDBufferFree();
a4221917 992
7d4906bf 993 if (PsndOut != NULL) {
a4221917 994 PsndOut = NULL;
995 sound_end();
7d4906bf 996 }
a4221917 997
7d4906bf 998 // save SRAM
999 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
8ab3e3c1 1000 emu_msg_cb("Writing SRAM/BRAM..");
7d4906bf 1001 emu_SaveLoadGame(0, 1);
1002 SRam.changed = 0;
1003 }
a4221917 1004
80db4442 1005 // clear fps counters and stuff
1006 memset32((int *)psp_video_get_active_fb() + 512*264*2/4, 0, 512*8*2/4);
7d4906bf 1007}
1008
1009
1820b5a7 1010void emu_ResetGame(void)
1011{
1012 PicoReset(0);
7d4906bf 1013 reset_timing = 1;
1820b5a7 1014}
1015