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 /********************************************************/
46 int16 WaveFinalMono[2048];
48 EXPSOUND GameExpSound={0,0,0};
57 static int32 count[5];
58 static int32 sqacc[2]={0,0};
70 static const uint8 Slengthtable[0x20]=
72 0x5,0x7f,0xA,0x1,0x14,0x2,0x28,0x3,0x50,0x4,0x1E,0x5,0x7,0x6,0x0E,0x7,
73 0x6,0x08,0xC,0x9,0x18,0xa,0x30,0xb,0x60,0xc,0x24,0xd,0x8,0xe,0x10,0xf
76 static uint32 lengthtable[0x20];
78 static const uint32 SNoiseFreqTable[0x10]=
80 2,4,8,0x10,0x20,0x30,0x40,0x50,0x65,0x7f,0xbe,0xfe,0x17d,0x1fc,0x3f9,0x7f2
82 static uint32 NoiseFreqTable[0x10];
84 static int32 nesincsize32;
87 static const uint8 NTSCPCMTable[0x10]=
89 0xd6,0xbe,0xaa,0xa0,0x8f,0x7f,0x71,0x6b,
90 0x5f,0x50,0x47,0x40,0x35,0x2a,0x24,0x1b
93 static const uint8 PALPCMTable[0x10]= // These values are just guessed.
95 0xc6,0xb0,0x9d,0x94,0x84,0x75,0x68,0x63,
96 0x58,0x4a,0x41,0x3b,0x31,0x27,0x21,0x19
102 // $4011 - Actual data outputted
103 // $4012 - Address register: $c000 + V*64
104 // $4013 - Size register: Size in bytes = (V+1)*64
107 static int32 PCMacc=0;
111 uint32 PCMAddressIndex=0;
112 int32 PCMSizeIndex=0;
116 static void Dummyfunc(int end) {};
118 static void (*DoNoise)(int end)=Dummyfunc;
119 static void (*DoTriangle)(int end)=Dummyfunc;
120 static void (*DoPCM)(int end)=Dummyfunc;
121 static void (*DoSQ1)(int end)=Dummyfunc;
122 static void (*DoSQ2)(int end)=Dummyfunc;
124 static void CalcDPCMIRQ(void)
131 freq=(PALPCMTable[PSG[0x10]&0xF]<<4);
133 freq=(NTSCPCMTable[PSG[0x10]&0xF]<<4);
135 cycles=(((PSG[0x13]<<4)+1));
137 honk=((PSG[0x13]<<4)+1)*freq;
140 //else honk/=(double)113.66666666;
142 //PCMIRQCount=honk*3; //180;
143 //if(PAL) PCMIRQCount*=.93;
147 static void PrepDPCM()
149 PCMAddressIndex=0x4000+(PSG[0x12]<<6);
150 PCMSizeIndex=(PSG[0x13]<<4)+1;
152 //PCMBuffer=ARead[0x8000+PCMAddressIndex](0x8000+PCMAddressIndex);
154 PCMfreq=PALPCMTable[PSG[0x10]&0xF];
156 PCMfreq=NTSCPCMTable[PSG[0x10]&0xF];
160 uint8 sweepon[2]={0,0};
161 int32 curfreq[2]={0,0};
167 uint8 DecCountTo1[3];
175 /* Instantaneous? Maybe the new freq value is being calculated all of the time... */
176 static int FASTAPASS(2) CheckFreq(uint32 cf, uint8 sr)
188 static DECLFW(Write0x11)
194 static uint8 DutyCount[2]={0,0};
196 static DECLFW(Write_PSG)
198 //if((A>=0x4004 && A<=0x4007) || A==0x4015)
199 //printf("$%04x:$%02x, %d\n",A,V,SOUNDTS);
220 lengthcount[0]=lengthtable[(V>>3)&0x1f];
223 sweepon[0]=PSG[1]&0x80;
224 curfreq[0]=PSG[0x2]|((V&7)<<8);
226 DecCountTo1[0]=(PSG[0]&0xF)+1;
227 SweepCount[0]=((PSG[0x1]>>4)&7)+1;
229 sqacc[0]=((int32)curfreq[0]+1)<<18;
249 lengthcount[1]=lengthtable[(V>>3)&0x1f];
252 sweepon[1]=PSG[0x5]&0x80;
253 curfreq[1]=PSG[0x6]|((V&7)<<8);
255 DecCountTo1[1]=(PSG[0x4]&0xF)+1;
256 SweepCount[1]=((PSG[0x5]>>4)&7)+1;
258 sqacc[1]=((int32)curfreq[1]+1)<<18;
271 case 0xa:DoTriangle(0);
278 lengthcount[2]=lengthtable[(V>>3)&0x1f];
281 tricoop=PSG[0x8]&0x7f;
282 trimode=PSG[0x8]&0x80;
288 case 0xE:DoNoise(0);break;
294 lengthcount[3]=lengthtable[(V>>3)&0x1f];
297 DecCountTo1[2]=(PSG[0xC]&0xF)+1;
301 X6502_IRQEnd(FCEU_IQDPCM);
320 if(!(PSG[0x15]&0x10))
330 X6502_IRQEnd(FCEU_IQDPCM);
342 ret=(PSG[0x15]&(sqnon|0x10))|SIRQStat;
344 X6502_IRQEnd(/*FCEU_IQDPCM|*/FCEU_IQFCOUNT);
348 DECLFR(Read_PSGDummy)
352 ret=(PSG[0x15]&sqnon)|SIRQStat;
354 X6502_IRQEnd(/*FCEU_IQDPCM|*/FCEU_IQFCOUNT);
358 static void FASTAPASS(1) FrameSoundStuff(int V)
361 uint32 end = (SOUNDTS<<16)/soundtsinc;
369 case 1: /* Envelope decay, linear counter, length counter, freq sweep */
370 if(PSG[0x15]&4 && sqnon&4)
376 if(lengthcount[2]<=0)
386 if(PSG[0x15]&(P+1) && sqnon&(P+1))
388 if(!(PSG[P<<2]&0x20))
393 if(lengthcount[P]<=0)
400 /* Frequency Sweep Code Here */
402 /* xxxx = hz. 120/(x+1)*/
407 if(SweepCount[P]>0) SweepCount[P]--;
410 SweepCount[P]=((PSG[(P<<2)+0x1]>>4)&7)+1; //+1;
412 if(PSG[(P<<2)+0x1]&0x8)
414 mod-=(P^1)+((curfreq[P])>>(PSG[(P<<2)+0x1]&7));
416 if(curfreq[P] && (PSG[(P<<2)+0x1]&7)/* && sweepon[P]&0x80*/)
423 mod=curfreq[P]>>(PSG[(P<<2)+0x1]&7);
424 if((mod+curfreq[P])&0x800)
431 if(curfreq[P] && (PSG[(P<<2)+0x1]&7)/* && sweepon[P]&0x80*/)
442 if(PSG[0x15]&0x8 && sqnon&8)
449 if(lengthcount[3]<=0)
457 case 0: /* Envelope decay + linear counter */
463 if(tricoop==1) DoTriangle(0);
470 if(DecCountTo1[P]>0) DecCountTo1[P]--;
471 if(DecCountTo1[P]<=0)
473 DecCountTo1[P]=(PSG[P<<2]&0xF)+1;
474 if(decvolume[P] || PSG[P<<2]&0x20)
477 /* Step from 0 to full volume seems to take twice as long
478 as the other steps. I don't know if this is the correct
479 way to double its length, though(or if it even matters).
481 if((PSG[P<<2]&0x20) && (decvolume[P]==0))
486 if(!(PSG[P<<2]&0x10))
487 realvolume[P]=decvolume[P];
490 if(DecCountTo1[2]>0) DecCountTo1[2]--;
491 if(DecCountTo1[2]<=0)
493 DecCountTo1[2]=(PSG[0xC]&0xF)+1;
494 if(decvolume[2] || PSG[0xC]&0x20)
497 /* Step from 0 to full volume seems to take twice as long
498 as the other steps. I don't know if this is the correct
499 way to double its length, though(or if it even matters).
501 if((PSG[0xC]&0x20) && (decvolume[2]==0))
507 realvolume[2]=decvolume[2];
514 void FrameSoundUpdate(void)
516 // Linear counter: Bit 0-6 of $4008
517 // Length counter: Bit 4-7 of $4003, $4007, $400b, $400f
523 if(!(PSG[0x17]&0xC0))
526 X6502_IRQBegin(FCEU_IQFCOUNT);
529 //if(SIRQStat&0x40) X6502_IRQBegin(FCEU_IQFCOUNT);
530 FrameSoundStuff(fcnt);
534 static uint32 ChannelBC[5];
536 static uint32 RectAmp[2][8];
538 static void FASTAPASS(1) CalcRectAmp(int P)
540 static int tal[4]={1,2,4,6};
543 uint32 *b=RectAmp[P];
548 //V=(PSG[P<<2]&15)<<4;
550 // V=decvolume[P]<<4;
551 m=tal[(PSG[P<<2]&0xC0)>>6];
558 static void RDoPCM(int32 end)
563 uint32 out=PSG[0x11]<<3;
566 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
567 if(end<=start) return;
575 for(V=start;V<end;V++)
577 PCMacc-=nesincsize32;
591 Wave[V>>4]+=PSG[0x11]<<3;
597 PCMBuffer=ARead[0x8000+PCMAddressIndex](0x8000+PCMAddressIndex);
598 PCMAddressIndex=(PCMAddressIndex+1)&0x7fff;
603 int t=(((PCMBuffer>>PCMBitIndex)&1)<<2)-2;
613 PCMBitIndex=(PCMBitIndex+1)&7;
615 Wave[V>>4]+=out; //(PSG[0x11]-64)<<3;
622 for(V=start;V<=(start|15);V++)
625 for(V=(start>>4)+1;V<(end>>4);V++)
628 for(V=end&(~15);V<end;V++)
632 for(V=start;V<end;V++)
638 static void RDoSQ1(int32 end)
645 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
646 if(end<=start) return;
649 if(!(PSG[0x15]&1 && sqnon&1))
652 if(curfreq[0]<8 || curfreq[0]>0x7ff)
654 if(!CheckFreq(curfreq[0],PSG[0x1]))
660 uint32 out=RectAmp[0][DutyCount[0]];
664 for(V=start;V<end;V++)
667 sqacc[0]-=nesincsize32;
673 if(sqacc[0]<=0) goto rea;
676 out=RectAmp[0][DutyCount[0]];
683 static void RDoSQ2(int32 end)
690 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
691 if(end<=start) return;
694 if(!(PSG[0x15]&2 && sqnon&2))
697 if(curfreq[1]<8 || curfreq[1]>0x7ff)
699 if(!CheckFreq(curfreq[1],PSG[0x5]))
705 uint32 out=RectAmp[1][DutyCount[1]];
710 for(V=start;V<end;V++)
713 sqacc[1]-=nesincsize32;
719 if(sqacc[1]<=0) goto rea;
722 out=RectAmp[1][DutyCount[1]];
730 static void RDoTriangle(int32 end)
732 static uint32 tcout=0;
734 int32 start; //,freq;
735 int32 freq=(((PSG[0xa]|((PSG[0xb]&7)<<8))+1));
738 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
739 if(end<=start) return;
742 if(! (PSG[0x15]&0x4 && sqnon&4 && tricoop) )
743 { // Counter is halted, but we still need to output.
744 for(V=start;V<end;V++)
747 else if(freq<=4) // 55.9Khz - Might be barely audible on a real NES, but
748 // it's too costly to generate audio at this high of a frequency
749 // (55.9Khz * 32 for the stepping).
750 // The same could probably be said for ~27.8Khz, so we'll
751 // take care of that too. We'll just output the average
752 // value(15/2 - scaled properly for our output format, of course).
753 // We'll also take care of ~18Khz and ~14Khz too, since they should be barely audible.
754 // (Some proof or anything to confirm/disprove this would be nice.).
756 for(V=start;V<end;V++)
757 Wave[V>>4]+=((0xF<<4)+(0xF<<2))>>1;
761 static int32 triacc=0;
765 for(V=start;V<end;V++)
767 triacc-=nesincsize32;
773 if(triacc<=0) goto rea;
776 if(tc&0x10) tcout^=0xF;
777 tcout=(tcout<<4)+(tcout<<2);
784 static void RDoNoise(int32 end)
790 if(end==0) end=(SOUNDTS<<16)/soundtsinc;
791 if(end<=start) return;
794 if(PSG[0x15]&0x8 && sqnon&8)
800 amplitude=realvolume[2];
802 // amplitude=(PSG[0xC]&0xF);
804 // amplitude=decvolume[2]&0xF;
806 inc=NoiseFreqTable[PSG[0xE]&0xF];
807 amptab[0]=((amplitude<<2)+amplitude+amplitude)<<1;
813 if(PSG[0xE]&0x80) // "short" noise
814 for(V=start;V<end;V++)
821 feedback=((nreg>>8)&1)^((nreg>>14)&1);
822 nreg=(nreg<<1)+feedback;
830 for(V=start;V<end;V++)
837 feedback=((nreg>>13)&1)^((nreg>>14)&1);
838 nreg=(nreg<<1)+feedback;
858 X6502_IRQEnd(FCEU_IQFCOUNT);
860 //IRQFrameMode=V; // IRQFrameMode is PSG[0x17] upper bits
863 void SetNESSoundMap(void)
865 SetWriteHandler(0x4000,0x4013,Write_PSG);
866 SetWriteHandler(0x4011,0x4011,Write0x11);
867 SetWriteHandler(0x4015,0x4015,Write_PSG);
868 SetWriteHandler(0x4017,0x4017,Write_IRQFM);
869 SetReadHandler(0x4015,0x4015,Read_PSG);
872 int32 highp; // 0 through 65536, 0 = no high pass, 65536 = max high pass
874 int32 lowp; // 0 through 65536, 65536 = max low pass(total attenuation)
875 // 65536 = no low pass
876 static void FilterSound(uint32 *in, int16 *outMono, int count)
878 static int32 acc=0, acc2=0;
879 // static int min=0, max=0;
881 for(;count;count--,in++,outMono++)
887 acc += (diff*highp)>>16;
888 acc2+= (int32) (((int64)((diff-acc2)*lowp))>>16);
891 *outMono = acc2*7 >> 2; // * 1.75
892 // if (acc2 < min) { printf("min: %i %04x\n", acc2, acc2); min = acc2; }
893 // if (acc2 > max) { printf("max: %i %04x\n", acc2, acc2); max = acc2; }
899 static int32 inbuf=0;
900 int FlushEmulateSound(void)
905 if(!timestamp) return(0);
907 if(!FSettings.SndRate || (soundvol == 0))
913 end=(SOUNDTS<<16)/soundtsinc;
920 if(GameExpSound.Fill)
921 GameExpSound.Fill(end&0xF);
923 FilterSound(Wave,WaveFinalMono,end>>4);
926 Wave[0]=Wave[(end>>4)];
931 ChannelBC[x]=end&0xF;
932 soundtsoffs=(soundtsinc*(end&0xF))>>16;
938 int GetSoundBuffer(int16 **W)
944 void PowerSound(void)
952 BWrite[0x4000+x](0x4000+x,0);
960 void ResetSound(void)
964 if(x!=1 && x!=5 && x!=0x14) BWrite[0x4000+x](0x4000+x,0);
971 void SetSoundVariables(void)
975 fhinc=PAL?16626:14915; // *2 CPU clock rate
978 lengthtable[x]=Slengthtable[x]<<1;
980 if(FSettings.SndRate)
983 DoTriangle=RDoTriangle;
990 DoNoise=DoTriangle=DoPCM=DoSQ1=DoSQ2=Dummyfunc;
993 if(!FSettings.SndRate) return;
994 if(GameExpSound.RChange)
995 GameExpSound.RChange();
997 // nesincsizeLL=(int64)((int64)562949953421312*(double)(PAL?PAL_CPU:NTSC_CPU)/(FSettings.SndRate OVERSAMPLE));
998 nesincsize=(int64)(((int64)1<<17)*(double)(PAL?PAL_CPU:NTSC_CPU)/(FSettings.SndRate * 16)); // 308845 - 1832727
999 nesincsize32=(int32)nesincsize;
1000 PSG_base=(uint32)(PAL?(long double)PAL_CPU/16:(long double)NTSC_CPU/16);
1005 z=SNoiseFreqTable[x]<<1;
1006 z=(PAL?PAL_CPU:NTSC_CPU)/z;
1007 z=(long double)((uint32)((FSettings.SndRate OVERSAMPLE)<<12))/z;
1008 NoiseFreqTable[x]=z;
1010 soundtsinc=(uint32)((uint64)(PAL?(long double)PAL_CPU*65536:(long double)NTSC_CPU*65536)/(FSettings.SndRate OVERSAMPLE));
1011 memset(Wave,0,sizeof(Wave));
1014 highp=(250<<16)/FSettings.SndRate; // Arbitrary
1015 lowp=(25000<<16)/FSettings.SndRate; // Arbitrary
1017 if(highp>(1<<16)) highp=1<<16;
1018 if(lowp>(1<<16)) lowp=1<<16;
1021 void FixOldSaveStateSFreq(void)
1026 curfreq[x]=PSG[0x2+(x<<2)]|((PSG[0x3+(x<<2)]&7)<<8);
1030 void FCEUI_Sound(int Rate)
1032 FSettings.SndRate=Rate;
1033 SetSoundVariables();
1036 void FCEUI_SetSoundVolume(uint32 volume)
1038 FSettings.SoundVolume=volume;