1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "../common/vidblit.h"
38 int stretch_offset=32;
40 unsigned long framesRendered;
42 unsigned long fps_samplecount=0;
43 unsigned long total_fps=-1;
46 pthread_t gp2x_video_thread=0;
47 uint8 * XBufHelper=NULL;
50 #define _sline srendline
51 #define _eline erendline
60 static int exs,eys,eefx;
61 #define NWIDTH (256-((eoptions&EO_CLIPSIDES)?16:0))
62 #define NOFFSET (eoptions&EO_CLIPSIDES?8:0)
68 static int screenSizeInBytes = 320 * 240;
75 static unsigned char fontdata8x8[] =
77 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
78 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
79 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
80 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
81 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
82 0x3C,0x42,0x99,0xBD,0xBD,0x99,0x42,0x3C,0x3C,0x42,0x81,0x81,0x81,0x81,0x42,0x3C,
83 0xFE,0x82,0x8A,0xD2,0xA2,0x82,0xFE,0x00,0xFE,0x82,0x82,0x82,0x82,0x82,0xFE,0x00,
84 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x64,0x74,0x7C,0x38,0x00,0x00,
85 0x80,0xC0,0xF0,0xFC,0xF0,0xC0,0x80,0x00,0x01,0x03,0x0F,0x3F,0x0F,0x03,0x01,0x00,
86 0x18,0x3C,0x7E,0x18,0x7E,0x3C,0x18,0x00,0xEE,0xEE,0xEE,0xCC,0x00,0xCC,0xCC,0x00,
87 0x00,0x00,0x30,0x68,0x78,0x30,0x00,0x00,0x00,0x38,0x64,0x74,0x7C,0x38,0x00,0x00,
88 0x3C,0x66,0x7A,0x7A,0x7E,0x7E,0x3C,0x00,0x0E,0x3E,0x3A,0x22,0x26,0x6E,0xE4,0x40,
89 0x18,0x3C,0x7E,0x3C,0x3C,0x3C,0x3C,0x00,0x3C,0x3C,0x3C,0x3C,0x7E,0x3C,0x18,0x00,
90 0x08,0x7C,0x7E,0x7E,0x7C,0x08,0x00,0x00,0x10,0x3E,0x7E,0x7E,0x3E,0x10,0x00,0x00,
91 0x58,0x2A,0xDC,0xC8,0xDC,0x2A,0x58,0x00,0x24,0x66,0xFF,0xFF,0x66,0x24,0x00,0x00,
92 0x00,0x10,0x10,0x38,0x38,0x7C,0xFE,0x00,0xFE,0x7C,0x38,0x38,0x10,0x10,0x00,0x00,
93 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x1C,0x1C,0x18,0x00,0x18,0x18,0x00,
94 0x6C,0x6C,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x28,0x7C,0x28,0x7C,0x28,0x00,0x00,
95 0x10,0x38,0x60,0x38,0x0C,0x78,0x10,0x00,0x40,0xA4,0x48,0x10,0x24,0x4A,0x04,0x00,
96 0x18,0x34,0x18,0x3A,0x6C,0x66,0x3A,0x00,0x18,0x18,0x20,0x00,0x00,0x00,0x00,0x00,
97 0x30,0x60,0x60,0x60,0x60,0x60,0x30,0x00,0x0C,0x06,0x06,0x06,0x06,0x06,0x0C,0x00,
98 0x10,0x54,0x38,0x7C,0x38,0x54,0x10,0x00,0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00,
99 0x00,0x00,0x00,0x00,0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,
100 0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x04,0x08,0x10,0x20,0x40,0x00,0x00,
101 0x38,0x4C,0xC6,0xC6,0xC6,0x64,0x38,0x00,0x18,0x38,0x18,0x18,0x18,0x18,0x7E,0x00,
102 0x7C,0xC6,0x0E,0x3C,0x78,0xE0,0xFE,0x00,0x7E,0x0C,0x18,0x3C,0x06,0xC6,0x7C,0x00,
103 0x1C,0x3C,0x6C,0xCC,0xFE,0x0C,0x0C,0x00,0xFC,0xC0,0xFC,0x06,0x06,0xC6,0x7C,0x00,
104 0x3C,0x60,0xC0,0xFC,0xC6,0xC6,0x7C,0x00,0xFE,0xC6,0x0C,0x18,0x30,0x30,0x30,0x00,
105 0x78,0xC4,0xE4,0x78,0x86,0x86,0x7C,0x00,0x7C,0xC6,0xC6,0x7E,0x06,0x0C,0x78,0x00,
106 0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x18,0x18,0x30,
107 0x1C,0x38,0x70,0xE0,0x70,0x38,0x1C,0x00,0x00,0x7C,0x00,0x00,0x7C,0x00,0x00,0x00,
108 0x70,0x38,0x1C,0x0E,0x1C,0x38,0x70,0x00,0x7C,0xC6,0xC6,0x1C,0x18,0x00,0x18,0x00,
109 0x3C,0x42,0x99,0xA1,0xA5,0x99,0x42,0x3C,0x38,0x6C,0xC6,0xC6,0xFE,0xC6,0xC6,0x00,
110 0xFC,0xC6,0xC6,0xFC,0xC6,0xC6,0xFC,0x00,0x3C,0x66,0xC0,0xC0,0xC0,0x66,0x3C,0x00,
111 0xF8,0xCC,0xC6,0xC6,0xC6,0xCC,0xF8,0x00,0xFE,0xC0,0xC0,0xFC,0xC0,0xC0,0xFE,0x00,
112 0xFE,0xC0,0xC0,0xFC,0xC0,0xC0,0xC0,0x00,0x3E,0x60,0xC0,0xCE,0xC6,0x66,0x3E,0x00,
113 0xC6,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0x00,0x7E,0x18,0x18,0x18,0x18,0x18,0x7E,0x00,
114 0x06,0x06,0x06,0x06,0xC6,0xC6,0x7C,0x00,0xC6,0xCC,0xD8,0xF0,0xF8,0xDC,0xCE,0x00,
115 0x60,0x60,0x60,0x60,0x60,0x60,0x7E,0x00,0xC6,0xEE,0xFE,0xFE,0xD6,0xC6,0xC6,0x00,
116 0xC6,0xE6,0xF6,0xFE,0xDE,0xCE,0xC6,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,
117 0xFC,0xC6,0xC6,0xC6,0xFC,0xC0,0xC0,0x00,0x7C,0xC6,0xC6,0xC6,0xDE,0xCC,0x7A,0x00,
118 0xFC,0xC6,0xC6,0xCE,0xF8,0xDC,0xCE,0x00,0x78,0xCC,0xC0,0x7C,0x06,0xC6,0x7C,0x00,
119 0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,
120 0xC6,0xC6,0xC6,0xEE,0x7C,0x38,0x10,0x00,0xC6,0xC6,0xD6,0xFE,0xFE,0xEE,0xC6,0x00,
121 0xC6,0xEE,0x3C,0x38,0x7C,0xEE,0xC6,0x00,0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x00,
122 0xFE,0x0E,0x1C,0x38,0x70,0xE0,0xFE,0x00,0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00,
123 0x60,0x60,0x30,0x18,0x0C,0x06,0x06,0x00,0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00,
124 0x18,0x3C,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
125 0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x06,0x3E,0x66,0x66,0x3C,0x00,
126 0x60,0x7C,0x66,0x66,0x66,0x66,0x7C,0x00,0x00,0x3C,0x66,0x60,0x60,0x66,0x3C,0x00,
127 0x06,0x3E,0x66,0x66,0x66,0x66,0x3E,0x00,0x00,0x3C,0x66,0x66,0x7E,0x60,0x3C,0x00,
128 0x1C,0x30,0x78,0x30,0x30,0x30,0x30,0x00,0x00,0x3E,0x66,0x66,0x66,0x3E,0x06,0x3C,
129 0x60,0x7C,0x76,0x66,0x66,0x66,0x66,0x00,0x18,0x00,0x38,0x18,0x18,0x18,0x18,0x00,
130 0x0C,0x00,0x1C,0x0C,0x0C,0x0C,0x0C,0x38,0x60,0x60,0x66,0x6C,0x78,0x6C,0x66,0x00,
131 0x38,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0xEC,0xFE,0xFE,0xFE,0xD6,0xC6,0x00,
132 0x00,0x7C,0x76,0x66,0x66,0x66,0x66,0x00,0x00,0x3C,0x66,0x66,0x66,0x66,0x3C,0x00,
133 0x00,0x7C,0x66,0x66,0x66,0x7C,0x60,0x60,0x00,0x3E,0x66,0x66,0x66,0x3E,0x06,0x06,
134 0x00,0x7E,0x70,0x60,0x60,0x60,0x60,0x00,0x00,0x3C,0x60,0x3C,0x06,0x66,0x3C,0x00,
135 0x30,0x78,0x30,0x30,0x30,0x30,0x1C,0x00,0x00,0x66,0x66,0x66,0x66,0x6E,0x3E,0x00,
136 0x00,0x66,0x66,0x66,0x66,0x3C,0x18,0x00,0x00,0xC6,0xD6,0xFE,0xFE,0x7C,0x6C,0x00,
137 0x00,0x66,0x3C,0x18,0x3C,0x66,0x66,0x00,0x00,0x66,0x66,0x66,0x66,0x3E,0x06,0x3C,
138 0x00,0x7E,0x0C,0x18,0x30,0x60,0x7E,0x00,0x0E,0x18,0x0C,0x38,0x0C,0x18,0x0E,0x00,
139 0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x00,0x70,0x18,0x30,0x1C,0x30,0x18,0x70,0x00,
140 0x00,0x00,0x76,0xDC,0x00,0x00,0x00,0x00,0x10,0x28,0x10,0x54,0xAA,0x44,0x00,0x00,
143 static void gp2x_text(unsigned char *screen, int x, int y, char *text, int color, int flip)
148 screen=screen+x+y*320;
155 screen[l*320+0]=(fontdata8x8[((text[i])*8)+l]&0x80)?color:screen[l*320+0];
156 screen[l*320+1]=(fontdata8x8[((text[i])*8)+l]&0x40)?color:screen[l*320+1];
157 screen[l*320+2]=(fontdata8x8[((text[i])*8)+l]&0x20)?color:screen[l*320+2];
158 screen[l*320+3]=(fontdata8x8[((text[i])*8)+l]&0x10)?color:screen[l*320+3];
159 screen[l*320+4]=(fontdata8x8[((text[i])*8)+l]&0x08)?color:screen[l*320+4];
160 screen[l*320+5]=(fontdata8x8[((text[i])*8)+l]&0x04)?color:screen[l*320+5];
161 screen[l*320+6]=(fontdata8x8[((text[i])*8)+l]&0x02)?color:screen[l*320+6];
162 screen[l*320+7]=(fontdata8x8[((text[i])*8)+l]&0x01)?color:screen[l*320+7];
172 void CleanSurface(void)
178 memset (gp2x_screen8, 0x80, screenSizeInBytes);
185 x=screen->pitch*screen->h;
187 if(SDL_MUSTLOCK(screen))
188 SDL_LockSurface(screen);
190 memset((uint8*)screen->pixels, 0x3F, x);
192 if(SDL_MUSTLOCK(screen))
193 SDL_UnlockSurface(screen);
195 SDL_UpdateRect(screen, 0, 0, 0, 0);
212 SDL_QuitSubSystem(SDL_INIT_VIDEO);
224 fps_str[0]='5'; fps_str[1]='0'; fps_str[2]=0;
231 puts("Initialized GP2X VIDEO via minimal");
239 const SDL_VideoInfo *vinf;
243 if(_fullscreen && _fshack)
244 setenv("SDL_VIDEODRIVER",_fshack,1);
248 unsetenv("SDL_VIDEODRIVER");
250 setenv("SDL_VIDEODRIVER",_fshacksave,1);
253 if(SDL_InitSubSystem(SDL_INIT_VIDEO)==-1)
255 puts(SDL_GetError());
261 tlines=_eline-_sline+1;
263 vinf=SDL_GetVideoInfo();
265 if(vinf->hw_available)
266 flags|=SDL_HWSURFACE;
269 flags|=SDL_FULLSCREEN;
270 flags|=SDL_HWPALETTE;
277 if(_xres<NWIDTH*exs || _yres<tlines*eys)
279 puts("xscale and/or yscale out of bounds.");
283 screen = SDL_SetVideoMode(_xres, _yres, 8, flags);
290 screen = SDL_SetVideoMode(NWIDTH*exs, tlines*eys, 8, flags);
294 puts(SDL_GetError());
301 SDL_WM_SetCaption("FCE Ultra","FCE Ultra");
303 printf("srendline %d, erendline %d\n", srendline, erendline);
312 _fullscreen=!_fullscreen;
316 _fullscreen=!_fullscreen;
319 puts("Gah, bailing out.");
328 static SDL_Color psdl[256];
330 void FCEUD_SetPalette(uint8 index, uint8 r, uint8 g, uint8 b)
340 void FCEUD_GetPalette(uint8 index, uint8 *r, uint8 *g, uint8 *b)
347 static void RedoPalette(void)
349 SDL_SetPalette(screen,SDL_PHYSPAL,psdl,0,256);
355 void FCEUD_SetPalette(uint8 index, uint8 r, uint8 g, uint8 b)
357 gp2x_video_color8(index, r, g, b);
358 gp2x_video_setpalette();
363 void FCEUD_GetPalette(uint8 index, uint8 * r, uint8 * g, uint8 * b)
365 *r = (uint8) gp2x_palette[(index << 1) + 1];
366 *g = (uint8) (gp2x_palette[(index << 1) + 0] >> 8);
367 *b = (uint8) gp2x_palette[(index << 1) + 0];
370 static void RedoPalette(void)
376 unsigned int gp2x_ticks_per_ms=7372800;
386 INLINE void printFps(uint8 *screen)
393 if (stretch_offset > 0)
399 int* dest=(int *) (screen+((y << 8) + (y << 6))+stretch_offset);
402 destt[0]=0x3F3F3F3F; destt[1]=0x3F3F3F3F; destt[2]=0x3F3F3F3F; destt[3]=0x3F3F3F3F;
403 destt[4]=0x3F3F3F3F; destt[5]=0x3F3F3F3F; destt[6]=0x3F3F3F3F; destt[7]=0x3F3F3F3F;
406 destt[0]=0x3F3F3F3F; destt[1]=0x3F3F3F3F; destt[2]=0x3F3F3F3F; destt[3]=0x3F3F3F3F;
407 destt[4]=0x3F3F3F3F; destt[5]=0x3F3F3F3F; destt[6]=0x3F3F3F3F; destt[7]=0x3F3F3F3F;
411 gp2x_text(screen, 0, 0, fps_str, FPS_COLOR, 0);
415 if (needfpsflip <= 3)
419 gp2x_text(screen, 0, 0, fps_str, FPS_COLOR, 0);
429 if ((fps < 59) && doskipping && ((framesRendered % skipval) == (skipval-1)))
434 if ((framesRendered & 0x3F) != 0x3F)
439 unsigned long currTime=gp2x_timer_read_ms();
440 fps= (unsigned long)( framesRendered * 7372800 / (currTime - ticks));
449 if (stretch_offset && showfps)
451 sprintf(fps_str, "%d", (int)fps);
454 gp2x_text(screen, 0, 0, fps_str, stretch_offset, 0);
468 printf("skipping every %dth frame, currfps=%d\n", skipval, fps);
493 void UnlockConsole(){}
498 void BlitScreen(uint8 * XBuf)
511 int* dest=(int *) (gp2x_screen8+((y << 8) + (y << 6))+stretch_offset);
513 int* src=(int *) (XBuf+yinc);
524 gp2x_video_setpalette();
528 printFps(gp2x_screen8);
536 extern void SpeedThrottle();
537 void BlitScreen(uint8 *XBuf)
552 if(SDL_MUSTLOCK(screen))
553 SDL_LockSurface(screen);
559 xo=(((screen->w-NWIDTH*exs))>>1);
561 if(screen->h>(tlines*eys))
563 yo=((screen->h-tlines*eys)>>1);
564 dest+=yo*screen->pitch;
568 Blit8To8(XBuf+NOFFSET,dest, NWIDTH, tlines, screen->pitch,exs,eys,eefx);
570 if(SDL_MUSTLOCK(screen))
571 SDL_UnlockSurface(screen);
573 SDL_UpdateRect(screen, xo, yo, NWIDTH*exs, tlines*eys);
577 gp2x_text(NULL, 0, 0, NULL, 0, 0);
581 uint32 PtoV(uint16 x, uint16 y)
589 if(eoptions&EO_CLIPSIDES)