2 // This file is part of the PicoDrive Megadrive Emulator
\r
4 // Copyright (c) 2011 FinalDave (emudave (at) gmail.com)
\r
6 // This code is licensed under the GNU General Public License version 2.0 and the MAME License.
\r
7 // You can choose the license that has the most advantages for you.
\r
9 // SVN repository can be found at http://code.google.com/p/cyclone68000/
\r
11 #include "PicoInt.h"
\r
16 int YM2612UpdateReq(int) { return 0; }
\r
17 void *errorlog=NULL;
\r
21 int PsndRate=0,PsndLen=0;
\r
22 short *PsndOut=NULL;
\r
24 // An operator is a single sine wave generator
\r
27 unsigned short angle; // 0-0xffff
\r
28 unsigned short freq; // Converted frequency
\r
30 unsigned char state; // 1=attack, 2=decay, 3=sustain, 4=release
\r
32 int delta; // Change in volume per sample
\r
33 int limit; // Next volume limit
\r
38 struct Operator op[4]; // 4 operators for the channel
\r
39 unsigned short note; // Written to 0xa4 and 0xa0
\r
42 static struct Channel Chan[8];
\r
44 unsigned char PicoSreg[0x200];
\r
46 static int WriteReg(int side,int a,int d)
\r
48 struct Channel *pc=NULL;
\r
50 PicoSreg[(side<<8)|a]=(unsigned char)d;
\r
56 if (d&0xf0) pc->op[0].state=1; // Attack
\r
57 else pc->op[0].state=4; // Release
\r
63 pc=Chan+(a&3); if (side) pc+=4;
\r
67 int n=0,f=0,mult=2455;
\r
69 if (PsndRate>0) mult=44100*2455/PsndRate;
\r
71 if (a&4) pc->note =(unsigned short)(d<<8);
\r
74 // Convert to an actual frequency:
\r
75 n=pc->note; f=(n&0x7ff)<<((n>>11)&7);
\r
77 pc->op[0].freq=(unsigned short)((f*mult)>>16);
\r
87 memset(&Chan,0,sizeof(Chan));
\r
88 memset(PicoSreg,0,sizeof(PicoSreg));
\r
90 // Change Sine wave into a square wave
\r
91 for (i=0x00; i<0x080; i++) Sine[i]= 0x2000;
\r
92 for (i=0x80; i<0x100; i++) Sine[i]=-0x2000;
\r
97 int PsndFm(int a,int d)
\r
102 YM2612Write(0,a&3,(unsigned char)d);
\r
105 a&=3; side=a>>1; // Which side: channels 0-2 or 3-5
\r
107 if (a&1) WriteReg(side,Pico.s.fmsel[side],d); // Write register
\r
108 else Pico.s.fmsel[side]=(unsigned char)d; // Select register
\r
113 static void BlankSound(short *dest,int len)
\r
119 // Init sound to silence:
\r
120 do { *dest=0; dest++; } while (dest<end);
\r
123 // Add to output and clip:
\r
124 static inline void AddClip(short *targ,int d)
\r
127 if ((total+0x8000)&0xffff0000)
\r
129 if (total>0) total=0x7fff; else total=-0x8000;
\r
131 *targ=(short)total;
\r
134 static void OpNewState(int c)
\r
136 struct Operator *op=Chan[c].op;
\r
139 off=((c&4)<<6)|(c&3);
\r
146 int ar=PicoSreg[0x50|off];
\r
147 ar&=0x1f; if (ar) ar+=0x10;
\r
148 op->delta=ar<<7; op->limit=0x1000000; break;
\r
153 int d1r=PicoSreg[0x60|off];
\r
154 d1r&=0x1f; if (d1r) d1r+=0x10;
\r
155 op->delta=-(d1r<<5); op->limit=0;
\r
163 d2r=PicoSreg[0x70|off];
\r
164 d2r&=0x1f; if (d2r) d2r+=0x10;
\r
165 rr =PicoSreg[0x80|off];
\r
168 op->delta=-(d2r<<5); op->limit=rr<<20;
\r
173 int rr=PicoSreg[0x80|off];
\r
174 rr&=0x0f; rr<<=1; rr+=0x10;
\r
175 op->delta=-(rr<<5); op->limit=0;
\r
180 // Merely adding this code in seems to bugger up the N-Gage???
\r
181 static void UpdateOp(int c)
\r
183 struct Operator *op=Chan[c].op;
\r
185 op->angle=(unsigned short)(op->angle+op->freq);
\r
186 op->vol+=op->delta;
\r
191 if (op->vol>=op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }
\r
193 case 2: case 3: // Decay/Sustain
\r
194 if (op->vol< op->limit) { op->vol=op->limit; op->state++; OpNewState(c); }
\r
197 if (op->vol< op->limit) { op->vol=op->limit; }
\r
202 static void AddChannel(int c,short *dest,int len)
\r
204 struct Channel *pc=Chan+c;
\r
205 struct Operator *op=pc->op;
\r
208 // Work out volume delta for this operator:
\r
215 d=Sine[(op->angle>>8)&0xff]>>2;
\r
217 d*=(op->vol>>8); d>>=16;
\r
232 if (PsndOut==NULL) return 1; // Not rendering sound
\r
237 short *wave[2]={NULL,NULL};
\r
239 wave[1]=PsndOut+PsndLen;
\r
240 YM2612UpdateOne(0,wave,PsndLen);
\r
244 if ((PicoOpt&1)==0)
\r
246 BlankSound(PsndOut,PsndLen);
\r
249 if (i!=3) AddChannel(i,PsndOut,PsndLen);
\r