-static uint8 lastd=0;
-void SetNESDeemph(uint8 d, int force)
-{
- static uint16 rtmul[7]={32768*1.239,32768*.794,32768*1.019,32768*.905,32768*1.023,32768*.741,32768*.75};
- static uint16 gtmul[7]={32768*.915,32768*1.086,32768*.98,32768*1.026,32768*.908,32768*.987,32768*.75};
- static uint16 btmul[7]={32768*.743,32768*.882,32768*.653,32768*1.277,32768*.979,32768*.101,32768*.75};
- uint32 r,g,b;
- int x;
-
- /* If it's not forced(only forced when the palette changes),
- don't waste cpu time if the same deemphasis bits are set as the last call.
- */
- if(!force)
- {
- if(d==lastd)
- return;
- }
- else /* Only set this when palette has changed. */
- {
- r=rtmul[6];
- g=rtmul[6];
- b=rtmul[6];
-
- for(x=0;x<0x40;x++)
- {
- uint32 m,n,o;
- m=palo[x].r;
- n=palo[x].g;
- o=palo[x].b;
- m=(m*r)>>15;
- n=(n*g)>>15;
- o=(o*b)>>15;
- if(m>0xff) m=0xff;
- if(n>0xff) n=0xff;
- if(o>0xff) o=0xff;
- FCEUD_SetPalette(x|0x40,m,n,o);
-
-
- }
- }
- if(!d) return; /* No deemphasis, so return. */
-
- r=rtmul[d-1];
- g=gtmul[d-1];
- b=btmul[d-1];
-
- for(x=0;x<0x40;x++)
- {
- uint32 m,n,o;
-
- m=palo[x].r;
- n=palo[x].g;
- o=palo[x].b;
- m=(m*r)>>15;
- n=(n*g)>>15;
- o=(o*b)>>15;
- if(m>0xff) m=0xff;
- if(n>0xff) n=0xff;
- if(o>0xff) o=0xff;
-
- FCEUD_SetPalette(x|0xC0,m,n,o);
-
- }
-
- lastd=d;
-}
-
-#define HUEVAL ((double)((double)ntschue/(double)2)+(double)300)
-#define TINTVAL ((double)((double)ntsctint/(double)128))
-
-static void CalculatePalette(void)
-{
- int x,z;
- int r,g,b;
- double s,y,theta;
- static uint8 cols[16]={0,24,21,18,15,12,9,6,3,0,33,30,27,0,0,0};
- static uint8 br1[4]={6,9,12,12};
- static double br2[4]={.29,.45,.73,.9};
- static double br3[4]={0,.24,.47,.77};
-
- for(x=0;x<=3;x++)
- for(z=0;z<16;z++)
- {
- s=(double)TINTVAL;
- y=(double)br2[x];
- if(z==0) {s=0;y=((double)br1[x])/12;}
-
- if(z>=13)
- {
- s=y=0;
- if(z==13)
- y=(double)br3[x];
- }
-
- theta=(double)M_PI*(double)(((double)cols[z]*10+HUEVAL)/(double)180);
- r=(int)(((double)y+(double)s*(double)sin(theta))*(double)256);
- g=(int)(((double)y-(double)((double)27/(double)53)*s*(double)sin(theta)+(double)((double)10/(double)53)*s*cos(theta))*(double)256);
- b=(int)(((double)y-(double)s*(double)cos(theta))*(double)256);
-
- // TODO: Fix RGB to compensate for phosphor changes(add to red??).
-
- if(r>255) r=255;
- if(g>255) g=255;
- if(b>255) b=255;
- if(r<0) r=0;
- if(g<0) g=0;
- if(b<0) b=0;
-
- paletten[(x<<4)+z].r=r;
- paletten[(x<<4)+z].g=g;
- paletten[(x<<4)+z].b=b;
- }
- WritePalette();
-}