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
21 /********************************************************/
24 /******* Sound emulation code and waveform synthesis */
25 /******* routines. A few ideas were inspired */
26 /******* by code from Marat Fayzullin's EMUlib */
28 /********************************************************/
42 uint32 Wave[2048+512];
43 int16 WaveFinalMono[2048+512];
45 EXPSOUND GameExpSound={0,0,0,0,0,0};
54 static int32 count[5];
55 static int32 sqacc[2]={0,0};
63 static int32 lengthcount[4];
67 static const uint8 Slengthtable[0x20]=
69 0x5,0x7f,0xA,0x1,0x14,0x2,0x28,0x3,0x50,0x4,0x1E,0x5,0x7,0x6,0x0E,0x7,
70 0x6,0x08,0xC,0x9,0x18,0xa,0x30,0xb,0x60,0xc,0x24,0xd,0x8,0xe,0x10,0xf
73 static uint32 lengthtable[0x20];
75 static const uint32 SNoiseFreqTable[0x10]=
77 2,4,8,0x10,0x20,0x30,0x40,0x50,0x65,0x7f,0xbe,0xfe,0x17d,0x1fc,0x3f9,0x7f2
79 static uint32 NoiseFreqTable[0x10];
86 static const uint8 NTSCPCMTable[0x10]=
88 0xd6,0xbe,0xaa,0xa0,0x8f,0x7f,0x71,0x6b,
89 0x5f,0x50,0x47,0x40,0x35,0x2a,0x24,0x1b
92 static const uint8 PALPCMTable[0x10]= // These values are just guessed.
94 0xc6,0xb0,0x9d,0x94,0x84,0x75,0x68,0x63,
95 0x58,0x4a,0x41,0x3b,0x31,0x27,0x21,0x19
101 // $4011 - Actual data outputted
102 // $4012 - Address register: $c000 + V*64
103 // $4013 - Size register: Size in bytes = (V+1)*64
106 static int32 PCMacc=0;
110 uint32 PCMAddressIndex=0;
111 int32 PCMSizeIndex=0;
115 static void Dummyfunc(int end) {};
117 static void (*DoNoise)(int end)=Dummyfunc;
118 static void (*DoTriangle)(int end)=Dummyfunc;
119 static void (*DoPCM)(int end)=Dummyfunc;
120 static void (*DoSQ1)(int end)=Dummyfunc;
121 static void (*DoSQ2)(int end)=Dummyfunc;
123 static void CalcDPCMIRQ(void)
130 freq=(PALPCMTable[PSG[0x10]&0xF]<<4);
132 freq=(NTSCPCMTable[PSG[0x10]&0xF]<<4);
134 cycles=(((PSG[0x13]<<4)+1));
136 honk=((PSG[0x13]<<4)+1)*freq;
139 //else honk/=(double)113.66666666;
141 //PCMIRQCount=honk*3; //180;
142 //if(PAL) PCMIRQCount*=.93;
146 static void PrepDPCM()
148 PCMAddressIndex=0x4000+(PSG[0x12]<<6);
149 PCMSizeIndex=(PSG[0x13]<<4)+1;
151 //PCMBuffer=ARead[0x8000+PCMAddressIndex](0x8000+PCMAddressIndex);
153 PCMfreq=PALPCMTable[PSG[0x10]&0xF];
155 PCMfreq=NTSCPCMTable[PSG[0x10]&0xF];
159 uint8 sweepon[2]={0,0};
160 int32 curfreq[2]={0,0};
166 uint8 DecCountTo1[3];
174 /* Instantaneous? Maybe the new freq value is being calculated all of the time... */
175 static int FASTAPASS(2) CheckFreq(uint32 cf, uint8 sr)
187 static DECLFW(Write0x11)
193 static uint8 DutyCount[2]={0,0};
195 static DECLFW(Write_PSG)
197 //if((A>=0x4004 && A<=0x4007) || A==0x4015)
198 //printf("$%04x:$%02x, %d\n",A,V,SOUNDTS);
219 lengthcount[0]=lengthtable[(V>>3)&0x1f];
222 sweepon[0]=PSG[1]&0x80;
223 curfreq[0]=PSG[0x2]|((V&7)<<8);
225 DecCountTo1[0]=(PSG[0]&0xF)+1;
226 SweepCount[0]=((PSG[0x1]>>4)&7)+1;
228 sqacc[0]=((int32)curfreq[0]+1)<<18;
248 lengthcount[1]=lengthtable[(V>>3)&0x1f];
251 sweepon[1]=PSG[0x5]&0x80;
252 curfreq[1]=PSG[0x6]|((V&7)<<8);
254 DecCountTo1[1]=(PSG[0x4]&0xF)+1;
255 SweepCount[1]=((PSG[0x5]>>4)&7)+1;
257 sqacc[1]=((int32)curfreq[1]+1)<<18;
270 case 0xa:DoTriangle(0);
277 lengthcount[2]=lengthtable[(V>>3)&0x1f];
280 tricoop=PSG[0x8]&0x7f;
281 trimode=PSG[0x8]&0x80;
287 case 0xE:DoNoise(0);break;
293 lengthcount[3]=lengthtable[(V>>3)&0x1f];
296 DecCountTo1[2]=(PSG[0xC]&0xF)+1;
300 X6502_IRQEnd(FCEU_IQDPCM);
319 if(!(PSG[0x15]&0x10))
329 X6502_IRQEnd(FCEU_IQDPCM);
341 ret=(PSG[0x15]&(sqnon|0x10))|SIRQStat;
343 X6502_IRQEnd(/*FCEU_IQDPCM|*/FCEU_IQFCOUNT);
347 DECLFR(Read_PSGDummy)
351 ret=(PSG[0x15]&sqnon)|SIRQStat;
353 X6502_IRQEnd(/*FCEU_IQDPCM|*/FCEU_IQFCOUNT);
357 static void FASTAPASS(1) FrameSoundStuff(int V)
360 uint32 end = (SOUNDTS<<16)/soundtsinc;
368 case 1: /* Envelope decay, linear counter, length counter, freq sweep */
369 if(PSG[0x15]&4 && sqnon&4)
375 if(lengthcount[2]<=0)
385 if(PSG[0x15]&(P+1) && sqnon&(P+1))
387 if(!(PSG[P<<2]&0x20))
392 if(lengthcount[P]<=0)
399 /* Frequency Sweep Code Here */
401 /* xxxx = hz. 120/(x+1)*/
406 if(SweepCount[P]>0) SweepCount[P]--;
409 SweepCount[P]=((PSG[(P<<2)+0x1]>>4)&7)+1; //+1;
411 if(PSG[(P<<2)+0x1]&0x8)
413 mod-=(P^1)+((curfreq[P])>>(PSG[(P<<2)+0x1]&7));
415 if(curfreq[P] && (PSG[(P<<2)+0x1]&7)/* && sweepon[P]&0x80*/)
422 mod=curfreq[P]>>(PSG[(P<<2)+0x1]&7);
423 if((mod+curfreq[P])&0x800)
430 if(curfreq[P] && (PSG[(P<<2)+0x1]&7)/* && sweepon[P]&0x80*/)
441 if(PSG[0x15]&0x8 && sqnon&8)
448 if(lengthcount[3]<=0)
456 case 0: /* Envelope decay + linear counter */
462 if(tricoop==1) DoTriangle(0);
469 if(DecCountTo1[P]>0) DecCountTo1[P]--;
470 if(DecCountTo1[P]<=0)
472 DecCountTo1[P]=(PSG[P<<2]&0xF)+1;
473 if(decvolume[P] || PSG[P<<2]&0x20)
476 /* Step from 0 to full volume seems to take twice as long
477 as the other steps. I don't know if this is the correct
478 way to double its length, though(or if it even matters).
480 if((PSG[P<<2]&0x20) && (decvolume[P]==0))
485 if(!(PSG[P<<2]&0x10))
486 realvolume[P]=decvolume[P];
489 if(DecCountTo1[2]>0) DecCountTo1[2]--;
490 if(DecCountTo1[2]<=0)
492 DecCountTo1[2]=(PSG[0xC]&0xF)+1;
493 if(decvolume[2] || PSG[0xC]&0x20)
496 /* Step from 0 to full volume seems to take twice as long
497 as the other steps. I don't know if this is the correct
498 way to double its length, though(or if it even matters).
500 if((PSG[0xC]&0x20) && (decvolume[2]==0))
506 realvolume[2]=decvolume[2];
513 void FrameSoundUpdate(void)
515 // Linear counter: Bit 0-6 of $4008
516 // Length counter: Bit 4-7 of $4003, $4007, $400b, $400f
522 if(!(PSG[0x17]&0xC0))
525 X6502_IRQBegin(FCEU_IQFCOUNT);
528 //if(SIRQStat&0x40) X6502_IRQBegin(FCEU_IQFCOUNT);
529 FrameSoundStuff(fcnt);
533 static uint32 ChannelBC[5];
535 static uint32 RectAmp[2][8];
537 static void FASTAPASS(1) CalcRectAmp(int P)
539 static int tal[4]={1,2,4,6};
542 uint32 *b=RectAmp[P];
547 //V=(PSG[P<<2]&15)<<4;
549 // V=decvolume[P]<<4;
550 m=tal[(PSG[P<<2]&0xC0)>>6];
557 static void RDoPCM(int32 end)
562 uint32 out=PSG[0x11]<<3;
565 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
566 if(end<=start) return;
574 for(V=start;V<end;V++)
590 Wave[V>>4]+=PSG[0x11]<<3;
596 PCMBuffer=ARead[0x8000+PCMAddressIndex](0x8000+PCMAddressIndex);
597 PCMAddressIndex=(PCMAddressIndex+1)&0x7fff;
602 int t=(((PCMBuffer>>PCMBitIndex)&1)<<2)-2;
612 PCMBitIndex=(PCMBitIndex+1)&7;
614 Wave[V>>4]+=out; //(PSG[0x11]-64)<<3;
621 for(V=start;V<=(start|15);V++)
624 for(V=(start>>4)+1;V<(end>>4);V++)
627 for(V=end&(~15);V<end;V++)
631 for(V=start;V<end;V++)
637 static void RDoSQ1(int32 end)
644 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
645 if(end<=start) return;
648 if(!(PSG[0x15]&1 && sqnon&1))
651 if(curfreq[0]<8 || curfreq[0]>0x7ff)
653 if(!CheckFreq(curfreq[0],PSG[0x1]))
659 uint32 out=RectAmp[0][DutyCount[0]];
663 for(V=start;V<end;V++)
666 sqacc[0]-=nesincsize;
672 if(sqacc[0]<=0) goto rea;
675 out=RectAmp[0][DutyCount[0]];
682 static void RDoSQ2(int32 end)
689 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
690 if(end<=start) return;
693 if(!(PSG[0x15]&2 && sqnon&2))
696 if(curfreq[1]<8 || curfreq[1]>0x7ff)
698 if(!CheckFreq(curfreq[1],PSG[0x5]))
704 uint32 out=RectAmp[1][DutyCount[1]];
709 for(V=start;V<end;V++)
712 sqacc[1]-=nesincsize;
718 if(sqacc[1]<=0) goto rea;
721 out=RectAmp[1][DutyCount[1]];
729 static void RDoTriangle(int32 end)
731 static uint32 tcout=0;
733 int32 start; //,freq;
734 int32 freq=(((PSG[0xa]|((PSG[0xb]&7)<<8))+1));
737 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
738 if(end<=start) return;
741 if(! (PSG[0x15]&0x4 && sqnon&4 && tricoop) )
742 { // Counter is halted, but we still need to output.
743 for(V=start;V<end;V++)
746 else if(freq<=4) // 55.9Khz - Might be barely audible on a real NES, but
747 // it's too costly to generate audio at this high of a frequency
748 // (55.9Khz * 32 for the stepping).
749 // The same could probably be said for ~27.8Khz, so we'll
750 // take care of that too. We'll just output the average
751 // value(15/2 - scaled properly for our output format, of course).
752 // We'll also take care of ~18Khz and ~14Khz too, since they should be barely audible.
753 // (Some proof or anything to confirm/disprove this would be nice.).
755 for(V=start;V<end;V++)
756 Wave[V>>4]+=((0xF<<4)+(0xF<<2))>>1;
760 static int32 triacc=0;
764 for(V=start;V<end;V++)
772 if(triacc<=0) goto rea;
775 if(tc&0x10) tcout^=0xF;
776 tcout=(tcout<<4)+(tcout<<2);
783 static void RDoNoise(int32 end)
789 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
790 if(end<=start) return;
793 if(PSG[0x15]&0x8 && sqnon&8)
799 amplitude=realvolume[2];
801 // amplitude=(PSG[0xC]&0xF);
803 // amplitude=decvolume[2]&0xF;
805 inc=NoiseFreqTable[PSG[0xE]&0xF];
806 amptab[0]=((amplitude<<2)+amplitude+amplitude)<<1;
812 if(PSG[0xE]&0x80) // "short" noise
813 for(V=start;V<end;V++)
820 feedback=((nreg>>8)&1)^((nreg>>14)&1);
821 nreg=(nreg<<1)+feedback;
829 for(V=start;V<end;V++)
836 feedback=((nreg>>13)&1)^((nreg>>14)&1);
837 nreg=(nreg<<1)+feedback;
857 X6502_IRQEnd(FCEU_IQFCOUNT);
859 //IRQFrameMode=V; // IRQFrameMode is PSG[0x17] upper bits
862 void SetNESSoundMap(void)
864 SetWriteHandler(0x4000,0x4013,Write_PSG);
865 SetWriteHandler(0x4011,0x4011,Write0x11);
866 SetWriteHandler(0x4015,0x4015,Write_PSG);
867 SetWriteHandler(0x4017,0x4017,Write_IRQFM);
868 SetReadHandler(0x4015,0x4015,Read_PSG);
871 int32 highp; // 0 through 65536, 0 = no high pass, 65536 = max high pass
873 int32 lowp; // 0 through 65536, 65536 = max low pass(total attenuation)
874 // 65536 = no low pass
875 static int32 flt_acc=0, flt_acc2=0;
877 static void FilterSound(uint32 *in, int16 *outMono, int count)
879 // static int min=0, max=0;
881 if (soundvol < 5) sh += 5 - soundvol;
883 for(;count;count--,in++,outMono++)
887 diff = *in - flt_acc;
889 flt_acc += (diff*highp)>>16;
890 flt_acc2+= (int32) (((int64)((diff-flt_acc2)*lowp))>>16);
893 *outMono = flt_acc2*7 >> sh; // * 7 >> 2 = * 1.75
894 // if (acc2 < min) { printf("min: %i %04x\n", acc2, acc2); min = acc2; }
895 // if (acc2 > max) { printf("max: %i %04x\n", acc2, acc2); max = acc2; }
901 static int32 inbuf=0;
902 int FlushEmulateSound(void)
907 if(!timestamp) return(0);
909 if(!FSettings.SndRate || (soundvol == 0))
915 end=(SOUNDTS<<16)/soundtsinc;
922 if(GameExpSound.Fill)
923 GameExpSound.Fill(end&0xF);
925 FilterSound(Wave,WaveFinalMono,end>>4);
928 Wave[0]=Wave[(end>>4)];
933 ChannelBC[x]=end&0xF;
934 soundtsoffs=(soundtsinc*(end&0xF))>>16;
940 int GetSoundBuffer(int16 **W)
946 void PowerSound(void)
954 BWrite[0x4000+x](0x4000+x,0);
962 void ResetSound(void)
966 if(x!=1 && x!=5 && x!=0x14) BWrite[0x4000+x](0x4000+x,0);
973 void SetSoundVariables(void)
977 fhinc=PAL?16626:14915; // *2 CPU clock rate
980 lengthtable[x]=Slengthtable[x]<<1;
982 if(FSettings.SndRate)
985 DoTriangle=RDoTriangle;
992 DoNoise=DoTriangle=DoPCM=DoSQ1=DoSQ2=Dummyfunc;
995 if(!FSettings.SndRate) return;
996 if(GameExpSound.RChange)
997 GameExpSound.RChange();
999 // nesincsizeLL=(int64)((int64)562949953421312*(double)(PAL?PAL_CPU:NTSC_CPU)/(FSettings.SndRate OVERSAMPLE));
1000 nesincsize=(int32)(((int64)1<<17)*(double)(PAL?PAL_CPU:NTSC_CPU)/(FSettings.SndRate * 16)); // 308845 - 1832727
1001 PSG_base=(uint32)(PAL?(long double)PAL_CPU/16:(long double)NTSC_CPU/16);
1006 z=SNoiseFreqTable[x]<<1;
1007 z=(PAL?PAL_CPU:NTSC_CPU)/z;
1008 z=(long double)((uint32)((FSettings.SndRate OVERSAMPLE)<<12))/z;
1009 NoiseFreqTable[x]=z;
1011 soundtsinc=(uint32)((uint64)(PAL?(long double)PAL_CPU*65536:(long double)NTSC_CPU*65536)/(FSettings.SndRate OVERSAMPLE));
1012 memset(Wave,0,sizeof(Wave));
1015 highp=(250<<16)/FSettings.SndRate; // Arbitrary
1016 lowp=(25000<<16)/FSettings.SndRate; // Arbitrary
1018 if(highp>(1<<16)) highp=1<<16;
1019 if(lowp>(1<<16)) lowp=1<<16;
1024 void FixOldSaveStateSFreq(void)
1029 curfreq[x]=PSG[0x2+(x<<2)]|((PSG[0x3+(x<<2)]&7)<<8);
1033 void FCEUI_Sound(int Rate)
1035 FSettings.SndRate=Rate;
1036 SetSoundVariables();
1039 void FCEUI_SetSoundVolume(uint32 volume)
1041 FSettings.SoundVolume=volume;