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 int32 WaveFinal[2048];
48 EXPSOUND GameExpSound={0,0,0};
57 static int32 count[5];
58 static int64 sqacc[2]={0,0};
66 static const uint8 Slengthtable[0x20]=
68 0x5,0x7f,0xA,0x1,0x14,0x2,0x28,0x3,0x50,0x4,0x1E,0x5,0x7,0x6,0x0E,0x7,
69 0x6,0x08,0xC,0x9,0x18,0xa,0x30,0xb,0x60,0xc,0x24,0xd,0x8,0xe,0x10,0xf
72 static uint32 lengthtable[0x20];
74 static const uint32 SNoiseFreqTable[0x10]=
76 2,4,8,0x10,0x20,0x30,0x40,0x50,0x65,0x7f,0xbe,0xfe,0x17d,0x1fc,0x3f9,0x7f2
78 static uint32 NoiseFreqTable[0x10];
82 static const uint8 NTSCPCMTable[0x10]=
84 0xd6,0xbe,0xaa,0xa0,0x8f,0x7f,0x71,0x6b,
85 0x5f,0x50,0x47,0x40,0x35,0x2a,0x24,0x1b
88 static const uint8 PALPCMTable[0x10]= // These values are just guessed.
90 0xc6,0xb0,0x9d,0x94,0x84,0x75,0x68,0x63,
91 0x58,0x4a,0x41,0x3b,0x31,0x27,0x21,0x19
97 // $4011 - Actual data outputted
98 // $4012 - Address register: $c000 + V*64
99 // $4013 - Size register: Size in bytes = (V+1)*64
102 static int64 PCMacc=0;
106 uint32 PCMAddressIndex=0;
107 int32 PCMSizeIndex=0;
111 static void Dummyfunc(void) {};
113 static void (*DoNoise)(void)=Dummyfunc;
114 static void (*DoTriangle)(void)=Dummyfunc;
115 static void (*DoPCM)(void)=Dummyfunc;
116 static void (*DoSQ1)(void)=Dummyfunc;
117 static void (*DoSQ2)(void)=Dummyfunc;
119 static void CalcDPCMIRQ(void)
126 freq=(PALPCMTable[PSG[0x10]&0xF]<<4);
128 freq=(NTSCPCMTable[PSG[0x10]&0xF]<<4);
130 cycles=(((PSG[0x13]<<4)+1));
132 honk=((PSG[0x13]<<4)+1)*freq;
135 //else honk/=(double)113.66666666;
137 //PCMIRQCount=honk*3; //180;
138 //if(PAL) PCMIRQCount*=.93;
142 static void PrepDPCM()
144 PCMAddressIndex=0x4000+(PSG[0x12]<<6);
145 PCMSizeIndex=(PSG[0x13]<<4)+1;
147 //PCMBuffer=ARead[0x8000+PCMAddressIndex](0x8000+PCMAddressIndex);
149 PCMfreq=PALPCMTable[PSG[0x10]&0xF];
151 PCMfreq=NTSCPCMTable[PSG[0x10]&0xF];
152 PCMacc=(int64)PCMfreq<<50;
155 uint8 sweepon[2]={0,0};
156 int32 curfreq[2]={0,0};
162 uint8 DecCountTo1[3];
170 /* Instantaneous? Maybe the new freq value is being calculated all of the time... */
171 static int FASTAPASS(2) CheckFreq(uint32 cf, uint8 sr)
183 static DECLFW(Write0x11)
189 static uint8 DutyCount[2]={0,0};
191 static DECLFW(Write_PSG)
193 //if((A>=0x4004 && A<=0x4007) || A==0x4015)
194 //printf("$%04x:$%02x, %d\n",A,V,timestamp);
215 lengthcount[0]=lengthtable[(V>>3)&0x1f];
218 sweepon[0]=PSG[1]&0x80;
219 curfreq[0]=PSG[0x2]|((V&7)<<8);
221 DecCountTo1[0]=(PSG[0]&0xF)+1;
222 SweepCount[0]=((PSG[0x1]>>4)&7)+1;
224 sqacc[0]=((int64)curfreq[0]+1)<<50;
244 lengthcount[1]=lengthtable[(V>>3)&0x1f];
247 sweepon[1]=PSG[0x5]&0x80;
248 curfreq[1]=PSG[0x6]|((V&7)<<8);
250 DecCountTo1[1]=(PSG[0x4]&0xF)+1;
251 SweepCount[1]=((PSG[0x5]>>4)&7)+1;
253 sqacc[1]=((int64)curfreq[1]+1)<<50;
266 case 0xa:DoTriangle();
273 lengthcount[2]=lengthtable[(V>>3)&0x1f];
276 tricoop=PSG[0x8]&0x7f;
277 trimode=PSG[0x8]&0x80;
283 case 0xE:DoNoise();break;
289 lengthcount[3]=lengthtable[(V>>3)&0x1f];
292 DecCountTo1[2]=(PSG[0xC]&0xF)+1;
296 X6502_IRQEnd(FCEU_IQDPCM);
315 if(!(PSG[0x15]&0x10))
325 X6502_IRQEnd(FCEU_IQDPCM);
334 X6502_IRQEnd(FCEU_IQFCOUNT);
346 ret=(PSG[0x15]&(sqnon|0x10))|SIRQStat;
348 X6502_IRQEnd(/*FCEU_IQDPCM|*/FCEU_IQFCOUNT);
352 DECLFR(Read_PSGDummy)
356 ret=(PSG[0x15]&sqnon)|SIRQStat;
358 X6502_IRQEnd(/*FCEU_IQDPCM|*/FCEU_IQFCOUNT);
362 static void FASTAPASS(1) FrameSoundStuff(int V)
372 case 1: /* Envelope decay, linear counter, length counter, freq sweep */
373 if(PSG[0x15]&4 && sqnon&4)
379 if(lengthcount[2]<=0)
389 if(PSG[0x15]&(P+1) && sqnon&(P+1))
391 if(!(PSG[P<<2]&0x20))
396 if(lengthcount[P]<=0)
403 /* Frequency Sweep Code Here */
405 /* xxxx = hz. 120/(x+1)*/
410 if(SweepCount[P]>0) SweepCount[P]--;
413 SweepCount[P]=((PSG[(P<<2)+0x1]>>4)&7)+1; //+1;
415 if(PSG[(P<<2)+0x1]&0x8)
417 mod-=(P^1)+((curfreq[P])>>(PSG[(P<<2)+0x1]&7));
419 if(curfreq[P] && (PSG[(P<<2)+0x1]&7)/* && sweepon[P]&0x80*/)
426 mod=curfreq[P]>>(PSG[(P<<2)+0x1]&7);
427 if((mod+curfreq[P])&0x800)
434 if(curfreq[P] && (PSG[(P<<2)+0x1]&7)/* && sweepon[P]&0x80*/)
445 if(PSG[0x15]&0x8 && sqnon&8)
452 if(lengthcount[3]<=0)
460 case 0: /* Envelope decay + linear counter */
466 if(tricoop==1) DoTriangle();
473 if(DecCountTo1[P]>0) DecCountTo1[P]--;
474 if(DecCountTo1[P]<=0)
476 DecCountTo1[P]=(PSG[P<<2]&0xF)+1;
477 if(decvolume[P] || PSG[P<<2]&0x20)
480 /* Step from 0 to full volume seems to take twice as long
481 as the other steps. I don't know if this is the correct
482 way to double its length, though(or if it even matters).
484 if((PSG[P<<2]&0x20) && (decvolume[P]==0))
489 if(!(PSG[P<<2]&0x10))
490 realvolume[P]=decvolume[P];
493 if(DecCountTo1[2]>0) DecCountTo1[2]--;
494 if(DecCountTo1[2]<=0)
496 DecCountTo1[2]=(PSG[0xC]&0xF)+1;
497 if(decvolume[2] || PSG[0xC]&0x20)
500 /* Step from 0 to full volume seems to take twice as long
501 as the other steps. I don't know if this is the correct
502 way to double its length, though(or if it even matters).
504 if((PSG[0xC]&0x20) && (decvolume[2]==0))
510 realvolume[2]=decvolume[2];
517 void FrameSoundUpdate(void)
519 // Linear counter: Bit 0-6 of $4008
520 // Length counter: Bit 4-7 of $4003, $4007, $400b, $400f
526 if(!(PSG[0x17]&0xC0))
529 X6502_IRQBegin(FCEU_IQFCOUNT);
532 //if(SIRQStat&0x40) X6502_IRQBegin(FCEU_IQFCOUNT);
533 FrameSoundStuff(fcnt);
537 static uint32 ChannelBC[5];
539 static uint32 RectAmp[2][8];
541 static void FASTAPASS(1) CalcRectAmp(int P)
543 static int tal[4]={1,2,4,6};
546 uint32 *b=RectAmp[P];
551 //V=(PSG[P<<2]&15)<<4;
553 // V=decvolume[P]<<4;
554 m=tal[(PSG[P<<2]&0xC0)>>6];
561 static void RDoPCM(void)
566 uint32 out=PSG[0x11]<<3;
569 end=(timestamp<<16)/soundtsinc;
570 if(end<=start) return;
578 for(V=start;V<end;V++)
580 PCMacc-=nesincsizeLL;
594 Wave[V>>4]+=PSG[0x11]<<3;
600 PCMBuffer=ARead[0x8000+PCMAddressIndex](0x8000+PCMAddressIndex);
601 PCMAddressIndex=(PCMAddressIndex+1)&0x7fff;
606 int t=(((PCMBuffer>>PCMBitIndex)&1)<<2)-2;
616 PCMBitIndex=(PCMBitIndex+1)&7;
618 Wave[V>>4]+=out; //(PSG[0x11]-64)<<3;
625 for(V=start;V<=(start|15);V++)
628 for(V=(start>>4)+1;V<(end>>4);V++)
631 for(V=end&(~15);V<end;V++)
635 for(V=start;V<end;V++)
641 static void RDoSQ1(void)
649 end=(timestamp<<16)/soundtsinc;
650 if(end<=start) return;
653 if(curfreq[0]<8 || curfreq[0]>0x7ff)
655 if(!CheckFreq(curfreq[0],PSG[0x1]))
658 if(PSG[0x15]&1 && sqnon&1)
660 uint32 out=RectAmp[0][DutyCount[0]];
664 for(V=start;V<end;V++)
667 sqacc[0]-=nesincsizeLL;
673 if(sqacc[0]<=0) goto rea;
676 out=RectAmp[0][DutyCount[0]];
684 static void RDoSQ2(void)
692 end=(timestamp<<16)/soundtsinc;
693 if(end<=start) return;
696 if(curfreq[1]<8 || curfreq[1]>0x7ff)
698 if(!CheckFreq(curfreq[1],PSG[0x5]))
701 if(PSG[0x15]&2 && sqnon&2)
703 uint32 out=RectAmp[1][DutyCount[1]];
708 for(V=start;V<end;V++)
711 sqacc[1]-=nesincsizeLL;
717 if(sqacc[1]<=0) goto rea;
720 out=RectAmp[1][DutyCount[1]];
729 static void RDoTriangle(void)
731 static uint32 tcout=0;
733 int32 start,end; //,freq;
734 int64 freq=(((PSG[0xa]|((PSG[0xb]&7)<<8))+1));
737 end=(timestamp<<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 int64 triacc=0;
764 for(V=start;V<end;V++)
766 triacc-=nesincsizeLL;
772 if(triacc<=0) goto rea;
775 if(tc&0x10) tcout^=0xF;
776 tcout=(tcout<<4)+(tcout<<2);
783 static void RDoNoise(void)
789 end=(timestamp<<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;
849 void SetNESSoundMap(void)
851 SetWriteHandler(0x4000,0x4013,Write_PSG);
852 SetWriteHandler(0x4011,0x4011,Write0x11);
853 SetWriteHandler(0x4015,0x4015,Write_PSG);
854 SetWriteHandler(0x4017,0x4017,Write_PSG);
855 SetReadHandler(0x4015,0x4015,Read_PSG);
858 static int32 WaveNSF[256];
860 int64 highp; // 0 through 65536, 0 = no high pass, 65536 = max high pass
862 int64 lowp; // 0 through 65536, 65536 = max low pass(total attenuation)
863 // 65536 = no low pass
864 static void FilterSound(uint32 *in, int32 *out, int count)
866 static int64 acc=0, acc2=0;
868 for(;count;count--,in++,out++)
872 diff=((int64)*in<<24)-acc;
874 acc+=(diff*highp)>>16;
875 acc2+=((diff-acc2)*lowp)>>16;
877 *out=(acc2*(int64)FSettings.SoundVolume)>>(24+16);
878 if(*out<-32767) *out=-32767;
879 if(*out>32767) *out=32767;
880 //*out=((int64)(acc2>>24)*(int64)FSettings.SoundVolume)>>16; //acc2>>24;
884 int FlushEmulateSound(void)
889 if(!timestamp) return(0);
891 if(!FSettings.SndRate)
897 end=(timestamp<<16)/soundtsinc;
904 if(GameExpSound.Fill)
905 GameExpSound.Fill(end&0xF);
907 FilterSound(Wave,WaveFinal,end>>4);
909 if(FCEUGameInfo.type==GIT_NSF)
911 int x,s=0,si=end/1024; // Only want 1/4 of the output buffer to be displayed
914 WaveNSF[x]=WaveFinal[s>>4];
920 Wave[0]=Wave[(end>>4)];
925 ChannelBC[x]=end&0xF;
926 timestampbase+=timestamp;
927 timestamp=(soundtsinc*(end&0xF))>>16;
928 timestampbase-=timestamp;
932 void GetSoundBuffer(int32 **W)
937 void PowerSound(void)
945 BWrite[0x4000+x](0x4000+x,0);
952 void ResetSound(void)
956 if(x!=1 && x!=5 && x!=0x14) BWrite[0x4000+x](0x4000+x,0);
963 void SetSoundVariables(void)
967 fhinc=PAL?16626:14915; // *2 CPU clock rate
970 lengthtable[x]=Slengthtable[x]<<1;
972 if(FSettings.SndRate)
975 DoTriangle=RDoTriangle;
982 DoNoise=DoTriangle=DoPCM=DoSQ1=DoSQ2=Dummyfunc;
985 if(!FSettings.SndRate) return;
986 if(GameExpSound.RChange)
987 GameExpSound.RChange();
989 nesincsizeLL=(int64)((int64)562949953421312*(long double)(PAL?PAL_CPU:NTSC_CPU)/(FSettings.SndRate OVERSAMPLE));
990 PSG_base=(uint32)(PAL?(long double)PAL_CPU/16:(long double)NTSC_CPU/16);
995 z=SNoiseFreqTable[x]<<1;
996 z=(PAL?PAL_CPU:NTSC_CPU)/z;
997 z=(long double)((uint32)((FSettings.SndRate OVERSAMPLE)<<12))/z;
1000 soundtsinc=(uint32)((uint64)(PAL?(long double)PAL_CPU*65536:(long double)NTSC_CPU*65536)/(FSettings.SndRate OVERSAMPLE));
1001 memset(Wave,0,2048*4);
1004 highp=(250<<16)/FSettings.SndRate; // Arbitrary
1005 lowp=((int64)25000<<16)/FSettings.SndRate; // Arbitrary
1007 if(highp>(1<<16)) highp=1<<16;
1008 if(lowp>(1<<16)) lowp=1<<16;
1011 void FixOldSaveStateSFreq(void)
1016 curfreq[x]=PSG[0x2+(x<<2)]|((PSG[0x3+(x<<2)]&7)<<8);
1020 void FCEUI_Sound(int Rate)
1022 FSettings.SndRate=Rate;
1023 SetSoundVariables();
1026 void FCEUI_SetSoundVolume(uint32 volume)
1028 FSettings.SoundVolume=(volume<<16)/100;