giz alpha1 release
[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"
e5f426aa 13#include "emu.h"
ea8c405f 14#include "menu.h"
15#include "giz.h"
16#include "asm_utils.h"
e5f426aa 17
ea8c405f 18#include <Pico/PicoInt.h>
19
20#ifdef BENCHMARK
21#define OSD_FPS_X 220
22#else
23#define OSD_FPS_X 260
24#endif
25
26// main 300K gfx-related buffer. Used by menu and renderers.
27unsigned char gfx_buffer[321*240*2*2];
e5f426aa 28char romFileName[MAX_PATH];
29int engineState;
e5f426aa 30
ea8c405f 31unsigned char *PicoDraw2FB = gfx_buffer; // temporary buffer for alt renderer ( (8+320)*(8+240+8) )
e5f426aa 32int reset_timing = 0;
ea8c405f 33
34static DWORD noticeMsgTime = 0;
35static int osd_fps_x;
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
46void emu_getMainDir(char *dst, int len)
47{
48 if (len > 0) *dst = 0;
49}
50
51static void emu_msg_cb(const char *msg)
52{
53 if (giz_screen == NULL)
54 giz_screen = Framework2D_LockBuffer();
55
56 memset32((int *)((char *)giz_screen + 321*232*2), 0, 321*8*2/4);
57 emu_textOut16(4, 232, msg);
58 noticeMsgTime = GetTickCount() - 2000;
59
60 /* assumption: emu_msg_cb gets called only when something slow is about to happen */
61 reset_timing = 1;
62}
63
64static void emu_state_cb(const char *str)
65{
66 clearArea(0);
67 blit("", str);
68}
69
70static void emu_msg_tray_open(void)
71{
72 strcpy(noticeMsg, "CD tray opened");
73 noticeMsgTime = GetTickCount();
74}
75
76
77void emu_Init(void)
78{
79 // make dirs for saves, cfgs, etc.
2ec14aec 80 mkdir("mds", 0777);
81 mkdir("srm", 0777);
82 mkdir("brm", 0777);
83 mkdir("cfg", 0777);
ea8c405f 84
85 PicoInit();
86 PicoMessage = emu_msg_cb;
87 PicoMCDopenTray = emu_msg_tray_open;
88 PicoMCDcloseTray = menu_loop_tray;
89}
90
91void emu_Deinit(void)
92{
93 // save SRAM
94 if((currentConfig.EmuOpt & 1) && SRam.changed) {
95 emu_SaveLoadGame(0, 1);
96 SRam.changed = 0;
97 }
98
99 if (!(currentConfig.EmuOpt & 0x20)) {
100 FILE *f = fopen(PicoConfigFile, "r+b");
101 if (!f) emu_WriteConfig(0);
102 else {
103 // if we already have config, reload it, except last ROM
104 fseek(f, sizeof(currentConfig.lastRomFile), SEEK_SET);
105 fread(&currentConfig.EmuOpt, 1, sizeof(currentConfig) - sizeof(currentConfig.lastRomFile), f);
106 fseek(f, 0, SEEK_SET);
107 fwrite(&currentConfig, 1, sizeof(currentConfig), f);
108 fflush(f);
109 fclose(f);
110 }
111 }
112
113 PicoExit();
114}
115
116void emu_setDefaultConfig(void)
117{
118 memset(&currentConfig, 0, sizeof(currentConfig));
119 currentConfig.lastRomFile[0] = 0;
120 currentConfig.EmuOpt = 0x1f | 0x600; // | confirm_save, cd_leds
121 currentConfig.PicoOpt = 0x0f | 0xc00; // | cd_pcm, cd_cdda
122 currentConfig.PsndRate = 22050;
123 currentConfig.PicoRegion = 0; // auto
124 currentConfig.PicoAutoRgnOrder = 0x184; // US, EU, JP
fd34fdd0 125 currentConfig.Frameskip = 0;//-1; // auto
ea8c405f 126 currentConfig.volume = 50;
127 currentConfig.KeyBinds[ 2] = 1<<0; // SACB RLDU
128 currentConfig.KeyBinds[ 3] = 1<<1;
129 currentConfig.KeyBinds[ 0] = 1<<2;
130 currentConfig.KeyBinds[ 1] = 1<<3;
131 currentConfig.KeyBinds[ 5] = 1<<4;
132 currentConfig.KeyBinds[ 6] = 1<<5;
133 currentConfig.KeyBinds[ 7] = 1<<6;
2ec14aec 134 currentConfig.KeyBinds[ 4] = 1<<7;
ea8c405f 135 currentConfig.KeyBinds[ 8] = 1<<27; // save state
136 currentConfig.KeyBinds[ 9] = 1<<28; // load state
137 currentConfig.KeyBinds[12] = 1<<29; // vol up
138 currentConfig.KeyBinds[11] = 1<<30; // vol down
139 currentConfig.PicoCDBuffers = 64;
140 currentConfig.scaling = 0;
141}
142
143
144static int EmuScan16(unsigned int num, void *sdata)
145{
146 if (!(Pico.video.reg[1]&8)) num += 8;
147 DrawLineDest = (unsigned short *) giz_screen + 321*(num+1);
148
149 return 0;
150}
151
152static int EmuScan8(unsigned int num, void *sdata)
153{
154 // draw like the fast renderer
155 if (!(Pico.video.reg[1]&8)) num += 8;
2ec14aec 156 HighCol = gfx_buffer + 328*(num+1);
ea8c405f 157
158 return 0;
159}
160
161static void osd_text(int x, int y, const char *text)
162{
163 int len = strlen(text) * 8;
164 int *p, i, h;
165 len = (len+1) >> 1;
166 for (h = 0; h < 8; h++) {
167 p = (int *) ((unsigned short *) giz_screen+x+321*(y+h));
168 p = (int *) ((int)p & ~3); // align
169 for (i = len; i; i--, p++) *p = 0;
170 }
171 emu_textOut16(x, y, text);
172}
173
174
175short localPal[0x100];
176static void (*vidCpy8to16)(void *dest, void *src, short *pal, int lines) = NULL;
177
178static void blit(const char *fps, const char *notice)
179{
180 int emu_opt = currentConfig.EmuOpt;
181
182 if (PicoOpt&0x10) {
183 // 8bit fast renderer
184 if (Pico.m.dirtyPal) {
185 Pico.m.dirtyPal = 0;
186 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
187 }
2ec14aec 188 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224);
ea8c405f 189 } else if (!(emu_opt&0x80)) {
190 // 8bit accurate renderer
191 if (Pico.m.dirtyPal) {
192 Pico.m.dirtyPal = 0;
193 vidConvCpyRGB565(localPal, Pico.cram, 0x40);
2ec14aec 194 if (Pico.video.reg[0xC]&8) { // shadow/hilight mode
ea8c405f 195 //vidConvCpyRGB32sh(localPal+0x40, Pico.cram, 0x40);
196 //vidConvCpyRGB32hi(localPal+0x80, Pico.cram, 0x40); // TODO
2ec14aec 197 blockcpy(localPal+0xc0, localPal+0x40, 0x40*2);
ea8c405f 198 localPal[0xc0] = 0x0600;
199 localPal[0xd0] = 0xc000;
200 localPal[0xe0] = 0x0000; // reserved pixels for OSD
201 localPal[0xf0] = 0xffff;
202 }
203 /* no support
204 else if (rendstatus & 0x20) { // mid-frame palette changes
205 vidConvCpyRGB565(localPal+0x40, HighPal, 0x40);
206 vidConvCpyRGB565(localPal+0x80, HighPal+0x40, 0x40);
207 } */
208 }
209 // TODO...
fd34fdd0 210 //lprintf("vidCpy8to16 %p %p\n", (unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8);
2ec14aec 211 vidCpy8to16((unsigned short *)giz_screen+321*8, PicoDraw2FB+328*8, localPal, 224);
fd34fdd0 212 //lprintf("after vidCpy8to16\n");
ea8c405f 213 }
214
215 if (notice || (emu_opt & 2)) {
216 int h = 232;
217 if (notice) osd_text(4, h, notice);
218 if (emu_opt & 2) osd_text(osd_fps_x, h, fps);
219 }
220// if ((emu_opt & 0x400) && (PicoMCD & 1))
221// cd_leds();
222
223 //gp2x_video_wait_vsync();
224
225 if (!(PicoOpt&0x10)) {
226 if (Pico.video.reg[1] & 8) {
227 if (currentConfig.EmuOpt&0x80)
228 DrawLineDest = (unsigned short *) giz_screen;
229 else
230 HighCol = gfx_buffer;
231 } else {
232 if (currentConfig.EmuOpt&0x80)
233 DrawLineDest = (unsigned short *) giz_screen + 320*8;
234 else
235 HighCol = gfx_buffer + 328*8;
236 }
237 }
238}
239
240// clears whole screen or just the notice area (in all buffers)
241static void clearArea(int full)
242{
243 if (giz_screen == NULL)
244 giz_screen = Framework2D_LockBuffer();
245 if (full) memset32(giz_screen, 0, 320*240*2/4);
246 else memset32((int *)((char *)giz_screen + 320*232*2), 0, 320*8*2/4);
247}
248
249static void vidResetMode(void)
250{
251 void *screen;
252 if (PicoOpt&0x10) {
253 } else if (currentConfig.EmuOpt&0x80) {
254 PicoDrawSetColorFormat(1);
255 PicoScan = EmuScan16;
256 } else {
257 PicoDrawSetColorFormat(0);
258 PicoScan = EmuScan8;
259 }
260 if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {
261 // setup pal for 8-bit modes
262 localPal[0xc0] = 0x0600;
263 localPal[0xd0] = 0xc000;
264 localPal[0xe0] = 0x0000; // reserved pixels for OSD
265 localPal[0xf0] = 0xffff;
266 }
267 Pico.m.dirtyPal = 1;
268 screen = Framework2D_LockBuffer();
269 memset32(screen, 0, 320*240*2/4);
270 Framework2D_UnlockBuffer();
271 giz_screen = NULL;
272}
273
274
275static void SkipFrame(int do_audio)
276{
277 PicoSkipFrame=do_audio ? 1 : 2;
278 PicoFrame();
279 PicoSkipFrame=0;
280}
281
282void emu_forcedFrame(void)
283{
284 // TODO
285}
286
287static void updateKeys(void)
288{
2ec14aec 289 unsigned int keys, allActions[2] = { 0, 0 }, events;
290 static unsigned int prevEvents = 0;
291 int i;
292
293 keys = Framework_PollGetButtons();
294 if (keys & BTN_HOME) {
295 engineState = PGS_Menu;
296 // wait until select is released, so menu would not resume game
297 while (Framework_PollGetButtons() & BTN_HOME) Sleep(50);
298 }
299
300 keys &= CONFIGURABLE_KEYS;
301
302 for (i = 0; i < 32; i++)
303 {
304 if (keys & (1 << i)) {
305 int pl, acts = currentConfig.KeyBinds[i];
306 if (!acts) continue;
307 pl = (acts >> 16) & 1;
308 /* TODO if (combo_keys & (1 << i)) {
309 int u = i+1, acts_c = acts & combo_acts;
310 // let's try to find the other one
311 if (acts_c)
312 for (; u < 32; u++)
313 if ( (currentConfig.KeyBinds[u] & acts_c) && (keys & (1 << u)) ) {
314 allActions[pl] |= acts_c;
315 keys &= ~((1 << i) | (1 << u));
316 break;
317 }
318 // add non-combo actions if combo ones were not found
319 if (!acts_c || u == 32)
320 allActions[pl] |= acts & ~combo_acts;
321 } else */ {
322 allActions[pl] |= acts;
323 }
324 }
325 }
326
327 PicoPad[0] = (unsigned short) allActions[0];
328 PicoPad[1] = (unsigned short) allActions[1];
329
330 events = (allActions[0] | allActions[1]) >> 16;
331
332 // volume is treated in special way and triggered every frame
333 if (events & 0x6000) {
334 int vol = currentConfig.volume;
335 if (events & 0x2000) {
336 if (vol < 100) vol++;
337 } else {
338 if (vol > 0) vol--;
339 }
340 //gp2x_sound_volume(vol, vol);
341 sprintf(noticeMsg, "VOL: %02i", vol);
342 noticeMsgTime = GetTickCount();
343 currentConfig.volume = vol;
344 }
345
346 events &= ~prevEvents;
347 //if (events) RunEvents(events); // TODO
348 if (movie_data) emu_updateMovie();
349
350 prevEvents = (allActions[0] | allActions[1]) >> 16;
ea8c405f 351}
352
353static void simpleWait(DWORD until)
354{
355}
356
357void emu_Loop(void)
358{
359 //static int PsndRate_old = 0, PicoOpt_old = 0, PsndLen_real = 0, pal_old = 0;
360 char fpsbuff[24]; // fps count c string
361 DWORD tval, tval_prev = 0, tval_thissec = 0; // timing
362 int frames_done = 0, frames_shown = 0, oldmodes = 0;
363 int target_fps, target_frametime, lim_time, tval_diff, i;
364 char *notice = NULL;
365
366 lprintf("entered emu_Loop()\n");
367
368 fpsbuff[0] = 0;
369
370 // make sure we are in correct mode
371 vidResetMode();
372 if (currentConfig.scaling) PicoOpt|=0x4000;
373 else PicoOpt&=~0x4000;
374 Pico.m.dirtyPal = 1;
375 oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;
376 //find_combos(); // TODO
377
378 // pal/ntsc might have changed, reset related stuff
379 target_fps = Pico.m.pal ? 50 : 60;
380 target_frametime = (1000<<8)/target_fps;
381 reset_timing = 1;
382
383 // prepare sound stuff
384/* if (currentConfig.EmuOpt & 4) {
385 int snd_excess_add;
386 if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
387 sound_rerate(Pico.m.frame_count ? 1 : 0);
388 }
389 snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
390 lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
391 PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);
392 gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);
393 gp2x_sound_volume(currentConfig.volume, currentConfig.volume);
394 PicoWriteSound = updateSound;
395 memset(sndBuffer, 0, sizeof(sndBuffer));
396 PsndOut = sndBuffer;
397 PsndRate_old = PsndRate;
398 PsndLen_real = PsndLen;
399 PicoOpt_old = PicoOpt;
400 pal_old = Pico.m.pal;
401 } else*/ {
402 PsndOut = 0;
403 }
404
405 // prepare CD buffer
406 if (PicoMCD & 1) PicoCDBufferInit();
407
408 // loop?
409 while (engineState == PGS_Running)
410 {
411 int modes;
412
413 tval = GetTickCount();
414 if (reset_timing || tval < tval_prev) {
415 reset_timing = 0;
416 tval_thissec = tval;
417 frames_shown = frames_done = 0;
418 }
419
420 // show notice message?
421 if (noticeMsgTime) {
422 static int noticeMsgSum;
423 if (tval - noticeMsgTime > 2000) { // > 2.0 sec
424 noticeMsgTime = 0;
425 clearArea(0);
426 notice = 0;
427 } else {
428 int sum = noticeMsg[0]+noticeMsg[1]+noticeMsg[2];
429 if (sum != noticeMsgSum) { clearArea(0); noticeMsgSum = sum; }
430 notice = noticeMsg;
431 }
432 }
433
434 // check for mode changes
435 modes = ((Pico.video.reg[12]&1)<<2)|(Pico.video.reg[1]&8);
436 if (modes != oldmodes) {
437 osd_fps_x = OSD_FPS_X;
438 //if (modes & 4)
439 vidCpy8to16 = vidCpy8to16_40;
440 //else
441 // vidCpy8to16 = vidCpy8to16_32col;
442 oldmodes = modes;
443 clearArea(1);
444 }
445
446 // second passed?
447 if (tval - tval_thissec >= 1000)
448 {
449#ifdef BENCHMARK
450 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
451 if(++bench == 10) {
452 bench = 0;
453 bench_fps_s = bench_fps;
454 bf[bfp++ & 3] = bench_fps;
455 bench_fps = 0;
456 }
457 bench_fps += frames_shown;
458 sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
459#else
460 if(currentConfig.EmuOpt & 2)
461 sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);
462#endif
463 tval_thissec = tval;
464
465 if (PsndOut == 0 && currentConfig.Frameskip >= 0) {
466 frames_done = frames_shown = 0;
467 } else {
468 // it is quite common for this implementation to leave 1 fame unfinished
469 // when second changes, but we don't want buffer to starve.
470 if (PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
471 updateKeys();
472 SkipFrame(1); frames_done++;
473 }
474
475 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
476 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
477 if (frames_shown > frames_done) frames_shown = frames_done;
478 }
479 }
480#ifdef PFRAMES
481 sprintf(fpsbuff, "%i", Pico.m.frame_count);
482#endif
483
484 tval_prev = tval;
485 lim_time = (frames_done+1) * target_frametime;
486 if (currentConfig.Frameskip >= 0) // frameskip enabled
487 {
488 for (i = 0; i < currentConfig.Frameskip; i++) {
489 updateKeys();
490 SkipFrame(1); frames_done++;
491 if (PsndOut) { // do framelimitting if sound is enabled
492 int tval_diff;
493 tval = GetTickCount();
494 tval_diff = (int)(tval - tval_thissec) << 8;
495 if (tval_diff < lim_time) // we are too fast
496 simpleWait(tval + ((lim_time - tval_diff)>>8));
497 }
498 lim_time += target_frametime;
499 }
500 }
501 else // auto frameskip
502 {
503 int tval_diff;
504 tval = GetTickCount();
505 tval_diff = (int)(tval - tval_thissec) << 8;
506 if (tval_diff > lim_time)
507 {
508 // no time left for this frame - skip
509 if (tval_diff - lim_time >= (300<<8)) {
510 /* something caused a slowdown for us (disk access? cache flush?)
511 * try to recover by resetting timing... */
512 reset_timing = 1;
513 continue;
514 }
515 updateKeys();
516 SkipFrame(tval_diff < lim_time+target_frametime*2); frames_done++;
517 continue;
518 }
519 }
520
521 updateKeys();
522
523 if (giz_screen == NULL)
524 giz_screen = Framework2D_LockBuffer();
525
526 PicoFrame();
527 blit(fpsbuff, notice);
528
529 if (giz_screen != NULL) {
530 Framework2D_UnlockBuffer();
531 giz_screen = NULL;
532 }
fd34fdd0 533 //lprintf("after unlock\n");
ea8c405f 534
535 // check time
536 tval = GetTickCount();
537 tval_diff = (int)(tval - tval_thissec) << 8;
538
539 if (currentConfig.Frameskip < 0 && tval_diff - lim_time >= (300<<8)) // slowdown detection
540 reset_timing = 1;
541 else if (PsndOut != NULL || currentConfig.Frameskip < 0)
542 {
543 // sleep if we are still too fast
544 if (tval_diff < lim_time)
545 {
546 // we are too fast
547 simpleWait(tval + ((lim_time - tval_diff) >> 8));
548 }
549 }
550
551 frames_done++; frames_shown++;
552 }
553
554
555 if (PicoMCD & 1) PicoCDBufferFree();
556
557 // save SRAM
558 if ((currentConfig.EmuOpt & 1) && SRam.changed) {
559 emu_state_cb("Writing SRAM/BRAM..");
560 emu_SaveLoadGame(0, 1);
561 SRam.changed = 0;
562 }
563}
564
565
566void emu_ResetGame(void)
567{
568 PicoReset(0);
569 reset_timing = 1;
570}
e5f426aa 571