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