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);
79 printf("%s\n", errmsg);
82 void FCEUI_SetRenderedLines(int ntscf, int ntscl, int palf, int pall)
84 FSettings.UsrFirstSLine[0]=ntscf;
85 FSettings.UsrLastSLine[0]=ntscl;
86 FSettings.UsrFirstSLine[1]=palf;
87 FSettings.UsrLastSLine[1]=pall;
90 FSettings.FirstSLine=FSettings.UsrFirstSLine[1];
91 FSettings.LastSLine=FSettings.UsrLastSLine[1];
95 FSettings.FirstSLine=FSettings.UsrFirstSLine[0];
96 FSettings.LastSLine=FSettings.UsrLastSLine[0];
101 void FCEUI_SetVidSystem(int a)
108 int FCEUI_GetCurrentVidSystem(int *slstart, int *slend)
111 *slstart=FSettings.FirstSLine;
113 *slend=FSettings.LastSLine;
118 void FCEUI_SetNetworkPlay(int type)
120 FSettings.NetworkPlay=type;
124 void FCEUI_SetGameGenie(int a)
126 FSettings.GameGenie=a?1:0;
129 static void CalculatePalette(void);
130 static void ChoosePalette(void);
131 static void WritePalette(void);
137 static uint8 StateShow=0;
146 uint8 CommandQueue=0;
148 static int controlselect=0;
149 static int ntsccol=0;
150 static int ntsctint=46+10;
151 static int ntschue=72;
152 static int controllength=0;
155 static pal *palpoint[8]=
167 void FCEUI_SetSnapName(int a)
169 FSettings.SnapName=a;
172 void FCEUI_SaveExtraDataUnderBase(int a)
174 FSettings.SUnderBase=a;
177 void FCEUI_SetPaletteArray(uint8 *pal)
184 palpoint[0]=palettec;
187 palpoint[0][x].r=*((uint8 *)pal+x+x+x);
188 palpoint[0][x].g=*((uint8 *)pal+x+x+x+1);
189 palpoint[0][x].b=*((uint8 *)pal+x+x+x+2);
195 void FCEUI_SelectState(int w)
197 if(netplay!=2 && FCEUGameInfo.type!=GIT_NSF)
201 void FCEUI_SaveState(void)
203 if(netplay!=2 && FCEUGameInfo.type!=GIT_NSF)
207 void FCEUI_LoadState(void)
209 if(netplay!=2 && FCEUGameInfo.type!=GIT_NSF)
213 int32 FCEUI_GetDesiredFPS(void)
216 return(838977920); // ~50.007
218 return(1008307711); // ~60.1
221 static int dosnapsave=0;
222 void FCEUI_SaveSnapshot(void)
227 /* I like the sounds of breaking necks. */
228 static void ReallySnap(void)
230 int x=SaveSnapshot();
232 FCEU_DispMessage("Error saving screen snapshot.");
234 FCEU_DispMessage("Screen snapshot %d saved.",x-1);
237 void DriverInterface(int w, void *d)
241 case DES_NTSCCOL:ntsccol=*(int *)d;FCEU_ResetPalette();break;
242 case DES_RESET:if(netplay!=2) CommandQueue=30;break;
243 case DES_POWER:if(netplay!=2) CommandQueue=31;break;
244 case DES_GETNTSCTINT:*(int*)d=ntsctint;break;
245 case DES_GETNTSCHUE:*(int*)d=ntschue;break;
246 case DES_SETNTSCTINT:ntsctint=*(int*)d;if(ntsccol)FCEU_ResetPalette();break;
247 case DES_SETNTSCHUE:ntschue=*(int*)d;if(ntsccol)FCEU_ResetPalette();break;
249 case DES_FDSINSERT:if(netplay!=2) CommandQueue=2;break;
250 case DES_FDSEJECT:if(netplay!=2) CommandQueue=3;break;
251 case DES_FDSSELECT:if(netplay!=2) CommandQueue=1;break;
253 case DES_NSFINC:NSFControl(1);break;
254 case DES_NSFDEC:NSFControl(2);break;
255 case DES_NSFRES:NSFControl(0);break;
257 case DES_VSUNIDIPSET:CommandQueue=10+(int)d;break;
258 case DES_VSUNITOGGLEDIPVIEW:CommandQueue=10;break;
259 case DES_VSUNICOIN:CommandQueue=19;break;
260 case DES_NTSCSELHUE:if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF){controlselect=1;controllength=360;}break;
261 case DES_NTSCSELTINT:if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF){controlselect=2;controllength=360;}break;
264 if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF)
271 which=controlselect==1?ntschue:ntsctint;
284 if(ntsccol && FCEUGameInfo.type!=GIT_VSUNI && !PAL && FCEUGameInfo.type!=GIT_NSF)
289 switch(controlselect)
292 if(ntschue>128) ntschue=128;
296 if(ntsctint>128) ntsctint=128;
307 static uint8 lastd=0;
308 void SetNESDeemph(uint8 d, int force)
310 static uint16 rtmul[7]={32768*1.239,32768*.794,32768*1.019,32768*.905,32768*1.023,32768*.741,32768*.75};
311 static uint16 gtmul[7]={32768*.915,32768*1.086,32768*.98,32768*1.026,32768*.908,32768*.987,32768*.75};
312 static uint16 btmul[7]={32768*.743,32768*.882,32768*.653,32768*1.277,32768*.979,32768*.101,32768*.75};
316 /* If it's not forced(only forced when the palette changes),
317 don't waste cpu time if the same deemphasis bits are set as the last call.
324 else /* Only set this when palette has changed. */
342 FCEUD_SetPalette(x|0x40,m,n,o);
347 if(!d) return; /* No deemphasis, so return. */
367 FCEUD_SetPalette(x|0xC0,m,n,o);
374 #define HUEVAL ((double)((double)ntschue/(double)2)+(double)300)
375 #define TINTVAL ((double)((double)ntsctint/(double)128))
377 static void CalculatePalette(void)
382 static uint8 cols[16]={0,24,21,18,15,12,9,6,3,0,33,30,27,0,0,0};
383 static uint8 br1[4]={6,9,12,12};
384 static double br2[4]={.29,.45,.73,.9};
385 static double br3[4]={0,.24,.47,.77};
392 if(z==0) {s=0;y=((double)br1[x])/12;}
401 theta=(double)M_PI*(double)(((double)cols[z]*10+HUEVAL)/(double)180);
402 r=(int)(((double)y+(double)s*(double)sin(theta))*(double)256);
403 g=(int)(((double)y-(double)((double)27/(double)53)*s*(double)sin(theta)+(double)((double)10/(double)53)*s*cos(theta))*(double)256);
404 b=(int)(((double)y-(double)s*(double)cos(theta))*(double)256);
406 // TODO: Fix RGB to compensate for phosphor changes(add to red??).
415 paletten[(x<<4)+z].r=r;
416 paletten[(x<<4)+z].g=g;
417 paletten[(x<<4)+z].b=b;
424 void FCEU_PutImageDummy(void)
426 if(FCEUGameInfo.type!=GIT_NSF)
428 if(controllength) controllength--;
430 if(StateShow) StateShow--; /* DrawState() */
431 if(howlong) howlong--; /* DrawMessage() */
434 extern uint64 frcount;
442 void FCEU_PutImage(void)
444 if(FCEUGameInfo.type==GIT_NSF)
447 /* Save snapshot after NSF screen is drawn. Why would we want to
458 /* Save snapshot before overlay stuff is written. */
464 if(FCEUGameInfo.type==GIT_VSUNI && DIPS&2)
466 if(StateShow) DrawState();
467 if(controllength) {controllength--;DrawBars();}
472 extern uint64 frcount;
479 static int ipalette=0;
481 void LoadGamePalette(void)
486 if((fp=fopen(FCEU_MakeFName(FCEUMKF_PALETTE,0,0),"rb")))
489 fread(ptmp,1,192,fp);
493 palettei[x].r=ptmp[x+x+x];
494 palettei[x].g=ptmp[x+x+x+1];
495 palettei[x].b=ptmp[x+x+x+2];
501 void FCEU_ResetPalette(void)
507 static void ChoosePalette(void)
509 if(FCEUGameInfo.type==GIT_NSF)
513 else if(ntsccol && !PAL && FCEUGameInfo.type!=GIT_VSUNI)
522 void WritePalette(void)
527 FCEUD_SetPalette(x+128,unvpalette[x].r,unvpalette[x].g,unvpalette[x].b);
528 if(FCEUGameInfo.type==GIT_NSF)
531 FCEUD_SetPalette(x,palo[x].r,palo[x].g,palo[x].b);
536 FCEUD_SetPalette(x,palo[x].r,palo[x].g,palo[x].b);
537 SetNESDeemph(lastd,1);
542 void FlushCommandQueue(void)
544 if(!netplay && CommandQueue) {DoCommand(CommandQueue);CommandQueue=0;}
547 void DoCommand(uint8 c)
551 case 1:FDSControl(FDS_SELECT);break;
552 case 2:FDSControl(FDS_IDISK);break;
553 case 3:FDSControl(FDS_EJECT);break;
555 case 10:DIPS^=2;break;
556 case 11:vsdip^=1;DIPS|=2;break;
557 case 12:vsdip^=2;DIPS|=2;break;
558 case 13:vsdip^=4;DIPS|=2;break;
559 case 14:vsdip^=8;DIPS|=2;break;
560 case 15:vsdip^=0x10;DIPS|=2;break;
561 case 16:vsdip^=0x20;DIPS|=2;break;
562 case 17:vsdip^=0x40;DIPS|=2;break;
563 case 18:vsdip^=0x80;DIPS|=2;break;
564 case 19:coinon=6;break;
565 case 30:ResetNES();break;
566 case 31:PowerNES();break;
567 case 40:CheckStates();StateShow=0;SaveState();break;
568 case 41:CheckStates();StateShow=0;LoadState();break;
569 case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49:
570 case 50: case 51:StateShow=180;CurrentState=c-42;CheckStates();break;