2 // This file is part of the PicoDrive Megadrive Emulator
\r
4 // This code is licensed under the GNU General Public License version 2.0 and the MAME License.
\r
5 // You can choose the license that has the most advantages for you.
\r
7 // SVN repository can be found at http://code.google.com/p/cyclone68000/
\r
14 int YM2612UpdateReq(int) { return 0; }
\r
15 void *errorlog=NULL;
\r
19 int PsndRate=0,PsndLen=0;
\r
20 short *PsndOut=NULL;
\r
22 // An operator is a single sine wave generator
\r
25 unsigned short angle; // 0-0xffff
\r
26 unsigned short freq; // Converted frequency
\r
28 unsigned char state; // 1=attack, 2=decay, 3=sustain, 4=release
\r
30 int delta; // Change in volume per sample
\r
31 int limit; // Next volume limit
\r
36 struct Operator op[4]; // 4 operators for the channel
\r
37 unsigned short note; // Written to 0xa4 and 0xa0
\r
40 static struct Channel Chan[8];
\r
42 unsigned char PicoSreg[0x200];
\r
44 static int WriteReg(int side,int a,int d)
\r
46 struct Channel *pc=NULL;
\r
48 PicoSreg[(side<<8)|a]=(unsigned char)d;
\r
54 if (d&0xf0) pc->op[0].state=1; // Attack
\r
55 else pc->op[0].state=4; // Release
\r
61 pc=Chan+(a&3); if (side) pc+=4;
\r
65 int n=0,f=0,mult=2455;
\r
67 if (PsndRate>0) mult=44100*2455/PsndRate;
\r
69 if (a&4) pc->note =(unsigned short)(d<<8);
\r
72 // Convert to an actual frequency:
\r
73 n=pc->note; f=(n&0x7ff)<<((n>>11)&7);
\r
75 pc->op[0].freq=(unsigned short)((f*mult)>>16);
\r
85 memset(&Chan,0,sizeof(Chan));
\r
86 memset(PicoSreg,0,sizeof(PicoSreg));
\r
88 // Change Sine wave into a square wave
\r
89 for (i=0x00; i<0x080; i++) Sine[i]= 0x2000;
\r
90 for (i=0x80; i<0x100; i++) Sine[i]=-0x2000;
\r
95 int PsndFm(int a,int d)
\r
100 YM2612Write(0,a&3,(unsigned char)d);
\r
103 a&=3; side=a>>1; // Which side: channels 0-2 or 3-5
\r
105 if (a&1) WriteReg(side,Pico.s.fmsel[side],d); // Write register
\r
106 else Pico.s.fmsel[side]=(unsigned char)d; // Select register
\r
111 static void BlankSound(short *dest,int len)
\r
117 // Init sound to silence:
\r
118 do { *dest=0; dest++; } while (dest<end);
\r
121 // Add to output and clip:
\r
122 static inline void AddClip(short *targ,int d)
\r
125 if ((total+0x8000)&0xffff0000)
\r
127 if (total>0) total=0x7fff; else total=-0x8000;
\r
129 *targ=(short)total;
\r
132 static void OpNewState(int c)
\r
134 struct Operator *op=Chan[c].op;
\r
137 off=((c&4)<<6)|(c&3);
\r
144 int ar=PicoSreg[0x50|off];
\r
145 ar&=0x1f; if (ar) ar+=0x10;
\r
146 op->delta=ar<<7; op->limit=0x1000000; break;
\r
151 int d1r=PicoSreg[0x60|off];
\r
152 d1r&=0x1f; if (d1r) d1r+=0x10;
\r
153 op->delta=-(d1r<<5); op->limit=0;
\r
161 d2r=PicoSreg[0x70|off];
\r
162 d2r&=0x1f; if (d2r) d2r+=0x10;
\r
163 rr =PicoSreg[0x80|off];
\r
166 op->delta=-(d2r<<5); op->limit=rr<<20;
\r
171 int rr=PicoSreg[0x80|off];
\r
172 rr&=0x0f; rr<<=1; rr+=0x10;
\r
173 op->delta=-(rr<<5); op->limit=0;
\r
178 // Merely adding this code in seems to bugger up the N-Gage???
\r
179 static void UpdateOp(int c)
\r
181 struct Operator *op=Chan[c].op;
\r
183 op->angle=(unsigned short)(op->angle+op->freq);
\r
184 op->vol+=op->delta;
\r
189 if (op->vol>=op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }
\r
191 case 2: case 3: // Decay/Sustain
\r
192 if (op->vol< op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }
\r
195 if (op->vol< op->limit) { op->vol=op->limit; }
\r
200 static void AddChannel(int c,short *dest,int len)
\r
202 struct Channel *pc=Chan+c;
\r
203 struct Operator *op=pc->op;
\r
206 // Work out volume delta for this operator:
\r
213 d=Sine[(op->angle>>8)&0xff]>>2;
\r
215 d*=(op->vol>>8); d>>=16;
\r
230 if (PsndOut==NULL) return 1; // Not rendering sound
\r
235 short *wave[2]={NULL,NULL};
\r
237 wave[1]=PsndOut+PsndLen;
\r
238 YM2612UpdateOne(0,wave,PsndLen);
\r
242 if ((PicoOpt&1)==0)
\r
244 BlankSound(PsndOut,PsndLen);
\r
247 if (i!=3) AddChannel(i,PsndOut,PsndLen);
\r