1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 1998 BERO
5 * Copyright (C) 2002 Ben Parnell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* SVGA High Level Routines
34 #define M_PI 3.14159265358979323846
55 static char errmsg[65];
57 void FCEU_PrintError(char *format, ...)
64 vsprintf(temp,format,ap);
65 FCEUD_PrintError(temp);
70 void FCEU_DispMessage(char *format, ...)
75 vsprintf(errmsg,format,ap);
81 void FCEUI_SetRenderedLines(int ntscf, int ntscl, int palf, int pall)
83 FSettings.UsrFirstSLine[0]=ntscf;
84 FSettings.UsrLastSLine[0]=ntscl;
85 FSettings.UsrFirstSLine[1]=palf;
86 FSettings.UsrLastSLine[1]=pall;
89 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
90 FSettings.LastSLine=FSettings.UsrLastSLine[1];
94 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
95 FSettings.LastSLine=FSettings.UsrLastSLine[0];
100 void FCEUI_SetVidSystem(int a)
107 int FCEUI_GetCurrentVidSystem(int *slstart, int *slend)
110 *slstart=FSettings.FirstSLine;
112 *slend=FSettings.LastSLine;
117 void FCEUI_SetNetworkPlay(int type)
119 FSettings.NetworkPlay=type;
123 void FCEUI_SetGameGenie(int a)
125 FSettings.GameGenie=a?1:0;
128 static void CalculatePalette(void);
129 static void ChoosePalette(void);
130 static void WritePalette(void);
136 static uint8 StateShow=0;
145 uint8 CommandQueue=0;
147 static int controlselect=0;
148 static int ntsccol=0;
149 static int ntsctint=46+10;
150 static int ntschue=72;
151 static int controllength=0;
154 static pal *palpoint[8]=
166 void FCEUI_SetSnapName(int a)
168 FSettings.SnapName=a;
171 void FCEUI_SaveExtraDataUnderBase(int a)
173 FSettings.SUnderBase=a;
176 void FCEUI_SetPaletteArray(uint8 *pal)
183 palpoint[0]=palettec;
186 palpoint[0][x].r=*((uint8 *)pal+x+x+x);
187 palpoint[0][x].g=*((uint8 *)pal+x+x+x+1);
188 palpoint[0][x].b=*((uint8 *)pal+x+x+x+2);
194 void FCEUI_SelectState(int w)
196 if(netplay!=2 && FCEUGameInfo.type!=GIT_NSF)
200 void FCEUI_SaveState(void)
202 if(netplay!=2 && FCEUGameInfo.type!=GIT_NSF)
206 void FCEUI_LoadState(void)
208 if(netplay!=2 && FCEUGameInfo.type!=GIT_NSF)
212 int32 FCEUI_GetDesiredFPS(void)
215 return(838977920); // ~50.007
217 return(1008307711); // ~60.1
220 static int dosnapsave=0;
221 void FCEUI_SaveSnapshot(void)
226 /* I like the sounds of breaking necks. */
227 static void ReallySnap(void)
229 int x=SaveSnapshot();
231 FCEU_DispMessage("Error saving screen snapshot.");
233 FCEU_DispMessage("Screen snapshot %d saved.",x-1);
236 void DriverInterface(int w, void *d)
240 case DES_NTSCCOL:ntsccol=*(int *)d;FCEU_ResetPalette();break;
241 case DES_RESET:if(netplay!=2) CommandQueue=30;break;
242 case DES_POWER:if(netplay!=2) CommandQueue=31;break;
243 case DES_GETNTSCTINT:*(int*)d=ntsctint;break;
244 case DES_GETNTSCHUE:*(int*)d=ntschue;break;
245 case DES_SETNTSCTINT:ntsctint=*(int*)d;if(ntsccol)FCEU_ResetPalette();break;
246 case DES_SETNTSCHUE:ntschue=*(int*)d;if(ntsccol)FCEU_ResetPalette();break;
248 case DES_FDSINSERT:if(netplay!=2) CommandQueue=2;break;
249 case DES_FDSEJECT:if(netplay!=2) CommandQueue=3;break;
250 case DES_FDSSELECT:if(netplay!=2) CommandQueue=1;break;
252 case DES_NSFINC:NSFControl(1);break;
253 case DES_NSFDEC:NSFControl(2);break;
254 case DES_NSFRES:NSFControl(0);break;
256 case DES_VSUNIDIPSET:CommandQueue=10+(int)d;break;
257 case DES_VSUNITOGGLEDIPVIEW:CommandQueue=10;break;
258 case DES_VSUNICOIN:CommandQueue=19;break;
259 case DES_NTSCSELHUE:if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF){controlselect=1;controllength=360;}break;
260 case DES_NTSCSELTINT:if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF){controlselect=2;controllength=360;}break;
263 if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF)
270 which=controlselect==1?ntschue:ntsctint;
283 if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF)
288 switch(controlselect)
291 if(ntschue>128) ntschue=128;
295 if(ntsctint>128) ntsctint=128;
306 static uint8 lastd=0;
307 void SetNESDeemph(uint8 d, int force)
309 static uint16 rtmul[7]={32768*1.239,32768*.794,32768*1.019,32768*.905,32768*1.023,32768*.741,32768*.75};
310 static uint16 gtmul[7]={32768*.915,32768*1.086,32768*.98,32768*1.026,32768*.908,32768*.987,32768*.75};
311 static uint16 btmul[7]={32768*.743,32768*.882,32768*.653,32768*1.277,32768*.979,32768*.101,32768*.75};
315 /* If it's not forced(only forced when the palette changes),
316 don't waste cpu time if the same deemphasis bits are set as the last call.
323 else /* Only set this when palette has changed. */
341 FCEUD_SetPalette(x|0x40,m,n,o);
346 if(!d) return; /* No deemphasis, so return. */
366 FCEUD_SetPalette(x|0xC0,m,n,o);
373 #define HUEVAL ((double)((double)ntschue/(double)2)+(double)300)
374 #define TINTVAL ((double)((double)ntsctint/(double)128))
376 static void CalculatePalette(void)
381 static uint8 cols[16]={0,24,21,18,15,12,9,6,3,0,33,30,27,0,0,0};
382 static uint8 br1[4]={6,9,12,12};
383 static double br2[4]={.29,.45,.73,.9};
384 static double br3[4]={0,.24,.47,.77};
391 if(z==0) {s=0;y=((double)br1[x])/12;}
400 theta=(double)M_PI*(double)(((double)cols[z]*10+HUEVAL)/(double)180);
401 r=(int)(((double)y+(double)s*(double)sin(theta))*(double)256);
402 g=(int)(((double)y-(double)((double)27/(double)53)*s*(double)sin(theta)+(double)((double)10/(double)53)*s*cos(theta))*(double)256);
403 b=(int)(((double)y-(double)s*(double)cos(theta))*(double)256);
405 // TODO: Fix RGB to compensate for phosphor changes(add to red??).
414 paletten[(x<<4)+z].r=r;
415 paletten[(x<<4)+z].g=g;
416 paletten[(x<<4)+z].b=b;
423 void FCEU_PutImageDummy(void)
425 if(FCEUGameInfo.type!=GIT_NSF)
427 if(controllength) controllength--;
429 if(StateShow) StateShow--; /* DrawState() */
430 if(howlong) howlong--; /* DrawMessage() */
433 extern uint64 frcount;
441 void FCEU_PutImage(void)
443 if(FCEUGameInfo.type==GIT_NSF)
446 /* Save snapshot after NSF screen is drawn. Why would we want to
457 /* Save snapshot before overlay stuff is written. */
463 if(FCEUGameInfo.type==GIT_VSUNI && DIPS&2)
465 if(StateShow) DrawState();
466 if(controllength) {controllength--;DrawBars();}
471 extern uint64 frcount;
478 static int ipalette=0;
480 void LoadGamePalette(void)
485 if((fp=fopen(FCEU_MakeFName(FCEUMKF_PALETTE,0,0),"rb")))
488 fread(ptmp,1,192,fp);
492 palettei[x].r=ptmp[x+x+x];
493 palettei[x].g=ptmp[x+x+x+1];
494 palettei[x].b=ptmp[x+x+x+2];
500 void FCEU_ResetPalette(void)
506 static void ChoosePalette(void)
508 if(FCEUGameInfo.type==GIT_NSF)
512 else if(ntsccol && !PAL && FCEUGameInfo.type!=GIT_VSUNI)
521 void WritePalette(void)
526 FCEUD_SetPalette(x+128,unvpalette[x].r,unvpalette[x].g,unvpalette[x].b);
527 if(FCEUGameInfo.type==GIT_NSF)
530 FCEUD_SetPalette(x,palo[x].r,palo[x].g,palo[x].b);
535 FCEUD_SetPalette(x,palo[x].r,palo[x].g,palo[x].b);
536 SetNESDeemph(lastd,1);
540 void FlushCommandQueue(void)
542 if(!netplay && CommandQueue) {DoCommand(CommandQueue);CommandQueue=0;}
545 void DoCommand(uint8 c)
549 case 1:FDSControl(FDS_SELECT);break;
550 case 2:FDSControl(FDS_IDISK);break;
551 case 3:FDSControl(FDS_EJECT);break;
553 case 10:DIPS^=2;break;
554 case 11:vsdip^=1;DIPS|=2;break;
555 case 12:vsdip^=2;DIPS|=2;break;
556 case 13:vsdip^=4;DIPS|=2;break;
557 case 14:vsdip^=8;DIPS|=2;break;
558 case 15:vsdip^=0x10;DIPS|=2;break;
559 case 16:vsdip^=0x20;DIPS|=2;break;
560 case 17:vsdip^=0x40;DIPS|=2;break;
561 case 18:vsdip^=0x80;DIPS|=2;break;
562 case 19:coinon=6;break;
563 case 30:ResetNES();break;
564 case 31:PowerNES();break;
565 case 40:CheckStates();StateShow=0;SaveState();break;
566 case 41:CheckStates();StateShow=0;LoadState();break;
567 case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49:
568 case 50: case 51:StateShow=180;CurrentState=c-42;CheckStates();break;