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