improve linux makefile
[picodrive.git] / platform / gizmondo / emu.c
CommitLineData
e5f426aa 1#include <windows.h>
ea8c405f 2#include <string.h>
e5f426aa 3
2ec14aec 4#include <sys/stat.h> // mkdir
5#include <sys/types.h>
6
7#include "kgsdk/Framework.h"
ea8c405f 8#include "kgsdk/Framework2D.h"
9#include "kgsdk/FrameworkAudio.h"
10#include "../common/emu.h"
11#include "../common/lprintf.h"
12#include "../common/arm_utils.h"
84100c0f 13#include "../common/config.h"
e5f426aa 14#include "emu.h"
ea8c405f 15#include "menu.h"
16#include "giz.h"
17#include "asm_utils.h"
e5f426aa 18
efcba75f 19#include <pico/pico_int.h>
ea8c405f 20
21#ifdef BENCHMARK
22#define OSD_FPS_X 220
23#else
24#define OSD_FPS_X 260
25#endif
26
27// main 300K gfx-related buffer. Used by menu and renderers.
28unsigned char gfx_buffer[321*240*2*2];
e5f426aa 29
ea8c405f 30unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
e5f426aa 31int reset_timing = 0;
ea8c405f 32
33static DWORD noticeMsgTime = 0;
fa283c9a 34static short *snd_cbuff = NULL;
35static int snd_cbuf_samples = 0, snd_all_samples = 0;
ea8c405f 36
37
38static void blit(const char *fps, const char *notice);
39static void clearArea(int full);
40
41void emu_noticeMsgUpdated(void)
42{
43 noticeMsgTime = GetTickCount();
44}
45
ca482e5d 46int emu_getMainDir(char *dst, int len)
ea8c405f 47{
48 if (len > 0) *dst = 0;
ca482e5d 49
50 return 0;
ea8c405f 51}
52
53static void emu_msg_cb(const char *msg)
54{
84100c0f 55 if (giz_screen != NULL) fb_unlock();
56 giz_screen = fb_lock(1);
ea8c405f 57
58 memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
59 emu_textOut16(4, 232, msg);
60 noticeMsgTime = GetTickCount() - 2000;
61
62 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
63 reset_timing = 1;
c77ca79e 64
84100c0f 65 fb_unlock();
66 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
ea8c405f 67}
68
c77ca79e 69void emu_stateCb(const char *str)
ea8c405f 70{
84100c0f 71 if (giz_screen != NULL) fb_unlock();
72 giz_screen = fb_lock(1);
9839d126 73
ea8c405f 74 clearArea(0);
75 blit("", str);
9839d126 76
84100c0f 77 fb_unlock();
9839d126 78 giz_screen = NULL;
da42200b 79
80 Sleep(0); /* yield the CPU, the system may need it */
ea8c405f 81}
82
83static void emu_msg_tray_open(void)
84{
85 strcpy(noticeMsg, "CD tray opened");
86 noticeMsgTime = GetTickCount();
87}
88
89
90void emu_Init(void)
91{
92 // make dirs for saves, cfgs, etc.
2ec14aec 93 mkdir("mds", 0777);
94 mkdir("srm", 0777);
95 mkdir("brm", 0777);
96 mkdir("cfg", 0777);
ea8c405f 97
98 PicoInit();
99 PicoMessage = emu_msg_cb;
100 PicoMCDopenTray = emu_msg_tray_open;
101 PicoMCDcloseTray = menu_loop_tray;
102}
103
104void emu_Deinit(void)
105{
106 // save SRAM
3c392aec 107 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
ea8c405f 108 emu_SaveLoadGame(0, 1);
109 SRam.changed = 0;
110 }
111
58c86d00 112 if (!(currentConfig.EmuOpt & 0x20))
113 config_writelrom(PicoConfigFile);
ea8c405f 114
115 PicoExit();
116}
117
84100c0f 118void emu_prepareDefaultConfig(void)
ea8c405f 119{
84100c0f 120 memset(&defaultConfig, 0, sizeof(defaultConfig));
121 defaultConfig.EmuOpt = 0x1d | 0x680; // | confirm_save, cd_leds, 16bit rend
122 defaultConfig.s_PicoOpt = 0x0f | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;
123 defaultConfig.s_PsndRate = 22050;
124 defaultConfig.s_PicoRegion = 0; // auto
125 defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP
126 defaultConfig.s_PicoCDBuffers = 0;
127 defaultConfig.Frameskip = -1; // auto
128 defaultConfig.volume = 50;
129 defaultConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
130 defaultConfig.KeyBinds[ 3] = 1<<1;
131 defaultConfig.KeyBinds[ 0] = 1<<2;
132 defaultConfig.KeyBinds[ 1] = 1<<3;
133 defaultConfig.KeyBinds[ 5] = 1<<4;
134 defaultConfig.KeyBinds[ 6] = 1<<5;
135 defaultConfig.KeyBinds[ 7] = 1<<6;
136 defaultConfig.KeyBinds[ 4] = 1<<7;
137 defaultConfig.KeyBinds[13] = 1<<26; // switch rend
138 defaultConfig.KeyBinds[ 8] = 1<<27; // save state
139 defaultConfig.KeyBinds[ 9] = 1<<28; // load state
140 defaultConfig.KeyBinds[12] = 1<<29; // vol up
141 defaultConfig.KeyBinds[11] = 1<<30; // vol down
142 defaultConfig.scaling = 0;
f0f0d2df 143 defaultConfig.turbo_rate = 15;
ea8c405f 144}
145
146
602133e1 147static int EmuScanBegin16(unsigned int num)
ea8c405f 148{
149 if (!(Pico.video.reg[1]&8)) num += 8;
602133e1 150 DrawLineDest = (unsigned short *) giz_screen + 321 * num;
ea8c405f 151
c77ca79e 152 if ((currentConfig.EmuOpt&0x4000) && (num&1) == 0) // (Pico.m.frame_count&1))
a8869ad1 153 return 1; // skip next line
154
ea8c405f 155 return 0;
156}
157
602133e1 158static int EmuScanBegin8(unsigned int num)
ea8c405f 159{
160 // draw like the fast renderer
161 if (!(Pico.video.reg[1]&8)) num += 8;
602133e1 162 HighCol = gfx_buffer + 328 * num;
ea8c405f 163
164 return 0;
165}
166
167static void osd_text(int x, int y, const char *text)
168{
499a0be3 169 int len = strlen(text) * 8 / 2;
170 int *p, h;
ea8c405f 171 for (h = 0; h < 8; h++) {
172 p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
173 p = (int *) ((int)p & ~3); // align
499a0be3 174 memset32(p, 0, len);
ea8c405f 175 }
176 emu_textOut16(x, y, text);
177}
178
499a0be3 179/*
180void log1(void *p1, void *p2)
181{
182 lprintf("%p %p %p\n", p1, p2, DrawLineDest);
183}
184*/
ea8c405f 185
9839d126 186static void cd_leds(void)
187{
188 static int old_reg = 0;
189 unsigned int col_g, col_r, *p;
190
191 if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change
192 old_reg = Pico_mcd->s68k_regs[0];
193
194 p = (unsigned int *)((short *)giz_screen + 321*2+4+2);
195 col_g = (old_reg & 2) ? 0x06000600 : 0;
196 col_r = (old_reg & 1) ? 0xc000c000 : 0;
197 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r; p += 321/2 - 12/2 + 1;
198 *p++ = col_g; p+=3; *p++ = col_r; p += 321/2 - 10/2;
199 *p++ = col_g; *p++ = col_g; p+=2; *p++ = col_r; *p++ = col_r;
200}
201
202
203static short localPal[0x100];
ea8c405f 204
205static void blit(const char *fps, const char *notice)
206{
207 int emu_opt = currentConfig.EmuOpt;
208
9839d126 209 if (PicoOpt&0x10)
210 {
211 int lines_flags = 224;
ea8c405f 212 // 8bit fast renderer
213 if (Pico.m.dirtyPal) {
214 Pico.m.dirtyPal = 0;
215 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
216 }
84100c0f 217 // a hack for VR
218 if (PicoRead16Hook == PicoSVPRead16)
219 memset32((int *)(PicoDraw2FB+328*8+328*223), 0xe0e0e0e0, 328);
a8869ad1 220 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
221 if (currentConfig.EmuOpt&0x4000)
c77ca79e 222 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
9839d126 223 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
224 }
225 else if (!(emu_opt&0x80))
226 {
227 int lines_flags;
ea8c405f 228 // 8bit accurate renderer
229 if (Pico.m.dirtyPal) {
230 Pico.m.dirtyPal = 0;
231 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
2ec14aec 232 if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
ea8c405f 233 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
9839d126 234 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO?
84100c0f 235 memcpy32((void *)(localPal+0xc0), (void *)(localPal+0x40), 0x40*2/4);
ea8c405f 236 localPal[0xc0] = 0x0600;
237 localPal[0xd0] = 0xc000;
238 localPal[0xe0] = 0x0000; // reserved pixels for OSD
239 localPal[0xf0] = 0xffff;
240 }
241 /* no support
242 else if (rendstatus & 0x20) { // mid-frame palette changes
243 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
244 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
245 } */
246 }
9839d126 247 lines_flags = (Pico.video.reg[1]&8) ? 240 : 224;
a8869ad1 248 if (!(Pico.video.reg[12]&1)) lines_flags|=0x10000;
249 if (currentConfig.EmuOpt&0x4000)
c77ca79e 250 lines_flags|=0x40000; // (Pico.m.frame_count&1)?0x20000:0x40000;
9839d126 251 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, lines_flags);
ea8c405f 252 }
253
254 if (notice || (emu_opt & 2)) {
255 int h = 232;
256 if (notice) osd_text(4, h, notice);
9839d126 257 if (emu_opt & 2) osd_text(OSD_FPS_X, h, fps);
ea8c405f 258 }
ea8c405f 259
602133e1 260 if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))
9839d126 261 cd_leds();
ea8c405f 262}
263
264// clears whole screen or just the notice area (in all buffers)
265static void clearArea(int full)
266{
267 if (giz_screen == NULL)
84100c0f 268 giz_screen = fb_lock(1);
ea8c405f 269 if (full) memset32(giz_screen, 0, 320*240*2/4);
499a0be3 270 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
c77ca79e 271
272 if (currentConfig.EmuOpt&0x8000) {
84100c0f 273 fb_unlock();
274 giz_screen = fb_lock(0);
c77ca79e 275 if (full) memset32(giz_screen, 0, 320*240*2/4);
276 else memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
277 }
ea8c405f 278}
279
280static void vidResetMode(void)
281{
84100c0f 282 giz_screen = fb_lock(1);
499a0be3 283
ea8c405f 284 if (PicoOpt&0x10) {
285 } else if (currentConfig.EmuOpt&0x80) {
286 PicoDrawSetColorFormat(1);
602133e1 287 PicoScanBegin = EmuScanBegin16;
ea8c405f 288 } else {
499a0be3 289 PicoDrawSetColorFormat(-1);
602133e1 290 PicoScanBegin = EmuScanBegin8;
ea8c405f 291 }
499a0be3 292 if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80)) {
ea8c405f 293 // setup pal for 8-bit modes
294 localPal[0xc0] = 0x0600;
295 localPal[0xd0] = 0xc000;
296 localPal[0xe0] = 0x0000; // reserved pixels for OSD
297 localPal[0xf0] = 0xffff;
298 }
299 Pico.m.dirtyPal = 1;
499a0be3 300
301 memset32(giz_screen, 0, 321*240*2/4);
c77ca79e 302 if (currentConfig.EmuOpt&0x8000) {
84100c0f 303 fb_unlock();
304 giz_screen = fb_lock(0);
c77ca79e 305 memset32(giz_screen, 0, 321*240*2/4);
306 }
84100c0f 307 fb_unlock();
ea8c405f 308 giz_screen = NULL;
309}
310
da42200b 311/*
fa283c9a 312#include <stdarg.h>
313static void stdbg(const char *fmt, ...)
ea8c405f 314{
fa283c9a 315 static int cnt = 0;
316 va_list vl;
317
318 sprintf(noticeMsg, "%x ", cnt++);
319 va_start(vl, fmt);
320 vsnprintf(noticeMsg+strlen(noticeMsg), sizeof(noticeMsg)-strlen(noticeMsg), fmt, vl);
321 va_end(vl);
322
323 noticeMsgTime = GetTickCount();
324}
da42200b 325*/
fa283c9a 326
327static void updateSound(int len)
328{
329 if (PicoOpt&8) len<<=1;
330
331 snd_all_samples += len;
332 PsndOut += len;
333 if (PsndOut - snd_cbuff >= snd_cbuf_samples)
334 {
da42200b 335 //if (PsndOut - snd_cbuff != snd_cbuf_samples)
336 // stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
fa283c9a 337 PsndOut = snd_cbuff;
338 }
339}
340
341
342static void SkipFrame(void)
343{
344 PicoSkipFrame=1;
ea8c405f 345 PicoFrame();
346 PicoSkipFrame=0;
347}
348
c77ca79e 349/* forced frame to front buffer */
0fc0e241 350void emu_forcedFrame(int opts)
ea8c405f 351{
fa283c9a 352 int po_old = PicoOpt;
353 int eo_old = currentConfig.EmuOpt;
354
0fc0e241 355 PicoOpt &= ~0x10;
356 PicoOpt |= opts|POPT_ACC_SPRITES;
fa283c9a 357 currentConfig.EmuOpt |= 0x80;
358
359 if (giz_screen == NULL)
84100c0f 360 giz_screen = fb_lock(1);
fa283c9a 361
362 PicoDrawSetColorFormat(1);
602133e1 363 PicoScanBegin = EmuScanBegin16;
fa283c9a 364 Pico.m.dirtyPal = 1;
365 PicoFrameDrawOnly();
366
84100c0f 367 fb_unlock();
fa283c9a 368 giz_screen = NULL;
369
370 PicoOpt = po_old;
371 currentConfig.EmuOpt = eo_old;
ea8c405f 372}
373
fa283c9a 374
9839d126 375static void RunEvents(unsigned int which)
376{
da42200b 377 if (which & 0x1800) // save or load (but not both)
378 {
9839d126 379 int do_it = 1;
da42200b 380
381 if (PsndOut != NULL)
382 FrameworkAudio_SetPause(1);
383 if (giz_screen == NULL)
84100c0f 384 giz_screen = fb_lock(1);
9839d126 385 if ( emu_checkSaveFile(state_slot) &&
a8869ad1 386 (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
387 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
388 {
9839d126 389 int keys;
390 blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)");
e5ab6faf 391 while( !((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) )
9839d126 392 Sleep(50);
e5ab6faf 393 if (keys & PBTN_STOP) do_it = 0;
394 while( ((keys = Framework_PollGetButtons()) & (PBTN_PLAY|PBTN_STOP)) ) // wait for release
da42200b 395 Sleep(50);
9839d126 396 clearArea(0);
397 }
da42200b 398
399 if (do_it)
400 {
9839d126 401 osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
c77ca79e 402 PicoStateProgressCB = emu_stateCb;
9839d126 403 emu_SaveLoadGame((which & 0x1000) >> 12, 0);
404 PicoStateProgressCB = NULL;
da42200b 405 Sleep(0);
9839d126 406 }
407
da42200b 408 if (PsndOut != NULL)
409 FrameworkAudio_SetPause(0);
9839d126 410 reset_timing = 1;
411 }
da42200b 412 if (which & 0x0400) // switch renderer
413 {
a8869ad1 414 if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
415 else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
9839d126 416
417 vidResetMode();
418
419 if (PicoOpt&0x10) {
420 strcpy(noticeMsg, " 8bit fast renderer");
421 } else if (currentConfig.EmuOpt&0x80) {
422 strcpy(noticeMsg, "16bit accurate renderer");
423 } else {
424 strcpy(noticeMsg, " 8bit accurate renderer");
425 }
426
427 noticeMsgTime = GetTickCount();
428 }
da42200b 429 if (which & 0x0300)
430 {
9839d126 431 if(which&0x0200) {
432 state_slot -= 1;
433 if(state_slot < 0) state_slot = 9;
434 } else {
435 state_slot += 1;
436 if(state_slot > 9) state_slot = 0;
437 }
438 sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");
439 noticeMsgTime = GetTickCount();
440 }
441}
442
ea8c405f 443static void updateKeys(void)
444{
2ec14aec 445 unsigned int keys, allActions[2] = { 0, 0 }, events;
446 static unsigned int prevEvents = 0;
447 int i;
448
449 keys = Framework_PollGetButtons();
e5ab6faf 450 if (keys & PBTN_HOME)
2ec14aec 451 engineState = PGS_Menu;
2ec14aec 452
453 keys &= CONFIGURABLE_KEYS;
454
455 for (i = 0; i < 32; i++)
456 {
fa283c9a 457 if (keys & (1 << i))
458 {
2ec14aec 459 int pl, acts = currentConfig.KeyBinds[i];
460 if (!acts) continue;
461 pl = (acts >> 16) & 1;
bdec53c9 462 if (kb_combo_keys & (1 << i))
fa283c9a 463 {
da310283 464 int u = i+1, acts_c = acts & kb_combo_acts;
2ec14aec 465 // let's try to find the other one
bdec53c9 466 if (acts_c) {
da310283 467 for (; u < 32; u++)
bdec53c9 468 if ( (keys & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {
469 allActions[pl] |= acts_c & currentConfig.KeyBinds[u];
2ec14aec 470 keys &= ~((1 << i) | (1 << u));
471 break;
472 }
bdec53c9 473 }
2ec14aec 474 // add non-combo actions if combo ones were not found
475 if (!acts_c || u == 32)
bdec53c9 476 allActions[pl] |= acts & ~kb_combo_acts;
fa283c9a 477 } else {
2ec14aec 478 allActions[pl] |= acts;
479 }
480 }
481 }
482
f0f0d2df 483 PicoPad[0] = allActions[0] & 0xfff;
484 PicoPad[1] = allActions[1] & 0xfff;
485
486 if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);
487 if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);
2ec14aec 488
489 events = (allActions[0] | allActions[1]) >> 16;
490
491 // volume is treated in special way and triggered every frame
fa283c9a 492 if ((events & 0x6000) && PsndOut != NULL)
493 {
2ec14aec 494 int vol = currentConfig.volume;
495 if (events & 0x2000) {
496 if (vol < 100) vol++;
497 } else {
498 if (vol > 0) vol--;
499 }
fa283c9a 500 FrameworkAudio_SetVolume(vol, vol);
c77ca79e 501 sprintf(noticeMsg, "VOL: %02i ", vol);
2ec14aec 502 noticeMsgTime = GetTickCount();
503 currentConfig.volume = vol;
504 }
505
506 events &= ~prevEvents;
9839d126 507 if (events) RunEvents(events);
2ec14aec 508 if (movie_data) emu_updateMovie();
509
510 prevEvents = (allActions[0] | allActions[1]) >> 16;
ea8c405f 511}
512
84100c0f 513void emu_platformDebugCat(char *str)
514{
515 // nothing
516}
fa283c9a 517
ea8c405f 518static void simpleWait(DWORD until)
519{
9839d126 520 DWORD tval;
521 int diff;
522
523 tval = GetTickCount();
524 diff = (int)until - (int)tval;
525 if (diff >= 2)
526 Sleep(diff - 1);
527
528 while ((tval = GetTickCount()) < until && until - tval < 512) // some simple overflow detection
529 spend_cycles(1024*2);
ea8c405f 530}
531
532void emu_Loop(void)
533{
fa283c9a 534 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
ea8c405f 535 char fpsbuff[24]; // fps count c string
536 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
fa283c9a 537 int frames_done = 0, frames_shown = 0, oldmodes = 0, sec_ms = 1000;
ea8c405f 538 int target_fps, target_frametime, lim_time, tval_diff, i;
539 char *notice = NULL;
540
541 lprintf("entered emu_Loop()\n");
542
543 fpsbuff[0] = 0;
544
545 // make sure we are in correct mode
546 vidResetMode();
547 if (currentConfig.scaling) PicoOpt|=0x4000;
548 else PicoOpt&=~0x4000;
549 Pico.m.dirtyPal = 1;
550 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
bdec53c9 551 emu_findKeyBindCombos();
ea8c405f 552
553 // pal/ntsc might have changed, reset related stuff
554 target_fps = Pico.m.pal ? 50 : 60;
fa283c9a 555 target_frametime = Pico.m.pal ? (1000<<8)/50 : (1000<<8)/60+1;
ea8c405f 556 reset_timing = 1;
557
fa283c9a 558 // prepare CD buffer
602133e1 559 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
fa283c9a 560
ea8c405f 561 // prepare sound stuff
fa283c9a 562 PsndOut = NULL;
563 if (currentConfig.EmuOpt & 4)
564 {
da42200b 565 int ret, snd_excess_add, stereo;
ea8c405f 566 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
9d917eea 567 PsndRerate(Pico.m.frame_count ? 1 : 0);
ea8c405f 568 }
da42200b 569 stereo=(PicoOpt&8)>>3;
ea8c405f 570 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
fa283c9a 571 snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
ea8c405f 572 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
f3d1de29 573 PsndRate, PsndLen, snd_excess_add, stereo, Pico.m.pal);
fa283c9a 574 ret = FrameworkAudio_Init(PsndRate, snd_cbuf_samples, stereo);
575 if (ret != 0) {
576 lprintf("FrameworkAudio_Init() failed: %i\n", ret);
577 sprintf(noticeMsg, "sound init failed (%i), snd disabled", ret);
578 noticeMsgTime = GetTickCount();
579 currentConfig.EmuOpt &= ~4;
580 } else {
581 FrameworkAudio_SetVolume(currentConfig.volume, currentConfig.volume);
582 PicoWriteSound = updateSound;
583 snd_cbuff = FrameworkAudio_56448Buffer();
584 PsndOut = snd_cbuff + snd_cbuf_samples / 2; // start writing at the middle
585 snd_all_samples = 0;
586 PsndRate_old = PsndRate;
587 PicoOpt_old = PicoOpt;
588 pal_old = Pico.m.pal;
589 }
ea8c405f 590 }
591
ea8c405f 592 // loop?
593 while (engineState == PGS_Running)
594 {
595 int modes;
596
597 tval = GetTickCount();
598 if (reset_timing || tval < tval_prev) {
da42200b 599 //stdbg("timing reset");
ea8c405f 600 reset_timing = 0;
601 tval_thissec = tval;
602 frames_shown = frames_done = 0;
603 }
604
605 // show notice message?
606 if (noticeMsgTime) {
607 static int noticeMsgSum;
608 if (tval - noticeMsgTime > 2000) { // > 2.0 sec
609 noticeMsgTime = 0;
610 clearArea(0);
611 notice = 0;
612 } else {
613 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
614 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
615 notice = noticeMsg;
616 }
617 }
618
619 // check for mode changes
620 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
621 if (modes != oldmodes) {
ea8c405f 622 oldmodes = modes;
623 clearArea(1);
624 }
625
626 // second passed?
fa283c9a 627 if (tval - tval_thissec >= sec_ms)
ea8c405f 628 {
629#ifdef BENCHMARK
630 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
631 if(++bench == 10) {
632 bench = 0;
633 bench_fps_s = bench_fps;
634 bf[bfp++ & 3] = bench_fps;
635 bench_fps = 0;
636 }
637 bench_fps += frames_shown;
638 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
639#else
640 if(currentConfig.EmuOpt & 2)
641 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
642#endif
fa283c9a 643 //tval_thissec += 1000;
644 tval_thissec += sec_ms;
ea8c405f 645
fa283c9a 646 if (PsndOut != NULL)
647 {
f3d1de29 648 /* some code which tries to sync things to audio clock, the dirty way */
649 static int audio_skew_prev = 0;
650 int audio_skew, adj, co = 9, shift = 7;
fa283c9a 651 audio_skew = snd_all_samples*2 - FrameworkAudio_BufferPos();
f3d1de29 652 if (PsndRate == 22050) co = 10;
653 if (PsndRate > 22050) co = 11;
654 if (PicoOpt&8) shift++;
655 if (audio_skew < 0) {
656 adj = -((-audio_skew) >> shift);
657 if (audio_skew > -(6<<co)) adj>>=1;
658 if (audio_skew > -(4<<co)) adj>>=1;
659 if (audio_skew > -(2<<co)) adj>>=1;
660 if (audio_skew > audio_skew_prev) adj>>=2; // going up already
661 } else {
662 adj = audio_skew >> shift;
663 if (audio_skew < (6<<co)) adj>>=1;
664 if (audio_skew < (4<<co)) adj>>=1;
665 if (audio_skew < (2<<co)) adj>>=1;
666 if (audio_skew < audio_skew_prev) adj>>=2;
667 }
668 audio_skew_prev = audio_skew;
fa283c9a 669 target_frametime += adj;
670 sec_ms = (target_frametime * target_fps) >> 8;
da42200b 671 //stdbg("%i %i %i", audio_skew, adj, sec_ms);
ea8c405f 672 frames_done = frames_shown = 0;
fa283c9a 673 }
674 else if (currentConfig.Frameskip < 0) {
ea8c405f 675 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
676 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
677 if (frames_shown > frames_done) frames_shown = frames_done;
fa283c9a 678 } else {
679 frames_done = frames_shown = 0;
ea8c405f 680 }
681 }
682#ifdef PFRAMES
683 sprintf(fpsbuff, "%i", Pico.m.frame_count);
684#endif
685
686 tval_prev = tval;
687 lim_time = (frames_done+1) * target_frametime;
688 if (currentConfig.Frameskip >= 0) // frameskip enabled
689 {
690 for (i = 0; i < currentConfig.Frameskip; i++) {
691 updateKeys();
fa283c9a 692 SkipFrame(); frames_done++;
ea8c405f 693 if (PsndOut) { // do framelimitting if sound is enabled
694 int tval_diff;
695 tval = GetTickCount();
696 tval_diff = (int)(tval - tval_thissec) << 8;
697 if (tval_diff < lim_time) // we are too fast
698 simpleWait(tval + ((lim_time - tval_diff)>>8));
699 }
700 lim_time += target_frametime;
701 }
702 }
703 else // auto frameskip
704 {
705 int tval_diff;
706 tval = GetTickCount();
707 tval_diff = (int)(tval - tval_thissec) << 8;
708 if (tval_diff > lim_time)
709 {
710 // no time left for this frame - skip
711 if (tval_diff - lim_time >= (300<<8)) {
712 /* something caused a slowdown for us (disk access? cache flush?)
713 * try to recover by resetting timing... */
714 reset_timing = 1;
715 continue;
716 }
717 updateKeys();
fa283c9a 718 SkipFrame(); frames_done++;
ea8c405f 719 continue;
720 }
721 }
722
723 updateKeys();
724
c77ca79e 725 if (currentConfig.EmuOpt&0x80)
726 /* be sure correct framebuffer is locked */
84100c0f 727 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
c77ca79e 728
ea8c405f 729 PicoFrame();
499a0be3 730
731 if (giz_screen == NULL)
84100c0f 732 giz_screen = fb_lock((currentConfig.EmuOpt&0x8000) ? 0 : 1);
499a0be3 733
ea8c405f 734 blit(fpsbuff, notice);
735
736 if (giz_screen != NULL) {
84100c0f 737 fb_unlock();
ea8c405f 738 giz_screen = NULL;
739 }
740
c77ca79e 741 if (currentConfig.EmuOpt&0x2000)
742 Framework2D_WaitVSync();
743
744 if (currentConfig.EmuOpt&0x8000)
84100c0f 745 fb_flip();
c77ca79e 746
ea8c405f 747 // check time
748 tval = GetTickCount();
749 tval_diff = (int)(tval - tval_thissec) << 8;
750
751 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
752 reset_timing = 1;
753 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
754 {
755 // sleep if we are still too fast
756 if (tval_diff < lim_time)
757 {
758 // we are too fast
759 simpleWait(tval + ((lim_time - tval_diff) >> 8));
760 }
761 }
762
763 frames_done++; frames_shown++;
764 }
765
766
602133e1 767 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
ea8c405f 768
fa283c9a 769 if (PsndOut != NULL) {
770 PsndOut = snd_cbuff = NULL;
771 FrameworkAudio_Close();
772 }
773
ea8c405f 774 // save SRAM
775 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
c77ca79e 776 emu_stateCb("Writing SRAM/BRAM..");
ea8c405f 777 emu_SaveLoadGame(0, 1);
778 SRam.changed = 0;
779 }
780}
781
782
783void emu_ResetGame(void)
784{
1cb1584b 785 PicoReset();
ea8c405f 786 reset_timing = 1;
787}
e5f426aa 788